comparison gnv/src/main/java/de/intevation/gnv/artifactdatabase/client/DefaultArtifactDatabaseClient.java @ 38:fccf90761825

Format Code to max 80 Chars per Row better Formatter choosen gnv/trunk@174 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Tim Englich <tim.englich@intevation.de>
date Fri, 02 Oct 2009 09:10:36 +0000
parents ad381cc47217
children 23a4f196d7d6
comparison
equal deleted inserted replaced
37:1ea46f542d07 38:fccf90761825
81 81
82 /** 82 /**
83 * @see de.intevation.gnv.artifactdatabase.client.ArtifactDatabaseClient#getArtifactFactories() 83 * @see de.intevation.gnv.artifactdatabase.client.ArtifactDatabaseClient#getArtifactFactories()
84 */ 84 */
85 public Collection<ArtifactObject> getArtifactFactories() 85 public Collection<ArtifactObject> getArtifactFactories()
86 throws ArtifactDatabaseClientException { 86 throws ArtifactDatabaseClientException {
87 Collection<ArtifactObject> resultValues = null; 87 Collection<ArtifactObject> resultValues = null;
88 if (!initialized) { 88 if (!initialized) {
89 this.initialize(); 89 this.initialize();
90 } 90 }
91 try { 91 try {
107 } 107 }
108 return resultValues; 108 return resultValues;
109 } 109 }
110 110
111 private Collection<ArtifactObject> getArtifactFactories(Document document, 111 private Collection<ArtifactObject> getArtifactFactories(Document document,
112 String server) { 112 String server) {
113 XMLUtils xmlUtils = new XMLUtils(); 113 XMLUtils xmlUtils = new XMLUtils();
114 NodeList artifactFactories = xmlUtils.getNodeSetXPath(document, 114 NodeList artifactFactories = xmlUtils.getNodeSetXPath(document,
115 "/result/factories/factory"); 115 "/result/factories/factory");
116 Collection<ArtifactObject> resultValues = new ArrayList<ArtifactObject>( 116 Collection<ArtifactObject> resultValues = new ArrayList<ArtifactObject>(
117 artifactFactories.getLength()); 117 artifactFactories.getLength());
132 132
133 /** 133 /**
134 * @throws IOException 134 * @throws IOException
135 */ 135 */
136 private Document doGetRequest(String requestUrl) throws IOException, 136 private Document doGetRequest(String requestUrl) throws IOException,
137 ArtifactDatabaseClientException { 137 ArtifactDatabaseClientException {
138 return this.doGetRequest(requestUrl, null); 138 return this.doGetRequest(requestUrl, null);
139 } 139 }
140 140
141 /** 141 /**
142 * @throws IOException 142 * @throws IOException
143 */ 143 */
144 private Document doGetRequest(String requestUrl, Document requestBody) 144 private Document doGetRequest(String requestUrl, Document requestBody)
145 throws IOException, ArtifactDatabaseClientException { 145 throws IOException,
146 ArtifactDatabaseClientException {
146 XMLUtils xmlUtils = new XMLUtils(); 147 XMLUtils xmlUtils = new XMLUtils();
147 Representation output = doGetRequestInternal(requestUrl, requestBody); 148 Representation output = doGetRequestInternal(requestUrl, requestBody);
148 Document document = xmlUtils.readDocument(output.getStream()); 149 Document document = xmlUtils.readDocument(output.getStream());
149 this.check4ExceptionReport(document); 150 this.check4ExceptionReport(document);
150 return document; 151 return document;
154 * @param requestUrl 155 * @param requestUrl
155 * @param requestBody 156 * @param requestBody
156 * @return 157 * @return
157 */ 158 */
158 private Representation doGetRequestInternal(String requestUrl, 159 private Representation doGetRequestInternal(String requestUrl,
159 Document requestBody) { 160 Document requestBody) {
160 Client client = new Client(Protocol.HTTP); 161 Client client = new Client(Protocol.HTTP);
161 Request request = new Request(Method.GET, requestUrl); 162 Request request = new Request(Method.GET, requestUrl);
162 if (requestBody != null) { 163 if (requestBody != null) {
163 String documentBody = new XMLUtils() 164 String documentBody = new XMLUtils()
164 .writeDocument2String(requestBody); 165 .writeDocument2String(requestBody);
174 175
175 /** 176 /**
176 * @throws IOException 177 * @throws IOException
177 */ 178 */
178 private InputStream doPostRequest(String requestUrl, Document requestBody) 179 private InputStream doPostRequest(String requestUrl, Document requestBody)
179 throws IOException { 180 throws IOException {
180 log.debug("##################################################"); 181 log.debug("##################################################");
181 log.debug(new XMLUtils().writeDocument2String(requestBody)); 182 log.debug(new XMLUtils().writeDocument2String(requestBody));
182 log.debug("##################################################"); 183 log.debug("##################################################");
183 Client client = new Client(Protocol.HTTP); 184 Client client = new Client(Protocol.HTTP);
184 Request request = new Request(Method.POST, requestUrl); 185 Request request = new Request(Method.POST, requestUrl);
209 210
210 /** 211 /**
211 * @see de.intevation.gnv.artifactdatabase.client.ArtifactDatabaseClient#createNewArtifact(de.intevation.gnv.artifactdatabase.objects.ArtifactObject) 212 * @see de.intevation.gnv.artifactdatabase.client.ArtifactDatabaseClient#createNewArtifact(de.intevation.gnv.artifactdatabase.objects.ArtifactObject)
212 */ 213 */
213 public ArtifactObject createNewArtifact(ArtifactObject artifactFactory) 214 public ArtifactObject createNewArtifact(ArtifactObject artifactFactory)
214 throws ArtifactDatabaseClientException { 215 throws ArtifactDatabaseClientException {
215 216
216 try { 217 try {
217 Document request = this.createCreateRequestBody(artifactFactory 218 Document request = this.createCreateRequestBody(artifactFactory
218 .getId()); 219 .getId());
219 Document result = doPostRequest(artifactFactory, request, "create"); 220 Document result = doPostRequest(artifactFactory, request, "create");
267 * @param xmlUtils 268 * @param xmlUtils
268 * @param request 269 * @param request
269 * @throws IOException 270 * @throws IOException
270 */ 271 */
271 private Document doPostRequest(ArtifactObject artifactFactory, 272 private Document doPostRequest(ArtifactObject artifactFactory,
272 Document request, String suburl) throws IOException, 273 Document request, String suburl)
273 ArtifactDatabaseClientException { 274 throws IOException,
275 ArtifactDatabaseClientException {
274 XMLUtils xmlUtils = new XMLUtils(); 276 XMLUtils xmlUtils = new XMLUtils();
275 String url = ((ArtifactFactory) artifactFactory).getDataBaseUrl(); 277 String url = ((ArtifactFactory) artifactFactory).getDataBaseUrl();
276 InputStream is = this.doPostRequest(url + "/" + suburl, request); 278 InputStream is = this.doPostRequest(url + "/" + suburl, request);
277 Document result = xmlUtils.readDocument(is); 279 Document result = xmlUtils.readDocument(is);
278 this.check4ExceptionReport(result); 280 this.check4ExceptionReport(result);
282 /** 284 /**
283 * @see de.intevation.gnv.artifactdatabase.client.ArtifactDatabaseClient#getCurrentStepDescription(de.intevation.gnv.artifactdatabase.objects.ArtifactFactory, 285 * @see de.intevation.gnv.artifactdatabase.client.ArtifactDatabaseClient#getCurrentStepDescription(de.intevation.gnv.artifactdatabase.objects.ArtifactFactory,
284 * de.intevation.gnv.artifactdatabase.objects.ArtifactObject) 286 * de.intevation.gnv.artifactdatabase.objects.ArtifactObject)
285 */ 287 */
286 public ArtifactDescription getCurrentStepDescription( 288 public ArtifactDescription getCurrentStepDescription(
287 ArtifactObject artifactFactory, ArtifactObject currentArtifact) 289 ArtifactObject artifactFactory,
288 throws ArtifactDatabaseClientException { 290 ArtifactObject currentArtifact)
291 throws ArtifactDatabaseClientException {
289 try { 292 try {
290 String url = this.getArtifactUrl(artifactFactory, currentArtifact); 293 String url = this.getArtifactUrl(artifactFactory, currentArtifact);
291 Document result = this.doGetRequest(url); 294 Document result = this.doGetRequest(url);
292 return this.readDescription(result, currentArtifact); 295 return this.readDescription(result, currentArtifact);
293 } catch (IOException e) { 296 } catch (IOException e) {
300 * @param artifactFactory 303 * @param artifactFactory
301 * @param currentArtifact 304 * @param currentArtifact
302 * @return 305 * @return
303 */ 306 */
304 private String getArtifactUrl(ArtifactObject artifactFactory, 307 private String getArtifactUrl(ArtifactObject artifactFactory,
305 ArtifactObject currentArtifact) { 308 ArtifactObject currentArtifact) {
306 String url = ((ArtifactFactory) artifactFactory).getDataBaseUrl() 309 String url = ((ArtifactFactory) artifactFactory).getDataBaseUrl()
307 + "/artifact/" + currentArtifact.getId(); 310 + "/artifact/" + currentArtifact.getId();
308 return url; 311 return url;
309 } 312 }
310 313
311 private ArtifactDescription readDescription(Document document, 314 private ArtifactDescription readDescription(Document document,
312 ArtifactObject artifact) throws ArtifactDatabaseClientException { 315 ArtifactObject artifact)
316 throws ArtifactDatabaseClientException {
313 // Check if there was an Error or Exception reported from the 317 // Check if there was an Error or Exception reported from the
314 // ArtifactDatabase 318 // ArtifactDatabase
315 this.check4ExceptionReport(document); 319 this.check4ExceptionReport(document);
316 XMLUtils xmlUtils = new XMLUtils(); 320 XMLUtils xmlUtils = new XMLUtils();
317 if (artifact instanceof ArtifactDescription) { 321 if (artifact instanceof ArtifactDescription) {
401 /** 405 /**
402 * @see de.intevation.gnv.artifactdatabase.client.ArtifactDatabaseClient#doNextStep(de.intevation.gnv.artifactdatabase.objects.ArtifactObject, 406 * @see de.intevation.gnv.artifactdatabase.client.ArtifactDatabaseClient#doNextStep(de.intevation.gnv.artifactdatabase.objects.ArtifactObject,
403 * de.intevation.gnv.artifactdatabase.objects.ArtifactObject, 407 * de.intevation.gnv.artifactdatabase.objects.ArtifactObject,
404 * java.lang.String, java.util.Collection) 408 * java.lang.String, java.util.Collection)
405 */ 409 */
406 public ArtifactDescription doNextStep(ArtifactObject artifactFactory, 410 public ArtifactDescription doNextStep(
407 ArtifactObject currentArtifact, String target, 411 ArtifactObject artifactFactory,
408 Collection<InputParameter> inputParameter) 412 ArtifactObject currentArtifact,
409 throws ArtifactDatabaseClientException { 413 String target,
414 Collection<InputParameter> inputParameter)
415 throws ArtifactDatabaseClientException {
410 416
411 try { 417 try {
412 // 1 Feed 418 // 1 Feed
413 this.doFeed(artifactFactory, currentArtifact, inputParameter); 419 this.doFeed(artifactFactory, currentArtifact, inputParameter);
414 // 2 Advance 420 // 2 Advance
426 log.error(e, e); 432 log.error(e, e);
427 throw new ArtifactDatabaseClientException(e); 433 throw new ArtifactDatabaseClientException(e);
428 } 434 }
429 } 435 }
430 436
431 private Document createFeedRequestBody(ArtifactObject currentArtifact, 437 private Document createFeedRequestBody(
432 Collection<InputParameter> inputParameter) { 438 ArtifactObject currentArtifact,
439 Collection<InputParameter> inputParameter) {
433 Document document = new XMLUtils().newDocument(); 440 Document document = new XMLUtils().newDocument();
434 Node rootNode = this.createRootNode(document); 441 Node rootNode = this.createRootNode(document);
435 442
436 Element typeNode = this.createArtifactElement(document, "type"); 443 Element typeNode = this.createArtifactElement(document, "type");
437 typeNode.setAttribute("name", "feed"); 444 typeNode.setAttribute("name", "feed");
456 * @param inputParameter 463 * @param inputParameter
457 * @param document 464 * @param document
458 * @param rootNode 465 * @param rootNode
459 */ 466 */
460 private Node createParameterNodes( 467 private Node createParameterNodes(
461 Collection<InputParameter> inputParameter, Document document, 468 Collection<InputParameter> inputParameter,
462 String nodeName) { 469 Document document, String nodeName) {
463 Element dataNode = this.createArtifactElement(document, nodeName); 470 Element dataNode = this.createArtifactElement(document, nodeName);
464 471
465 if (inputParameter != null) { 472 if (inputParameter != null) {
466 Iterator<InputParameter> it = inputParameter.iterator(); 473 Iterator<InputParameter> it = inputParameter.iterator();
467 while (it.hasNext()) { 474 while (it.hasNext()) {
482 } 489 }
483 return dataNode; 490 return dataNode;
484 } 491 }
485 492
486 private Document createAdvanceRequestBody(ArtifactObject currentArtifact, 493 private Document createAdvanceRequestBody(ArtifactObject currentArtifact,
487 String target) { 494 String target) {
488 Document document = new XMLUtils().newDocument(); 495 Document document = new XMLUtils().newDocument();
489 Node rootNode = this.createRootNode(document); 496 Node rootNode = this.createRootNode(document);
490 497
491 Element typeNode = this.createArtifactElement(document, "type"); 498 Element typeNode = this.createArtifactElement(document, "type");
492 typeNode.setAttribute("name", "advance"); 499 typeNode.setAttribute("name", "advance");
510 * de.intevation.gnv.artifactdatabase.objects.ArtifactObject, 517 * de.intevation.gnv.artifactdatabase.objects.ArtifactObject,
511 * java.io.OutputStream, java.lang.String, java.lang.String, 518 * java.io.OutputStream, java.lang.String, java.lang.String,
512 * java.util.Collection) 519 * java.util.Collection)
513 */ 520 */
514 public void doOutput(ArtifactObject artifactFactory, 521 public void doOutput(ArtifactObject artifactFactory,
515 ArtifactObject currentArtifact, OutputStream stream, 522 ArtifactObject currentArtifact, OutputStream stream,
516 String targetName, String mimeType, 523 String targetName, String mimeType,
517 Collection<InputParameter> inputParameter) 524 Collection<InputParameter> inputParameter)
518 throws ArtifactDatabaseClientException { 525 throws ArtifactDatabaseClientException {
519 try { 526 try {
520 XMLUtils xmlUtils = new XMLUtils(); 527 XMLUtils xmlUtils = new XMLUtils();
521 Document requestBody = this.createOutRequestBody(currentArtifact, 528 Document requestBody = this.createOutRequestBody(currentArtifact,
522 targetName, mimeType, inputParameter); 529 targetName, mimeType, inputParameter);
523 530
524 String requestUrl = this.getArtifactUrl(artifactFactory, 531 String requestUrl = this.getArtifactUrl(artifactFactory,
525 currentArtifact) 532 currentArtifact)
526 + "/" + targetName; 533 + "/" + targetName;
527 InputStream is = this.doPostRequest(requestUrl, requestBody); 534 InputStream is = this.doPostRequest(requestUrl, requestBody);
528 535
529 byte[] b = new byte[4096]; 536 byte[] b = new byte[4096];
530 int i = -1; 537 int i = -1;
531 while ((i = is.read(b)) > 0) { 538 while ((i = is.read(b)) > 0) {
535 log.error(e, e); 542 log.error(e, e);
536 throw new ArtifactDatabaseClientException(e); 543 throw new ArtifactDatabaseClientException(e);
537 } 544 }
538 } 545 }
539 546
540 private Document createOutRequestBody(ArtifactObject currentArtifact, 547 private Document createOutRequestBody(
541 String target, String mimeType, 548 ArtifactObject currentArtifact,
542 Collection<InputParameter> inputParameter) { 549 String target,
550 String mimeType,
551 Collection<InputParameter> inputParameter) {
543 Document document = new XMLUtils().newDocument(); 552 Document document = new XMLUtils().newDocument();
544 Node rootNode = this.createRootNode(document); 553 Node rootNode = this.createRootNode(document);
545 554
546 Element typeNode = this.createArtifactElement(document, "type"); 555 Element typeNode = this.createArtifactElement(document, "type");
547 typeNode.setAttribute("name", "out"); 556 typeNode.setAttribute("name", "out");
574 * @see de.intevation.gnv.artifactdatabase.client.ArtifactDatabaseClient#doFeed(de.intevation.gnv.artifactdatabase.objects.ArtifactObject, 583 * @see de.intevation.gnv.artifactdatabase.client.ArtifactDatabaseClient#doFeed(de.intevation.gnv.artifactdatabase.objects.ArtifactObject,
575 * de.intevation.gnv.artifactdatabase.objects.ArtifactObject, 584 * de.intevation.gnv.artifactdatabase.objects.ArtifactObject,
576 * java.util.Collection) 585 * java.util.Collection)
577 */ 586 */
578 public void doFeed(ArtifactObject artifactFactory, 587 public void doFeed(ArtifactObject artifactFactory,
579 ArtifactObject currentArtifact, 588 ArtifactObject currentArtifact,
580 Collection<InputParameter> inputParameter) 589 Collection<InputParameter> inputParameter)
581 throws ArtifactDatabaseClientException { 590 throws ArtifactDatabaseClientException {
582 591
583 try { 592 try {
584 Document feedDocument = this.createFeedRequestBody(currentArtifact, 593 Document feedDocument = this.createFeedRequestBody(currentArtifact,
585 inputParameter); 594 inputParameter);
586 String url = this.getArtifactUrl(artifactFactory, currentArtifact); 595 String url = this.getArtifactUrl(artifactFactory, currentArtifact);
593 throw new ArtifactDatabaseClientException(e); 602 throw new ArtifactDatabaseClientException(e);
594 } 603 }
595 } 604 }
596 605
597 private void check4ExceptionReport(Document document) 606 private void check4ExceptionReport(Document document)
598 throws ArtifactDatabaseClientException { 607 throws ArtifactDatabaseClientException {
599 608
600 String message = new XMLUtils().getStringXPath(document, 609 String message = new XMLUtils().getStringXPath(document,
601 "/exceptionreport/exception"); 610 "/exceptionreport/exception");
602 if (message != null) { 611 if (message != null) {
603 throw new ArtifactDatabaseClientException(message); 612 throw new ArtifactDatabaseClientException(message);
604 } 613 }
605 614
606 } 615 }
607 616
608 public Collection<ArtifactStatisticValue> calculateStatistics( 617 public Collection<ArtifactStatisticValue> calculateStatistics(
609 ArtifactObject artifactFactory, ArtifactObject currentArtifact) 618 ArtifactObject artifactFactory,
610 throws ArtifactDatabaseClientException { 619 ArtifactObject currentArtifact)
620 throws ArtifactDatabaseClientException {
611 log.debug("DefaultArtifactDatabaseClient.calculateStatistics"); 621 log.debug("DefaultArtifactDatabaseClient.calculateStatistics");
612 Collection<ArtifactStatisticValue> result; 622 Collection<ArtifactStatisticValue> result;
613 try { 623 try {
614 result = null; 624 result = null;
615 String targetName = "statistics"; 625 String targetName = "statistics";
616 String requestUrl = this.getArtifactUrl(artifactFactory, 626 String requestUrl = this.getArtifactUrl(artifactFactory,
617 currentArtifact) 627 currentArtifact)
618 + "/" + targetName; 628 + "/" + targetName;
619 Document requestBody = this.createOutRequestBody(currentArtifact, 629 Document requestBody = this.createOutRequestBody(currentArtifact,
620 targetName, "text/xml", null); 630 targetName, "text/xml", null);
621 XMLUtils xmlUtils = new XMLUtils(); 631 XMLUtils xmlUtils = new XMLUtils();
622 InputStream is = this.doPostRequest(requestUrl, requestBody); 632 InputStream is = this.doPostRequest(requestUrl, requestBody);
623 Document resultDocument = xmlUtils.readDocument(is); 633 Document resultDocument = xmlUtils.readDocument(is);

http://dive4elements.wald.intevation.org