comparison artifact-database/src/main/java/de/intevation/artifactdatabase/rest/ArtifactOutResource.java @ 29:22b03d5c84c5

Added REST out handler for artifacts reachable via HTTP GET '/artifact/{uuid}/{type}'. artifacts/trunk@69 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Thu, 10 Sep 2009 15:49:17 +0000
parents
children c2d53bd30ab8
comparison
equal deleted inserted replaced
28:019b9f02d523 29:22b03d5c84c5
1 package de.intevation.artifactdatabase.rest;
2
3 import org.apache.log4j.Logger;
4
5 import org.restlet.resource.Get;
6 import org.restlet.resource.ServerResource;
7
8 import org.restlet.data.Request;
9 import org.restlet.data.Response;
10 import org.restlet.data.Status;
11 import org.restlet.data.MediaType;
12
13 import org.restlet.representation.Representation;
14 import org.restlet.representation.EmptyRepresentation;
15
16 import org.restlet.ext.xml.DomRepresentation;
17
18 import de.intevation.artifactdatabase.XMLUtils;
19
20 import de.intevation.artifacts.ArtifactNamespaceContext;
21 import de.intevation.artifacts.ArtifactDatabase;
22 import de.intevation.artifacts.Artifact;
23
24 import org.w3c.dom.Document;
25
26 import java.io.IOException;
27
28 /**
29 * @author Sascha L. Teichmann (sascha.teichmann@intevation)
30 */
31 public class ArtifactOutResource
32 extends ServerResource
33 {
34 public static final String PATH = "/artifact/{uuid}/{type}";
35
36 public static final String XPATH_MIME_TYPE = "/action/out/mime-type/@value";
37
38 public static final MediaType DEFAULT_MIME_TYPE =
39 MediaType.APPLICATION_OCTET_STREAM;
40
41 private static Logger logger = Logger.getLogger(ArtifactOutResource.class);
42
43 @Get
44 public Representation represent() {
45
46 Request request = getRequest();
47
48 Representation requestRepr = request.getEntity();
49
50 Document inputDocument = null;
51 try {
52 DomRepresentation input = new DomRepresentation(requestRepr);
53 inputDocument = input.getDocument();
54 }
55 catch (IOException ioe) {
56 logger.error(ioe.getMessage());
57 Response response = getResponse();
58 response.setStatus(Status.CLIENT_ERROR_BAD_REQUEST, ioe);
59 return new EmptyRepresentation();
60 }
61
62 ArtifactDatabase db = (ArtifactDatabase)getContext()
63 .getAttributes().get("database");
64
65 String identifier = (String)request.getAttributes().get("uuid");
66
67 if (logger.isDebugEnabled()) {
68 logger.debug("looking for artifact id '" + identifier + "'");
69 }
70
71 Artifact artifact = db.getArtifact(identifier);
72
73 if (artifact == null) {
74 Response response = getResponse();
75 response.setStatus(
76 Status.CLIENT_ERROR_NOT_FOUND, ArtifactResource.NO_ARTIFACT_FOUND);
77 return new EmptyRepresentation();
78 }
79
80 String mimeTypeString = XMLUtils.xpathString(
81 inputDocument,
82 XPATH_MIME_TYPE,
83 ArtifactNamespaceContext.INSTANCE);
84
85 MediaType mimeType = DEFAULT_MIME_TYPE;
86
87 if (mimeTypeString != null && mimeTypeString.length() != 0) {
88 try {
89 mimeType = MediaType.valueOf(mimeTypeString);
90 }
91 catch (Exception e) {
92 logger.error(e.getLocalizedMessage());
93 }
94 }
95
96 return new OutRepresentation(
97 mimeType, artifact, inputDocument, db.getArtifactContext());
98 }
99 }
100 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8:

http://dive4elements.wald.intevation.org