/
HttpOutgoingTransport.java
348 lines (298 loc) · 12.4 KB
/
HttpOutgoingTransport.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
/**
* Licensed to JumpMind Inc under one or more contributor
* license agreements. See the NOTICE file distributed
* with this work for additional information regarding
* copyright ownership. JumpMind Inc licenses this file
* to you under the GNU General Public License, version 3.0 (GPLv3)
* (the "License"); you may not use this file except in compliance
* with the License.
*
* You should have received a copy of the GNU General Public License,
* version 3.0 (GPLv3) along with this library; if not, see
* <http://www.gnu.org/licenses/>.
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.jumpmind.symmetric.transport.http;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.Map;
import java.util.zip.GZIPOutputStream;
import org.apache.commons.io.IOUtils;
import org.jumpmind.exception.HttpException;
import org.jumpmind.exception.IoException;
import org.jumpmind.symmetric.io.IoConstants;
import org.jumpmind.symmetric.model.ChannelMap;
import org.jumpmind.symmetric.model.Node;
import org.jumpmind.symmetric.service.IConfigurationService;
import org.jumpmind.symmetric.service.RegistrationRequiredException;
import org.jumpmind.symmetric.transport.AuthenticationException;
import org.jumpmind.symmetric.transport.ConnectionRejectedException;
import org.jumpmind.symmetric.transport.IOutgoingWithResponseTransport;
import org.jumpmind.symmetric.transport.NoReservationException;
import org.jumpmind.symmetric.transport.ServiceUnavailableException;
import org.jumpmind.symmetric.transport.SyncDisabledException;
import org.jumpmind.symmetric.web.WebConstants;
public class HttpOutgoingTransport implements IOutgoingWithResponseTransport {
static final String CRLF = "\r\n";
private String boundary;
private URL url;
private OutputStream os;
private BufferedWriter writer;
private BufferedReader reader;
private HttpURLConnection connection;
private int httpTimeout;
private boolean useCompression;
private int compressionStrategy;
private int compressionLevel;
private String basicAuthUsername;
private String basicAuthPassword;
private boolean streamOutputEnabled = false;
private int streamOutputChunkSize = 30720;
private boolean fileUpload = false;
private Map<String, String> requestProperties;
public HttpOutgoingTransport(URL url, int httpTimeout, boolean useCompression,
int compressionStrategy, int compressionLevel, String basicAuthUsername,
String basicAuthPassword, boolean streamOutputEnabled, int streamOutputSize,
boolean fileUpload) {
this.url = url;
this.httpTimeout = httpTimeout;
this.useCompression = useCompression;
this.compressionLevel = compressionLevel;
this.compressionStrategy = compressionStrategy;
this.basicAuthUsername = basicAuthUsername;
this.basicAuthPassword = basicAuthPassword;
this.streamOutputChunkSize = streamOutputSize;
this.streamOutputEnabled = streamOutputEnabled;
this.fileUpload = fileUpload;
}
public HttpOutgoingTransport(URL url, int httpTimeout, boolean useCompression,
int compressionStrategy, int compressionLevel, String basicAuthUsername,
String basicAuthPassword, boolean streamOutputEnabled, int streamOutputSize,
boolean fileUpload, Map<String, String> requestProperties) {
this.url = url;
this.httpTimeout = httpTimeout;
this.useCompression = useCompression;
this.compressionLevel = compressionLevel;
this.compressionStrategy = compressionStrategy;
this.basicAuthUsername = basicAuthUsername;
this.basicAuthPassword = basicAuthPassword;
this.streamOutputChunkSize = streamOutputSize;
this.streamOutputEnabled = streamOutputEnabled;
this.fileUpload = fileUpload;
this.requestProperties = requestProperties;
}
public void close() {
closeWriter(true);
closeOutputStream(true);
closeReader();
if (connection != null) {
connection.disconnect();
connection = null;
}
}
private void closeReader() {
if (reader != null) {
IOUtils.closeQuietly(reader);
reader = null;
}
}
private void closeOutputStream(boolean closeQuietly) {
if (os != null) {
try {
if (fileUpload) {
IOUtils.write(CRLF + "--" + boundary + "--" + CRLF, os);
}
os.flush();
} catch (IOException ex) {
throw new IoException(ex);
} finally {
if (closeQuietly) {
IOUtils.closeQuietly(os);
} else {
try {
os.close();
} catch (IOException ex) {
throw new IoException(ex);
}
}
os = null;
}
}
}
private void closeWriter(boolean closeQuietly) {
if (writer != null) {
try {
if (fileUpload) {
IOUtils.write(CRLF + "--" + boundary + "--" + CRLF, os);
}
writer.flush();
} catch (IOException ex) {
throw new IoException(ex);
} finally {
if (closeQuietly) {
IOUtils.closeQuietly(writer);
} else {
try {
writer.close();
} catch (IOException ex) {
throw new IoException(ex);
}
}
writer = null;
os = null;
}
}
}
/**
* Before streaming data to the remote node, make sure it is ok to. We have
* found that we can be more efficient on a push by relying on HTTP
* keep-alive.
*
* @throws IOException
* @throws {@link ConnectionRejectedException}
* @throws {@link AuthenticationException}
*/
private HttpURLConnection requestReservation(String queue) {
try {
connection = HttpTransportManager.openConnection(url, basicAuthUsername,
basicAuthPassword);
connection.setUseCaches(false);
connection.setConnectTimeout(httpTimeout);
connection.setReadTimeout(httpTimeout);
connection.setRequestMethod("HEAD");
connection.setRequestProperty(WebConstants.CHANNEL_QUEUE, queue);
analyzeResponseCode(connection.getResponseCode());
} catch (IOException ex) {
throw new IoException(ex);
}
return connection;
}
public OutputStream openStream() {
try {
connection = HttpTransportManager.openConnection(url, basicAuthUsername,
basicAuthPassword);
if (streamOutputEnabled) {
connection.setChunkedStreamingMode(streamOutputChunkSize);
}
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setUseCaches(false);
connection.setConnectTimeout(httpTimeout);
connection.setReadTimeout(httpTimeout);
if (this.requestProperties != null) {
for (Map.Entry<String, String> requestProperty : this.requestProperties.entrySet()) {
connection.setRequestProperty(requestProperty.getKey(), requestProperty.getValue());
}
}
boundary = Long.toHexString(System.currentTimeMillis());
if (!fileUpload) {
connection.setRequestMethod("PUT");
connection.setRequestProperty("Accept-Encoding", "gzip");
if (useCompression) {
connection.addRequestProperty("Content-Type", "gzip"); // application/x-gzip?
}
} else {
connection.setRequestProperty("Content-Type", "multipart/form-data; boundary="
+ boundary);
}
os = connection.getOutputStream();
if (!fileUpload && useCompression) {
os = new GZIPOutputStream(os, 128, true) {
{
this.def.setLevel(compressionLevel);
this.def.setStrategy(compressionStrategy);
}
};
}
if (fileUpload) {
final String fileName = "file.zip";
IOUtils.write("--" + boundary + CRLF, os);
IOUtils.write("Content-Disposition: form-data; name=\"binaryFile\"; filename=\""
+ fileName + "\"" + CRLF, os);
IOUtils.write("Content-Type: " + URLConnection.guessContentTypeFromName(fileName)
+ CRLF, os);
IOUtils.write("Content-Transfer-Encoding: binary" + CRLF + CRLF, os);
os.flush();
}
return os;
} catch (IOException ex) {
throw new IoException(ex);
}
}
public BufferedWriter openWriter() {
try {
OutputStreamWriter wout = new OutputStreamWriter(openStream(), IoConstants.ENCODING);
writer = new BufferedWriter(wout);
return writer;
} catch (IOException ex) {
throw new IoException(ex);
}
}
@Override
public BufferedWriter getWriter() {
return writer;
}
/**
* @throws {@link ConnectionRejectedException}
* @throws {@link AuthenticationException}
*/
private void analyzeResponseCode(int code) throws IOException {
if (WebConstants.SC_SERVICE_BUSY == code) {
throw new ConnectionRejectedException();
} else if (WebConstants.SC_SERVICE_UNAVAILABLE == code) {
throw new ServiceUnavailableException();
} else if (WebConstants.SC_NO_RESERVATION == code) {
throw new NoReservationException();
} else if (WebConstants.SC_FORBIDDEN == code) {
throw new AuthenticationException();
} else if (WebConstants.SYNC_DISABLED == code) {
throw new SyncDisabledException();
} else if (WebConstants.REGISTRATION_REQUIRED == code) {
throw new RegistrationRequiredException();
} else if (code != WebConstants.SC_OK) {
throw new HttpException(code, "Received an unexpected response code of " + code + " from the server");
}
}
public BufferedReader readResponse() throws IOException {
closeWriter(false);
closeOutputStream(false);
analyzeResponseCode(connection.getResponseCode());
this.reader = HttpTransportManager.getReaderFrom(connection);
return this.reader;
}
public boolean isOpen() {
return connection != null;
}
public ChannelMap getSuspendIgnoreChannelLists(IConfigurationService configurationService, String queue, Node targetNode) {
HttpURLConnection connection = requestReservation(queue);
// Connection contains remote suspend/ignore channels list if
// reservation was successful.
ChannelMap suspendIgnoreChannelsList = new ChannelMap();
String suspends = connection.getHeaderField(WebConstants.SUSPENDED_CHANNELS);
String ignores = connection.getHeaderField(WebConstants.IGNORED_CHANNELS);
suspendIgnoreChannelsList.addSuspendChannels(suspends);
suspendIgnoreChannelsList.addIgnoreChannels(ignores);
ChannelMap localSuspendIgnoreChannelsList = configurationService
.getSuspendIgnoreChannelLists(targetNode.getNodeId());
suspendIgnoreChannelsList.addSuspendChannels(
localSuspendIgnoreChannelsList.getSuspendChannels());
suspendIgnoreChannelsList.addIgnoreChannels(
localSuspendIgnoreChannelsList.getIgnoreChannels());
return suspendIgnoreChannelsList;
}
public HttpURLConnection getConnection() {
return connection;
}
}