comparison artifact-database/src/main/java/org/dive4elements/artifactdatabase/rest/CollectionResource.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/CollectionResource.java@e92d5944fe4b
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.ArtifactDatabase;
11 import de.intevation.artifacts.ArtifactDatabaseException;
12 import de.intevation.artifacts.CallMeta;
13
14 import de.intevation.artifacts.common.ArtifactNamespaceContext;
15 import de.intevation.artifacts.common.utils.XMLUtils;
16
17 import de.intevation.artifactdatabase.ArtifactDatabaseImpl;
18
19 import java.io.IOException;
20
21 import javax.xml.xpath.XPathConstants;
22
23 import org.apache.log4j.Logger;
24
25 import org.restlet.data.MediaType;
26 import org.restlet.data.Status;
27 import org.restlet.ext.xml.DomRepresentation;
28 import org.restlet.representation.EmptyRepresentation;
29 import org.restlet.representation.Representation;
30 import org.restlet.Request;
31 import org.restlet.Response;
32
33 import org.w3c.dom.Document;
34 import org.w3c.dom.Node;
35
36
37 /**
38 * @author <a href="mailto:ingo.weinzierl@intevation">Ingo Weinzierl</a>
39 */
40 public class CollectionResource
41 extends BaseResource
42 {
43 /** The logger that is used in this class.*/
44 private static Logger logger = Logger.getLogger(CollectionResource.class);
45
46 /** server URL where to reach the resource.*/
47 public static final String PATH = "/collection/{uuid}";
48
49 /**
50 * XPath to figure out the type of action (feed, advance) via the
51 * incoming POST request.
52 */
53 public static final String XPATH_ACTION = "/art:action/art:type/@name";
54
55 /**
56 * XPath to figure out the identifier of the artifact described in the
57 * action.
58 */
59 public static final String XPATH_ARTIFACT =
60 "/art:action/art:type/art:artifact/@uuid";
61
62 /** Error message if no action was given.*/
63 public static final String NO_ACTION_MSG = "no action given";
64
65 /** Error message if a unknown action was given.*/
66 public static final String NO_SUCH_ACTION_MSG = "no such action";
67
68 /** Action name for deleting a collection.*/
69 public static final String ACTION_DELETE = "delete";
70
71 /** Action name for describing the collection.*/
72 public static final String ACTION_DESCRIBE = "describe";
73
74 /** Action name for retrieving the attribute of a collection.*/
75 public static final String ACTION_GET_ATTRIBUTE = "getattribute";
76
77 /** Action name for retrieving the attributes of an artifact stored in the
78 * collection.*/
79 public static final String ACTION_GET_ITEM_ATTRIBUTE = "getitemattribute";
80
81 /** Action name for setting the attribute of a collection.*/
82 public static final String ACTION_SET_ATTRIBUTE = "setattribute";
83
84 /** Action name for setting the attribute for an artifact stored in the
85 * collection.*/
86 public static final String ACTION_SET_ITEM_ATTRIBUTE = "setitemattribute";
87
88 /** Action name for adding a new artifact to the collection.*/
89 public static final String ACTION_ADD_ARTIFACT = "addartifact";
90
91 /** Action name for removing an artifact from the collection.*/
92 public static final String ACTION_REMOVE_ARTIFACT = "removeartifact";
93
94 /** Action name for listing the artifacts of the collection.*/
95 public static final String ACTION_LIST_ARTIFACTS = "listartifacts";
96
97 /** Action name for setting the ttl of a collection.*/
98 public static final String ACTION_SET_TTL = "settimetolive";
99
100 /** Action name for setting the name of a collection.*/
101 public static final String ACTION_SET_NAME = "setname";
102
103
104 /**
105 * Method to figure out which POST action was triggered and perform this
106 * operation on the collection specified by 'identifier' and found in the
107 * artifact database 'db'.
108 *
109 * @param identifier The identifier of the collection.
110 * @param action The action to be performed.
111 * @param source The input document to further parameterize the operation.
112 * @param db The artifact database where to find the collection.
113 *
114 * @return The representation produced by the performed action.
115 */
116 protected Representation dispatch(
117 String identifier,
118 String action,
119 Document source,
120 ArtifactDatabase db
121 ) {
122 Document out = null;
123
124 try {
125 CallMeta meta = getCallMeta();
126
127 if (action.equals(ACTION_DELETE)) {
128 logger.info("Delete collection '" + identifier + "'");
129 out = db.deleteCollection(identifier, getCallMeta());
130 }
131 else if (action.equals(ACTION_DESCRIBE)) {
132 logger.info("Describe collection '" + identifier + "'");
133
134 out = db.describeCollection(identifier, meta);
135 }
136 else if (action.equals(ACTION_ADD_ARTIFACT)) {
137 String art = getArtifactIdentifier(source);
138
139 logger.info("Add artifact '" + art + "' to collection.");
140 out = db.addCollectionArtifact(identifier, art, source, meta);
141 }
142 else if (action.equals(ACTION_REMOVE_ARTIFACT)) {
143 String art = getArtifactIdentifier(source);
144
145 logger.info("Remove artifact '" + art + "' from collection.");
146 out = db.removeCollectionArtifact(identifier, art, meta);
147 }
148 else if (action.equals(ACTION_LIST_ARTIFACTS)) {
149 logger.info("List artifacts of collection '" + identifier +"'");
150 out = db.listCollectionArtifacts(identifier, meta);
151 }
152 else if (action.equals(ACTION_SET_ATTRIBUTE)) {
153 String art = getArtifactIdentifier(source);
154
155 logger.info("Set attribute for collection '" + identifier + "'");
156
157 Document attr = getCollectionAttribute(source);
158
159 out = db.setCollectionAttribute(identifier, meta, attr);
160 }
161 else if (action.equals(ACTION_SET_ITEM_ATTRIBUTE)) {
162 String art = getArtifactIdentifier(source);
163
164 logger.info("Set attribute for artifact '" + art + "'");
165 out = db.setCollectionItemAttribute(identifier, art, source, meta);
166 }
167 else if (action.equals(ACTION_GET_ATTRIBUTE)) {
168 String art = getArtifactIdentifier(source);
169
170 logger.info("Retrieve attribute of collection '" + identifier + "'");
171 out = db.getCollectionAttribute(identifier, meta);
172 }
173 else if (action.equals(ACTION_GET_ITEM_ATTRIBUTE)) {
174 String art = getArtifactIdentifier(source);
175
176 logger.info("Retrieve attribute of artifact '" + art + "'");
177 out = db.getCollectionItemAttribute(identifier, art, meta);
178 }
179 else if (action.equals(ACTION_SET_TTL)) {
180 out = db.setCollectionTTL(identifier, source, meta);
181 }
182 else if (action.equals(ACTION_SET_NAME)) {
183 out = db.setCollectionName(identifier, source, meta);
184 }
185 else {
186 throw new ArtifactDatabaseException(NO_SUCH_ACTION_MSG);
187 }
188 }
189 catch (ArtifactDatabaseException adbe) {
190 logger.warn(adbe.getLocalizedMessage(), adbe);
191
192 Response response = getResponse();
193 response.setStatus(
194 Status.CLIENT_ERROR_BAD_REQUEST, adbe.getMessage());
195 return new EmptyRepresentation();
196 }
197
198 return new DomRepresentation(MediaType.APPLICATION_XML, out);
199 }
200
201
202 @Override
203 protected Representation innerPost(Representation requestRepr) {
204 Document input = null;
205 try {
206 DomRepresentation in = new DomRepresentation(requestRepr);
207 in.setNamespaceAware(true);
208 input = in.getDocument();
209 }
210 catch (IOException ioe) {
211 logger.error(ioe.getLocalizedMessage(), ioe);
212
213 Response response = getResponse();
214 response.setStatus(Status.CLIENT_ERROR_BAD_REQUEST, ioe);
215 return new EmptyRepresentation();
216 }
217
218 String action = XMLUtils.xpathString(
219 input, XPATH_ACTION, ArtifactNamespaceContext.INSTANCE);
220
221 if (action == null || action.length() == 0) {
222 Response response = getResponse();
223 response.setStatus(
224 Status.CLIENT_ERROR_BAD_REQUEST, NO_ACTION_MSG);
225 return new EmptyRepresentation();
226 }
227
228 Request request = getRequest();
229
230 String identifier = (String) request.getAttributes().get("uuid");
231
232 ArtifactDatabase db = getArtifactDatabase();
233
234 return dispatch(identifier, action, input, db);
235 }
236
237
238 /**
239 * Retrieves the identifier of the artifact used in the action.
240 *
241 * @param source The incoming document that describes the operation.
242 *
243 * @return the uuid of the artifact described in the document.
244 */
245 protected String getArtifactIdentifier(Document source) {
246 return XMLUtils.xpathString(
247 source, XPATH_ARTIFACT, ArtifactNamespaceContext.INSTANCE);
248 }
249
250
251 /**
252 * Returns the attribute for a collection of the incoming request document.
253 *
254 * @param request The request document.
255 *
256 * @return the contained attribute as document.
257 */
258 protected Document getCollectionAttribute(Document request) {
259 Node attr = (Node) XMLUtils.xpath(
260 request,
261 ArtifactDatabaseImpl.XPATH_COLLECTION_ATTRIBUTE,
262 XPathConstants.NODE,
263 ArtifactNamespaceContext.INSTANCE);
264
265 Document newAttr = XMLUtils.newDocument();
266
267 if (attr == null) {
268 logger.error("Collection attribute document not found!");
269 return newAttr;
270 }
271
272 newAttr.appendChild(newAttr.importNode(attr, true));
273
274 return newAttr;
275 }
276 }

http://dive4elements.wald.intevation.org