comparison artifact-database/src/main/java/de/intevation/artifactdatabase/rest/ArtifactResource.java @ 28:019b9f02d523

Added REST handler for 'advance' and 'feed' to be called by HTTP POST '/artifact/{uuid}'. artifacts/trunk@65 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Thu, 10 Sep 2009 14:44:25 +0000
parents 75bdaf900473
children 22b03d5c84c5
comparison
equal deleted inserted replaced
27:75bdaf900473 28:019b9f02d523
20 20
21 import org.apache.log4j.Logger; 21 import org.apache.log4j.Logger;
22 22
23 import org.w3c.dom.Document; 23 import org.w3c.dom.Document;
24 24
25 import java.io.IOException;
26
27 import de.intevation.artifacts.ArtifactNamespaceContext;
28
29 import de.intevation.artifactdatabase.XMLUtils;
30
25 /** 31 /**
26 * @author Sascha L. Teichmann (sascha.teichmann@intevation) 32 * @author Sascha L. Teichmann (sascha.teichmann@intevation)
27 */ 33 */
28 public class ArtifactResource 34 public class ArtifactResource
29 extends ServerResource 35 extends ServerResource
30 { 36 {
31 private static Logger logger = Logger.getLogger(ArtifactResource.class); 37 private static Logger logger = Logger.getLogger(ArtifactResource.class);
32 38
39 public static final String XPATH_ACTION = "/action/type/@name";
40
33 public static final String PATH = "/artifact/{uuid}"; 41 public static final String PATH = "/artifact/{uuid}";
34 42
43 public static final String NO_ACTION_MESSAGE = "no action given";
44 public static final String NO_SUCH_ACTION_MESSAGE = "no such action";
45
35 public static final String NO_ARTIFACT_FOUND = "Artifact not found"; 46 public static final String NO_ARTIFACT_FOUND = "Artifact not found";
47
48 public static final String ADVANCE = "advance";
49 public static final String FEED = "feed";
36 50
37 @Get 51 @Get
38 public Representation represent() { 52 public Representation represent() {
39 53
40 Request request = getRequest(); 54 Request request = getRequest();
64 78
65 return new DomRepresentation( 79 return new DomRepresentation(
66 MediaType.APPLICATION_XML, description); 80 MediaType.APPLICATION_XML, description);
67 } 81 }
68 82
69 /*
70 @Post 83 @Post
71 public Representation representPost() { 84 public Representation representPost() {
72 return new EmptyRepresentation(); 85 Request request = getRequest();
86
87 Representation requestRepr = request.getEntity();
88
89 Document inputDocument = null;
90 try {
91 DomRepresentation input = new DomRepresentation(requestRepr);
92 inputDocument = input.getDocument();
93 }
94 catch (IOException ioe) {
95 logger.error(ioe.getMessage());
96 Response response = getResponse();
97 response.setStatus(Status.CLIENT_ERROR_BAD_REQUEST, ioe);
98 return new EmptyRepresentation();
99 }
100
101 String action = XMLUtils.xpathString(
102 inputDocument,
103 XPATH_ACTION,
104 ArtifactNamespaceContext.INSTANCE);
105
106 if (action == null || action.length() == 0) {
107 Response response = getResponse();
108 response.setStatus(Status.CLIENT_ERROR_BAD_REQUEST, NO_ACTION_MESSAGE);
109 return new EmptyRepresentation();
110 }
111
112 int actionType = -1;
113
114 if (FEED .equals(action)) actionType = 0;
115 else if (ADVANCE.equals(action)) actionType = 1;
116 else {
117 Response response = getResponse();
118 response.setStatus(Status.CLIENT_ERROR_BAD_REQUEST, NO_SUCH_ACTION_MESSAGE);
119 return new EmptyRepresentation();
120 }
121
122 String identifier = (String)request.getAttributes().get("uuid");
123
124 if (logger.isDebugEnabled()) {
125 logger.debug("looking for artifact id '" + identifier + "'");
126 }
127
128 ArtifactDatabase db = (ArtifactDatabase)getContext()
129 .getAttributes().get("database");
130
131 Artifact artifact = db.getArtifact(identifier);
132
133 if (artifact == null) {
134 Response response = getResponse();
135 response.setStatus(Status.CLIENT_ERROR_NOT_FOUND, NO_ARTIFACT_FOUND);
136 return new EmptyRepresentation();
137 }
138
139 Document document = null;
140
141 switch (actionType) {
142 case 0:
143 document = artifact.feed(inputDocument, db.getArtifactContext());
144 break;
145 case 1:
146 document = artifact.advance(inputDocument, db.getArtifactContext());
147 break;
148 default:
149 // should not happen
150 return new EmptyRepresentation();
151 }
152
153 return new DomRepresentation(MediaType.APPLICATION_XML, document);
73 } 154 }
74 */
75 } 155 }
76 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8: 156 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8:

http://dive4elements.wald.intevation.org