comparison artifact-database/src/main/java/de/intevation/artifactdatabase/rest/BaseOutResource.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
children b2115f484edb
comparison
equal deleted inserted replaced
142:e986e32bc7d4 143:7e20702a90ed
1 /*
2 * Copyright (c) 2011 by Intevation GmbH
3 *
4 * This program is free software under the LGPL (>=v2.1)
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.
7 */
8 package de.intevation.artifactdatabase.rest;
9
10 import de.intevation.artifactdatabase.XMLUtils;
11
12 import de.intevation.artifacts.ArtifactDatabase;
13 import de.intevation.artifacts.ArtifactDatabaseException;
14 import de.intevation.artifacts.ArtifactNamespaceContext;
15 import de.intevation.artifacts.CallMeta;
16
17 import java.io.IOException;
18
19 import org.apache.log4j.Logger;
20
21 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
34 import org.w3c.dom.Document;
35
36
37 /**
38 * Base Resource to serve the out()-outputs of collections and artifacts.
39 *
40 * @author <a href="mailto:ingo.weinzierl@intevation">Ingo Weinzierl</a>
41 */
42 public abstract class BaseOutResource
43 extends BaseResource
44 {
45 /** The logger used in this class.*/
46 private static Logger logger = Logger.getLogger(BaseOutResource.class);
47
48 /** XPath to figure out the MIME type of the requested result.*/
49 public static final String XPATH_MIME_TYPE =
50 "/art:action/art:out/art:mime-type/@value";
51
52 /** Default result MIME type: octet stream.*/
53 public static final MediaType DEFAULT_MIME_TYPE =
54 MediaType.APPLICATION_OCTET_STREAM;
55
56
57 @Override
58 protected Representation innerPost(Representation requestRepr)
59 throws ResourceException
60 {
61 Document inputDocument = null;
62
63 try {
64 DomRepresentation input = new DomRepresentation(requestRepr);
65 input.setNamespaceAware(true);
66 inputDocument = input.getDocument();
67 }
68 catch (IOException ioe) {
69 logger.error(ioe.getMessage());
70 Response response = getResponse();
71 response.setStatus(Status.CLIENT_ERROR_BAD_REQUEST, ioe);
72 return new EmptyRepresentation();
73 }
74
75 ArtifactDatabase db = getArtifactDatabase();
76
77 Request request = getRequest();
78
79 String identifier = getIdentifier();
80
81 if (logger.isDebugEnabled()) {
82 logger.debug("looking for artifact id '" + identifier + "'");
83 }
84
85 String mimeTypeString = XMLUtils.xpathString(
86 inputDocument,
87 XPATH_MIME_TYPE,
88 ArtifactNamespaceContext.INSTANCE);
89
90 MediaType mimeType = DEFAULT_MIME_TYPE;
91
92 if (mimeTypeString != null && mimeTypeString.length() != 0) {
93 try {
94 mimeType = MediaType.valueOf(mimeTypeString);
95 }
96 catch (Exception e) {
97 logger.error(e.getLocalizedMessage());
98 }
99 }
100
101 try {
102 return new OutRepresentation(
103 mimeType,
104 doOut(identifier, inputDocument, db, getCallMeta()));
105 }
106 catch (ArtifactDatabaseException adbe) {
107 Response response = getResponse();
108 response.setStatus(
109 Status.CLIENT_ERROR_NOT_FOUND, adbe.getMessage());
110 return new EmptyRepresentation();
111 }
112 }
113
114 /**
115 * Returns the identifier of the artifact or collection.
116 *
117 * @return the identifier.
118 */
119 protected abstract String getIdentifier();
120
121 /**
122 * This method is called to process the operation on artifacts or
123 * collections.
124 *
125 * @param identifier The identifier of the artifact or collection.
126 * @param input The input document of the request.
127 * @param db The artifact database.
128 * @param meta The CallMeta object.
129 *
130 * @return the result of the operation.
131 */
132 protected abstract ArtifactDatabase.DeferredOutput doOut(
133 String identifier,
134 Document input,
135 ArtifactDatabase db,
136 CallMeta meta)
137 throws ArtifactDatabaseException;
138 }
139 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org