1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.mortbay.jetty.client;
16
17 import java.io.File;
18 import java.io.FileInputStream;
19 import java.io.IOException;
20 import java.io.InputStream;
21 import java.io.OutputStream;
22 import java.io.UnsupportedEncodingException;
23
24 import org.mortbay.io.Buffer;
25 import org.mortbay.io.BufferUtil;
26 import org.mortbay.jetty.HttpHeaders;
27 import org.mortbay.util.ByteArrayOutputStream2;
28 import org.mortbay.util.StringUtil;
29
30
31
32
33
34
35 public class ContentExchange extends CachedExchange
36 {
37 protected int _responseStatus;
38 protected int _contentLength = -1;
39 protected String _encoding = "utf-8";
40 protected ByteArrayOutputStream2 _responseContent;
41
42 protected File _fileForUpload;
43
44
45 public ContentExchange()
46 {
47 super(false);
48 }
49
50
51 public ContentExchange(boolean keepHeaders)
52 {
53 super(keepHeaders);
54 }
55
56
57 public int getResponseStatus()
58 {
59 if (getStatus() < HttpExchange.STATUS_PARSING_HEADERS)
60 throw new IllegalStateException("Response not received");
61 return _responseStatus;
62 }
63
64
65
66
67
68
69
70 public String getResponseContent() throws UnsupportedEncodingException
71 {
72 if (_responseContent != null)
73 return _responseContent.toString(_encoding);
74 return null;
75 }
76
77
78
79
80
81 public byte[] getResponseBytes()
82 {
83 if (_responseContent != null)
84 {
85 if (_contentLength>=0 && _responseContent.getBuf().length==_contentLength)
86 return _responseContent.getBuf();
87 return _responseContent.toByteArray();
88 }
89 return null;
90 }
91
92
93
94
95
96
97 public void writeResponseBytesTo(OutputStream out) throws IOException
98 {
99 if (_responseContent != null)
100 out.write(_responseContent.getBuf(),0,_responseContent.getCount());
101 }
102
103
104 protected void onResponseStatus(Buffer version, int status, Buffer reason) throws IOException
105 {
106 _responseStatus = status;
107 super.onResponseStatus(version,status,reason);
108 }
109
110
111
112 protected void onResponseHeader(Buffer name, Buffer value) throws IOException
113 {
114 super.onResponseHeader(name,value);
115 int header = HttpHeaders.CACHE.getOrdinal(name);
116 switch (header)
117 {
118 case HttpHeaders.CONTENT_LENGTH_ORDINAL:
119 _contentLength = BufferUtil.toInt(value);
120 break;
121 case HttpHeaders.CONTENT_TYPE_ORDINAL:
122 String mime = StringUtil.asciiToLowerCase(value.toString());
123 int i = mime.indexOf("charset=");
124 if (i > 0)
125 _encoding = mime.substring(i + 8);
126 break;
127 }
128 }
129
130
131 protected void onResponseContent(Buffer content) throws IOException
132 {
133 super.onResponseContent( content );
134 if (_responseContent == null)
135 _responseContent = (_contentLength>=0)?new ByteArrayOutputStream2(_contentLength):new ByteArrayOutputStream2();
136
137 content.writeTo(_responseContent);
138 }
139
140
141 protected void onRetry() throws IOException
142 {
143 if ( _fileForUpload != null )
144 {
145 _requestContent = null;
146 _requestContentSource = getInputStream();
147 }
148 else if ( _requestContentSource != null )
149 {
150 throw new IOException("Unsupported Retry attempt, no registered file for upload.");
151 }
152
153 super.onRetry();
154 }
155
156
157 private InputStream getInputStream() throws IOException
158 {
159 return new FileInputStream( _fileForUpload );
160 }
161
162
163 public File getFileForUpload()
164 {
165 return _fileForUpload;
166 }
167
168
169 public void setFileForUpload(File fileForUpload) throws IOException
170 {
171 this._fileForUpload = fileForUpload;
172 _requestContentSource = getInputStream();
173 }
174 }