Mercurial > dive4elements > gnv-client
diff gnv/src/main/java/de/intevation/gnv/artifactdatabase/client/DefaultArtifactDatabaseClient.java @ 7:fe6a64545552
Support for creating an Artifact integrated
gnv/trunk@83 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Tim Englich <tim.englich@intevation.de> |
---|---|
date | Mon, 14 Sep 2009 13:46:25 +0000 |
parents | 5e94403971af |
children | a4cb6d175a6e |
line wrap: on
line diff
--- a/gnv/src/main/java/de/intevation/gnv/artifactdatabase/client/DefaultArtifactDatabaseClient.java Fri Sep 11 15:25:43 2009 +0000 +++ b/gnv/src/main/java/de/intevation/gnv/artifactdatabase/client/DefaultArtifactDatabaseClient.java Mon Sep 14 13:46:25 2009 +0000 @@ -30,12 +30,14 @@ import org.restlet.data.Request; import org.restlet.data.Response; import org.restlet.representation.Representation; +import org.restlet.representation.StringRepresentation; import org.w3c.dom.Document; +import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; -import org.xml.sax.SAXException; import de.intevation.gnv.artifactdatabase.client.exception.ArtifactDatabaseClientException; +import de.intevation.gnv.artifactdatabase.objects.Artifact; import de.intevation.gnv.artifactdatabase.objects.ArtifactFactory; import de.intevation.gnv.artifactdatabase.objects.ArtifactObject; import de.intevation.gnv.util.XMLUtils; @@ -46,6 +48,16 @@ */ public class DefaultArtifactDatabaseClient implements ArtifactDatabaseClient { /** + * The URI of the namespace of the artifacts. + */ + public final static String NAMESPACE_URI = "http://www.intevation.de/2009/artifacts"; + + /** + * The XML prefix for the artifacts namespace. + */ + public final static String NAMESPACE_PREFIX = "art"; + + /** * the logger, used to log exceptions and additonaly information */ private static Logger log = Logger.getLogger(DefaultArtifactDatabaseClient.class); @@ -119,6 +131,20 @@ return output.getStream(); } + /** + * @throws IOException + */ + private InputStream doPostRequest(String requestUrl, Document requestBody) throws IOException { + Client client = new Client(Protocol.HTTP); + Request request = new Request(Method.POST, requestUrl); + String documentBody = new XMLUtils().writeDocument2String(requestBody); + Representation representation = new StringRepresentation(documentBody); + request.setEntity(representation); + Response response = client.handle(request); + Representation output = response.getEntity(); + return output.getStream(); + } + private synchronized void initialize(){ if (!initialized){ this.artifactDatabases = new HashMap<String, String>(); @@ -127,6 +153,68 @@ } } + + /** + * @see de.intevation.gnv.artifactdatabase.client.ArtifactDatabaseClient#createNewArtifact(de.intevation.gnv.artifactdatabase.objects.ArtifactObject) + */ + public ArtifactObject createNewArtifact(ArtifactObject artifactFactory) + throws ArtifactDatabaseClientException { + + try { + XMLUtils xmlUtils = new XMLUtils(); + Document request = this.createCreateRequestBody(artifactFactory.getId()); + String url = ((ArtifactFactory)artifactFactory).getDataBaseUrl(); + InputStream is = this.doPostRequest(url+"/create", request); + Document result = xmlUtils.readDocument(is); + // TODO: Fehleranalyse des Dokumentes + log.debug(xmlUtils.writeDocument2String(result)); + return this.getArtifact(result); + } catch (IOException e) { + log.error(e,e); + throw new ArtifactDatabaseClientException(e); + } + } + + private ArtifactObject getArtifact(Document document){ + XMLUtils xmlUtils = new XMLUtils(); + String uuid = xmlUtils.getStringXPath(document, "/result/uuid/@value"); + String hash = xmlUtils.getStringXPath(document, "/result/hash/@value"); + log.info("NEW Artifact: "+uuid+" / "+hash); + return new Artifact(uuid, hash); + } + + + + + private Document createCreateRequestBody(String artifactFactoryName){ + Document document = new XMLUtils().newDocument(); + Node rootNode = this.createRootNode(document); + Element typeNode = this.createArtifactElement(document, "type"); + typeNode.setAttribute("name", "create"); + rootNode.appendChild(typeNode); + + Element factoyNode = this.createArtifactElement(document, "factory"); + factoyNode.setAttribute("name", artifactFactoryName); + rootNode.appendChild(factoyNode); + + return document; + } + + private Element createRootNode(Document document){ + Element rootNode = this.createArtifactElement(document,"action"); + document.appendChild(rootNode); + return rootNode; + } + + /** + * @param document + * @return + */ + private Element createArtifactElement(Document document, String name) { + Element node = document.createElementNS(NAMESPACE_URI, name); + node.setPrefix(NAMESPACE_PREFIX); + return node; + } }