Mercurial > dive4elements > framework
annotate artifact-database/src/main/java/de/intevation/artifactdatabase/rest/CreateResource.java @ 36:f7d2cd59a0d5
Bugfix done. Now the feed Method works fine
artifacts/trunk@93 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Tim Englich <tim.englich@intevation.de> |
---|---|
date | Wed, 16 Sep 2009 07:53:26 +0000 |
parents | c2d53bd30ab8 |
children | af22d4de275c |
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 |
1cd268e07e44
Added resource to create artifacts to web server.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
7 import org.restlet.resource.Post; |
1cd268e07e44
Added resource to create artifacts to web server.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
8 import org.restlet.resource.ServerResource; |
1cd268e07e44
Added resource to create artifacts to web server.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
9 |
1cd268e07e44
Added resource to create artifacts to web server.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
10 import org.restlet.data.Request; |
1cd268e07e44
Added resource to create artifacts to web server.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
11 import org.restlet.data.Response; |
1cd268e07e44
Added resource to create artifacts to web server.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
12 import org.restlet.data.Status; |
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.restlet.data.MediaType; |
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 org.apache.log4j.Logger; |
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 de.intevation.artifactdatabase.XMLUtils; |
1cd268e07e44
Added resource to create artifacts to web server.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
19 |
1cd268e07e44
Added resource to create artifacts to web server.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
20 import org.restlet.representation.Representation; |
1cd268e07e44
Added resource to create artifacts to web server.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
21 import org.restlet.representation.EmptyRepresentation; |
1cd268e07e44
Added resource to create artifacts to web server.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
22 |
1cd268e07e44
Added resource to create artifacts to web server.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
23 import java.io.IOException; |
1cd268e07e44
Added resource to create artifacts to web server.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
24 |
26
83a059c204f8
Connected /create with artifact db.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
22
diff
changeset
|
25 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
|
26 import de.intevation.artifacts.ArtifactDatabaseException; |
26
83a059c204f8
Connected /create with artifact db.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
22
diff
changeset
|
27 import de.intevation.artifacts.ArtifactDatabase; |
83a059c204f8
Connected /create with artifact db.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
22
diff
changeset
|
28 |
21
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 * @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
|
31 */ |
1cd268e07e44
Added resource to create artifacts to web server.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
32 public class CreateResource |
1cd268e07e44
Added resource to create artifacts to web server.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
33 extends ServerResource |
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 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
|
36 |
1cd268e07e44
Added resource to create artifacts to web server.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
37 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
|
38 |
26
83a059c204f8
Connected /create with artifact db.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
22
diff
changeset
|
39 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
|
40 |
83a059c204f8
Connected /create with artifact db.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
22
diff
changeset
|
41 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
|
42 |
83a059c204f8
Connected /create with artifact db.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
22
diff
changeset
|
43 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
|
44 |
22
72abee95fd64
Removed meta data from resources.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
21
diff
changeset
|
45 @Post |
21
1cd268e07e44
Added resource to create artifacts to web server.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
46 public Representation represent() { |
1cd268e07e44
Added resource to create artifacts to web server.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
47 |
1cd268e07e44
Added resource to create artifacts to web server.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
48 Request request = getRequest(); |
1cd268e07e44
Added resource to create artifacts to web server.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
49 |
1cd268e07e44
Added resource to create artifacts to web server.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
50 Representation requestRepr = request.getEntity(); |
1cd268e07e44
Added resource to create artifacts to web server.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
51 |
1cd268e07e44
Added resource to create artifacts to web server.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
52 Document inputDocument = null; |
1cd268e07e44
Added resource to create artifacts to web server.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
53 try { |
1cd268e07e44
Added resource to create artifacts to web server.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
54 DomRepresentation input = new DomRepresentation(requestRepr); |
1cd268e07e44
Added resource to create artifacts to web server.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
55 inputDocument = input.getDocument(); |
1cd268e07e44
Added resource to create artifacts to web server.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
56 } |
1cd268e07e44
Added resource to create artifacts to web server.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
57 catch (IOException ioe) { |
1cd268e07e44
Added resource to create artifacts to web server.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
58 logger.error(ioe.getMessage()); |
1cd268e07e44
Added resource to create artifacts to web server.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
59 Response response = getResponse(); |
1cd268e07e44
Added resource to create artifacts to web server.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
60 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
|
61 return new EmptyRepresentation(); |
1cd268e07e44
Added resource to create artifacts to web server.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
62 } |
26
83a059c204f8
Connected /create with artifact db.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
22
diff
changeset
|
63 |
83a059c204f8
Connected /create with artifact db.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
22
diff
changeset
|
64 String factory = XMLUtils.xpathString( |
83a059c204f8
Connected /create with artifact db.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
22
diff
changeset
|
65 inputDocument, |
83a059c204f8
Connected /create with artifact db.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
22
diff
changeset
|
66 XPATH_FACTORY, |
83a059c204f8
Connected /create with artifact db.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
22
diff
changeset
|
67 ArtifactNamespaceContext.INSTANCE); |
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 if (factory == null || factory.length() == 0) { |
83a059c204f8
Connected /create with artifact db.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
22
diff
changeset
|
70 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
|
71 response.setStatus( |
c2d53bd30ab8
Re-factored artifact API for better integration of background processing.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
26
diff
changeset
|
72 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
|
73 return new EmptyRepresentation(); |
83a059c204f8
Connected /create with artifact db.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
22
diff
changeset
|
74 } |
83a059c204f8
Connected /create with artifact db.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
22
diff
changeset
|
75 |
83a059c204f8
Connected /create with artifact db.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
22
diff
changeset
|
76 if (logger.isDebugEnabled()) { |
83a059c204f8
Connected /create with artifact db.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
22
diff
changeset
|
77 logger.debug("Create artifact with factory '" + factory + "'"); |
83a059c204f8
Connected /create with artifact db.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
22
diff
changeset
|
78 } |
83a059c204f8
Connected /create with artifact db.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
22
diff
changeset
|
79 |
83a059c204f8
Connected /create with artifact db.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
22
diff
changeset
|
80 ArtifactDatabase db = (ArtifactDatabase)getContext() |
83a059c204f8
Connected /create with artifact db.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
22
diff
changeset
|
81 .getAttributes().get("database"); |
83a059c204f8
Connected /create with artifact db.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
22
diff
changeset
|
82 |
32
c2d53bd30ab8
Re-factored artifact API for better integration of background processing.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
26
diff
changeset
|
83 try { |
c2d53bd30ab8
Re-factored artifact API for better integration of background processing.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
26
diff
changeset
|
84 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
|
85 MediaType.APPLICATION_XML, |
c2d53bd30ab8
Re-factored artifact API for better integration of background processing.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
26
diff
changeset
|
86 db.createArtifactWithFactory(factory)); |
c2d53bd30ab8
Re-factored artifact API for better integration of background processing.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
26
diff
changeset
|
87 } |
c2d53bd30ab8
Re-factored artifact API for better integration of background processing.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
26
diff
changeset
|
88 catch (ArtifactDatabaseException adbe) { |
26
83a059c204f8
Connected /create with artifact db.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
22
diff
changeset
|
89 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
|
90 response.setStatus( |
c2d53bd30ab8
Re-factored artifact API for better integration of background processing.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
26
diff
changeset
|
91 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
|
92 return new EmptyRepresentation(); |
83a059c204f8
Connected /create with artifact db.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
22
diff
changeset
|
93 } |
21
1cd268e07e44
Added resource to create artifacts to web server.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
94 } |
1cd268e07e44
Added resource to create artifacts to web server.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
95 } |
1cd268e07e44
Added resource to create artifacts to web server.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
96 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8: |