1 52 53 package com.go.teaservlet; 54 55 import java.util.*; 56 import javax.servlet.http.*; 57 import com.go.teaservlet.util.FilteredHttpServletRequest; 58 59 71 class SpiderableRequest extends FilteredHttpServletRequest { 72 private final String mQuerySeparator; 73 private final String mParameterSeparator; 74 private final String mValueSeparator; 75 76 private final int mQuerySepLen; 77 private final int mParamSepLen; 78 private final int mValSepLen; 79 80 private String mPathInfo; 81 private Map mParameters; 82 83 public SpiderableRequest(HttpServletRequest request, 84 String querySeparator, 85 String parameterSeparator, 86 String valueSeparator) { 87 super(request); 88 mQuerySeparator = querySeparator; 89 mParameterSeparator = parameterSeparator; 90 mValueSeparator = valueSeparator; 91 92 mQuerySepLen = querySeparator.length(); 93 mParamSepLen = parameterSeparator.length(); 94 mValSepLen = valueSeparator.length(); 95 96 mPathInfo = request.getPathInfo(); 97 mParameters = new HashMap(37); 98 99 fillParameters(); 100 fillDefaultParameters(); 101 } 102 103 public String getPathInfo() { 104 return mPathInfo; 105 } 106 107 public String getParameter(String name) { 108 String [] values = getParameterValues(name); 109 if (values != null && values.length > 0) { 110 return values[0]; 111 } 112 return null; 113 } 114 115 public String [] getParameterValues(String name) { 116 return (String [])mParameters.get(name); 117 } 118 119 public Enumeration getParameterNames() { 120 return Collections.enumeration(mParameters.keySet()); 121 } 122 123 private void fillParameters() { 124 String str; 125 int startIndex, midIndex, endIndex; 126 127 if ("?".equals(mQuerySeparator)) { 128 if ((str = mRequest.getQueryString()) == null) { 129 return; 130 } 131 startIndex = 0; 132 } 133 else { 134 startIndex = mPathInfo.indexOf(mQuerySeparator); 135 if (startIndex >= 0) { 136 mPathInfo = mPathInfo.substring(0, startIndex); 137 } 138 139 str = mRequest.getRequestURI(); 140 startIndex = str.indexOf(mQuerySeparator); 141 if (startIndex < 0) { 142 return; 143 } 144 startIndex += mQuerySepLen; 145 } 146 147 int length = str.length(); 148 149 for (; startIndex < length; startIndex = endIndex + mParamSepLen) { 150 endIndex = str.indexOf(mParameterSeparator, startIndex); 151 152 if (endIndex < 0) { 153 endIndex = length; 154 } 155 156 midIndex = str.indexOf(mValueSeparator, startIndex); 157 158 String key; 159 String value; 160 161 if (midIndex < 0 || midIndex > endIndex) { 162 if (endIndex - startIndex > 1) { 163 key = str.substring(startIndex, endIndex); 164 value = ""; 165 } 166 else { 167 continue; 168 } 169 } 170 else if (midIndex - startIndex > 1) { 171 key = str.substring(startIndex, midIndex); 172 value = str.substring(midIndex + mValSepLen, endIndex); 173 } 174 else { 175 continue; 176 } 177 178 putParameter(key, value); 179 } 180 } 181 182 private void fillDefaultParameters() { 183 Enumeration names = mRequest.getParameterNames(); 184 while (names.hasMoreElements()) { 185 String name = (String )names.nextElement(); 186 putParameters(name, mRequest.getParameterValues(name)); 187 } 188 } 189 190 private void putParameter(String key, String value) { 191 String [] currentValues = (String [])mParameters.get(key); 192 if (currentValues == null) { 193 currentValues = new String [] {value}; 194 } 195 else { 196 String [] newValues = new String [currentValues.length + 1]; 197 int i; 198 for (i=0; i<currentValues.length; i++) { 199 newValues[i] = currentValues[i]; 200 } 201 currentValues = newValues; 202 currentValues[i] = value; 203 } 204 205 mParameters.put(key, currentValues); 206 } 207 208 209 private void putParameters(String key, String [] values) { 210 if (values == null || values.length == 0) { 211 return; 212 } 213 else if (values.length == 1) { 214 putParameter(key, values[0]); 215 return; 216 } 217 218 String [] currentValues = (String [])mParameters.get(key); 219 if (currentValues == null) { 220 currentValues = values; 221 } 222 else { 223 String [] newValues = 224 new String [currentValues.length + values.length]; 225 int i, j; 226 for (i=0; i<currentValues.length; i++) { 227 newValues[i] = currentValues[i]; 228 } 229 for (j=0; j<values.length; j++) { 230 newValues[i + j] = values[j]; 231 } 232 currentValues = newValues; 233 } 234 235 mParameters.put(key, currentValues); 236 } 237 } 238
| Popular Tags
|