|
32 | 32 | import java.io.InputStream; |
33 | 33 | import java.io.OutputStream; |
34 | 34 | import java.io.OutputStreamWriter; |
| 35 | +import java.util.Arrays; |
35 | 36 |
|
36 | 37 | /** |
37 | 38 | * Implementation of the HTML components document request handler to allow simple |
@@ -116,41 +117,67 @@ private InputStream resourceRequested(final DocumentInfo docInfo, final IOCallba |
116 | 117 |
|
117 | 118 | protected ConnectionRequest createConnectionRequest(final DocumentInfo docInfo, |
118 | 119 | final IOCallback callback, final Object[] response) { |
119 | | - return new ConnectionRequest() { |
| 120 | + return new AsyncDocumentConnectionRequest(docInfo, callback, response); |
| 121 | + } |
120 | 122 |
|
121 | | - protected void buildRequestBody(OutputStream os) throws IOException { |
122 | | - if (isPost()) { |
123 | | - if (docInfo.getParams() != null) { |
124 | | - OutputStreamWriter w = new OutputStreamWriter(os, docInfo.getEncoding()); |
125 | | - w.write(docInfo.getParams()); |
126 | | - } |
127 | | - } |
128 | | - } |
| 123 | + private static class AsyncDocumentConnectionRequest extends ConnectionRequest { |
| 124 | + private final DocumentInfo docInfo; |
| 125 | + private final IOCallback callback; |
| 126 | + private final Object[] response; |
129 | 127 |
|
130 | | - protected void handleIOException(IOException err) { |
131 | | - if (callback == null) { |
132 | | - response[0] = err; |
| 128 | + public AsyncDocumentConnectionRequest(DocumentInfo docInfo, IOCallback callback, Object[] response) { |
| 129 | + this.docInfo = docInfo; |
| 130 | + this.callback = callback; |
| 131 | + this.response = response; |
| 132 | + } |
| 133 | + |
| 134 | + protected void buildRequestBody(OutputStream os) throws IOException { |
| 135 | + if (isPost()) { |
| 136 | + if (docInfo.getParams() != null) { |
| 137 | + OutputStreamWriter w = new OutputStreamWriter(os, docInfo.getEncoding()); |
| 138 | + w.write(docInfo.getParams()); |
133 | 139 | } |
134 | | - super.handleIOException(err); |
135 | 140 | } |
| 141 | + } |
136 | 142 |
|
137 | | - protected boolean shouldAutoCloseResponse() { |
138 | | - return callback != null; |
| 143 | + protected void handleIOException(IOException err) { |
| 144 | + if (callback == null) { |
| 145 | + response[0] = err; |
139 | 146 | } |
| 147 | + super.handleIOException(err); |
| 148 | + } |
140 | 149 |
|
141 | | - protected void readResponse(InputStream input) throws IOException { |
142 | | - if (callback != null) { |
143 | | - callback.streamReady(input, docInfo); |
144 | | - } else { |
145 | | - response[0] = input; |
146 | | - synchronized (LOCK) { |
147 | | - LOCK.notifyAll(); |
148 | | - } |
| 150 | + protected boolean shouldAutoCloseResponse() { |
| 151 | + return callback != null; |
| 152 | + } |
| 153 | + |
| 154 | + protected void readResponse(InputStream input) throws IOException { |
| 155 | + if (callback != null) { |
| 156 | + callback.streamReady(input, docInfo); |
| 157 | + } else { |
| 158 | + response[0] = input; |
| 159 | + synchronized (LOCK) { |
| 160 | + LOCK.notifyAll(); |
149 | 161 | } |
150 | 162 | } |
| 163 | + } |
151 | 164 |
|
152 | | - }; |
| 165 | + @Override |
| 166 | + public final boolean equals(Object o) { |
| 167 | + if (!(o instanceof AsyncDocumentConnectionRequest)) return false; |
| 168 | + if (!super.equals(o)) return false; |
153 | 169 |
|
154 | | - } |
| 170 | + AsyncDocumentConnectionRequest that = (AsyncDocumentConnectionRequest) o; |
| 171 | + return (docInfo == null ? that.docInfo == null : docInfo.equals(that.docInfo)) && (callback == null ? that.callback == null : callback.equals(that.callback)) && Arrays.equals(response, that.response); |
| 172 | + } |
155 | 173 |
|
| 174 | + @Override |
| 175 | + public int hashCode() { |
| 176 | + int result = super.hashCode(); |
| 177 | + result = 31 * result + (docInfo != null ? docInfo.hashCode() : 0); |
| 178 | + result = 31 * result + (callback != null ? callback.hashCode() : 0); |
| 179 | + result = 31 * result + Arrays.hashCode(response); |
| 180 | + return result; |
| 181 | + } |
| 182 | + } |
156 | 183 | } |
0 commit comments