Mercurial > dive4elements > gnv-client
comparison gnv/src/main/java/de/intevation/gnv/artifactdatabase/client/DefaultArtifactDatabaseClient.java @ 13:2535158e2687
Chartoutput Added to the View
gnv/trunk@95 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Tim Englich <tim.englich@intevation.de> |
---|---|
date | Wed, 16 Sep 2009 12:41:27 +0000 |
parents | 4ebe57b170d3 |
children | 1557bea6cb55 |
comparison
equal
deleted
inserted
replaced
12:4ebe57b170d3 | 13:2535158e2687 |
---|---|
3 */ | 3 */ |
4 package de.intevation.gnv.artifactdatabase.client; | 4 package de.intevation.gnv.artifactdatabase.client; |
5 | 5 |
6 import java.io.IOException; | 6 import java.io.IOException; |
7 import java.io.InputStream; | 7 import java.io.InputStream; |
8 import java.io.OutputStream; | |
8 import java.util.ArrayList; | 9 import java.util.ArrayList; |
9 import java.util.Collection; | 10 import java.util.Collection; |
10 import java.util.HashMap; | 11 import java.util.HashMap; |
11 import java.util.Iterator; | 12 import java.util.Iterator; |
12 import java.util.Map; | 13 import java.util.Map; |
110 | 111 |
111 /** | 112 /** |
112 * @throws IOException | 113 * @throws IOException |
113 */ | 114 */ |
114 private Document doGetRequest(String requestUrl) throws IOException { | 115 private Document doGetRequest(String requestUrl) throws IOException { |
116 return this.doGetRequest(requestUrl, null); | |
117 } | |
118 /** | |
119 * @throws IOException | |
120 */ | |
121 private Document doGetRequest(String requestUrl, Document requestBody) throws IOException { | |
115 XMLUtils xmlUtils = new XMLUtils(); | 122 XMLUtils xmlUtils = new XMLUtils(); |
116 Client client = new Client(Protocol.HTTP); | 123 Representation output = doGetRequestInternal(requestUrl, requestBody); |
117 Response response = client.get(requestUrl); | |
118 Representation output = response.getEntity(); | |
119 Document document = xmlUtils.readDocument(output.getStream()); | 124 Document document = xmlUtils.readDocument(output.getStream()); |
120 log.debug(xmlUtils.writeDocument2String(document)); | 125 log.debug(xmlUtils.writeDocument2String(document)); |
121 return document; | 126 return document; |
127 } | |
128 | |
129 /** | |
130 * @throws IOException | |
131 */ | |
132 private void doGetRequest(OutputStream outputStream, String requestUrl, Document requestBody) throws IOException { | |
133 Representation output = doGetRequestInternal(requestUrl, requestBody); | |
134 InputStream inputStream = output.getStream(); | |
135 | |
136 byte[] buffer = new byte[4096]; | |
137 while (inputStream.read(buffer) > 0){ | |
138 outputStream.write(buffer); | |
139 } | |
140 | |
141 } | |
142 | |
143 /** | |
144 * @param requestUrl | |
145 * @param requestBody | |
146 * @return | |
147 */ | |
148 private Representation doGetRequestInternal(String requestUrl, | |
149 Document requestBody) { | |
150 Client client = new Client(Protocol.HTTP); | |
151 Request request = new Request(Method.GET, requestUrl); | |
152 if (requestBody != null){ | |
153 String documentBody = new XMLUtils().writeDocument2String(requestBody); | |
154 Representation representation = new StringRepresentation(documentBody); | |
155 request.setEntity(representation); | |
156 } | |
157 Response response = client.handle(request); | |
158 Representation output = response.getEntity(); | |
159 return output; | |
122 } | 160 } |
123 | 161 |
124 /** | 162 /** |
125 * @throws IOException | 163 * @throws IOException |
126 */ | 164 */ |
312 Collection<InputParameter> inputParameter) | 350 Collection<InputParameter> inputParameter) |
313 throws ArtifactDatabaseClientException { | 351 throws ArtifactDatabaseClientException { |
314 | 352 |
315 try { | 353 try { |
316 // 1 Feed | 354 // 1 Feed |
317 Document feedDocument = this.createFeedRequestBody(currentArtifact, inputParameter); | 355 this.doFeed(artifactFactory, currentArtifact, inputParameter); |
356 // 2 Advance | |
318 String url = this.getArtifactUrl(artifactFactory, currentArtifact); | 357 String url = this.getArtifactUrl(artifactFactory, currentArtifact); |
319 InputStream feedResult = this.doPostRequest(url, feedDocument); | |
320 // TODO feedResult auswerten und ggf. Fehler werfen. | |
321 // 2 Advance | |
322 Document advanceDocument = this.createAdvanceRequestBody(currentArtifact, target); | 358 Document advanceDocument = this.createAdvanceRequestBody(currentArtifact, target); |
323 log.debug(new XMLUtils().writeDocument2String(advanceDocument)); | 359 log.debug(new XMLUtils().writeDocument2String(advanceDocument)); |
324 InputStream advanceResult = this.doPostRequest(url, advanceDocument); | 360 InputStream advanceResult = this.doPostRequest(url, advanceDocument); |
325 // TODO feedResult auswerten und ggf. Fehler werfen. | 361 // TODO feedResult auswerten und ggf. Fehler werfen. |
326 // 3 Descibe | 362 // 3 Descibe |
346 | 382 |
347 Element hashNode = this.createArtifactElement(document, "hash"); | 383 Element hashNode = this.createArtifactElement(document, "hash"); |
348 hashNode.setAttribute("value", currentArtifact.getHash()); | 384 hashNode.setAttribute("value", currentArtifact.getHash()); |
349 rootNode.appendChild(hashNode); | 385 rootNode.appendChild(hashNode); |
350 | 386 |
351 Element dataNode = this.createArtifactElement(document, "data"); | 387 Node dataNode = this.createParameterNodes(inputParameter, document, "data"); |
352 rootNode.appendChild(dataNode); | 388 rootNode.appendChild(dataNode); |
389 | |
390 return document; | |
391 } | |
392 | |
393 /** | |
394 * @param inputParameter | |
395 * @param document | |
396 * @param rootNode | |
397 */ | |
398 private Node createParameterNodes(Collection<InputParameter> inputParameter, Document document, String nodeName) { | |
399 Element dataNode = this.createArtifactElement(document, nodeName); | |
400 | |
353 if (inputParameter != null){ | 401 if (inputParameter != null){ |
354 Iterator<InputParameter> it = inputParameter.iterator(); | 402 Iterator<InputParameter> it = inputParameter.iterator(); |
355 while(it.hasNext()){ | 403 while(it.hasNext()){ |
356 InputParameter ip = it.next(); | 404 InputParameter ip = it.next(); |
357 String name = ip.getName(); | 405 String name = ip.getName(); |
365 dataNode.appendChild(inputNode); | 413 dataNode.appendChild(inputNode); |
366 } | 414 } |
367 } | 415 } |
368 } | 416 } |
369 } | 417 } |
370 | 418 return dataNode; |
371 | |
372 return document; | |
373 } | 419 } |
374 | 420 |
375 private Document createAdvanceRequestBody(ArtifactObject currentArtifact, String target){ | 421 private Document createAdvanceRequestBody(ArtifactObject currentArtifact, String target){ |
376 Document document = new XMLUtils().newDocument(); | 422 Document document = new XMLUtils().newDocument(); |
377 Node rootNode = this.createRootNode(document); | 423 Node rootNode = this.createRootNode(document); |
390 Element targetNode = this.createArtifactElement(document, "target"); | 436 Element targetNode = this.createArtifactElement(document, "target"); |
391 targetNode.setAttribute("name", target); | 437 targetNode.setAttribute("name", target); |
392 rootNode.appendChild(targetNode); | 438 rootNode.appendChild(targetNode); |
393 return document; | 439 return document; |
394 } | 440 } |
441 | |
442 /** | |
443 * @see de.intevation.gnv.artifactdatabase.client.ArtifactDatabaseClient#doOutput(de.intevation.gnv.artifactdatabase.objects.ArtifactObject, de.intevation.gnv.artifactdatabase.objects.ArtifactObject, java.io.OutputStream, java.lang.String, java.lang.String, java.util.Collection) | |
444 */ | |
445 public void doOutput(ArtifactObject artifactFactory, | |
446 ArtifactObject currentArtifact, OutputStream stream, | |
447 String targetName, String mimeType, | |
448 Collection<InputParameter> inputParameter) | |
449 throws ArtifactDatabaseClientException { | |
450 try { | |
451 XMLUtils xmlUtils = new XMLUtils(); | |
452 Document requestBody = this.createOutRequestBody(currentArtifact, targetName, mimeType, inputParameter); | |
453 log.debug(xmlUtils.writeDocument2String(requestBody)); | |
454 | |
455 String requestUrl = this.getArtifactUrl(artifactFactory, currentArtifact)+"/"+targetName; | |
456 InputStream is = this.doPostRequest( requestUrl, requestBody); | |
457 | |
458 byte[] b = new byte[4096]; | |
459 int i = -1 ; | |
460 while ((i = is.read(b)) > 0) | |
461 { | |
462 stream.write(b, 0, i); | |
463 } | |
464 } catch (IOException e) { | |
465 log.error(e,e); | |
466 throw new ArtifactDatabaseClientException(e); | |
467 } | |
468 } | |
469 | |
470 private Document createOutRequestBody(ArtifactObject currentArtifact, String target, String mimeType, Collection<InputParameter> inputParameter){ | |
471 Document document = new XMLUtils().newDocument(); | |
472 Node rootNode = this.createRootNode(document); | |
473 | |
474 Element typeNode = this.createArtifactElement(document, "type"); | |
475 typeNode.setAttribute("name", "out"); | |
476 rootNode.appendChild(typeNode); | |
477 | |
478 Element uuidNode = this.createArtifactElement(document, "uuid"); | |
479 uuidNode.setAttribute("value", currentArtifact.getId()); | |
480 rootNode.appendChild(uuidNode); | |
481 | |
482 Element hashNode = this.createArtifactElement(document, "hash"); | |
483 hashNode.setAttribute("value", currentArtifact.getHash()); | |
484 rootNode.appendChild(hashNode); | |
485 | |
486 Element outNode = this.createArtifactElement(document, "out"); | |
487 outNode.setAttribute("name", target); | |
488 rootNode.appendChild(outNode); | |
489 | |
490 Element mimeTypeNode = this.createArtifactElement(document, "out"); | |
491 mimeTypeNode.setAttribute("value", mimeType); | |
492 outNode.appendChild(mimeTypeNode); | |
493 | |
494 Node parameterNode = this.createParameterNodes(inputParameter, document, "params"); | |
495 outNode.appendChild(parameterNode); | |
496 | |
497 return document; | |
395 } | 498 } |
499 | |
500 /** | |
501 * @see de.intevation.gnv.artifactdatabase.client.ArtifactDatabaseClient#doFeed(de.intevation.gnv.artifactdatabase.objects.ArtifactObject, de.intevation.gnv.artifactdatabase.objects.ArtifactObject, java.util.Collection) | |
502 */ | |
503 public void doFeed(ArtifactObject artifactFactory, | |
504 ArtifactObject currentArtifact, | |
505 Collection<InputParameter> inputParameter) | |
506 throws ArtifactDatabaseClientException { | |
507 | |
508 try { | |
509 Document feedDocument = this.createFeedRequestBody(currentArtifact, inputParameter); | |
510 String url = this.getArtifactUrl(artifactFactory, currentArtifact); | |
511 InputStream feedResult = this.doPostRequest(url, feedDocument); | |
512 // TODO feedResult auswerten und ggf. Fehler werfen. | |
513 } catch (IOException e) { | |
514 log.error(e,e); | |
515 } | |
516 } | |
517 } |