Mercurial > dive4elements > framework
comparison artifact-database/src/main/java/de/intevation/artifactdatabase/rest/CollectionResource.java @ 142:e986e32bc7d4
Added a Rest resource that handles operations specific to a collection.
artifacts/trunk@1367 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Ingo Weinzierl <ingo.weinzierl@intevation.de> |
---|---|
date | Wed, 02 Mar 2011 15:10:34 +0000 |
parents | |
children | fbaeb5931d10 |
comparison
equal
deleted
inserted
replaced
141:5d40faf1484d | 142:e986e32bc7d4 |
---|---|
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 java.io.IOException; | |
18 | |
19 import org.apache.log4j.Logger; | |
20 | |
21 import org.restlet.data.MediaType; | |
22 import org.restlet.data.Status; | |
23 import org.restlet.ext.xml.DomRepresentation; | |
24 import org.restlet.representation.EmptyRepresentation; | |
25 import org.restlet.representation.Representation; | |
26 import org.restlet.Request; | |
27 import org.restlet.Response; | |
28 | |
29 import org.w3c.dom.Document; | |
30 | |
31 | |
32 /** | |
33 * @author <a href="mailto:ingo.weinzierl@intevation">Ingo Weinzierl</a> | |
34 */ | |
35 public class CollectionResource | |
36 extends BaseResource | |
37 { | |
38 /** The logger that is used in this class.*/ | |
39 private static Logger logger = Logger.getLogger(CollectionResource.class); | |
40 | |
41 /** server URL where to reach the resource.*/ | |
42 public static final String PATH = "/collection/{uuid}"; | |
43 | |
44 /** | |
45 * XPath to figure out the type of action (feed, advance) via the | |
46 * incoming POST request. | |
47 */ | |
48 public static final String XPATH_ACTION = "/art:action/art:type/@name"; | |
49 | |
50 /** | |
51 * XPath to figure out the identifier of the artifact described in the | |
52 * action. | |
53 */ | |
54 public static final String XPATH_ARTIFACT = | |
55 "/art:action/art:type/art:artifact/@uuid"; | |
56 | |
57 /** Error message if no action was given.*/ | |
58 public static final String NO_ACTION_MSG = "no action given"; | |
59 | |
60 /** Error message if a unknown action was given.*/ | |
61 public static final String NO_SUCH_ACTION_MSG = "no such action"; | |
62 | |
63 /** Action name for deleting a collection.*/ | |
64 public static final String ACTION_DELETE = "delete"; | |
65 | |
66 /** Action name for retrieving the attributes of an artifact stored in the | |
67 * collection.*/ | |
68 public static final String ACTION_GET_ATTRIBUTE = "getattribute"; | |
69 | |
70 /** Action name for setting the attribute for an artifact stored in the | |
71 * collection.*/ | |
72 public static final String ACTION_SET_ATTRIBUTE = "setattribute"; | |
73 | |
74 /** Action name for adding a new artifact to the collection.*/ | |
75 public static final String ACTION_ADD_ARTIFACT = "addartifact"; | |
76 | |
77 /** Action name for removing an artifact from the collection.*/ | |
78 public static final String ACTION_REMOVE_ARTIFACT = "removeartifact"; | |
79 | |
80 /** Action name for listing the artifacts of the collection.*/ | |
81 public static final String ACTION_LIST_ARTIFACTS = "listartifacts"; | |
82 | |
83 | |
84 /** | |
85 * Method to figure out which POST action was triggered and perform this | |
86 * operation on the collection specified by 'identifier' and found in the | |
87 * artifact database 'db'. | |
88 * | |
89 * @param identifier The identifier of the collection. | |
90 * @param action The action to be performed. | |
91 * @param source The input document to further parameterize the operation. | |
92 * @param db The artifact database where to find the collection. | |
93 * | |
94 * @return The representation produced by the performed action. | |
95 */ | |
96 protected Representation dispatch( | |
97 String identifier, | |
98 String action, | |
99 Document source, | |
100 ArtifactDatabase db | |
101 ) { | |
102 Document out = null; | |
103 | |
104 try { | |
105 CallMeta meta = getCallMeta(); | |
106 | |
107 if (action.equals(ACTION_DELETE)) { | |
108 logger.info("Delete collection '" + identifier + "'"); | |
109 out = db.deleteCollection(identifier, getCallMeta()); | |
110 } | |
111 else if (action.equals(ACTION_ADD_ARTIFACT)) { | |
112 String art = getArtifactIdentifier(source); | |
113 | |
114 logger.info("Add artifact '" + art + "' to collection."); | |
115 out = db.addCollectionArtifact(identifier, art, meta); | |
116 } | |
117 else if (action.equals(ACTION_REMOVE_ARTIFACT)) { | |
118 String art = getArtifactIdentifier(source); | |
119 | |
120 logger.info("Remove artifact '" + art + "' from collection."); | |
121 out = db.removeCollectionArtifact(identifier, art, meta); | |
122 } | |
123 else if (action.equals(ACTION_LIST_ARTIFACTS)) { | |
124 logger.info("List artifacts of collection '" + identifier +"'"); | |
125 out = db.listCollectionArtifacts(identifier, meta); | |
126 } | |
127 else if (action.equals(ACTION_SET_ATTRIBUTE)) { | |
128 String art = getArtifactIdentifier(source); | |
129 | |
130 logger.info("Set attribute for artifact '" + art + "'"); | |
131 out = db.setCollectionAttribute(identifier, art, source, meta); | |
132 } | |
133 else if (action.equals(ACTION_GET_ATTRIBUTE)) { | |
134 String art = getArtifactIdentifier(source); | |
135 | |
136 logger.info("Retrieve attribute of artifact '" + art + "'"); | |
137 out = db.getCollectionAttribute(identifier, art, meta); | |
138 } | |
139 else { | |
140 throw new ArtifactDatabaseException(NO_SUCH_ACTION_MSG); | |
141 } | |
142 } | |
143 catch (ArtifactDatabaseException adbe) { | |
144 logger.warn(adbe.getLocalizedMessage(), adbe); | |
145 | |
146 Response response = getResponse(); | |
147 response.setStatus( | |
148 Status.CLIENT_ERROR_BAD_REQUEST, adbe.getMessage()); | |
149 return new EmptyRepresentation(); | |
150 } | |
151 | |
152 return new DomRepresentation(MediaType.APPLICATION_XML, out); | |
153 } | |
154 | |
155 | |
156 @Override | |
157 protected Representation innerPost(Representation requestRepr) { | |
158 Document input = null; | |
159 try { | |
160 DomRepresentation in = new DomRepresentation(requestRepr); | |
161 in.setNamespaceAware(true); | |
162 input = in.getDocument(); | |
163 } | |
164 catch (IOException ioe) { | |
165 logger.error(ioe.getLocalizedMessage(), ioe); | |
166 | |
167 Response response = getResponse(); | |
168 response.setStatus(Status.CLIENT_ERROR_BAD_REQUEST, ioe); | |
169 return new EmptyRepresentation(); | |
170 } | |
171 | |
172 String action = XMLUtils.xpathString( | |
173 input, XPATH_ACTION, ArtifactNamespaceContext.INSTANCE); | |
174 | |
175 if (action == null || action.length() == 0) { | |
176 Response response = getResponse(); | |
177 response.setStatus( | |
178 Status.CLIENT_ERROR_BAD_REQUEST, NO_ACTION_MSG); | |
179 return new EmptyRepresentation(); | |
180 } | |
181 | |
182 Request request = getRequest(); | |
183 | |
184 String identifier = (String) request.getAttributes().get("uuid"); | |
185 | |
186 ArtifactDatabase db = getArtifactDatabase(); | |
187 | |
188 return dispatch(identifier, action, input, db); | |
189 } | |
190 | |
191 | |
192 /** | |
193 * Retrieves the identifier of the artifact used in the action. | |
194 * | |
195 * @param source The incoming document that describes the operation. | |
196 * | |
197 * @return the uuid of the artifact described in the document. | |
198 */ | |
199 protected String getArtifactIdentifier(Document source) { | |
200 return XMLUtils.xpathString( | |
201 source, XPATH_ARTIFACT, ArtifactNamespaceContext.INSTANCE); | |
202 } | |
203 } |