Mercurial > dive4elements > http-client
comparison src/main/java/org/dive4elements/artifacts/httpclient/http/HttpClientImpl.java @ 96:4ffbc9f79905 3.0.17
Removed encoding handling from http client.
author | Sascha L. Teichmann <teichmann@intevation.de> |
---|---|
date | Thu, 31 Oct 2013 19:00:13 +0100 |
parents | 31dc80c48936 |
children | c92b50c9c99a e602a29f1dcc |
comparison
equal
deleted
inserted
replaced
95:bacb661e6d3d | 96:4ffbc9f79905 |
---|---|
5 * Read the file LGPL.txt coming with the software for details | 5 * Read the file LGPL.txt coming with the software for details |
6 * or visit http://www.gnu.org/licenses/ if it does not exist. | 6 * or visit http://www.gnu.org/licenses/ if it does not exist. |
7 */ | 7 */ |
8 package org.dive4elements.artifacts.httpclient.http; | 8 package org.dive4elements.artifacts.httpclient.http; |
9 | 9 |
10 import java.io.BufferedReader; | |
11 import java.io.BufferedWriter; | |
12 import java.io.InputStream; | 10 import java.io.InputStream; |
13 import java.io.InputStreamReader; | |
14 import java.io.IOException; | 11 import java.io.IOException; |
15 import java.io.OutputStream; | 12 import java.io.OutputStream; |
16 import java.io.OutputStreamWriter; | |
17 | |
18 import java.nio.charset.Charset; | |
19 | 13 |
20 import java.util.ArrayList; | 14 import java.util.ArrayList; |
21 import java.util.List; | 15 import java.util.List; |
22 | 16 |
23 import org.apache.log4j.Logger; | 17 import org.apache.log4j.Logger; |
44 import org.dive4elements.artifacts.httpclient.objects.Artifact; | 38 import org.dive4elements.artifacts.httpclient.objects.Artifact; |
45 import org.dive4elements.artifacts.httpclient.objects.ArtifactFactory; | 39 import org.dive4elements.artifacts.httpclient.objects.ArtifactFactory; |
46 import org.dive4elements.artifacts.httpclient.utils.ArtifactProtocolUtils; | 40 import org.dive4elements.artifacts.httpclient.utils.ArtifactProtocolUtils; |
47 import org.dive4elements.artifacts.httpclient.utils.ArtifactCreator; | 41 import org.dive4elements.artifacts.httpclient.utils.ArtifactCreator; |
48 | 42 |
49 | |
50 /** | 43 /** |
51 * Client to artifact-server. | 44 * Client to artifact-server. |
52 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a> | 45 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a> |
53 */ | 46 */ |
54 public class HttpClientImpl implements HttpClient { | 47 public class HttpClientImpl implements HttpClient { |
82 | 75 |
83 private String serverUrl; | 76 private String serverUrl; |
84 | 77 |
85 private String localeString; | 78 private String localeString; |
86 | 79 |
87 /** | |
88 * The Charset used for encoding the collectionOut output. | |
89 * If not set, the system/java default will be used (UTF-8). | |
90 */ | |
91 private Charset charset; | |
92 | 80 |
93 private static final ThreadLocal<Client> CLIENT = | 81 private static final ThreadLocal<Client> CLIENT = |
94 new ThreadLocal<Client>() { | 82 new ThreadLocal<Client>() { |
95 @Override | 83 @Override |
96 protected Client initialValue() { | 84 protected Client initialValue() { |
99 } | 87 } |
100 }; | 88 }; |
101 | 89 |
102 public HttpClientImpl(String serverUrl) { | 90 public HttpClientImpl(String serverUrl) { |
103 this.serverUrl = serverUrl; | 91 this.serverUrl = serverUrl; |
104 setOutEncoding(null); | |
105 } | |
106 | |
107 | |
108 /** | |
109 * Sets the encoding. | |
110 * | |
111 * Currently the encoding is only | |
112 * respected in the collectionOut-method! | |
113 * | |
114 * It was implemented to allow csv export to be in another than the | |
115 * sensible default (UTF-8) and allow easier integration | |
116 * e.g. in proprietary products. | |
117 */ | |
118 public void setOutEncoding(Charset charset) { | |
119 this.charset = charset; | |
120 } | 92 } |
121 | 93 |
122 | 94 |
123 /** | 95 /** |
124 * This constructor might be used to modify the request's locale manually. | 96 * This constructor might be used to modify the request's locale manually. |
292 body); | 264 body); |
293 | 265 |
294 request.setEntity(representation); | 266 request.setEntity(representation); |
295 Response response = client.handle(request); | 267 Response response = client.handle(request); |
296 | 268 |
269 logger.debug("RESPONSE: " + response); | |
270 | |
297 Status status = response.getStatus(); | 271 Status status = response.getStatus(); |
298 if (status.getCode() != 200) { | 272 if (status.getCode() != 200) { |
299 logger.error("Response status: " + status.getCode()); | 273 logger.error("Response status: " + status.getCode()); |
300 throw new IOException(status.getDescription()); | 274 throw new IOException(status.getDescription()); |
301 } | 275 } |
435 catch (IOException ioe) { | 409 catch (IOException ioe) { |
436 throw new ConnectionException(ioe.getMessage(), ioe); | 410 throw new ConnectionException(ioe.getMessage(), ioe); |
437 } | 411 } |
438 } | 412 } |
439 | 413 |
440 | |
441 /** | |
442 * Write out() operation of a Collection to <i>out</i>, using buffered | |
443 * reading and writing and a charset. | |
444 * | |
445 * @param in collection out to read from. | |
446 * @param out The OutputStream to write transcoded answer to. | |
447 */ | |
448 private void collectionOutEnc( | |
449 InputStream in, | |
450 OutputStream out) | |
451 throws ConnectionException | |
452 { | |
453 try { | |
454 BufferedReader reader = | |
455 new BufferedReader(new InputStreamReader(in)); | |
456 | |
457 BufferedWriter writer = | |
458 new BufferedWriter(new OutputStreamWriter(out, this.charset)); | |
459 | |
460 try { | |
461 char[] c = new char[4096]; | |
462 int i; | |
463 while ((i = reader.read(c)) >= 0) { | |
464 writer.write(c, 0, i); | |
465 } | |
466 } | |
467 finally { | |
468 writer.flush(); | |
469 in.close(); | |
470 } | |
471 } | |
472 catch (IOException ioe) { | |
473 throw new ConnectionException(ioe.getMessage(), ioe); | |
474 } | |
475 } | |
476 | |
477 | |
478 /** | 414 /** |
479 * Write out() operation of a Collection to <i>out</i>. | 415 * Write out() operation of a Collection to <i>out</i>. |
480 * | 416 * |
481 * @param doc The request document for the out() operation. | 417 * @param doc The request document for the out() operation. |
482 * @param uuid The identifier of the Collection. | 418 * @param uuid The identifier of the Collection. |
490 OutputStream out) | 426 OutputStream out) |
491 throws ConnectionException | 427 throws ConnectionException |
492 { | 428 { |
493 try { | 429 try { |
494 InputStream stream = collectionOut(doc, uuid, type); | 430 InputStream stream = collectionOut(doc, uuid, type); |
495 | |
496 // Transcode if charset is given. | |
497 if (charset != null) { | |
498 collectionOutEnc(stream, out); | |
499 return; | |
500 } | |
501 | 431 |
502 try { | 432 try { |
503 byte[] b = new byte[4096]; | 433 byte[] b = new byte[4096]; |
504 int i; | 434 int i; |
505 while ((i = stream.read(b)) >= 0) { | 435 while ((i = stream.read(b)) >= 0) { |