001    package com.softwarementors.extjs.djn.router.processor;
002    
003    import java.io.UnsupportedEncodingException;
004    import java.net.URLDecoder;
005    import java.util.HashMap;
006    import java.util.Map;
007    
008    import com.softwarementors.extjs.djn.EncodingUtils;
009    import com.softwarementors.extjs.djn.StringUtils;
010    import com.softwarementors.extjs.djn.UnexpectedException;
011    
012    public class RequestProcessorUtils {
013      
014      // ************************************************************
015      // * Request string handling support
016      // ************************************************************
017      public static Map<String, String> getDecodedRequestParameters(String requestString) {
018        assert requestString != null;
019        Map<String,String> result = new HashMap<String,String>();
020        if( !requestString.equals("")) {
021          String[] entries = requestString.split( "&");
022          for( String entry : entries ) {
023            String[] keyValue = entry.split("=");
024            assert keyValue.length >= 1 && keyValue.length <= 2;
025            String key = keyValue[0];
026            assert !StringUtils.isEmpty(key);
027    
028            String value = "";
029            if( keyValue.length == 2 )
030              value = keyValue[1];
031            try {
032              key = URLDecoder.decode( key, EncodingUtils.UTF8);
033    //ññ: Why not decode this from String to JSON object?
034              value = URLDecoder.decode( value, EncodingUtils.UTF8);
035            }
036            catch (UnsupportedEncodingException e) {
037              UnexpectedException.forExpectingUTF8UrlEncodingIsAlwaysSupportedByURLEncoder(e);
038            }
039            result.put( key, value );
040            // addEscapedIfIsKeysIsMultiValued( result, key, value );
041          }
042        }
043        return result;
044      }
045    
046    /*  
047      private static final String MULTIPLE_VALUES_SEPARATOR = "\n";
048      
049      private static String escape(String value ) {
050        return value;
051      }
052      
053      private static boolean isEscaped(String value) {
054        return value.endsWith(MULTIPLE_VALUES_SEPARATOR);
055      }
056      
057      private static void addEscapedIfIsKeysIsMultiValued( Map<String, String> map, String key, String value ) {
058        String newValue = value;
059        String oldValue = map.get(key);
060        
061        if( oldValue != null ) {
062          newValue = escape( value );
063          if( isEscaped(oldValue) ) {
064            newValue = oldValue + newValue + MULTIPLE_VALUES_SEPARATOR;
065          }
066          else {
067            oldValue = escape(oldValue);
068            newValue = oldValue + MULTIPLE_VALUES_SEPARATOR + newValue + MULTIPLE_VALUES_SEPARATOR;
069          }
070        }
071        map.put( key, newValue );
072      }
073      
074      public static boolean isMultiValued( Map<String, String> map, String key ) {
075        String value = map.get(key);
076        if( value == null ) {
077          return false;
078        }
079        return isEscaped(value);
080      }
081      
082      public static List<String> getValues( Map<String,String> map, String key) {
083        String value = map.get(key);
084        if( value != null ) {
085          List<String> result = new ArrayList<String>();
086          if( !isEscaped(value)) {
087            result.add(value);
088          }
089          else {
090            String[] resultStrings = value.split(MULTIPLE_VALUES_SEPARATOR);
091            Collections.addAll( result, resultStrings );
092          }
093          return result; 
094        }
095        return null;
096      }
097    */  
098    }