1
2
3
4
5
6
7
8
9
10
11
12
13
14 package org.eclipse.jetty.client;
15
16 import java.io.IOException;
17 import java.io.InputStream;
18
19 import org.eclipse.jetty.http.HttpFields;
20 import org.eclipse.jetty.http.HttpHeaders;
21 import org.eclipse.jetty.http.HttpMethods;
22 import org.eclipse.jetty.http.HttpSchemes;
23 import org.eclipse.jetty.http.HttpURI;
24 import org.eclipse.jetty.http.HttpVersions;
25 import org.eclipse.jetty.io.Buffer;
26 import org.eclipse.jetty.io.BufferCache.CachedBuffer;
27 import org.eclipse.jetty.io.ByteArrayBuffer;
28 import org.eclipse.jetty.util.log.Log;
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63 public class HttpExchange
64 {
65 public static final int STATUS_START = 0;
66 public static final int STATUS_WAITING_FOR_CONNECTION = 1;
67 public static final int STATUS_WAITING_FOR_COMMIT = 2;
68 public static final int STATUS_SENDING_REQUEST = 3;
69 public static final int STATUS_WAITING_FOR_RESPONSE = 4;
70 public static final int STATUS_PARSING_HEADERS = 5;
71 public static final int STATUS_PARSING_CONTENT = 6;
72 public static final int STATUS_COMPLETED = 7;
73 public static final int STATUS_EXPIRED = 8;
74 public static final int STATUS_EXCEPTED = 9;
75
76
77 private String _method = HttpMethods.GET;
78 private Buffer _scheme = HttpSchemes.HTTP_BUFFER;
79 private String _uri;
80 private int _version = HttpVersions.HTTP_1_1_ORDINAL;
81 private Address _address;
82 private final HttpFields _requestFields = new HttpFields();
83 private Buffer _requestContent;
84 private InputStream _requestContentSource;
85
86 private int _status = STATUS_START;
87 private Buffer _requestContentChunk;
88 private boolean _retryStatus = false;
89
90 private boolean _configureListeners = true;
91 private HttpEventListener _listener = new Listener();
92
93 public int getStatus()
94 {
95 synchronized (this)
96 {
97 return _status;
98 }
99 }
100
101
102
103
104
105
106 public void waitForStatus(int status) throws InterruptedException
107 {
108 synchronized (this)
109 {
110 while (_status < status)
111 {
112 this.wait();
113 }
114 }
115 }
116
117 public int waitForDone () throws InterruptedException
118 {
119 synchronized (this)
120 {
121 while (!isDone(_status))
122 this.wait();
123 return _status;
124 }
125 }
126
127 public void reset()
128 {
129 setStatus(STATUS_START);
130 }
131
132 void setStatus(int status)
133 {
134 synchronized (this)
135 {
136 _status = status;
137 this.notifyAll();
138 }
139
140 try
141 {
142 switch (status)
143 {
144 case STATUS_WAITING_FOR_CONNECTION:
145 break;
146
147 case STATUS_WAITING_FOR_COMMIT:
148 break;
149
150 case STATUS_SENDING_REQUEST:
151 break;
152
153 case HttpExchange.STATUS_WAITING_FOR_RESPONSE:
154 getEventListener().onRequestCommitted();
155 break;
156
157 case STATUS_PARSING_HEADERS:
158 break;
159
160 case STATUS_PARSING_CONTENT:
161 getEventListener().onResponseHeaderComplete();
162 break;
163
164 case STATUS_COMPLETED:
165 getEventListener().onResponseComplete();
166 break;
167
168 case STATUS_EXPIRED:
169 getEventListener().onExpire();
170 break;
171
172 }
173 }
174 catch (IOException e)
175 {
176 Log.warn(e);
177 }
178 }
179
180 public boolean isDone (int status)
181 {
182 return ((status == STATUS_COMPLETED) || (status == STATUS_EXPIRED) || (status == STATUS_EXCEPTED));
183 }
184
185 public HttpEventListener getEventListener()
186 {
187 return _listener;
188 }
189
190 public void setEventListener(HttpEventListener listener)
191 {
192 _listener=listener;
193 }
194
195
196
197
198 public void setURL(String url)
199 {
200 HttpURI uri = new HttpURI(url);
201 String scheme = uri.getScheme();
202 if (scheme != null)
203 {
204 if (HttpSchemes.HTTP.equalsIgnoreCase(scheme))
205 setScheme(HttpSchemes.HTTP_BUFFER);
206 else if (HttpSchemes.HTTPS.equalsIgnoreCase(scheme))
207 setScheme(HttpSchemes.HTTPS_BUFFER);
208 else
209 setScheme(new ByteArrayBuffer(scheme));
210 }
211
212 int port = uri.getPort();
213 if (port <= 0)
214 port = "https".equalsIgnoreCase(scheme)?443:80;
215
216 setAddress(new Address(uri.getHost(),port));
217
218 String completePath = uri.getCompletePath();
219 if (completePath == null)
220 completePath = "/";
221
222 setURI(completePath);
223 }
224
225
226
227
228 public void setAddress(Address address)
229 {
230 _address = address;
231 }
232
233
234
235
236 public Address getAddress()
237 {
238 return _address;
239 }
240
241
242
243
244 public void setScheme(Buffer scheme)
245 {
246 _scheme = scheme;
247 }
248
249
250
251
252 public Buffer getScheme()
253 {
254 return _scheme;
255 }
256
257
258
259
260 public void setVersion(int version)
261 {
262 _version = version;
263 }
264
265
266
267
268 public void setVersion(String version)
269 {
270 CachedBuffer v = HttpVersions.CACHE.get(version);
271 if (v == null)
272 _version = 10;
273 else
274 _version = v.getOrdinal();
275 }
276
277
278
279
280
281 public int getVersion()
282 {
283 return _version;
284 }
285
286
287
288
289 public void setMethod(String method)
290 {
291 _method = method;
292 }
293
294
295
296
297 public String getMethod()
298 {
299 return _method;
300 }
301
302
303
304
305 public String getURI()
306 {
307 return _uri;
308 }
309
310
311
312
313 public void setURI(String uri)
314 {
315 _uri = uri;
316 }
317
318
319
320
321
322
323 public void addRequestHeader(String name, String value)
324 {
325 getRequestFields().add(name,value);
326 }
327
328
329
330
331
332
333 public void addRequestHeader(Buffer name, Buffer value)
334 {
335 getRequestFields().add(name,value);
336 }
337
338
339
340
341
342
343 public void setRequestHeader(String name, String value)
344 {
345 getRequestFields().put(name,value);
346 }
347
348
349
350
351
352
353 public void setRequestHeader(Buffer name, Buffer value)
354 {
355 getRequestFields().put(name,value);
356 }
357
358
359
360
361 public void setRequestContentType(String value)
362 {
363 getRequestFields().put(HttpHeaders.CONTENT_TYPE_BUFFER,value);
364 }
365
366
367
368
369 public HttpFields getRequestFields()
370 {
371 return _requestFields;
372 }
373
374
375
376
377 public void setRequestContent(Buffer requestContent)
378 {
379 _requestContent = requestContent;
380 }
381
382
383
384
385 public void setRequestContentSource(InputStream stream)
386 {
387 _requestContentSource = stream;
388 }
389
390
391
392
393 public InputStream getRequestContentSource()
394 {
395 return _requestContentSource;
396 }
397
398 public Buffer getRequestContentChunk() throws IOException
399 {
400 synchronized (this)
401 {
402 if (_requestContentChunk == null)
403 _requestContentChunk = new ByteArrayBuffer(4096);
404 else
405 {
406 if (_requestContentChunk.hasContent())
407 throw new IllegalStateException();
408 _requestContentChunk.clear();
409 }
410
411 int read = _requestContentChunk.capacity();
412 int length = _requestContentSource.read(_requestContentChunk.array(),0,read);
413 if (length >= 0)
414 {
415 _requestContentChunk.setPutIndex(length);
416 return _requestContentChunk;
417 }
418 return null;
419 }
420 }
421
422
423
424
425 public Buffer getRequestContent()
426 {
427 return _requestContent;
428 }
429
430
431
432
433 public boolean getRetryStatus()
434 {
435 return _retryStatus;
436 }
437
438
439
440
441 public void setRetryStatus( boolean retryStatus )
442 {
443 _retryStatus = retryStatus;
444 }
445
446
447
448
449 public void cancel()
450 {
451
452 }
453
454 public String toString()
455 {
456 return getClass().getSimpleName() + "@" + hashCode() + "=" + _method + "//" + _address + _uri + "#" + getStatus();
457 }
458
459
460
461
462
463
464 protected void onRequestCommitted() throws IOException
465 {
466 }
467
468
469
470
471
472
473 protected void onRequestComplete() throws IOException
474 {
475 }
476
477
478
479
480
481
482
483
484
485 protected void onResponseStatus(Buffer version, int status, Buffer reason) throws IOException
486 {
487 }
488
489
490
491
492
493
494
495
496 protected void onResponseHeader(Buffer name, Buffer value) throws IOException
497 {
498 }
499
500
501
502
503
504
505 protected void onResponseHeaderComplete() throws IOException
506 {
507 }
508
509
510
511
512
513
514
515 protected void onResponseContent(Buffer content) throws IOException
516 {
517 }
518
519
520
521
522
523
524 protected void onResponseComplete() throws IOException
525 {
526 }
527
528
529
530
531
532
533
534 protected void onConnectionFailed(Throwable x)
535 {
536 Log.warn("CONNECTION FAILED " + this,x);
537 }
538
539
540
541
542
543
544 protected void onException(Throwable x)
545 {
546 Log.warn("EXCEPTION " + this,x);
547 }
548
549
550
551
552 protected void onExpire()
553 {
554 Log.warn("EXPIRED " + this);
555 }
556
557
558
559
560
561
562
563 protected void onRetry() throws IOException
564 {
565 }
566
567
568
569
570
571
572 public boolean configureListeners()
573 {
574 return _configureListeners;
575 }
576
577
578
579
580 public void setConfigureListeners(boolean autoConfigure )
581 {
582 this._configureListeners = autoConfigure;
583 }
584
585 private class Listener implements HttpEventListener
586 {
587 public void onConnectionFailed(Throwable ex)
588 {
589 HttpExchange.this.onConnectionFailed(ex);
590 }
591
592 public void onException(Throwable ex)
593 {
594 HttpExchange.this.onException(ex);
595 }
596
597 public void onExpire()
598 {
599 HttpExchange.this.onExpire();
600 }
601
602 public void onRequestCommitted() throws IOException
603 {
604 HttpExchange.this.onRequestCommitted();
605 }
606
607 public void onRequestComplete() throws IOException
608 {
609 HttpExchange.this.onRequestComplete();
610 }
611
612 public void onResponseComplete() throws IOException
613 {
614 HttpExchange.this.onResponseComplete();
615 }
616
617 public void onResponseContent(Buffer content) throws IOException
618 {
619 HttpExchange.this.onResponseContent(content);
620 }
621
622 public void onResponseHeader(Buffer name, Buffer value) throws IOException
623 {
624 HttpExchange.this.onResponseHeader(name,value);
625 }
626
627 public void onResponseHeaderComplete() throws IOException
628 {
629 HttpExchange.this.onResponseHeaderComplete();
630 }
631
632 public void onResponseStatus(Buffer version, int status, Buffer reason) throws IOException
633 {
634 HttpExchange.this.onResponseStatus(version,status,reason);
635 }
636
637 public void onRetry()
638 {
639 HttpExchange.this.setRetryStatus( true );
640 try
641 {
642 HttpExchange.this.onRetry();
643 }
644 catch (IOException e)
645 {
646 Log.debug(e);
647 }
648 }
649 }
650
651
652
653
654 public static class CachedExchange extends org.eclipse.jetty.client.CachedExchange
655 {
656 public CachedExchange(boolean cacheFields)
657 {
658 super(cacheFields);
659 }
660 }
661
662
663
664
665 public static class ContentExchange extends org.eclipse.jetty.client.ContentExchange
666 {
667 }
668 }