comparison artifact-database/src/main/java/org/dive4elements/artifactdatabase/rest/BaseOutResource.java @ 473:d0ac790a6c89 dive4elements-move

Moved directories to org.dive4elements
author Sascha L. Teichmann <teichmann@intevation.de>
date Thu, 25 Apr 2013 10:57:18 +0200
parents artifact-database/src/main/java/de/intevation/artifactdatabase/rest/BaseOutResource.java@d9a99b28a847
children 415df0fc4fa1
comparison
equal deleted inserted replaced
472:783cc1b6b615 473:d0ac790a6c89
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.artifacts.common.utils.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 String outType = getType();
81
82 if (logger.isDebugEnabled()) {
83 logger.debug("looking for artifact id '" + identifier + "'");
84 }
85
86 String mimeTypeString = XMLUtils.xpathString(
87 inputDocument,
88 XPATH_MIME_TYPE,
89 ArtifactNamespaceContext.INSTANCE);
90
91 MediaType mimeType = DEFAULT_MIME_TYPE;
92
93 if (mimeTypeString != null && mimeTypeString.length() != 0) {
94 try {
95 mimeType = MediaType.valueOf(mimeTypeString);
96 }
97 catch (Exception e) {
98 logger.error(e.getLocalizedMessage());
99 }
100 }
101
102 try {
103 return new OutRepresentation(
104 mimeType,
105 doOut(identifier, outType, inputDocument, db, getCallMeta()));
106 }
107 catch (ArtifactDatabaseException adbe) {
108 Response response = getResponse();
109 response.setStatus(
110 Status.CLIENT_ERROR_NOT_FOUND, adbe.getMessage());
111 return new EmptyRepresentation();
112 }
113 }
114
115 /**
116 * Returns the identifier of the artifact or collection.
117 *
118 * @return the identifier.
119 */
120 protected abstract String getIdentifier();
121
122
123 /**
124 * Returns the concrete output type of the artifact or collection.
125 *
126 * @return the output type.
127 */
128 protected abstract String getType();
129
130 /**
131 * This method is called to process the operation on artifacts or
132 * collections.
133 *
134 * @param identifier The identifier of the artifact or collection.
135 * @param type The output type.
136 * @param input The input document of the request.
137 * @param db The artifact database.
138 * @param meta The CallMeta object.
139 *
140 * @return the result of the operation.
141 */
142 protected abstract ArtifactDatabase.DeferredOutput doOut(
143 String identifier,
144 String type,
145 Document input,
146 ArtifactDatabase db,
147 CallMeta meta)
148 throws ArtifactDatabaseException;
149 }
150 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org