annotate artifact-database/src/main/java/de/intevation/artifactdatabase/rest/CreateResource.java @ 57:cf9848c85755 0.2

RELEASE 0.2: Summarized NEWS and Changes artifacts/trunk@338 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Hans Plum <hans.plum@intevation.de>
date Fri, 13 Nov 2009 18:37:51 +0000
parents 41c225c8bd41
children 82f62ef25a8f
rev   line source
21
1cd268e07e44 Added resource to create artifacts to web server.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
1 package de.intevation.artifactdatabase.rest;
1cd268e07e44 Added resource to create artifacts to web server.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
2
1cd268e07e44 Added resource to create artifacts to web server.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
3 import org.w3c.dom.Document;
1cd268e07e44 Added resource to create artifacts to web server.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
4
1cd268e07e44 Added resource to create artifacts to web server.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
5 import org.restlet.ext.xml.DomRepresentation;
1cd268e07e44 Added resource to create artifacts to web server.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
6
40
af22d4de275c Log RuntimeExceptions in REST calls to log4j.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 32
diff changeset
7 import org.restlet.resource.ResourceException;
21
1cd268e07e44 Added resource to create artifacts to web server.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
8
1cd268e07e44 Added resource to create artifacts to web server.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
9 import org.restlet.data.Response;
1cd268e07e44 Added resource to create artifacts to web server.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
10 import org.restlet.data.Status;
1cd268e07e44 Added resource to create artifacts to web server.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
11
1cd268e07e44 Added resource to create artifacts to web server.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
12 import org.restlet.data.MediaType;
1cd268e07e44 Added resource to create artifacts to web server.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
13
1cd268e07e44 Added resource to create artifacts to web server.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
14 import org.apache.log4j.Logger;
1cd268e07e44 Added resource to create artifacts to web server.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
15
1cd268e07e44 Added resource to create artifacts to web server.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
16 import de.intevation.artifactdatabase.XMLUtils;
1cd268e07e44 Added resource to create artifacts to web server.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
17
1cd268e07e44 Added resource to create artifacts to web server.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
18 import org.restlet.representation.Representation;
1cd268e07e44 Added resource to create artifacts to web server.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
19 import org.restlet.representation.EmptyRepresentation;
1cd268e07e44 Added resource to create artifacts to web server.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
20
1cd268e07e44 Added resource to create artifacts to web server.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
21 import java.io.IOException;
1cd268e07e44 Added resource to create artifacts to web server.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
22
26
83a059c204f8 Connected /create with artifact db.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 22
diff changeset
23 import de.intevation.artifacts.ArtifactNamespaceContext;
32
c2d53bd30ab8 Re-factored artifact API for better integration of background processing.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 26
diff changeset
24 import de.intevation.artifacts.ArtifactDatabaseException;
26
83a059c204f8 Connected /create with artifact db.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 22
diff changeset
25 import de.intevation.artifacts.ArtifactDatabase;
83a059c204f8 Connected /create with artifact db.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 22
diff changeset
26
21
1cd268e07e44 Added resource to create artifacts to web server.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
27 /**
1cd268e07e44 Added resource to create artifacts to web server.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
28 * @author Sascha L. Teichmann (sascha.teichmann@intevation)
1cd268e07e44 Added resource to create artifacts to web server.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
29 */
1cd268e07e44 Added resource to create artifacts to web server.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
30 public class CreateResource
40
af22d4de275c Log RuntimeExceptions in REST calls to log4j.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 32
diff changeset
31 extends BaseResource
21
1cd268e07e44 Added resource to create artifacts to web server.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
32 {
1cd268e07e44 Added resource to create artifacts to web server.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
33 private static Logger logger = Logger.getLogger(CreateResource.class);
1cd268e07e44 Added resource to create artifacts to web server.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
34
1cd268e07e44 Added resource to create artifacts to web server.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
35 public static final String PATH = "/create";
1cd268e07e44 Added resource to create artifacts to web server.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
36
26
83a059c204f8 Connected /create with artifact db.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 22
diff changeset
37 public static final String XPATH_FACTORY = "/action/factory/@name";
83a059c204f8 Connected /create with artifact db.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 22
diff changeset
38
83a059c204f8 Connected /create with artifact db.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 22
diff changeset
39 public static final String NO_FACTORY_MESSAGE = "No factory given";
83a059c204f8 Connected /create with artifact db.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 22
diff changeset
40
83a059c204f8 Connected /create with artifact db.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 22
diff changeset
41 public static final String NO_ARTIFACT_CREATED = "No artifact created";
83a059c204f8 Connected /create with artifact db.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 22
diff changeset
42
40
af22d4de275c Log RuntimeExceptions in REST calls to log4j.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 32
diff changeset
43 protected Representation innerPost(Representation requestRepr)
af22d4de275c Log RuntimeExceptions in REST calls to log4j.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 32
diff changeset
44 throws ResourceException
af22d4de275c Log RuntimeExceptions in REST calls to log4j.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 32
diff changeset
45 {
21
1cd268e07e44 Added resource to create artifacts to web server.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
46 Document inputDocument = null;
1cd268e07e44 Added resource to create artifacts to web server.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
47 try {
1cd268e07e44 Added resource to create artifacts to web server.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
48 DomRepresentation input = new DomRepresentation(requestRepr);
1cd268e07e44 Added resource to create artifacts to web server.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
49 inputDocument = input.getDocument();
1cd268e07e44 Added resource to create artifacts to web server.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
50 }
1cd268e07e44 Added resource to create artifacts to web server.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
51 catch (IOException ioe) {
1cd268e07e44 Added resource to create artifacts to web server.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
52 logger.error(ioe.getMessage());
1cd268e07e44 Added resource to create artifacts to web server.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
53 Response response = getResponse();
1cd268e07e44 Added resource to create artifacts to web server.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
54 response.setStatus(Status.CLIENT_ERROR_BAD_REQUEST, ioe);
1cd268e07e44 Added resource to create artifacts to web server.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
55 return new EmptyRepresentation();
1cd268e07e44 Added resource to create artifacts to web server.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
56 }
26
83a059c204f8 Connected /create with artifact db.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 22
diff changeset
57
83a059c204f8 Connected /create with artifact db.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 22
diff changeset
58 String factory = XMLUtils.xpathString(
83a059c204f8 Connected /create with artifact db.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 22
diff changeset
59 inputDocument,
83a059c204f8 Connected /create with artifact db.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 22
diff changeset
60 XPATH_FACTORY,
83a059c204f8 Connected /create with artifact db.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 22
diff changeset
61 ArtifactNamespaceContext.INSTANCE);
83a059c204f8 Connected /create with artifact db.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 22
diff changeset
62
83a059c204f8 Connected /create with artifact db.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 22
diff changeset
63 if (factory == null || factory.length() == 0) {
83a059c204f8 Connected /create with artifact db.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 22
diff changeset
64 Response response = getResponse();
32
c2d53bd30ab8 Re-factored artifact API for better integration of background processing.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 26
diff changeset
65 response.setStatus(
c2d53bd30ab8 Re-factored artifact API for better integration of background processing.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 26
diff changeset
66 Status.CLIENT_ERROR_BAD_REQUEST, NO_FACTORY_MESSAGE);
26
83a059c204f8 Connected /create with artifact db.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 22
diff changeset
67 return new EmptyRepresentation();
83a059c204f8 Connected /create with artifact db.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 22
diff changeset
68 }
83a059c204f8 Connected /create with artifact db.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 22
diff changeset
69
83a059c204f8 Connected /create with artifact db.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 22
diff changeset
70 if (logger.isDebugEnabled()) {
83a059c204f8 Connected /create with artifact db.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 22
diff changeset
71 logger.debug("Create artifact with factory '" + factory + "'");
83a059c204f8 Connected /create with artifact db.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 22
diff changeset
72 }
83a059c204f8 Connected /create with artifact db.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 22
diff changeset
73
83a059c204f8 Connected /create with artifact db.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 22
diff changeset
74 ArtifactDatabase db = (ArtifactDatabase)getContext()
83a059c204f8 Connected /create with artifact db.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 22
diff changeset
75 .getAttributes().get("database");
83a059c204f8 Connected /create with artifact db.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 22
diff changeset
76
32
c2d53bd30ab8 Re-factored artifact API for better integration of background processing.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 26
diff changeset
77 try {
c2d53bd30ab8 Re-factored artifact API for better integration of background processing.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 26
diff changeset
78 return new DomRepresentation(
c2d53bd30ab8 Re-factored artifact API for better integration of background processing.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 26
diff changeset
79 MediaType.APPLICATION_XML,
48
41c225c8bd41 Add i18n support via "Accept-Language" HTTP headers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 40
diff changeset
80 db.createArtifactWithFactory(factory, getCallMeta()));
32
c2d53bd30ab8 Re-factored artifact API for better integration of background processing.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 26
diff changeset
81 }
c2d53bd30ab8 Re-factored artifact API for better integration of background processing.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 26
diff changeset
82 catch (ArtifactDatabaseException adbe) {
26
83a059c204f8 Connected /create with artifact db.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 22
diff changeset
83 Response response = getResponse();
32
c2d53bd30ab8 Re-factored artifact API for better integration of background processing.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 26
diff changeset
84 response.setStatus(
c2d53bd30ab8 Re-factored artifact API for better integration of background processing.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 26
diff changeset
85 Status.CLIENT_ERROR_UNPROCESSABLE_ENTITY, adbe.getMessage());
26
83a059c204f8 Connected /create with artifact db.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 22
diff changeset
86 return new EmptyRepresentation();
83a059c204f8 Connected /create with artifact db.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 22
diff changeset
87 }
21
1cd268e07e44 Added resource to create artifacts to web server.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
88 }
1cd268e07e44 Added resource to create artifacts to web server.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
89 }
1cd268e07e44 Added resource to create artifacts to web server.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
90 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8:

http://dive4elements.wald.intevation.org