changeset 25:13ba9f6e87a2

Codecleanup Exceptionreporthandling integrated gnv/trunk@127 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Tim Englich <tim.englich@intevation.de>
date Thu, 24 Sep 2009 12:20:27 +0000
parents ec56ef8f3e58
children 01e5ae4e7a12
files gnv/Changelog gnv/src/main/java/de/intevation/gnv/artifactdatabase/client/DefaultArtifactDatabaseClient.java
diffstat 2 files changed, 55 insertions(+), 64 deletions(-) [+]
line wrap: on
line diff
--- a/gnv/Changelog	Thu Sep 24 08:18:34 2009 +0000
+++ b/gnv/Changelog	Thu Sep 24 12:20:27 2009 +0000
@@ -1,3 +1,9 @@
+2009-09-24  Tim Englich  <tim.englich@intevation.de>
+
+    * src/main/java/de/intevation/gnv/artifactdatabase/client/DefaultArtifactDatabaseClient.java Edited:
+      Codecleanup
+      ExceptionReporthandling integrated
+      
 2009-09-24  Tim Englich  <tim.englich@intevation.de>
 
     * src/main/webapp/WEB-INF/config/log4j.properties Added:
--- a/gnv/src/main/java/de/intevation/gnv/artifactdatabase/client/DefaultArtifactDatabaseClient.java	Thu Sep 24 08:18:34 2009 +0000
+++ b/gnv/src/main/java/de/intevation/gnv/artifactdatabase/client/DefaultArtifactDatabaseClient.java	Thu Sep 24 12:20:27 2009 +0000
@@ -8,9 +8,7 @@
 import java.io.OutputStream;
 import java.util.ArrayList;
 import java.util.Collection;
-import java.util.HashMap;
 import java.util.Iterator;
-import java.util.Map;
 
 import org.apache.log4j.Logger;
 import org.restlet.Client;
@@ -55,9 +53,14 @@
      */
     private static Logger log = Logger.getLogger(DefaultArtifactDatabaseClient.class);
     
-    // TODO Container for ArtifactDatabases should be used.
-    private static Map<String, String> artifactDatabases = null;
+    /**
+     * The Databases which could be used
+     */
+    private static Collection<String> artifactDatabases = null;
     
+    /**
+     * Is the Class initialized?
+     */
     private static boolean initialized = false;
     
     /**
@@ -76,9 +79,8 @@
             this.initialize();
         }
         try {
-           XMLUtils xmlUtils = new XMLUtils();
             log.debug("DefaultArtifactDatabaseClient.getArtifactFactories");
-            Iterator<String> it = artifactDatabases.values().iterator();
+            Iterator<String> it = artifactDatabases.iterator();
             while (it.hasNext()){
                 String server = it.next();
                 String url =  server+ "/factories";
@@ -114,31 +116,19 @@
     /**
      * @throws IOException
      */
-    private Document doGetRequest(String requestUrl) throws IOException {
+    private Document doGetRequest(String requestUrl) throws IOException, ArtifactDatabaseClientException {
         return this.doGetRequest(requestUrl, null);
     }
-    /**
-     * @throws IOException
-     */
-    private Document doGetRequest(String requestUrl, Document requestBody) throws IOException {
-        XMLUtils xmlUtils = new XMLUtils();
-        Representation output = doGetRequestInternal(requestUrl, requestBody);
-        Document document = xmlUtils.readDocument(output.getStream());
-        return document;
-    }
     
     /**
      * @throws IOException
      */
-    private void doGetRequest(OutputStream outputStream, String requestUrl, Document requestBody) throws IOException {
+    private Document doGetRequest(String requestUrl, Document requestBody) throws IOException, ArtifactDatabaseClientException {
+        XMLUtils xmlUtils = new XMLUtils();
         Representation output = doGetRequestInternal(requestUrl, requestBody);
-        InputStream inputStream = output.getStream();
-        
-        byte[] buffer = new byte[4096];
-        while (inputStream.read(buffer) > 0){
-            outputStream.write(buffer);
-        }
-        
+        Document document = xmlUtils.readDocument(output.getStream());
+        this.check4ExceptionReport(document);
+        return document;
     }
 
     /**
@@ -156,6 +146,7 @@
             request.setEntity(representation);
         }
         Response response = client.handle(request);
+        // TODO RESPONSESTATUS AUSWERTEN.
         Representation output = response.getEntity();
         return output;
     }
@@ -170,6 +161,7 @@
         Representation representation = new StringRepresentation(documentBody);
         request.setEntity(representation);
         Response response = client.handle(request);
+       // TODO RESPONSESTATUS AUSWERTEN.
         Representation output = response.getEntity();
         return output.getStream();
     }
@@ -178,9 +170,9 @@
         if (!initialized){
             PropertiesReader pr = PropertiesReaderFactory.getInstance().getPropertiesReader();
             int count = Integer.parseInt(pr.getPropertieValue(ARTIFACTDATABASE_COUNT_ID, "0"));
-            this.artifactDatabases = new HashMap<String, String>(count);
+            artifactDatabases = new ArrayList<String>(count);
             for (int i = 0; i < count ; i++){
-                this.artifactDatabases.put(""+(i+1), pr.getPropertieValue(ARTIFACTDATABASE_URL_ID+"."+(i+1), "N/N"));
+                artifactDatabases.add(pr.getPropertieValue(ARTIFACTDATABASE_URL_ID+"."+(i+1), "N/N"));
             }
             initialized = true;
         }
@@ -245,39 +237,22 @@
         return node;
     }
 
-/**
- * @param artifactFactory
- * @param xmlUtils
- * @param request
- * @throws IOException
- */
-private Document doPostRequest(ArtifactObject artifactFactory,
-        Document request, String suburl) throws IOException {
-    XMLUtils xmlUtils = new XMLUtils();
-    String url = ((ArtifactFactory)artifactFactory).getDataBaseUrl();
-    InputStream is = this.doPostRequest(url+"/"+suburl, request);
-    Document result = xmlUtils.readDocument(is);
-    // TODO: Fehleranalyse des Dokumentes
-    return result;
-}
+    /**
+     * @param artifactFactory
+     * @param xmlUtils
+     * @param request
+     * @throws IOException
+     */
+    private Document doPostRequest(ArtifactObject artifactFactory,
+            Document request, String suburl) throws IOException, ArtifactDatabaseClientException {
+        XMLUtils xmlUtils = new XMLUtils();
+        String url = ((ArtifactFactory)artifactFactory).getDataBaseUrl();
+        InputStream is = this.doPostRequest(url+"/"+suburl, request);
+        Document result = xmlUtils.readDocument(is);
+        this.check4ExceptionReport(result);
+        return result;
+    }
     
-    private Document createDescribeRequestBody(ArtifactObject currentArtifact){
-        Document document = new XMLUtils().newDocument();
-        Node rootNode  = this.createRootNode(document);
-        Element typeNode = this.createArtifactElement(document, "type");
-        typeNode.setAttribute("name", "describe");
-        rootNode.appendChild(typeNode);
-             
-        Element uuidNode = this.createArtifactElement(document, "uuid");
-        uuidNode.setAttribute("value", currentArtifact.getId());
-        rootNode.appendChild(uuidNode);
-        
-        Element hashNode = this.createArtifactElement(document, "hash");
-        hashNode.setAttribute("value", currentArtifact.getHash());
-        rootNode.appendChild(hashNode);
-        
-        return document;
-    }
 
     /**
      * @see de.intevation.gnv.artifactdatabase.client.ArtifactDatabaseClient#getCurrentStepDescription(de.intevation.gnv.artifactdatabase.objects.ArtifactFactory, de.intevation.gnv.artifactdatabase.objects.ArtifactObject)
@@ -286,8 +261,6 @@
             ArtifactObject currentArtifact)
             throws ArtifactDatabaseClientException {
         try {
-            
-            Document request = this.createDescribeRequestBody(currentArtifact);
             String url = this.getArtifactUrl(artifactFactory, currentArtifact);
             Document result = this.doGetRequest(url);
             return this.readDescription(result,currentArtifact);
@@ -309,11 +282,12 @@
     }
     
     private ArtifactDescription readDescription(Document document, ArtifactObject artifact) throws ArtifactDatabaseClientException{
+        // Check if there was an Error or Exception reported from the ArtifactDatabase
+        this.check4ExceptionReport(document);
         XMLUtils xmlUtils = new XMLUtils();
         if (artifact instanceof ArtifactDescription){
             ArtifactDescription ad = (ArtifactDescription)artifact;
             Node uiNode =  xmlUtils.getNodeXPath(document, "/result/ui");
-//            uiNode = uiNode.getFirstChild();
             Node outputNode = xmlUtils.getNodeXPath(document, "/result/outputs");
             String currentState = xmlUtils.getStringXPath(document, "/result/state/@name");
             NodeList statesList = xmlUtils.getNodeSetXPath(document, "/result/reachable-states/state/@name");
@@ -361,8 +335,8 @@
             String url = this.getArtifactUrl(artifactFactory, currentArtifact);
             Document advanceDocument = this.createAdvanceRequestBody(currentArtifact, target);
             InputStream advanceResult = this.doPostRequest(url, advanceDocument);
-            // TODO feedResult auswerten und ggf. Fehler werfen.
-            // 3 Descibe
+            this.check4ExceptionReport(new XMLUtils().readDocument(advanceResult));
+            // 3 Describe
             return this.getCurrentStepDescription(artifactFactory, currentArtifact);
         } catch (IOException e) {
             log.error(e,e);
@@ -512,9 +486,20 @@
             Document feedDocument = this.createFeedRequestBody(currentArtifact, inputParameter);
             String url = this.getArtifactUrl(artifactFactory, currentArtifact);
             InputStream feedResult = this.doPostRequest(url, feedDocument);
-            // TODO feedResult auswerten und ggf. Fehler werfen.
+            Document feedResultDocument = new XMLUtils().readDocument(feedResult);
+            this.check4ExceptionReport(feedResultDocument);
         } catch (IOException e) {
             log.error(e,e);
+            throw new ArtifactDatabaseClientException(e);
         }
     }
+    
+    private void check4ExceptionReport(Document document) throws ArtifactDatabaseClientException{
+        
+        String message = new XMLUtils().getStringXPath(document,"/exceptionreport/exception");
+        if (message != null){
+            throw new ArtifactDatabaseClientException(message);
+        }
+        
+    }
 }

http://dive4elements.wald.intevation.org