comparison artifact-database/src/main/java/de/intevation/artifactdatabase/rest/ArtifactResource.java @ 32:c2d53bd30ab8

Re-factored artifact API for better integration of background processing. artifacts/trunk@78 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Sun, 13 Sep 2009 14:50:53 +0000
parents 22b03d5c84c5
children 9935e1c928de
comparison
equal deleted inserted replaced
31:c4d85a8532d1 32:c2d53bd30ab8
8 import org.restlet.representation.EmptyRepresentation; 8 import org.restlet.representation.EmptyRepresentation;
9 9
10 import org.restlet.ext.xml.DomRepresentation; 10 import org.restlet.ext.xml.DomRepresentation;
11 11
12 import de.intevation.artifacts.ArtifactDatabase; 12 import de.intevation.artifacts.ArtifactDatabase;
13 import de.intevation.artifacts.ArtifactDatabaseException;
13 import de.intevation.artifacts.Artifact; 14 import de.intevation.artifacts.Artifact;
14 import de.intevation.artifacts.ArtifactNamespaceContext; 15 import de.intevation.artifacts.ArtifactNamespaceContext;
15 16
16 import org.restlet.data.Request; 17 import org.restlet.data.Request;
17 import org.restlet.data.Response; 18 import org.restlet.data.Response;
58 } 59 }
59 60
60 ArtifactDatabase db = (ArtifactDatabase)getContext() 61 ArtifactDatabase db = (ArtifactDatabase)getContext()
61 .getAttributes().get("database"); 62 .getAttributes().get("database");
62 63
63 Artifact artifact = db.getArtifact(identifier); 64 try {
65 return new DomRepresentation(
66 MediaType.APPLICATION_XML,
67 db.describe(identifier));
68 }
69 catch (ArtifactDatabaseException adbe) {
70 Response response = getResponse();
71 response.setStatus(
72 Status.CLIENT_ERROR_NOT_FOUND, adbe.getMessage());
73 return new EmptyRepresentation();
74 }
75 }
64 76
65 if (artifact == null) { 77 protected Representation dispatch(
78 String identifier,
79 String action,
80 Document source,
81 ArtifactDatabase db
82 ) {
83 Document out = null;
84
85 try {
86 if (action.equals(FEED)) {
87 out = db.feed(identifier, source);
88 }
89 else if (action.equals(ADVANCE)) {
90 out = db.advance(identifier, source);
91 }
92 else {
93 throw new ArtifactDatabaseException(NO_SUCH_ACTION_MESSAGE);
94 }
95 }
96 catch (ArtifactDatabaseException adbe) {
66 Response response = getResponse(); 97 Response response = getResponse();
67 response.setStatus(Status.CLIENT_ERROR_NOT_FOUND, NO_ARTIFACT_FOUND); 98 response.setStatus(
99 Status.CLIENT_ERROR_BAD_REQUEST, adbe.getMessage());
68 return new EmptyRepresentation(); 100 return new EmptyRepresentation();
69 } 101 }
70 102
71 Document description = artifact.describe(db.getArtifactContext()); 103 return new DomRepresentation(MediaType.APPLICATION_XML, out);
72
73 if (logger.isDebugEnabled()) {
74 logger.debug("out document: " + description);
75 }
76
77 return new DomRepresentation(
78 MediaType.APPLICATION_XML, description);
79 } 104 }
80 105
81 @Post 106 @Post
82 public Representation representPost() { 107 public Representation representPost() {
83 Request request = getRequest(); 108 Request request = getRequest();
101 XPATH_ACTION, 126 XPATH_ACTION,
102 ArtifactNamespaceContext.INSTANCE); 127 ArtifactNamespaceContext.INSTANCE);
103 128
104 if (action == null || action.length() == 0) { 129 if (action == null || action.length() == 0) {
105 Response response = getResponse(); 130 Response response = getResponse();
106 response.setStatus(Status.CLIENT_ERROR_BAD_REQUEST, NO_ACTION_MESSAGE); 131 response.setStatus(
107 return new EmptyRepresentation(); 132 Status.CLIENT_ERROR_BAD_REQUEST, NO_ACTION_MESSAGE);
108 }
109
110 int actionType = -1;
111
112 if (FEED .equals(action)) actionType = 0;
113 else if (ADVANCE.equals(action)) actionType = 1;
114 else {
115 Response response = getResponse();
116 response.setStatus(Status.CLIENT_ERROR_BAD_REQUEST, NO_SUCH_ACTION_MESSAGE);
117 return new EmptyRepresentation(); 133 return new EmptyRepresentation();
118 } 134 }
119 135
120 String identifier = (String)request.getAttributes().get("uuid"); 136 String identifier = (String)request.getAttributes().get("uuid");
121 137
122 if (logger.isDebugEnabled()) {
123 logger.debug("looking for artifact id '" + identifier + "'");
124 }
125
126 ArtifactDatabase db = (ArtifactDatabase)getContext() 138 ArtifactDatabase db = (ArtifactDatabase)getContext()
127 .getAttributes().get("database"); 139 .getAttributes().get("database");
128 140
129 Artifact artifact = db.getArtifact(identifier); 141 return dispatch(identifier, action, inputDocument, db);
130
131 if (artifact == null) {
132 Response response = getResponse();
133 response.setStatus(Status.CLIENT_ERROR_NOT_FOUND, NO_ARTIFACT_FOUND);
134 return new EmptyRepresentation();
135 }
136
137 Document document = null;
138
139 switch (actionType) {
140 case 0:
141 document = artifact.feed(inputDocument, db.getArtifactContext());
142 break;
143 case 1:
144 document = artifact.advance(inputDocument, db.getArtifactContext());
145 break;
146 default:
147 // should not happen
148 return new EmptyRepresentation();
149 }
150
151 return new DomRepresentation(MediaType.APPLICATION_XML, document);
152 } 142 }
153 } 143 }
154 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8: 144 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8:

http://dive4elements.wald.intevation.org