comparison gnv/src/main/java/de/intevation/gnv/artifactdatabase/client/DefaultArtifactDatabaseClient.java @ 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 b11f941f7eb2
children 07e9c137a2f1
comparison
equal deleted inserted replaced
24:ec56ef8f3e58 25:13ba9f6e87a2
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.io.OutputStream;
9 import java.util.ArrayList; 9 import java.util.ArrayList;
10 import java.util.Collection; 10 import java.util.Collection;
11 import java.util.HashMap;
12 import java.util.Iterator; 11 import java.util.Iterator;
13 import java.util.Map;
14 12
15 import org.apache.log4j.Logger; 13 import org.apache.log4j.Logger;
16 import org.restlet.Client; 14 import org.restlet.Client;
17 import org.restlet.data.Method; 15 import org.restlet.data.Method;
18 import org.restlet.data.Protocol; 16 import org.restlet.data.Protocol;
53 /** 51 /**
54 * the logger, used to log exceptions and additonaly information 52 * the logger, used to log exceptions and additonaly information
55 */ 53 */
56 private static Logger log = Logger.getLogger(DefaultArtifactDatabaseClient.class); 54 private static Logger log = Logger.getLogger(DefaultArtifactDatabaseClient.class);
57 55
58 // TODO Container for ArtifactDatabases should be used. 56 /**
59 private static Map<String, String> artifactDatabases = null; 57 * The Databases which could be used
60 58 */
59 private static Collection<String> artifactDatabases = null;
60
61 /**
62 * Is the Class initialized?
63 */
61 private static boolean initialized = false; 64 private static boolean initialized = false;
62 65
63 /** 66 /**
64 * Constructor 67 * Constructor
65 */ 68 */
74 Collection<ArtifactObject> resultValues = null; 77 Collection<ArtifactObject> resultValues = null;
75 if (!initialized){ 78 if (!initialized){
76 this.initialize(); 79 this.initialize();
77 } 80 }
78 try { 81 try {
79 XMLUtils xmlUtils = new XMLUtils();
80 log.debug("DefaultArtifactDatabaseClient.getArtifactFactories"); 82 log.debug("DefaultArtifactDatabaseClient.getArtifactFactories");
81 Iterator<String> it = artifactDatabases.values().iterator(); 83 Iterator<String> it = artifactDatabases.iterator();
82 while (it.hasNext()){ 84 while (it.hasNext()){
83 String server = it.next(); 85 String server = it.next();
84 String url = server+ "/factories"; 86 String url = server+ "/factories";
85 Document document = this.doGetRequest(url); 87 Document document = this.doGetRequest(url);
86 if (resultValues == null){ 88 if (resultValues == null){
112 } 114 }
113 115
114 /** 116 /**
115 * @throws IOException 117 * @throws IOException
116 */ 118 */
117 private Document doGetRequest(String requestUrl) throws IOException { 119 private Document doGetRequest(String requestUrl) throws IOException, ArtifactDatabaseClientException {
118 return this.doGetRequest(requestUrl, null); 120 return this.doGetRequest(requestUrl, null);
119 } 121 }
122
120 /** 123 /**
121 * @throws IOException 124 * @throws IOException
122 */ 125 */
123 private Document doGetRequest(String requestUrl, Document requestBody) throws IOException { 126 private Document doGetRequest(String requestUrl, Document requestBody) throws IOException, ArtifactDatabaseClientException {
124 XMLUtils xmlUtils = new XMLUtils(); 127 XMLUtils xmlUtils = new XMLUtils();
125 Representation output = doGetRequestInternal(requestUrl, requestBody); 128 Representation output = doGetRequestInternal(requestUrl, requestBody);
126 Document document = xmlUtils.readDocument(output.getStream()); 129 Document document = xmlUtils.readDocument(output.getStream());
130 this.check4ExceptionReport(document);
127 return document; 131 return document;
128 }
129
130 /**
131 * @throws IOException
132 */
133 private void doGetRequest(OutputStream outputStream, String requestUrl, Document requestBody) throws IOException {
134 Representation output = doGetRequestInternal(requestUrl, requestBody);
135 InputStream inputStream = output.getStream();
136
137 byte[] buffer = new byte[4096];
138 while (inputStream.read(buffer) > 0){
139 outputStream.write(buffer);
140 }
141
142 } 132 }
143 133
144 /** 134 /**
145 * @param requestUrl 135 * @param requestUrl
146 * @param requestBody 136 * @param requestBody
154 String documentBody = new XMLUtils().writeDocument2String(requestBody); 144 String documentBody = new XMLUtils().writeDocument2String(requestBody);
155 Representation representation = new StringRepresentation(documentBody); 145 Representation representation = new StringRepresentation(documentBody);
156 request.setEntity(representation); 146 request.setEntity(representation);
157 } 147 }
158 Response response = client.handle(request); 148 Response response = client.handle(request);
149 // TODO RESPONSESTATUS AUSWERTEN.
159 Representation output = response.getEntity(); 150 Representation output = response.getEntity();
160 return output; 151 return output;
161 } 152 }
162 153
163 /** 154 /**
168 Request request = new Request(Method.POST, requestUrl); 159 Request request = new Request(Method.POST, requestUrl);
169 String documentBody = new XMLUtils().writeDocument2String(requestBody); 160 String documentBody = new XMLUtils().writeDocument2String(requestBody);
170 Representation representation = new StringRepresentation(documentBody); 161 Representation representation = new StringRepresentation(documentBody);
171 request.setEntity(representation); 162 request.setEntity(representation);
172 Response response = client.handle(request); 163 Response response = client.handle(request);
164 // TODO RESPONSESTATUS AUSWERTEN.
173 Representation output = response.getEntity(); 165 Representation output = response.getEntity();
174 return output.getStream(); 166 return output.getStream();
175 } 167 }
176 168
177 private synchronized void initialize(){ 169 private synchronized void initialize(){
178 if (!initialized){ 170 if (!initialized){
179 PropertiesReader pr = PropertiesReaderFactory.getInstance().getPropertiesReader(); 171 PropertiesReader pr = PropertiesReaderFactory.getInstance().getPropertiesReader();
180 int count = Integer.parseInt(pr.getPropertieValue(ARTIFACTDATABASE_COUNT_ID, "0")); 172 int count = Integer.parseInt(pr.getPropertieValue(ARTIFACTDATABASE_COUNT_ID, "0"));
181 this.artifactDatabases = new HashMap<String, String>(count); 173 artifactDatabases = new ArrayList<String>(count);
182 for (int i = 0; i < count ; i++){ 174 for (int i = 0; i < count ; i++){
183 this.artifactDatabases.put(""+(i+1), pr.getPropertieValue(ARTIFACTDATABASE_URL_ID+"."+(i+1), "N/N")); 175 artifactDatabases.add(pr.getPropertieValue(ARTIFACTDATABASE_URL_ID+"."+(i+1), "N/N"));
184 } 176 }
185 initialized = true; 177 initialized = true;
186 } 178 }
187 179
188 } 180 }
243 Element node = document.createElementNS(NAMESPACE_URI, name); 235 Element node = document.createElementNS(NAMESPACE_URI, name);
244 node.setPrefix(NAMESPACE_PREFIX); 236 node.setPrefix(NAMESPACE_PREFIX);
245 return node; 237 return node;
246 } 238 }
247 239
248 /** 240 /**
249 * @param artifactFactory 241 * @param artifactFactory
250 * @param xmlUtils 242 * @param xmlUtils
251 * @param request 243 * @param request
252 * @throws IOException 244 * @throws IOException
253 */ 245 */
254 private Document doPostRequest(ArtifactObject artifactFactory, 246 private Document doPostRequest(ArtifactObject artifactFactory,
255 Document request, String suburl) throws IOException { 247 Document request, String suburl) throws IOException, ArtifactDatabaseClientException {
256 XMLUtils xmlUtils = new XMLUtils(); 248 XMLUtils xmlUtils = new XMLUtils();
257 String url = ((ArtifactFactory)artifactFactory).getDataBaseUrl(); 249 String url = ((ArtifactFactory)artifactFactory).getDataBaseUrl();
258 InputStream is = this.doPostRequest(url+"/"+suburl, request); 250 InputStream is = this.doPostRequest(url+"/"+suburl, request);
259 Document result = xmlUtils.readDocument(is); 251 Document result = xmlUtils.readDocument(is);
260 // TODO: Fehleranalyse des Dokumentes 252 this.check4ExceptionReport(result);
261 return result; 253 return result;
262 } 254 }
263 255
264 private Document createDescribeRequestBody(ArtifactObject currentArtifact){
265 Document document = new XMLUtils().newDocument();
266 Node rootNode = this.createRootNode(document);
267 Element typeNode = this.createArtifactElement(document, "type");
268 typeNode.setAttribute("name", "describe");
269 rootNode.appendChild(typeNode);
270
271 Element uuidNode = this.createArtifactElement(document, "uuid");
272 uuidNode.setAttribute("value", currentArtifact.getId());
273 rootNode.appendChild(uuidNode);
274
275 Element hashNode = this.createArtifactElement(document, "hash");
276 hashNode.setAttribute("value", currentArtifact.getHash());
277 rootNode.appendChild(hashNode);
278
279 return document;
280 }
281 256
282 /** 257 /**
283 * @see de.intevation.gnv.artifactdatabase.client.ArtifactDatabaseClient#getCurrentStepDescription(de.intevation.gnv.artifactdatabase.objects.ArtifactFactory, de.intevation.gnv.artifactdatabase.objects.ArtifactObject) 258 * @see de.intevation.gnv.artifactdatabase.client.ArtifactDatabaseClient#getCurrentStepDescription(de.intevation.gnv.artifactdatabase.objects.ArtifactFactory, de.intevation.gnv.artifactdatabase.objects.ArtifactObject)
284 */ 259 */
285 public ArtifactDescription getCurrentStepDescription(ArtifactObject artifactFactory, 260 public ArtifactDescription getCurrentStepDescription(ArtifactObject artifactFactory,
286 ArtifactObject currentArtifact) 261 ArtifactObject currentArtifact)
287 throws ArtifactDatabaseClientException { 262 throws ArtifactDatabaseClientException {
288 try { 263 try {
289
290 Document request = this.createDescribeRequestBody(currentArtifact);
291 String url = this.getArtifactUrl(artifactFactory, currentArtifact); 264 String url = this.getArtifactUrl(artifactFactory, currentArtifact);
292 Document result = this.doGetRequest(url); 265 Document result = this.doGetRequest(url);
293 return this.readDescription(result,currentArtifact); 266 return this.readDescription(result,currentArtifact);
294 } catch (IOException e) { 267 } catch (IOException e) {
295 log.error(e,e); 268 log.error(e,e);
307 String url = ((ArtifactFactory)artifactFactory).getDataBaseUrl()+"/artifact/"+ currentArtifact.getId(); 280 String url = ((ArtifactFactory)artifactFactory).getDataBaseUrl()+"/artifact/"+ currentArtifact.getId();
308 return url; 281 return url;
309 } 282 }
310 283
311 private ArtifactDescription readDescription(Document document, ArtifactObject artifact) throws ArtifactDatabaseClientException{ 284 private ArtifactDescription readDescription(Document document, ArtifactObject artifact) throws ArtifactDatabaseClientException{
285 // Check if there was an Error or Exception reported from the ArtifactDatabase
286 this.check4ExceptionReport(document);
312 XMLUtils xmlUtils = new XMLUtils(); 287 XMLUtils xmlUtils = new XMLUtils();
313 if (artifact instanceof ArtifactDescription){ 288 if (artifact instanceof ArtifactDescription){
314 ArtifactDescription ad = (ArtifactDescription)artifact; 289 ArtifactDescription ad = (ArtifactDescription)artifact;
315 Node uiNode = xmlUtils.getNodeXPath(document, "/result/ui"); 290 Node uiNode = xmlUtils.getNodeXPath(document, "/result/ui");
316 // uiNode = uiNode.getFirstChild();
317 Node outputNode = xmlUtils.getNodeXPath(document, "/result/outputs"); 291 Node outputNode = xmlUtils.getNodeXPath(document, "/result/outputs");
318 String currentState = xmlUtils.getStringXPath(document, "/result/state/@name"); 292 String currentState = xmlUtils.getStringXPath(document, "/result/state/@name");
319 NodeList statesList = xmlUtils.getNodeSetXPath(document, "/result/reachable-states/state/@name"); 293 NodeList statesList = xmlUtils.getNodeSetXPath(document, "/result/reachable-states/state/@name");
320 Collection<String> reachableStates = new ArrayList<String>(statesList.getLength()); 294 Collection<String> reachableStates = new ArrayList<String>(statesList.getLength());
321 for (int i = 0; i < statesList.getLength(); i++){ 295 for (int i = 0; i < statesList.getLength(); i++){
359 this.doFeed(artifactFactory, currentArtifact, inputParameter); 333 this.doFeed(artifactFactory, currentArtifact, inputParameter);
360 // 2 Advance 334 // 2 Advance
361 String url = this.getArtifactUrl(artifactFactory, currentArtifact); 335 String url = this.getArtifactUrl(artifactFactory, currentArtifact);
362 Document advanceDocument = this.createAdvanceRequestBody(currentArtifact, target); 336 Document advanceDocument = this.createAdvanceRequestBody(currentArtifact, target);
363 InputStream advanceResult = this.doPostRequest(url, advanceDocument); 337 InputStream advanceResult = this.doPostRequest(url, advanceDocument);
364 // TODO feedResult auswerten und ggf. Fehler werfen. 338 this.check4ExceptionReport(new XMLUtils().readDocument(advanceResult));
365 // 3 Descibe 339 // 3 Describe
366 return this.getCurrentStepDescription(artifactFactory, currentArtifact); 340 return this.getCurrentStepDescription(artifactFactory, currentArtifact);
367 } catch (IOException e) { 341 } catch (IOException e) {
368 log.error(e,e); 342 log.error(e,e);
369 throw new ArtifactDatabaseClientException(e); 343 throw new ArtifactDatabaseClientException(e);
370 } 344 }
510 484
511 try { 485 try {
512 Document feedDocument = this.createFeedRequestBody(currentArtifact, inputParameter); 486 Document feedDocument = this.createFeedRequestBody(currentArtifact, inputParameter);
513 String url = this.getArtifactUrl(artifactFactory, currentArtifact); 487 String url = this.getArtifactUrl(artifactFactory, currentArtifact);
514 InputStream feedResult = this.doPostRequest(url, feedDocument); 488 InputStream feedResult = this.doPostRequest(url, feedDocument);
515 // TODO feedResult auswerten und ggf. Fehler werfen. 489 Document feedResultDocument = new XMLUtils().readDocument(feedResult);
490 this.check4ExceptionReport(feedResultDocument);
516 } catch (IOException e) { 491 } catch (IOException e) {
517 log.error(e,e); 492 log.error(e,e);
518 } 493 throw new ArtifactDatabaseClientException(e);
494 }
495 }
496
497 private void check4ExceptionReport(Document document) throws ArtifactDatabaseClientException{
498
499 String message = new XMLUtils().getStringXPath(document,"/exceptionreport/exception");
500 if (message != null){
501 throw new ArtifactDatabaseClientException(message);
502 }
503
519 } 504 }
520 } 505 }

http://dive4elements.wald.intevation.org