Mercurial > dive4elements > http-client
comparison src/main/java/de/intevation/artifacts/httpclient/http/HttpClientImpl.java @ 21:79a5a2455d6d
Use Apache HTTP Client as the underlaying transport agent. Use thread local instances to re-use agents.
http-client/trunk@2042 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Wed, 01 Jun 2011 14:32:24 +0000 |
parents | 5cb8513b9e78 |
children | c4431f39926a |
comparison
equal
deleted
inserted
replaced
20:c1f6e2636c5b | 21:79a5a2455d6d |
---|---|
68 | 68 |
69 private String serverUrl; | 69 private String serverUrl; |
70 | 70 |
71 private String localeString; | 71 private String localeString; |
72 | 72 |
73 private static final ThreadLocal<Client> CLIENT = | |
74 new ThreadLocal<Client>() { | |
75 @Override | |
76 protected Client initialValue() { | |
77 logger.debug("create new HTTP client"); | |
78 return new Client(Protocol.HTTP); | |
79 } | |
80 }; | |
73 | 81 |
74 public HttpClientImpl(String serverUrl) { | 82 public HttpClientImpl(String serverUrl) { |
75 this.serverUrl = serverUrl; | 83 this.serverUrl = serverUrl; |
76 } | 84 } |
77 | 85 |
234 //============================== | 242 //============================== |
235 // HTTP specific methods | 243 // HTTP specific methods |
236 //============================== | 244 //============================== |
237 | 245 |
238 private Response doPost(String url, Document body) throws IOException { | 246 private Response doPost(String url, Document body) throws IOException { |
239 logger.info("Start HTTP-POST request to: "+ url); | 247 if (logger.isDebugEnabled()) { |
240 | 248 logger.debug("Start HTTP-POST request to: " + url); |
241 Client client = new Client(Protocol.HTTP); | 249 } |
250 | |
251 Client client = getClient(); | |
242 Request request = prepareRequest(Method.POST, url); | 252 Request request = prepareRequest(Method.POST, url); |
243 | 253 |
244 Representation representation = new DomRepresentation( | 254 Representation representation = new DomRepresentation( |
245 MediaType.APPLICATION_XML, | 255 MediaType.APPLICATION_XML, |
246 body); | 256 body); |
256 | 266 |
257 return response; | 267 return response; |
258 } | 268 } |
259 | 269 |
260 | 270 |
271 private static Client getClient() { | |
272 return CLIENT.get(); | |
273 } | |
274 | |
275 | |
261 private Response doGet(String url) throws IOException { | 276 private Response doGet(String url) throws IOException { |
262 logger.info("Start HTTP-POST request to: "+ url); | 277 if (logger.isDebugEnabled()) { |
263 | 278 logger.debug("Start HTTP-POST request to: "+ url); |
264 Client client = new Client(Protocol.HTTP); | 279 } |
280 | |
281 Client client = getClient(); | |
265 Request request = prepareRequest(Method.GET, url); | 282 Request request = prepareRequest(Method.GET, url); |
266 | 283 |
267 Response response = client.handle(request); | 284 Response response = client.handle(request); |
268 | 285 |
269 Status status = response.getStatus(); | 286 Status status = response.getStatus(); |
311 new ArrayList<Preference<Language>>(); | 328 new ArrayList<Preference<Language>>(); |
312 | 329 |
313 Language lang = Language.valueOf(localeString); | 330 Language lang = Language.valueOf(localeString); |
314 | 331 |
315 if (lang != null) { | 332 if (lang != null) { |
316 logger.info("Set locale of the request object: " + lang.toString()); | 333 if (logger.isDebugEnabled()) { |
334 logger.debug( | |
335 "Set locale of the request object: " + lang.toString()); | |
336 } | |
317 | 337 |
318 Preference<Language> pref = new Preference<Language>(); | 338 Preference<Language> pref = new Preference<Language>(); |
319 pref.setMetadata(lang); | 339 pref.setMetadata(lang); |
320 accepted.add(pref); | 340 accepted.add(pref); |
321 | 341 |
453 *******************************/ | 473 *******************************/ |
454 | 474 |
455 public Document callService(String url, String service, Document input) | 475 public Document callService(String url, String service, Document input) |
456 throws ConnectionException | 476 throws ConnectionException |
457 { | 477 { |
458 logger.info("Start service call to '" + service + "'"); | 478 if (logger.isDebugEnabled()) { |
459 | 479 logger.debug("Start service call to '" + service + "'"); |
460 DocumentResponseHandler handler = new DocumentResponseHandler(); | 480 } |
461 | 481 |
462 try { | 482 DocumentResponseHandler handler = new DocumentResponseHandler(); |
463 String serverUrl = url + PATH_SERVICE + "/" + service; | 483 |
464 return (Document) handler.handle(doPost(serverUrl, input)); | 484 try { |
465 } | 485 String serverUrl = url + PATH_SERVICE + "/" + service; |
466 catch (IOException ioe) { | 486 return (Document) handler.handle(doPost(serverUrl, input)); |
467 throw new ConnectionException( | 487 } |
468 "Connection to server failed: " + ioe.getMessage()); | 488 catch (IOException ioe) { |
469 } | 489 throw new ConnectionException( |
490 "Connection to server failed: " + ioe.getMessage()); | |
491 } | |
470 } | 492 } |
471 | 493 |
472 | 494 |
473 /******************************* | 495 /******************************* |
474 * Users API | 496 * Users API |