1
2
3
4
5
6
7
8
9
10
11
12
13 package org.eclipse.jetty.client;
14
15 import java.io.IOException;
16
17 import org.eclipse.jetty.http.HttpFields;
18 import org.eclipse.jetty.io.Buffer;
19
20
21
22
23 public class CachedExchange extends HttpExchange
24 {
25 private final HttpFields _responseFields;
26 private volatile int _responseStatus;
27
28
29
30
31
32
33 public CachedExchange(boolean cacheHeaders)
34 {
35 _responseFields = cacheHeaders ? new HttpFields() : null;
36 }
37
38 public synchronized int getResponseStatus()
39 {
40 if (getStatus() < HttpExchange.STATUS_PARSING_HEADERS)
41 throw new IllegalStateException("Response not received yet");
42 return _responseStatus;
43 }
44
45 public synchronized HttpFields getResponseFields()
46 {
47 if (getStatus() < HttpExchange.STATUS_PARSING_CONTENT)
48 throw new IllegalStateException("Headers not completely received yet");
49 return _responseFields;
50 }
51
52 @Override
53 protected synchronized void onResponseStatus(Buffer version, int status, Buffer reason) throws IOException
54 {
55 _responseStatus = status;
56 super.onResponseStatus(version, status, reason);
57 }
58
59 @Override
60 protected synchronized void onResponseHeader(Buffer name, Buffer value) throws IOException
61 {
62 if (_responseFields != null)
63 {
64 _responseFields.add(name, value.asImmutableBuffer());
65 }
66
67 super.onResponseHeader(name, value);
68 }
69 }