comparison artifact-database/src/main/java/de/intevation/artifactdatabase/rest/ArtifactOutResource.java @ 143:7e20702a90ed

Implemented an abstract class for the output of artifacts and collections. Added a CollectionOutResource. artifacts/trunk@1368 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Wed, 02 Mar 2011 16:02:47 +0000
parents 7fc0650f194c
children d9a99b28a847
comparison
equal deleted inserted replaced
142:e986e32bc7d4 143:7e20702a90ed
1 /* 1 /*
2 * Copyright (c) 2010 by Intevation GmbH 2 * Copyright (c) 2010, 2011 by Intevation GmbH
3 * 3 *
4 * This program is free software under the LGPL (>=v2.1) 4 * This program is free software under the LGPL (>=v2.1)
5 * Read the file LGPL.txt coming with the software for details 5 * Read the file LGPL.txt coming with the software for details
6 * or visit http://www.gnu.org/licenses/ if it does not exist. 6 * or visit http://www.gnu.org/licenses/ if it does not exist.
7 */ 7 */
8 8
9 package de.intevation.artifactdatabase.rest; 9 package de.intevation.artifactdatabase.rest;
10 10
11 import de.intevation.artifactdatabase.XMLUtils;
12
13 import de.intevation.artifacts.ArtifactDatabase; 11 import de.intevation.artifacts.ArtifactDatabase;
14 import de.intevation.artifacts.ArtifactDatabaseException; 12 import de.intevation.artifacts.ArtifactDatabaseException;
15 import de.intevation.artifacts.ArtifactNamespaceContext; 13 import de.intevation.artifacts.CallMeta;
16
17 import java.io.IOException;
18 14
19 import org.apache.log4j.Logger; 15 import org.apache.log4j.Logger;
20 16
21 import org.restlet.Request; 17 import org.restlet.Request;
22 import org.restlet.Response;
23
24 import org.restlet.data.MediaType;
25 import org.restlet.data.Status;
26
27 import org.restlet.ext.xml.DomRepresentation;
28
29 import org.restlet.representation.EmptyRepresentation;
30 import org.restlet.representation.Representation;
31
32 import org.restlet.resource.ResourceException;
33 18
34 import org.w3c.dom.Document; 19 import org.w3c.dom.Document;
35 20
36 /** 21 /**
37 * Resource to serve the out()-outputs of artifacts. 22 * Resource to serve the out()-outputs of artifacts.
38 * @author <a href="mailto:sascha.teichmann@intevation">Sascha L. Teichmann</a> 23 * @author <a href="mailto:sascha.teichmann@intevation">Sascha L. Teichmann</a>
24 * @author <a href="mailto:ingo.weinzierl@intevation">Ingo Weinzierl</a>
39 */ 25 */
40 public class ArtifactOutResource 26 public class ArtifactOutResource
41 extends BaseResource 27 extends BaseOutResource
42 { 28 {
43 /** 29 /**
44 * server URL where to find the resource. 30 * server URL where to find the resource.
45 */ 31 */
46 public static final String PATH = "/artifact/{uuid}/{type}"; 32 public static final String PATH = "/artifact/{uuid}/{type}";
47 33
48 /** 34 private static Logger logger = Logger.getLogger(ArtifactOutResource.class);
49 * XPath to figure out the MIME type of the requested result. 35
50 */
51 public static final String XPATH_MIME_TYPE =
52 "/art:action/art:out/art:mime-type/@value";
53 36
54 /** 37 /**
55 * Default result MIME type: octet stream 38 * Returns the identifier of the collection.
39 *
40 * @return the identifier of the collection.
56 */ 41 */
57 public static final MediaType DEFAULT_MIME_TYPE = 42 protected String getIdentifier() {
58 MediaType.APPLICATION_OCTET_STREAM;
59
60 private static Logger logger = Logger.getLogger(ArtifactOutResource.class);
61
62 @Override
63 protected Representation innerPost(Representation requestRepr)
64 throws ResourceException
65 {
66 Document inputDocument = null;
67 try {
68 DomRepresentation input = new DomRepresentation(requestRepr);
69 input.setNamespaceAware(true);
70 inputDocument = input.getDocument();
71 }
72 catch (IOException ioe) {
73 logger.error(ioe.getMessage());
74 Response response = getResponse();
75 response.setStatus(Status.CLIENT_ERROR_BAD_REQUEST, ioe);
76 return new EmptyRepresentation();
77 }
78
79 ArtifactDatabase db = (ArtifactDatabase)getContext()
80 .getAttributes().get("database");
81
82 Request request = getRequest(); 43 Request request = getRequest();
83 44
84 String identifier = (String)request.getAttributes().get("uuid"); 45 return (String) request.getAttributes().get("uuid");
46 }
85 47
86 if (logger.isDebugEnabled()) {
87 logger.debug("looking for artifact id '" + identifier + "'");
88 }
89 48
90 String mimeTypeString = XMLUtils.xpathString( 49 /**
91 inputDocument, 50 * Call the ArtifactDatabase.out method.
92 XPATH_MIME_TYPE, 51 */
93 ArtifactNamespaceContext.INSTANCE); 52 protected ArtifactDatabase.DeferredOutput doOut(
53 String identifier,
54 Document input,
55 ArtifactDatabase db,
56 CallMeta meta)
57 throws ArtifactDatabaseException
58 {
59 logger.debug("ArtifactOutResource.doOut");
94 60
95 MediaType mimeType = DEFAULT_MIME_TYPE; 61 return db.out(identifier, input, meta);
96
97 if (mimeTypeString != null && mimeTypeString.length() != 0) {
98 try {
99 mimeType = MediaType.valueOf(mimeTypeString);
100 }
101 catch (Exception e) {
102 logger.error(e.getLocalizedMessage());
103 }
104 }
105
106 try {
107 return new OutRepresentation(
108 mimeType,
109 db.out(identifier, inputDocument, getCallMeta()));
110 }
111 catch (ArtifactDatabaseException adbe) {
112 Response response = getResponse();
113 response.setStatus(
114 Status.CLIENT_ERROR_NOT_FOUND, adbe.getMessage());
115 return new EmptyRepresentation();
116 }
117 } 62 }
118 } 63 }
119 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : 64 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org