1
2
3
4
5
6
7
8
9
10
11
12
13
14 package org.eclipse.jetty.client;
15
16 import java.io.ByteArrayOutputStream;
17 import java.io.File;
18 import java.io.FileInputStream;
19 import java.io.IOException;
20 import java.io.InputStream;
21 import java.io.UnsupportedEncodingException;
22
23 import org.eclipse.jetty.http.HttpHeaders;
24 import org.eclipse.jetty.io.Buffer;
25 import org.eclipse.jetty.io.BufferUtil;
26 import org.eclipse.jetty.util.StringUtil;
27
28
29
30
31 public class ContentExchange extends CachedExchange
32 {
33 private int _bufferSize = 4096;
34 private String _encoding = "utf-8";
35 private ByteArrayOutputStream _responseContent;
36 private File _fileForUpload;
37
38 public ContentExchange()
39 {
40 super(false);
41 }
42
43 public ContentExchange(boolean cacheFields)
44 {
45 super(cacheFields);
46 }
47
48 public synchronized String getResponseContent() throws UnsupportedEncodingException
49 {
50 if (_responseContent != null)
51 return _responseContent.toString(_encoding);
52 return null;
53 }
54
55 public synchronized byte[] getResponseContentBytes()
56 {
57 if (_responseContent != null)
58 return _responseContent.toByteArray();
59 return null;
60 }
61
62 @Override
63 protected synchronized void onResponseStatus(Buffer version, int status, Buffer reason) throws IOException
64 {
65 if (_responseContent!=null)
66 _responseContent.reset();
67 super.onResponseStatus(version,status,reason);
68 }
69
70 @Override
71 protected synchronized void onResponseHeader(Buffer name, Buffer value) throws IOException
72 {
73 super.onResponseHeader(name, value);
74 int header = HttpHeaders.CACHE.getOrdinal(name);
75 switch (header)
76 {
77 case HttpHeaders.CONTENT_LENGTH_ORDINAL:
78 _bufferSize = BufferUtil.toInt(value);
79 break;
80 case HttpHeaders.CONTENT_TYPE_ORDINAL:
81 String mime = StringUtil.asciiToLowerCase(value.toString());
82 int i = mime.indexOf("charset=");
83 if (i > 0)
84 {
85 _encoding = mime.substring(i + 8);
86 i = _encoding.indexOf(';');
87 if (i > 0)
88 _encoding = _encoding.substring(0, i);
89 }
90 break;
91 }
92 }
93
94 @Override
95 protected synchronized void onResponseContent(Buffer content) throws IOException
96 {
97 super.onResponseContent(content);
98 if (_responseContent == null)
99 _responseContent = new ByteArrayOutputStream(_bufferSize);
100 content.writeTo(_responseContent);
101 }
102
103 @Override
104 protected synchronized void onRetry() throws IOException
105 {
106 if (_fileForUpload != null)
107 {
108 setRequestContent(null);
109 setRequestContentSource(getInputStream());
110 }
111 else
112 super.onRetry();
113 }
114
115 private synchronized InputStream getInputStream() throws IOException
116 {
117 return new FileInputStream(_fileForUpload);
118 }
119
120 public synchronized File getFileForUpload()
121 {
122 return _fileForUpload;
123 }
124
125 public synchronized void setFileForUpload(File fileForUpload) throws IOException
126 {
127 this._fileForUpload = fileForUpload;
128 setRequestContentSource(getInputStream());
129 }
130 }