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              value = URLDecoder.decode( value, EncodingUtils.UTF8);
034            }
035            catch (UnsupportedEncodingException e) {
036              UnexpectedException.forExpectingUTF8UrlEncodingIsAlwaysSupportedByURLEncoder(e);
037            }
038            result.put( key, value );
039            // addEscapedIfIsKeysIsMultiValued( result, key, value );
040          }
041        }
042        return result;
043      }
044    
045    /*  
046      private static final String MULTIPLE_VALUES_SEPARATOR = "\n";
047      
048      private static String escape(String value ) {
049        return value;
050      }
051      
052      private static boolean isEscaped(String value) {
053        return value.endsWith(MULTIPLE_VALUES_SEPARATOR);
054      }
055      
056      private static void addEscapedIfIsKeysIsMultiValued( Map<String, String> map, String key, String value ) {
057        String newValue = value;
058        String oldValue = map.get(key);
059        
060        if( oldValue != null ) {
061          newValue = escape( value );
062          if( isEscaped(oldValue) ) {
063            newValue = oldValue + newValue + MULTIPLE_VALUES_SEPARATOR;
064          }
065          else {
066            oldValue = escape(oldValue);
067            newValue = oldValue + MULTIPLE_VALUES_SEPARATOR + newValue + MULTIPLE_VALUES_SEPARATOR;
068          }
069        }
070        map.put( key, newValue );
071      }
072      
073      public static boolean isMultiValued( Map<String, String> map, String key ) {
074        String value = map.get(key);
075        if( value == null ) {
076          return false;
077        }
078        return isEscaped(value);
079      }
080      
081      public static List<String> getValues( Map<String,String> map, String key) {
082        String value = map.get(key);
083        if( value != null ) {
084          List<String> result = new ArrayList<String>();
085          if( !isEscaped(value)) {
086            result.add(value);
087          }
088          else {
089            String[] resultStrings = value.split(MULTIPLE_VALUES_SEPARATOR);
090            Collections.addAll( result, resultStrings );
091          }
092          return result; 
093        }
094        return null;
095      }
096    */  
097    }