Mercurial > dive4elements > framework
comparison artifact-database/src/main/java/de/intevation/artifactdatabase/rest/CreateResource.java @ 26:83a059c204f8
Connected /create with artifact db.
artifacts/trunk@61 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Thu, 10 Sep 2009 12:29:57 +0000 |
parents | 72abee95fd64 |
children | c2d53bd30ab8 |
comparison
equal
deleted
inserted
replaced
25:ccc6aae25585 | 26:83a059c204f8 |
---|---|
20 import org.restlet.representation.Representation; | 20 import org.restlet.representation.Representation; |
21 import org.restlet.representation.EmptyRepresentation; | 21 import org.restlet.representation.EmptyRepresentation; |
22 | 22 |
23 import java.io.IOException; | 23 import java.io.IOException; |
24 | 24 |
25 import de.intevation.artifacts.ArtifactNamespaceContext; | |
26 import de.intevation.artifacts.ArtifactDatabase; | |
27 import de.intevation.artifacts.Artifact; | |
28 | |
25 /** | 29 /** |
26 * @author Sascha L. Teichmann (sascha.teichmann@intevation) | 30 * @author Sascha L. Teichmann (sascha.teichmann@intevation) |
27 */ | 31 */ |
28 public class CreateResource | 32 public class CreateResource |
29 extends ServerResource | 33 extends ServerResource |
30 { | 34 { |
31 private static Logger logger = Logger.getLogger(CreateResource.class); | 35 private static Logger logger = Logger.getLogger(CreateResource.class); |
32 | 36 |
33 public static final String PATH = "/create"; | 37 public static final String PATH = "/create"; |
38 | |
39 public static final String XPATH_FACTORY = "/action/factory/@name"; | |
40 | |
41 public static final String NO_FACTORY_MESSAGE = "No factory given"; | |
42 | |
43 public static final String NO_ARTIFACT_CREATED = "No artifact created"; | |
34 | 44 |
35 @Post | 45 @Post |
36 public Representation represent() { | 46 public Representation represent() { |
37 | 47 |
38 Request request = getRequest(); | 48 Request request = getRequest(); |
48 logger.error(ioe.getMessage()); | 58 logger.error(ioe.getMessage()); |
49 Response response = getResponse(); | 59 Response response = getResponse(); |
50 response.setStatus(Status.CLIENT_ERROR_BAD_REQUEST, ioe); | 60 response.setStatus(Status.CLIENT_ERROR_BAD_REQUEST, ioe); |
51 return new EmptyRepresentation(); | 61 return new EmptyRepresentation(); |
52 } | 62 } |
63 | |
64 String factory = XMLUtils.xpathString( | |
65 inputDocument, | |
66 XPATH_FACTORY, | |
67 ArtifactNamespaceContext.INSTANCE); | |
68 | |
69 if (factory == null || factory.length() == 0) { | |
70 Response response = getResponse(); | |
71 response.setStatus(Status.CLIENT_ERROR_BAD_REQUEST, NO_FACTORY_MESSAGE); | |
72 return new EmptyRepresentation(); | |
73 } | |
74 | |
75 if (logger.isDebugEnabled()) { | |
76 logger.debug("Create artifact with factory '" + factory + "'"); | |
77 } | |
78 | |
79 ArtifactDatabase db = (ArtifactDatabase)getContext() | |
80 .getAttributes().get("database"); | |
81 | |
82 Artifact artifact = db.createArtifactWithFactory(factory); | |
83 | |
84 if (artifact == null) { | |
85 Response response = getResponse(); | |
86 response.setStatus(Status.CLIENT_ERROR_UNPROCESSABLE_ENTITY, NO_ARTIFACT_CREATED); | |
87 return new EmptyRepresentation(); | |
88 } | |
53 | 89 |
54 Document outputDocument = XMLUtils.newDocument(); | 90 Document outputDocument = artifact.describe( |
91 db.getArtifactContext()); | |
55 | 92 |
56 return new DomRepresentation( | 93 return new DomRepresentation( |
57 MediaType.APPLICATION_XML, outputDocument); | 94 MediaType.APPLICATION_XML, outputDocument); |
58 } | 95 } |
59 } | 96 } |