Mercurial > dive4elements > gnv-client
comparison 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 |
comparison
equal
deleted
inserted
replaced
6:6592580ae823 | 7:fe6a64545552 |
---|---|
28 import org.restlet.data.Method; | 28 import org.restlet.data.Method; |
29 import org.restlet.data.Protocol; | 29 import org.restlet.data.Protocol; |
30 import org.restlet.data.Request; | 30 import org.restlet.data.Request; |
31 import org.restlet.data.Response; | 31 import org.restlet.data.Response; |
32 import org.restlet.representation.Representation; | 32 import org.restlet.representation.Representation; |
33 import org.restlet.representation.StringRepresentation; | |
33 import org.w3c.dom.Document; | 34 import org.w3c.dom.Document; |
35 import org.w3c.dom.Element; | |
34 import org.w3c.dom.Node; | 36 import org.w3c.dom.Node; |
35 import org.w3c.dom.NodeList; | 37 import org.w3c.dom.NodeList; |
36 import org.xml.sax.SAXException; | |
37 | 38 |
38 import de.intevation.gnv.artifactdatabase.client.exception.ArtifactDatabaseClientException; | 39 import de.intevation.gnv.artifactdatabase.client.exception.ArtifactDatabaseClientException; |
40 import de.intevation.gnv.artifactdatabase.objects.Artifact; | |
39 import de.intevation.gnv.artifactdatabase.objects.ArtifactFactory; | 41 import de.intevation.gnv.artifactdatabase.objects.ArtifactFactory; |
40 import de.intevation.gnv.artifactdatabase.objects.ArtifactObject; | 42 import de.intevation.gnv.artifactdatabase.objects.ArtifactObject; |
41 import de.intevation.gnv.util.XMLUtils; | 43 import de.intevation.gnv.util.XMLUtils; |
42 | 44 |
43 /** | 45 /** |
44 * @author Tim Englich <tim.englich@intevation.de> | 46 * @author Tim Englich <tim.englich@intevation.de> |
45 * | 47 * |
46 */ | 48 */ |
47 public class DefaultArtifactDatabaseClient implements ArtifactDatabaseClient { | 49 public class DefaultArtifactDatabaseClient implements ArtifactDatabaseClient { |
50 /** | |
51 * The URI of the namespace of the artifacts. | |
52 */ | |
53 public final static String NAMESPACE_URI = "http://www.intevation.de/2009/artifacts"; | |
54 | |
55 /** | |
56 * The XML prefix for the artifacts namespace. | |
57 */ | |
58 public final static String NAMESPACE_PREFIX = "art"; | |
59 | |
48 /** | 60 /** |
49 * the logger, used to log exceptions and additonaly information | 61 * the logger, used to log exceptions and additonaly information |
50 */ | 62 */ |
51 private static Logger log = Logger.getLogger(DefaultArtifactDatabaseClient.class); | 63 private static Logger log = Logger.getLogger(DefaultArtifactDatabaseClient.class); |
52 | 64 |
117 Response response = client.handle(request); | 129 Response response = client.handle(request); |
118 Representation output = response.getEntity(); | 130 Representation output = response.getEntity(); |
119 return output.getStream(); | 131 return output.getStream(); |
120 } | 132 } |
121 | 133 |
134 /** | |
135 * @throws IOException | |
136 */ | |
137 private InputStream doPostRequest(String requestUrl, Document requestBody) throws IOException { | |
138 Client client = new Client(Protocol.HTTP); | |
139 Request request = new Request(Method.POST, requestUrl); | |
140 String documentBody = new XMLUtils().writeDocument2String(requestBody); | |
141 Representation representation = new StringRepresentation(documentBody); | |
142 request.setEntity(representation); | |
143 Response response = client.handle(request); | |
144 Representation output = response.getEntity(); | |
145 return output.getStream(); | |
146 } | |
147 | |
122 private synchronized void initialize(){ | 148 private synchronized void initialize(){ |
123 if (!initialized){ | 149 if (!initialized){ |
124 this.artifactDatabases = new HashMap<String, String>(); | 150 this.artifactDatabases = new HashMap<String, String>(); |
125 this.artifactDatabases.put("test", "http://localhost:8181"); // TODO Read from Config | 151 this.artifactDatabases.put("test", "http://localhost:8181"); // TODO Read from Config |
126 initialized = true; | 152 initialized = true; |
127 } | 153 } |
128 | 154 |
129 } | 155 } |
130 | 156 |
157 /** | |
158 * @see de.intevation.gnv.artifactdatabase.client.ArtifactDatabaseClient#createNewArtifact(de.intevation.gnv.artifactdatabase.objects.ArtifactObject) | |
159 */ | |
160 public ArtifactObject createNewArtifact(ArtifactObject artifactFactory) | |
161 throws ArtifactDatabaseClientException { | |
162 | |
163 try { | |
164 XMLUtils xmlUtils = new XMLUtils(); | |
165 Document request = this.createCreateRequestBody(artifactFactory.getId()); | |
166 String url = ((ArtifactFactory)artifactFactory).getDataBaseUrl(); | |
167 InputStream is = this.doPostRequest(url+"/create", request); | |
168 Document result = xmlUtils.readDocument(is); | |
169 // TODO: Fehleranalyse des Dokumentes | |
170 log.debug(xmlUtils.writeDocument2String(result)); | |
171 return this.getArtifact(result); | |
172 } catch (IOException e) { | |
173 log.error(e,e); | |
174 throw new ArtifactDatabaseClientException(e); | |
175 } | |
176 } | |
177 | |
178 | |
179 private ArtifactObject getArtifact(Document document){ | |
180 XMLUtils xmlUtils = new XMLUtils(); | |
181 String uuid = xmlUtils.getStringXPath(document, "/result/uuid/@value"); | |
182 String hash = xmlUtils.getStringXPath(document, "/result/hash/@value"); | |
183 log.info("NEW Artifact: "+uuid+" / "+hash); | |
184 return new Artifact(uuid, hash); | |
185 } | |
186 | |
187 | |
188 | |
189 | |
190 private Document createCreateRequestBody(String artifactFactoryName){ | |
191 Document document = new XMLUtils().newDocument(); | |
192 Node rootNode = this.createRootNode(document); | |
193 Element typeNode = this.createArtifactElement(document, "type"); | |
194 typeNode.setAttribute("name", "create"); | |
195 rootNode.appendChild(typeNode); | |
196 | |
197 Element factoyNode = this.createArtifactElement(document, "factory"); | |
198 factoyNode.setAttribute("name", artifactFactoryName); | |
199 rootNode.appendChild(factoyNode); | |
200 | |
201 return document; | |
202 } | |
203 | |
204 private Element createRootNode(Document document){ | |
205 Element rootNode = this.createArtifactElement(document,"action"); | |
206 document.appendChild(rootNode); | |
207 return rootNode; | |
208 } | |
209 | |
210 /** | |
211 * @param document | |
212 * @return | |
213 */ | |
214 private Element createArtifactElement(Document document, String name) { | |
215 Element node = document.createElementNS(NAMESPACE_URI, name); | |
216 node.setPrefix(NAMESPACE_PREFIX); | |
217 return node; | |
218 } | |
131 | 219 |
132 } | 220 } |