001 package com.softwarementors.extjs.djn.router.processor; 002 003 import java.io.PrintWriter; 004 import java.io.StringWriter; 005 import java.io.UnsupportedEncodingException; 006 import java.io.Writer; 007 import java.lang.reflect.InvocationTargetException; 008 import java.net.URLDecoder; 009 import java.util.HashMap; 010 import java.util.Map; 011 012 import com.softwarementors.extjs.djn.ClassUtils; 013 import com.softwarementors.extjs.djn.EncodingUtils; 014 import com.softwarementors.extjs.djn.StringUtils; 015 import com.softwarementors.extjs.djn.UnexpectedException; 016 import com.softwarementors.extjs.djn.router.dispatcher.MethodExecutionException; 017 018 public class RequestProcessorUtils { 019 020 // ************************************************************ 021 // * Request string handling support 022 // ************************************************************ 023 public static Map<String, String> getDecodedRequestParameters(String requestString) { 024 assert requestString != null; 025 Map<String,String> result = new HashMap<String,String>(); 026 if( !requestString.equals("")) { 027 String[] entries = requestString.split( "&"); 028 for( String entry : entries ) { 029 String[] keyValue = entry.split("="); 030 assert keyValue.length >= 1 && keyValue.length <= 2; 031 String key = keyValue[0]; 032 assert !StringUtils.isEmpty(key); 033 034 String value = ""; 035 if( keyValue.length == 2 ) 036 value = keyValue[1]; 037 try { 038 key = URLDecoder.decode( key, EncodingUtils.UTF8); 039 value = URLDecoder.decode( value, EncodingUtils.UTF8); 040 } 041 catch (UnsupportedEncodingException e) { 042 UnexpectedException.forExpectingUTF8UrlEncodingIsAlwaysSupportedByURLEncoder(e); 043 } 044 result.put( key, value ); 045 // addEscapedIfIsKeysIsMultiValued( result, key, value ); 046 } 047 } 048 return result; 049 } 050 051 /* 052 private static final String MULTIPLE_VALUES_SEPARATOR = "\n"; 053 054 private static String escape(String value ) { 055 return value; 056 } 057 058 private static boolean isEscaped(String value) { 059 return value.endsWith(MULTIPLE_VALUES_SEPARATOR); 060 } 061 062 private static void addEscapedIfIsKeysIsMultiValued( Map<String, String> map, String key, String value ) { 063 String newValue = value; 064 String oldValue = map.get(key); 065 066 if( oldValue != null ) { 067 newValue = escape( value ); 068 if( isEscaped(oldValue) ) { 069 newValue = oldValue + newValue + MULTIPLE_VALUES_SEPARATOR; 070 } 071 else { 072 oldValue = escape(oldValue); 073 newValue = oldValue + MULTIPLE_VALUES_SEPARATOR + newValue + MULTIPLE_VALUES_SEPARATOR; 074 } 075 } 076 map.put( key, newValue ); 077 } 078 079 public static boolean isMultiValued( Map<String, String> map, String key ) { 080 String value = map.get(key); 081 if( value == null ) { 082 return false; 083 } 084 return isEscaped(value); 085 } 086 087 public static List<String> getValues( Map<String,String> map, String key) { 088 String value = map.get(key); 089 if( value != null ) { 090 List<String> result = new ArrayList<String>(); 091 if( !isEscaped(value)) { 092 result.add(value); 093 } 094 else { 095 String[] resultStrings = value.split(MULTIPLE_VALUES_SEPARATOR); 096 Collections.addAll( result, resultStrings ); 097 } 098 return result; 099 } 100 return null; 101 } 102 */ 103 // ************************************************************ 104 // * Error handling support 105 // ************************************************************ 106 public static Throwable getExceptionToReport(Throwable t) { 107 assert t != null; 108 109 Throwable reportedException = t; 110 if( reportedException.getClass().equals( MethodExecutionException.class)) { 111 assert reportedException.getCause() != null; 112 reportedException = reportedException.getCause(); 113 114 } 115 if( reportedException.getClass().equals(InvocationTargetException.class )) { 116 assert reportedException.getCause() != null; 117 reportedException = reportedException.getCause(); 118 } 119 return reportedException; 120 } 121 122 public static String getExceptionMessage(Throwable t) { 123 assert t!= null; 124 125 String result = ClassUtils.getSimpleName( t.getClass() ); 126 if( t.getMessage() != null ) { 127 result += ": " + t.getMessage(); 128 } 129 return result; 130 } 131 132 public static String getExceptionWhere(Throwable t, boolean debugMode) { 133 assert t != null; 134 135 if( debugMode ) { 136 Writer where = new StringWriter(); 137 PrintWriter printWriter = new PrintWriter( where ); 138 t.printStackTrace(printWriter); 139 return where.toString(); 140 } 141 return ""; 142 } 143 144 145 }