Mercurial > dive4elements > framework
comparison artifact-database/src/main/java/org/dive4elements/artifactdatabase/rest/ArtifactResource.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/ArtifactResource.java@3447fa62f007 |
children | 415df0fc4fa1 |
comparison
equal
deleted
inserted
replaced
472:783cc1b6b615 | 473:d0ac790a6c89 |
---|---|
1 /* | |
2 * Copyright (c) 2010 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 | |
9 package de.intevation.artifactdatabase.rest; | |
10 | |
11 import de.intevation.artifacts.common.utils.XMLUtils; | |
12 | |
13 import de.intevation.artifacts.Artifact; | |
14 import de.intevation.artifacts.ArtifactDatabase; | |
15 import de.intevation.artifacts.ArtifactDatabaseException; | |
16 import de.intevation.artifacts.ArtifactNamespaceContext; | |
17 | |
18 import java.io.IOException; | |
19 | |
20 import org.apache.log4j.Logger; | |
21 | |
22 import org.restlet.Request; | |
23 import org.restlet.Response; | |
24 | |
25 import org.restlet.data.MediaType; | |
26 import org.restlet.data.Status; | |
27 | |
28 import org.restlet.ext.xml.DomRepresentation; | |
29 | |
30 import org.restlet.representation.EmptyRepresentation; | |
31 import org.restlet.representation.Representation; | |
32 | |
33 import org.restlet.resource.ResourceException; | |
34 | |
35 import org.w3c.dom.Document; | |
36 | |
37 /** | |
38 * Resource to expose the core artifact methods | |
39 * (describe, feed and advance) via REST. | |
40 * | |
41 * <ul> | |
42 * <li>describe() is modelled via GET.</li> | |
43 * <li>advance() and feed() are modelled via POST.</li> | |
44 * </ul> | |
45 * @author <a href="mailto:sascha.teichmann@intevation">Sascha L. Teichmann</a> | |
46 */ | |
47 public class ArtifactResource | |
48 extends BaseResource | |
49 { | |
50 private static Logger logger = Logger.getLogger(ArtifactResource.class); | |
51 | |
52 /** | |
53 * XPath to figure out the type of action (feed, advance) via the | |
54 * incoming POST request. | |
55 */ | |
56 public static final String XPATH_ACTION = "/art:action/art:type/@name"; | |
57 | |
58 /** | |
59 * server URL where to reach the resource. | |
60 */ | |
61 public static final String PATH = "/artifact/{uuid}"; | |
62 | |
63 /** | |
64 * Error message if no action was given. | |
65 */ | |
66 public static final String NO_ACTION_MESSAGE = "no action given"; | |
67 | |
68 /** | |
69 * Error message if a unknown action was given. | |
70 */ | |
71 public static final String NO_SUCH_ACTION_MESSAGE = "no such action"; | |
72 | |
73 /** | |
74 * Error message if the requested artifact was not found in | |
75 * the artifact database. | |
76 */ | |
77 public static final String NO_ARTIFACT_FOUND = "Artifact not found"; | |
78 | |
79 /** | |
80 * Action name 'advance'. | |
81 */ | |
82 public static final String ADVANCE = "advance"; | |
83 /** | |
84 * Action name 'feed'. | |
85 */ | |
86 public static final String FEED = "feed"; | |
87 /** | |
88 * Action name 'describe'. | |
89 */ | |
90 public static final String DESCRIBE = "describe"; | |
91 | |
92 @Override | |
93 protected Representation innerGet() | |
94 throws ResourceException | |
95 { | |
96 Request request = getRequest(); | |
97 | |
98 String identifier = (String)request.getAttributes().get("uuid"); | |
99 | |
100 if (logger.isDebugEnabled()) { | |
101 logger.debug("looking for artifact id '" + identifier + "'"); | |
102 } | |
103 | |
104 ArtifactDatabase db = (ArtifactDatabase)getContext() | |
105 .getAttributes().get("database"); | |
106 | |
107 try { | |
108 return new DomRepresentation( | |
109 MediaType.APPLICATION_XML, | |
110 db.describe(identifier, null, getCallMeta())); | |
111 } | |
112 catch (ArtifactDatabaseException adbe) { | |
113 logger.warn(adbe.getLocalizedMessage(), adbe); | |
114 Response response = getResponse(); | |
115 response.setStatus( | |
116 Status.CLIENT_ERROR_NOT_FOUND, adbe.getMessage()); | |
117 return new EmptyRepresentation(); | |
118 } | |
119 } | |
120 | |
121 /** | |
122 * Method to figure out which POST action (feed or advance) was | |
123 * triggered and perform this operation on the artifact specified | |
124 * by 'identifier' and found in the artifact database 'db'. | |
125 * | |
126 * @param identifier The identifier of the artifact. | |
127 * @param action The action to be performed. | |
128 * @param source The input document to further parameterize the | |
129 * operation. | |
130 * @param db The artifact database where to find the artifact. | |
131 * @return The representation produced by the performed action. | |
132 */ | |
133 protected Representation dispatch( | |
134 String identifier, | |
135 String action, | |
136 Document source, | |
137 ArtifactDatabase db | |
138 ) { | |
139 Document out = null; | |
140 | |
141 try { | |
142 if (action.equals(FEED)) { | |
143 out = db.feed(identifier, source, getCallMeta()); | |
144 } | |
145 else if (action.equals(ADVANCE)) { | |
146 out = db.advance(identifier, source, getCallMeta()); | |
147 } | |
148 else if (action.equals(DESCRIBE)) { | |
149 out = db.describe(identifier, source, getCallMeta()); | |
150 } | |
151 else { | |
152 throw new ArtifactDatabaseException(NO_SUCH_ACTION_MESSAGE); | |
153 } | |
154 } | |
155 catch (ArtifactDatabaseException adbe) { | |
156 logger.warn(adbe.getLocalizedMessage(), adbe); | |
157 Response response = getResponse(); | |
158 response.setStatus( | |
159 Status.CLIENT_ERROR_BAD_REQUEST, adbe.getMessage()); | |
160 return new EmptyRepresentation(); | |
161 } | |
162 | |
163 return new DomRepresentation(MediaType.APPLICATION_XML, out); | |
164 } | |
165 | |
166 @Override | |
167 protected Representation innerPost(Representation requestRepr) { | |
168 | |
169 Document inputDocument = null; | |
170 try { | |
171 DomRepresentation input = new DomRepresentation(requestRepr); | |
172 input.setNamespaceAware(true); | |
173 inputDocument = input.getDocument(); | |
174 } | |
175 catch (IOException ioe) { | |
176 logger.error(ioe.getMessage()); | |
177 Response response = getResponse(); | |
178 response.setStatus(Status.CLIENT_ERROR_BAD_REQUEST, ioe); | |
179 return new EmptyRepresentation(); | |
180 } | |
181 | |
182 String action = XMLUtils.xpathString( | |
183 inputDocument, | |
184 XPATH_ACTION, | |
185 ArtifactNamespaceContext.INSTANCE); | |
186 | |
187 if (action == null || action.length() == 0) { | |
188 Response response = getResponse(); | |
189 response.setStatus( | |
190 Status.CLIENT_ERROR_BAD_REQUEST, NO_ACTION_MESSAGE); | |
191 return new EmptyRepresentation(); | |
192 } | |
193 | |
194 Request request = getRequest(); | |
195 | |
196 String identifier = (String)request.getAttributes().get("uuid"); | |
197 | |
198 ArtifactDatabase db = (ArtifactDatabase)getContext() | |
199 .getAttributes().get("database"); | |
200 | |
201 return dispatch(identifier, action, inputDocument, db); | |
202 } | |
203 } | |
204 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |