1 16 package org.directwebremoting.dwrp; 17 18 import java.io.BufferedReader ; 19 import java.io.IOException ; 20 import java.io.InputStreamReader ; 21 import java.util.HashMap ; 22 import java.util.Iterator ; 23 import java.util.Map ; 24 import java.util.StringTokenizer ; 25 26 import javax.servlet.http.HttpServletRequest ; 27 28 import org.directwebremoting.extend.ServerException; 29 import org.directwebremoting.util.LocalUtil; 30 import org.directwebremoting.util.Logger; 31 import org.directwebremoting.util.Messages; 32 33 37 public class ParseUtil 38 { 39 48 public static Map parsePost(HttpServletRequest req) throws ServerException 49 { 50 Map paramMap = new HashMap (); 51 52 BufferedReader in = null; 53 try 54 { 55 in = new BufferedReader (new InputStreamReader (req.getInputStream())); 63 64 while (true) 65 { 66 String line = in.readLine(); 67 68 if (line == null) 69 { 70 break; 71 } 72 73 if (line.indexOf('&') != -1) 74 { 75 log.debug("Using iframe POST mode"); 78 StringTokenizer st = new StringTokenizer (line, "&"); 79 while (st.hasMoreTokens()) 80 { 81 String part = st.nextToken(); 82 part = LocalUtil.decode(part); 83 84 parsePostLine(part, paramMap); 85 } 86 } 87 else 88 { 89 parsePostLine(line, paramMap); 91 } 92 } 93 } 94 catch (Exception ex) 95 { 96 throw new ServerException(Messages.getString("ParseUtil.InputReadFailed"), ex); 97 } 98 finally 99 { 100 if (in != null) 101 { 102 try 103 { 104 in.close(); 105 } 106 catch (IOException ex) 107 { 108 } 110 } 111 } 112 113 if (paramMap.size() == 1) 120 { 121 log.debug("Using Broken Safari POST mode"); 123 124 Iterator it = paramMap.keySet().iterator(); 126 if (!it.hasNext()) 127 { 128 throw new IllegalStateException ("No entries in non empty map!"); 129 } 130 131 String key = (String ) it.next(); 133 String value = (String ) paramMap.get(key); 134 String line = key + ProtocolConstants.INBOUND_DECL_SEPARATOR + value; 135 136 StringTokenizer st = new StringTokenizer (line, "\n"); 137 while (st.hasMoreTokens()) 138 { 139 String part = st.nextToken(); 140 part = LocalUtil.decode(part); 141 142 parsePostLine(part, paramMap); 143 } 144 } 145 146 return paramMap; 147 } 148 149 154 private static void parsePostLine(String line, Map paramMap) 155 { 156 if (line.length() == 0) 157 { 158 return; 159 } 160 161 int sep = line.indexOf(ProtocolConstants.INBOUND_DECL_SEPARATOR); 162 if (sep == -1) 163 { 164 log.warn("Missing separator in POST line: " + line); 165 } 166 else 167 { 168 String key = line.substring(0, sep); 169 String value = line.substring(sep + ProtocolConstants.INBOUND_DECL_SEPARATOR.length()); 170 171 paramMap.put(key, value); 172 } 173 } 174 175 184 public static Map parseGet(HttpServletRequest req) throws ServerException 185 { 186 Map convertedMap = new HashMap (); 187 Map paramMap = req.getParameterMap(); 188 189 for (Iterator it = paramMap.entrySet().iterator(); it.hasNext();) 190 { 191 Map.Entry entry = (Map.Entry ) it.next(); 192 String key = (String ) entry.getKey(); 193 String [] array = (String []) entry.getValue(); 194 195 if (array.length == 1) 196 { 197 convertedMap.put(key, array[0]); 198 } 199 else 200 { 201 throw new ServerException(Messages.getString("ParseUtil.MultiValues", key)); 202 } 203 } 204 205 return convertedMap; 206 } 207 208 214 public static String [] splitInbound(String data) 215 { 216 String [] reply = new String [2]; 217 218 int colon = data.indexOf(ProtocolConstants.INBOUND_TYPE_SEPARATOR); 219 if (colon == -1) 220 { 221 log.error("Missing : in conversion data (" + data + ')'); 222 reply[LocalUtil.INBOUND_INDEX_TYPE] = ProtocolConstants.TYPE_STRING; 223 reply[LocalUtil.INBOUND_INDEX_VALUE] = data; 224 } 225 else 226 { 227 reply[LocalUtil.INBOUND_INDEX_TYPE] = data.substring(0, colon); 228 reply[LocalUtil.INBOUND_INDEX_VALUE] = data.substring(colon + 1); 229 } 230 231 return reply; 232 } 233 234 237 private static final Logger log = Logger.getLogger(ParseUtil.class); 238 } 239 | Popular Tags |