Changeset 1392 for trunk/autoquest-httpmonitor
- Timestamp:
- 02/21/14 19:27:26 (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/autoquest-httpmonitor/src/main/java/de/ugoe/cs/autoquest/httpmonitor/proxy/HttpMonitoringProxyServlet.java
r1389 r1392 16 16 17 17 import java.io.IOException; 18 import java.io.UnsupportedEncodingException; 18 19 import java.net.URI; 19 20 import java.net.URISyntaxException; 21 import java.net.URLEncoder; 20 22 import java.nio.ByteBuffer; 23 import java.nio.charset.UnsupportedCharsetException; 21 24 import java.util.Iterator; 25 import java.util.LinkedList; 26 import java.util.List; 22 27 23 28 import javax.servlet.http.HttpServletRequest; 24 29 import javax.servlet.http.HttpServletResponse; 25 30 31 import org.eclipse.jetty.client.HttpClient; 32 import org.eclipse.jetty.client.HttpRequest; 26 33 import org.eclipse.jetty.client.api.ContentProvider; 27 34 import org.eclipse.jetty.client.api.Request; 28 35 import org.eclipse.jetty.client.api.Response; 29 36 import org.eclipse.jetty.proxy.ProxyServlet; 37 import org.eclipse.jetty.util.Fields; 30 38 31 39 import de.ugoe.cs.autoquest.httpmonitor.exchange.Status; … … 41 49 * recorded exchange. 42 50 * </p> 51 * <p> 52 * The implementation also overrides the default implementation of the jetty HTTP client 53 * and the jetty HTTP request to fix a bug in forwarding queries. 54 * </p> 43 55 * 44 56 * @author Patrick Harms … … 97 109 return null; 98 110 } 111 } 112 113 /* (non-Javadoc) 114 * @see org.eclipse.jetty.proxy.ProxyServlet#newHttpClient() 115 */ 116 @Override 117 protected HttpClient newHttpClient() { 118 return new BugfixedHttpClient(); 99 119 } 100 120 … … 200 220 201 221 /** 202 * This iterator is used to implement the {@link DubbingContentProvider}. It works in the 203 * same manner and uses delegation for wrapping the original iterator and forwards all copied 204 * data to the exchange listener manager. 222 * This iterator is used to implement the {@link DubbingContentProvider}. It uses delegation 223 * for wrapping the original iterator and forwards all copied data to the exchange listener 224 * manager. Furthermore, it combines several buffers into one. This seems to be required if 225 * SOAP messages are proxied. 205 226 */ 206 227 public class DubbingByteBufferIterator implements Iterator<ByteBuffer> { … … 237 258 @Override 238 259 public ByteBuffer next() { 239 ByteBuffer next = delegate.next(); 240 exchangeListenerManager.onRequestContent(request, next.duplicate()); 241 return next; 260 List<ByteBuffer> buffers = new LinkedList<ByteBuffer>(); 261 262 int size = 0; 263 while (delegate.hasNext()) { 264 ByteBuffer next = delegate.next(); 265 exchangeListenerManager.onRequestContent(request, next.duplicate()); 266 267 ByteBuffer copy = ByteBuffer.allocate(next.limit()); 268 copy.put(next); 269 copy.position(0); 270 buffers.add(copy); 271 272 size += next.limit(); 273 } 274 275 ByteBuffer buffer = ByteBuffer.allocate(size); 276 277 for (ByteBuffer orig : buffers) { 278 buffer.put(orig); 279 } 280 281 buffer.position(0); 282 283 return buffer; 242 284 } 243 285 … … 247 289 @Override 248 290 public void remove() { 249 delegate.remove(); 250 } 251 252 } 253 291 while (delegate.hasNext()) { 292 delegate.remove(); 293 } 294 } 295 296 } 297 298 /** 299 * <p> 300 * bugfix implementation of the jetty HTTP client to be able to use only bugfixed HTTP requests 301 * </p> 302 * 303 * @author Patrick Harms 304 */ 305 private class BugfixedHttpClient extends HttpClient { 306 307 /* (non-Javadoc) 308 * @see org.eclipse.jetty.client.HttpClient#newRequest(java.net.URI) 309 */ 310 @Override 311 public Request newRequest(URI uri) { 312 return new BugfixedHttpRequest(this, uri); 313 } 314 315 } 316 317 /** 318 * <p> 319 * bugfix implementation of the jetty HTTP request. This ensures that query string 320 * representations are correctly forwarded in the case that a query parameter does not have 321 * a value. 322 * </p> 323 * 324 * @author Patrick Harms 325 */ 326 private class BugfixedHttpRequest extends HttpRequest { 327 328 /** 329 * <p> 330 * Constructor to override parent constructor 331 * </p> 332 */ 333 BugfixedHttpRequest(HttpClient client, URI uri) { 334 super(client, uri); 335 } 336 337 /* 338 * (non-Javadoc) 339 * 340 * @see org.eclipse.jetty.client.HttpRequest#getQuery() 341 */ 342 @Override 343 public String getQuery() { 344 return buildQuery(); 345 } 346 347 /** 348 * <p> 349 * this corrects the bug implemented in the parent class on creating the query string 350 * </p> 351 * 352 * @return the correct query string 353 */ 354 private String buildQuery() { 355 StringBuilder result = new StringBuilder(); 356 for (Iterator<Fields.Field> iterator = super.getParams().iterator(); iterator.hasNext();) 357 { 358 Fields.Field field = iterator.next(); 359 String[] values = field.values(); 360 for (int i = 0; i < values.length; ++i) { 361 if (i > 0) { 362 result.append("&"); 363 } 364 365 result.append(field.name()); 366 367 if ((values[i] != null) && (!"".equals(values[i].trim()))) { 368 result.append("="); 369 result.append(urlEncode(values[i])); 370 } 371 } 372 if (iterator.hasNext()) { 373 result.append("&"); 374 } 375 } 376 return result.toString(); 377 } 378 379 /** 380 * <p> 381 * convenience method copied from parent class, as it is private there. 382 * </p> 383 * 384 * @see HttpRequest#urlEncode(String) 385 */ 386 private String urlEncode(String value) { 387 String encoding = "UTF-8"; 388 try { 389 return URLEncoder.encode(value, encoding); 390 } 391 catch (UnsupportedEncodingException e) { 392 throw new UnsupportedCharsetException(encoding); 393 } 394 } 395 } 254 396 }
Note: See TracChangeset
for help on using the changeset viewer.