Mercurial > dive4elements > framework
comparison artifact-database/src/main/java/de/intevation/artifactdatabase/rest/UserResource.java @ 137:f92ad0e40df2
Added a resource that handles user specific actions.
artifacts/trunk@1362 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Ingo Weinzierl <ingo.weinzierl@intevation.de> |
---|---|
date | Wed, 02 Mar 2011 11:07:21 +0000 |
parents | |
children | b77465617071 |
comparison
equal
deleted
inserted
replaced
136:e2aad8d8ebd7 | 137:f92ad0e40df2 |
---|---|
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 | |
13 import de.intevation.artifacts.common.ArtifactNamespaceContext; | |
14 import de.intevation.artifacts.common.utils.XMLUtils; | |
15 | |
16 import java.io.IOException; | |
17 | |
18 import org.apache.log4j.Logger; | |
19 | |
20 import org.restlet.data.MediaType; | |
21 import org.restlet.data.Status; | |
22 import org.restlet.ext.xml.DomRepresentation; | |
23 import org.restlet.representation.EmptyRepresentation; | |
24 import org.restlet.representation.Representation; | |
25 import org.restlet.Request; | |
26 import org.restlet.Response; | |
27 | |
28 import org.w3c.dom.Document; | |
29 | |
30 /** | |
31 * A resource that handles actions to a specific user. | |
32 * | |
33 * @author <a href="mailto:ingo.weinzierl@intevation">Ingo Weinzierl</a> | |
34 */ | |
35 public class UserResource | |
36 extends BaseResource | |
37 { | |
38 /** The logger that is used in this class.*/ | |
39 private static Logger logger = Logger.getLogger(UserResource.class); | |
40 | |
41 /** server URL where to reach the resource.*/ | |
42 public static final String PATH = "/user/{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 /** Error message if no action was given.*/ | |
51 public static final String NO_ACTION_MSG = "no action given"; | |
52 | |
53 /** Error message if a unknown action was given.*/ | |
54 public static final String NO_SUCH_ACTION_MSG = "no such action"; | |
55 | |
56 /** Action name for deleting users.*/ | |
57 public static final String ACTION_DELETE = "delete"; | |
58 | |
59 | |
60 @Override | |
61 protected Representation innerPost(Representation requestRepr) { | |
62 Document inputDocument = null; | |
63 | |
64 try { | |
65 DomRepresentation input = new DomRepresentation(requestRepr); | |
66 input.setNamespaceAware(true); | |
67 inputDocument = input.getDocument(); | |
68 } | |
69 catch (IOException ioe) { | |
70 logger.error(ioe.getMessage()); | |
71 Response response = getResponse(); | |
72 response.setStatus(Status.CLIENT_ERROR_BAD_REQUEST, ioe); | |
73 return new EmptyRepresentation(); | |
74 } | |
75 | |
76 String action = XMLUtils.xpathString( | |
77 inputDocument, | |
78 XPATH_ACTION, | |
79 ArtifactNamespaceContext.INSTANCE); | |
80 | |
81 if (action == null || action.length() == 0) { | |
82 Response response = getResponse(); | |
83 response.setStatus( | |
84 Status.CLIENT_ERROR_BAD_REQUEST, NO_ACTION_MSG); | |
85 return new EmptyRepresentation(); | |
86 } | |
87 | |
88 Request request = getRequest(); | |
89 | |
90 String identifier = (String)request.getAttributes().get("uuid"); | |
91 | |
92 ArtifactDatabase db = getArtifactDatabase(); | |
93 | |
94 return dispatch(identifier, action, inputDocument, db); | |
95 } | |
96 | |
97 /** | |
98 * Method to figure out which POST action (feed or advance) was | |
99 * triggered and perform this operation on the artifact specified | |
100 * by 'identifier' and found in the artifact database 'db' | |
101 * @param identifier The identifier of the artifact. | |
102 * @param action The action to be performed. | |
103 * @param source The input document to further parameterize the | |
104 * operation. | |
105 * @param db The artifact database where to find the artifact. | |
106 * @return The representation produced by the performed action. | |
107 */ | |
108 protected Representation dispatch( | |
109 String identifier, | |
110 String action, | |
111 Document source, | |
112 ArtifactDatabase db) | |
113 { | |
114 Document out = null; | |
115 | |
116 logger.info("Action: " + action + " | User: " + identifier); | |
117 | |
118 try { | |
119 if (action.equals(ACTION_DELETE)) { | |
120 out = db.deleteUser(identifier, getCallMeta()); | |
121 } | |
122 else { | |
123 throw new ArtifactDatabaseException(NO_SUCH_ACTION_MSG); | |
124 } | |
125 } | |
126 catch (ArtifactDatabaseException adbe) { | |
127 logger.warn(adbe.getLocalizedMessage(), adbe); | |
128 Response response = getResponse(); | |
129 response.setStatus( | |
130 Status.CLIENT_ERROR_BAD_REQUEST, adbe.getMessage()); | |
131 return new EmptyRepresentation(); | |
132 } | |
133 | |
134 return new DomRepresentation(MediaType.APPLICATION_XML, out); | |
135 } | |
136 } | |
137 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |