Mercurial > dive4elements > gnv-client
diff gnv/src/main/java/de/intevation/gnv/artifactdatabase/client/DefaultArtifactDatabaseClient.java @ 690:254f062e334b
Added JavaDoc.
gnv/trunk@908 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Ingo Weinzierl <ingo.weinzierl@intevation.de> |
---|---|
date | Mon, 12 Apr 2010 16:45:16 +0000 |
parents | 57fa8019fbdc |
children | af22fa5567a6 |
line wrap: on
line diff
--- a/gnv/src/main/java/de/intevation/gnv/artifactdatabase/client/DefaultArtifactDatabaseClient.java Mon Apr 12 10:56:55 2010 +0000 +++ b/gnv/src/main/java/de/intevation/gnv/artifactdatabase/client/DefaultArtifactDatabaseClient.java Mon Apr 12 16:45:16 2010 +0000 @@ -63,6 +63,9 @@ import org.w3c.dom.NodeList; /** + * The default implementation of <code>ArtifactDatabaseClient</code> which + * implements methods to communicate via HTTP protocol with the artifact server. + * * @author <a href="mailto:tim.englich@intevation.de">Tim Englich</a> * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a> */ @@ -119,9 +122,6 @@ } - /** - * @see de.intevation.gnv.artifactdatabase.client.ArtifactDatabaseClient#getArtifactFactories() - */ public Collection<ArtifactObject> getArtifactFactories() throws ArtifactDatabaseClientException, ArtifactDatabaseInputException { Collection<ArtifactObject> resultValues = null; @@ -149,23 +149,31 @@ return resultValues; } + + /** + * Returns a collection of artifact factories retrieved by <i>server</i>. + * + * @param document + * @param server + * @param geometry + * @return a collection of factories. + */ private Collection<ArtifactObject> getArtifactFactories(Document document, String server, String geometry) { - XMLUtils xmlUtils = new XMLUtils(); - NodeList artifactFactories = xmlUtils.getNodeSetXPath(document, + NodeList artifactFactories = XMLUtils.getNodeSetXPath(document, "/art:result/art:factories/art:factory"); Collection<ArtifactObject> resultValues = new ArrayList<ArtifactObject>( artifactFactories.getLength()); if (artifactFactories != null) { for (int i = 0; i < artifactFactories.getLength(); i++) { Node artifactFactoryNode = artifactFactories.item(i); - String name = xmlUtils.getStringXPath(artifactFactoryNode, + String name = XMLUtils.getStringXPath(artifactFactoryNode, "@art:name"); - String description = xmlUtils.getStringXPath( + String description = XMLUtils.getStringXPath( artifactFactoryNode, "@art:description"); - NodeList parameterNodeList = xmlUtils.getNodeSetXPath( + NodeList parameterNodeList = XMLUtils.getNodeSetXPath( artifactFactoryNode, "art:parameter"); @@ -201,22 +209,18 @@ return resultValues; } - /** - * @throws IOException - */ + private Document doGetRequest(String requestUrl) throws IOException, ArtifactDatabaseClientException, ArtifactDatabaseInputException { return this.doGetRequest(requestUrl, null); } - /** - * @throws IOException - */ + private Document doGetRequest(String requestUrl, Document requestBody) throws IOException, ArtifactDatabaseClientException, ArtifactDatabaseInputException { XMLUtils xmlUtils = new XMLUtils(); Representation output = doGetRequestInternal(requestUrl, requestBody); - Document document = xmlUtils.readDocument(output.getStream()); + Document document = XMLUtils.readDocument(output.getStream()); this.check4ExceptionReport(document); return document; } @@ -225,6 +229,14 @@ return initialize(request, locale); } + /** + * Initialize the <code>ClientInfo</code> object of <i>request</i> with the + * given <i>locale</i>. + * + * @param request The request object. + * @param locale The locale. + * @return the prepared request object. + */ protected Request initialize(Request request, Locale locale) { if (locale != null) { ClientInfo clientInfo = request.getClientInfo(); @@ -239,17 +251,13 @@ return request; } - /** - * @param requestUrl - * @param requestBody - * @return - */ + private Representation doGetRequestInternal(String requestUrl, Document requestBody) throws IOException { Client client = new Client(Protocol.HTTP); Request request = initialize(new Request(Method.GET, requestUrl)); if (requestBody != null) { - String documentBody = new XMLUtils() + String documentBody = XMLUtils .writeDocument2String(requestBody); Representation representation = new StringRepresentation( documentBody); @@ -263,14 +271,12 @@ return output; } - /** - * @throws IOException - */ + private InputStream doPostRequest(String requestUrl, Document requestBody) throws IOException { Client client = new Client(Protocol.HTTP); Request request = initialize(new Request(Method.POST, requestUrl)); - String documentBody = new XMLUtils().writeDocument2String(requestBody); + String documentBody = XMLUtils.writeDocument2String(requestBody); Representation representation = new StringRepresentation(documentBody); request.setEntity(representation); Response response = client.handle(request); @@ -297,8 +303,16 @@ } + /** - * @see de.intevation.gnv.artifactdatabase.client.ArtifactDatabaseClient#createNewArtifact(de.intevation.gnv.artifactdatabase.objects.ArtifactObject) + * Creates a new artifact with the given <i>artifactFactory</i>. A POST call + * is sent to the artifact server which creates a new artifact. + * + * @param artifactFactory The artifact factory used to create a new + * artifact. + * @return the created artifact of the artifact server. + * @throws ArtifactDatabaseClientException + * @throws ArtifactDatabaseInputException */ public ArtifactObject createNewArtifact(ArtifactObject artifactFactory) throws ArtifactDatabaseClientException, ArtifactDatabaseInputException { @@ -313,16 +327,24 @@ } } + /** + * Returns a new artifact defined by uuid and hash values of the document + * returned by the artifact server after creating a new artifact. + * + * @param document Contains information about the server-side created + * artifact. + * @return a new artifact object. + */ private ArtifactObject getArtifact(Document document) { XMLUtils xmlUtils = new XMLUtils(); - String uuid = xmlUtils.getStringXPath(document, "/art:result/art:uuid/@value"); - String hash = xmlUtils.getStringXPath(document, "/art:result/art:hash/@value"); + String uuid = XMLUtils.getStringXPath(document, "/art:result/art:uuid/@value"); + String hash = XMLUtils.getStringXPath(document, "/art:result/art:hash/@value"); log.info("NEW Artifact: " + uuid + " / " + hash); return new Artifact(uuid, hash); } private Document createCreateRequestBody(ArtifactObject artifactFactory) { - Document document = new XMLUtils().newDocument(); + Document document = XMLUtils.newDocument(); Node rootNode = this.createRootNode(document); Element typeNode = this.createArtifactElement(document, "type"); typeNode.setAttribute("name", "create"); @@ -360,10 +382,7 @@ return rootNode; } - /** - * @param document - * @return - */ + private Element createArtifactElement(Document document, String name) { XMLUtils.ElementCreator creator = new XMLUtils.ElementCreator( document, @@ -374,26 +393,30 @@ return creator.create(name); } - /** - * @param artifactFactory - * @param xmlUtils - * @param request - * @throws IOException - */ + private Document doPostRequest(ArtifactObject artifactFactory, Document request, String suburl) throws IOException, ArtifactDatabaseClientException, ArtifactDatabaseInputException { XMLUtils xmlUtils = new XMLUtils(); String url = ((ArtifactFactory) artifactFactory).getDataBaseUrl(); InputStream is = this.doPostRequest(url + "/" + suburl, request); - Document result = xmlUtils.readDocument(is); + Document result = XMLUtils.readDocument(is); this.check4ExceptionReport(result); return result; } + /** - * @see de.intevation.gnv.artifactdatabase.client.ArtifactDatabaseClient#getCurrentStepDescription(de.intevation.gnv.artifactdatabase.objects.ArtifactFactory, - * de.intevation.gnv.artifactdatabase.objects.ArtifactObject) + * Retrieve the current artifact description after calling the describe + * operation of the artifact server. + * + * @param artifactFactory + * @param currentArtifact + * @param includeUI Set this option to true, if the user interface + * description is required. + * @return the artifact description. + * @throws ArtifactDatabaseClientException + * @throws ArtifactDatabaseInputException */ public ArtifactDescription getCurrentStepDescription( ArtifactObject artifactFactory, @@ -402,12 +425,11 @@ throws ArtifactDatabaseClientException, ArtifactDatabaseInputException { try { String url = this.getArtifactUrl(artifactFactory, currentArtifact); - log.debug("DESCRIBE URL: " + url); Document request = this.createDescribeRequestBody(currentArtifact, includeUI); InputStream describeResult = this.doPostRequest(url, request); - return this.readDescription(new XMLUtils().readDocument(describeResult), + return this.readDescription(XMLUtils.readDocument(describeResult), currentArtifact); } catch (IOException e) { log.error(e, e); @@ -418,8 +440,7 @@ private Document createDescribeRequestBody(ArtifactObject currentArtifact, boolean includeUI){ - - Document document = new XMLUtils().newDocument(); + Document document = XMLUtils.newDocument(); Node rootNode = this.createRootNode(document); Element typeNode = this.createArtifactElement(document, "type"); @@ -439,11 +460,7 @@ return document; } - /** - * @param artifactFactory - * @param currentArtifact - * @return - */ + private String getArtifactUrl(ArtifactObject artifactFactory, ArtifactObject currentArtifact) { String url = ((ArtifactFactory) artifactFactory).getDataBaseUrl() @@ -457,28 +474,27 @@ // 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, "/art:result/art:ui"); - Node outputNode = xmlUtils + Node uiNode = XMLUtils.getNodeXPath(document, "/art:result/art:ui"); + Node outputNode = XMLUtils .getNodeXPath(document, "/art:result/art:outputs"); Map<String, OutputMode> outputModes = null; if (outputNode != null) { - NodeList outputModesNodes = xmlUtils.getNodeSetXPath( + NodeList outputModesNodes = XMLUtils.getNodeSetXPath( outputNode, "art:output"); if (outputModesNodes != null) { outputModes = new HashMap<String, OutputMode>( outputModesNodes.getLength()); for (int i = 0; i < outputModesNodes.getLength(); i++) { Node outputModeNode = outputModesNodes.item(i); - String name = xmlUtils.getStringXPath(outputModeNode, + String name = XMLUtils.getStringXPath(outputModeNode, "@name"); - String mimeType = xmlUtils.getStringXPath( + String mimeType = XMLUtils.getStringXPath( outputModeNode, "@mime-type"); - NodeList parameterNodes = xmlUtils.getNodeSetXPath( + NodeList parameterNodes = XMLUtils.getNodeSetXPath( outputModeNode, "art:parameter/art:parameter"); Collection<OutputParameter> parameter = null; if (parameterNodes != null) { @@ -488,18 +504,18 @@ Node outputParameterNode = parameterNodes .item(j); parameter.add(new DefaultOutputParameter( - xmlUtils.getStringXPath( + XMLUtils.getStringXPath( outputParameterNode, "@name"), - xmlUtils.getStringXPath( + XMLUtils.getStringXPath( outputParameterNode, "@value"), - xmlUtils.getStringXPath( + XMLUtils.getStringXPath( outputParameterNode, "@name"), - xmlUtils.getStringXPath( + XMLUtils.getStringXPath( outputParameterNode, "@type"))); } } - NodeList exportNodes = xmlUtils.getNodeSetXPath( + NodeList exportNodes = XMLUtils.getNodeSetXPath( outputModeNode, "art:exports/art:export"); Map exports = null; @@ -509,12 +525,12 @@ for (int k = 0; k < size; k++) { Node export = exportNodes.item(k); - String expName = xmlUtils.getStringXPath( + String expName = XMLUtils.getStringXPath( export, "@name"); exports.put(expName, new DefaultExportMode( expName, - xmlUtils.getStringXPath(export, "@description"), - xmlUtils.getStringXPath(export, "@mime-type"))); + XMLUtils.getStringXPath(export, "@description"), + XMLUtils.getStringXPath(export, "@mime-type"))); } } @@ -525,9 +541,9 @@ } } - String currentState = xmlUtils.getStringXPath(document, + String currentState = XMLUtils.getStringXPath(document, "/art:result/art:state/@name"); - NodeList statesList = xmlUtils.getNodeSetXPath(document, + NodeList statesList = XMLUtils.getNodeSetXPath(document, "/art:result/art:reachable-states/art:state/@name"); Collection<String> reachableStates = new ArrayList<String>( statesList.getLength()); @@ -535,14 +551,14 @@ reachableStates.add(statesList.item(i).getNodeValue()); } - NodeList inputNodes = xmlUtils.getNodeSetXPath(document, + NodeList inputNodes = XMLUtils.getNodeSetXPath(document, "/art:result/art:model/art:input"); if (inputNodes != null) { Collection<String> inputParameter = new ArrayList<String>( inputNodes.getLength()); for (int i = 0; i < inputNodes.getLength(); i++) { Node inputNode = inputNodes.item(i); - String name = xmlUtils.getStringXPath(inputNode, "@name"); + String name = XMLUtils.getStringXPath(inputNode, "@name"); inputParameter.add(name); } ad.setInputParameter(inputParameter); @@ -562,10 +578,19 @@ } + /** - * @see de.intevation.gnv.artifactdatabase.client.ArtifactDatabaseClient#doNextStep(de.intevation.gnv.artifactdatabase.objects.ArtifactObject, - * de.intevation.gnv.artifactdatabase.objects.ArtifactObject, - * java.lang.String, java.util.Collection) + * Feed, advance and describe in one single method. + * + * @param artifactFactory The factory which created the current artifact. + * @param currentArtifact The current artifact. + * @param target The target state for advance. + * @param inputParameter Input data for feed. + * @return the artifact description after calling feed and advance. + * @throws ArtifactDatabaseClientException if a general error occured in + * the artifact server. + * @throws ArtifactDatabaseInputException if the input data was invalid used + * for feed. */ public ArtifactDescription doNextStep( ArtifactObject artifactFactory, @@ -601,6 +626,19 @@ } + /** + * Call the advance operation of the artifact server to step to the next + * step of the parameterization. + * + * @param factory The artifact factory which created the current artifact. + * @param artifact The current artifact. + * @param target The target state. + * @throws IOException if an error occured while reading/writing from/to + * stream. + * @throws ArtifactDatabaseClientException if a general error occured in + * the artifact server. + * @throws ArtifactDatabaseInputException if the input data was invalid. + */ public void doAdvance( ArtifactObject factory, ArtifactObject artifact, @@ -609,13 +647,13 @@ String url = getArtifactUrl(factory, artifact); Document advanceDocument = createAdvanceRequestBody(artifact, target); InputStream advanceResult = doPostRequest(url, advanceDocument); - check4ExceptionReport(new XMLUtils().readDocument(advanceResult)); + check4ExceptionReport(XMLUtils.readDocument(advanceResult)); } private Document createFeedRequestBody( ArtifactObject currentArtifact, Collection<InputParameter> inputParameter) { - Document document = new XMLUtils().newDocument(); + Document document = XMLUtils.newDocument(); Node rootNode = this.createRootNode(document); Element typeNode = this.createArtifactElement(document, "type"); @@ -637,11 +675,7 @@ return document; } - /** - * @param inputParameter - * @param document - * @param rootNode - */ + private Node createParameterNodes( Collection<InputParameter> inputParameter, Document document, String nodeName) { @@ -668,9 +702,17 @@ return dataNode; } + + /** + * Creates the xml body for the advance request. + * + * @param currentArtifact + * @param target + * @return the advance xml document. + */ private Document createAdvanceRequestBody(ArtifactObject currentArtifact, String target) { - Document document = new XMLUtils().newDocument(); + Document document = XMLUtils.newDocument(); Node rootNode = this.createRootNode(document); Element typeNode = this.createArtifactElement(document, "type"); @@ -691,10 +733,11 @@ } /** - * @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) + * Call the out operation of the artifact server and writes the result to + * <i>stream</i>. + * + * @param exportMode + * @param inputParameter */ public void doOutput( ArtifactObject artifactFactory, @@ -731,6 +774,15 @@ } + /** + * Call the out method of the artifact server with a specific export mode. + * + * @param artifactFactory + * @param currentArtifact + * @param out + * @throws ArtifactDatabaseClientException if an error occured while + * reading/writing from/to stream. + */ public void doExport( ArtifactObject artifactFactory, ArtifactObject currentArtifact, @@ -777,6 +829,15 @@ } + /** + * Start to import an artifact from xml document. + * + * @param factory + * @param document The artifact as xml document. + * @return A status description of the operation (success or failure). + * @throws ArtifactDatabaseClientException + * @throws IOException + */ public Document doImport( ArtifactFactory factory, Document document) @@ -789,6 +850,16 @@ } + /** + * Creates the xml request body used for an out-call of the artifact server. + * + * @param currentArtifact + * @param target + * @param exportMode + * @param mimeType + * @param inputParameter + * @return the request body. + */ private Document createOutRequestBody( ArtifactObject currentArtifact, String target, @@ -796,7 +867,7 @@ String mimeType, Collection<InputParameter> inputParameter) { - Document document = new XMLUtils().newDocument(); + Document document = XMLUtils.newDocument(); Node rootNode = this.createRootNode(document); Element typeNode = this.createArtifactElement(document, "type"); @@ -830,10 +901,16 @@ return document; } + /** - * @see de.intevation.gnv.artifactdatabase.client.ArtifactDatabaseClient#doFeed(de.intevation.gnv.artifactdatabase.objects.ArtifactObject, - * de.intevation.gnv.artifactdatabase.objects.ArtifactObject, - * java.util.Collection) + * Calls the feed operation of the artifact server. + * + * @param artifactFactory The artifact factory which created the current + * artifact. + * @param currentArtifact The current artifact. + * @param inputParameter The user input. + * @throws ArtifactDatabaseClientException + * @throws ArtifactDatabaseInputException */ public void doFeed( ArtifactObject artifactFactory, @@ -846,7 +923,7 @@ inputParameter); String url = this.getArtifactUrl(artifactFactory, currentArtifact); InputStream feedResult = this.doPostRequest(url, feedDocument); - Document feedResultDocument = new XMLUtils() + Document feedResultDocument = XMLUtils .readDocument(feedResult); this.check4ExceptionReport(feedResultDocument); } catch (IOException e) { @@ -857,7 +934,7 @@ private void check4ExceptionReport(Document document) throws ArtifactDatabaseClientException, ArtifactDatabaseInputException { - String message = new XMLUtils().getStringXPath(document, + String message = XMLUtils.getStringXPath(document, EXCEPTION_GENERAL); if (message != null) { throw new ArtifactDatabaseClientException(message); @@ -869,6 +946,19 @@ } } + + /** + * Call the specific wms export mode of the out operation of the artifact + * server. + * + * @param factory + * @param artifact + * @param inputParameter + * @return a document that contains information about the wms service and + * the created layer. + * @throws ArtifactDatabaseClientException + * @throws ArtifactDatabaseInputException + */ public Document publishWMS( ArtifactObject factory, ArtifactObject artifact, @@ -892,6 +982,7 @@ } } + public Collection<ArtifactStatisticsSet> calculateStatistics( ArtifactObject artifactFactory, ArtifactObject currentArtifact) @@ -908,10 +999,10 @@ targetName, "", "text/xml", null); XMLUtils xmlUtils = new XMLUtils(); InputStream is = this.doPostRequest(requestUrl, requestBody); - Document resultDocument = xmlUtils.readDocument(is); + Document resultDocument = XMLUtils.readDocument(is); if (resultDocument != null) { - NodeList statisticSetNodes = xmlUtils.getNodeSetXPath(resultDocument, + NodeList statisticSetNodes = XMLUtils.getNodeSetXPath(resultDocument, "/art:statistics/art:statistic"); resultValues = new ArrayList<ArtifactStatisticsSet> (statisticSetNodes.getLength()); @@ -920,7 +1011,7 @@ String name = statisticSetNode.getAttribute("name"); ArtifactStatisticsSet set = new DefaultArtifactStatisticsSet(name); - NodeList resultNodes = xmlUtils.getNodeSetXPath(statisticSetNode, + NodeList resultNodes = XMLUtils.getNodeSetXPath(statisticSetNode, "art:statistic-value"); if (resultNodes != null) { for (int j = 0; j < resultNodes.getLength(); j++) { @@ -949,9 +1040,7 @@ this.locale = locale; } - /** - * @see de.intevation.gnv.artifactdatabase.client.ArtifactDatabaseClient#getArtifactFactoryMetaInformation(java.util.Collection, java.lang.String, java.lang.String) - */ + public Collection<ArtifactObject> getArtifactFactoryMetaInformation( Collection<MapService> mapServices, String geometry, @@ -985,7 +1074,7 @@ String geometry) throws ArtifactDatabaseClientException, ArtifactDatabaseInputException { XMLUtils xmlUtils = new XMLUtils(); - Document document = xmlUtils.readDocument(inputStream); + Document document = XMLUtils.readDocument(inputStream); this.check4ExceptionReport(document); return this.getArtifactFactories(document, server,geometry); } @@ -994,7 +1083,7 @@ String geometry, String srs){ log.debug("DefaultArtifactDatabaseClient.createMetaDataRequestBody"); - Document document = new XMLUtils().newDocument(); + Document document = XMLUtils.newDocument(); Node rootNode = this.createArtifactElement(document, "GetMetaData"); document.appendChild(rootNode); @@ -1033,3 +1122,4 @@ return document; } } +// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 : \ No newline at end of file