Mercurial > dive4elements > framework
annotate artifact-database/src/main/java/de/intevation/artifactdatabase/rest/ServiceResource.java @ 453:a2efc9d65a11 2.9.8
Doc.
author | Felix Wolfsteller <felix.wolfsteller@intevation.de> |
---|---|
date | Fri, 28 Dec 2012 13:32:32 +0100 |
parents | 9798e4d83681 |
children |
rev | line source |
---|---|
100
933bbc9fc11f
Added license file and license headers.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
93
diff
changeset
|
1 /* |
933bbc9fc11f
Added license file and license headers.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
93
diff
changeset
|
2 * Copyright (c) 2010 by Intevation GmbH |
933bbc9fc11f
Added license file and license headers.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
93
diff
changeset
|
3 * |
933bbc9fc11f
Added license file and license headers.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
93
diff
changeset
|
4 * This program is free software under the LGPL (>=v2.1) |
933bbc9fc11f
Added license file and license headers.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
93
diff
changeset
|
5 * Read the file LGPL.txt coming with the software for details |
933bbc9fc11f
Added license file and license headers.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
93
diff
changeset
|
6 * or visit http://www.gnu.org/licenses/ if it does not exist. |
933bbc9fc11f
Added license file and license headers.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
93
diff
changeset
|
7 */ |
933bbc9fc11f
Added license file and license headers.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
93
diff
changeset
|
8 |
73
d1b8c91b4506
Added url '/service/{service}' to REST server to offer the actual service over HTTP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
9 package de.intevation.artifactdatabase.rest; |
d1b8c91b4506
Added url '/service/{service}' to REST server to offer the actual service over HTTP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
10 |
d1b8c91b4506
Added url '/service/{service}' to REST server to offer the actual service over HTTP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
11 import de.intevation.artifacts.ArtifactDatabase; |
d1b8c91b4506
Added url '/service/{service}' to REST server to offer the actual service over HTTP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
12 import de.intevation.artifacts.ArtifactDatabaseException; |
d1b8c91b4506
Added url '/service/{service}' to REST server to offer the actual service over HTTP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
13 |
93
e27cf9c84eb8
Unified imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
89
diff
changeset
|
14 import java.io.IOException; |
e27cf9c84eb8
Unified imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
89
diff
changeset
|
15 |
e27cf9c84eb8
Unified imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
89
diff
changeset
|
16 import org.apache.log4j.Logger; |
e27cf9c84eb8
Unified imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
89
diff
changeset
|
17 |
101
7fc0650f194c
Upgraded the Restlet version to 2.0.4 (current stable).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
100
diff
changeset
|
18 import org.restlet.Request; |
7fc0650f194c
Upgraded the Restlet version to 2.0.4 (current stable).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
100
diff
changeset
|
19 import org.restlet.Response; |
93
e27cf9c84eb8
Unified imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
89
diff
changeset
|
20 import org.restlet.data.MediaType; |
73
d1b8c91b4506
Added url '/service/{service}' to REST server to offer the actual service over HTTP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
21 import org.restlet.data.Status; |
d1b8c91b4506
Added url '/service/{service}' to REST server to offer the actual service over HTTP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
22 |
93
e27cf9c84eb8
Unified imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
89
diff
changeset
|
23 import org.restlet.ext.xml.DomRepresentation; |
e27cf9c84eb8
Unified imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
89
diff
changeset
|
24 |
e27cf9c84eb8
Unified imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
89
diff
changeset
|
25 import org.restlet.representation.EmptyRepresentation; |
e27cf9c84eb8
Unified imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
89
diff
changeset
|
26 import org.restlet.representation.Representation; |
73
d1b8c91b4506
Added url '/service/{service}' to REST server to offer the actual service over HTTP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
27 |
d1b8c91b4506
Added url '/service/{service}' to REST server to offer the actual service over HTTP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
28 import org.w3c.dom.Document; |
d1b8c91b4506
Added url '/service/{service}' to REST server to offer the actual service over HTTP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
29 |
380
9798e4d83681
Services are now able to return more than just XML documents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
101
diff
changeset
|
30 import de.intevation.artifacts.Service; |
9798e4d83681
Services are now able to return more than just XML documents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
101
diff
changeset
|
31 |
73
d1b8c91b4506
Added url '/service/{service}' to REST server to offer the actual service over HTTP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
32 /** |
88
69c84cf7c5d7
Added javadoc to the REST package of the artifact database (complete).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
77
diff
changeset
|
33 * Resource to process incoming XML documents with a given service. |
69c84cf7c5d7
Added javadoc to the REST package of the artifact database (complete).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
77
diff
changeset
|
34 * |
77
48d1a9a082c2
Bring @author javadoc tags in form '@author <a href="john.doe@example.com">John Doe</a>'
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
73
diff
changeset
|
35 * @author <a href="mailto:sascha.teichmann@intevation">Sascha L. Teichmann</a> |
73
d1b8c91b4506
Added url '/service/{service}' to REST server to offer the actual service over HTTP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
36 */ |
d1b8c91b4506
Added url '/service/{service}' to REST server to offer the actual service over HTTP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
37 public class ServiceResource |
d1b8c91b4506
Added url '/service/{service}' to REST server to offer the actual service over HTTP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
38 extends BaseResource |
d1b8c91b4506
Added url '/service/{service}' to REST server to offer the actual service over HTTP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
39 { |
d1b8c91b4506
Added url '/service/{service}' to REST server to offer the actual service over HTTP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
40 private static Logger logger = Logger.getLogger(ServiceResource.class); |
d1b8c91b4506
Added url '/service/{service}' to REST server to offer the actual service over HTTP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
41 |
88
69c84cf7c5d7
Added javadoc to the REST package of the artifact database (complete).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
77
diff
changeset
|
42 /** |
69c84cf7c5d7
Added javadoc to the REST package of the artifact database (complete).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
77
diff
changeset
|
43 * server URL where to reach the resource. |
69c84cf7c5d7
Added javadoc to the REST package of the artifact database (complete).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
77
diff
changeset
|
44 */ |
73
d1b8c91b4506
Added url '/service/{service}' to REST server to offer the actual service over HTTP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
45 public static final String PATH = "/service/{service}"; |
d1b8c91b4506
Added url '/service/{service}' to REST server to offer the actual service over HTTP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
46 |
88
69c84cf7c5d7
Added javadoc to the REST package of the artifact database (complete).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
77
diff
changeset
|
47 /** |
69c84cf7c5d7
Added javadoc to the REST package of the artifact database (complete).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
77
diff
changeset
|
48 * Error message if no corresponing service is provided by |
69c84cf7c5d7
Added javadoc to the REST package of the artifact database (complete).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
77
diff
changeset
|
49 * the artifact database. |
69c84cf7c5d7
Added javadoc to the REST package of the artifact database (complete).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
77
diff
changeset
|
50 */ |
73
d1b8c91b4506
Added url '/service/{service}' to REST server to offer the actual service over HTTP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
51 public static final String NO_SUCH_ACTION_MESSAGE = "no such service"; |
d1b8c91b4506
Added url '/service/{service}' to REST server to offer the actual service over HTTP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
52 |
89
d348fe1fd822
More javadoc (fixes small glitches, too).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
88
diff
changeset
|
53 @Override |
73
d1b8c91b4506
Added url '/service/{service}' to REST server to offer the actual service over HTTP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
54 protected Representation innerPost(Representation requestRepr) { |
d1b8c91b4506
Added url '/service/{service}' to REST server to offer the actual service over HTTP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
55 |
d1b8c91b4506
Added url '/service/{service}' to REST server to offer the actual service over HTTP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
56 Document inputDocument = null; |
d1b8c91b4506
Added url '/service/{service}' to REST server to offer the actual service over HTTP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
57 try { |
d1b8c91b4506
Added url '/service/{service}' to REST server to offer the actual service over HTTP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
58 DomRepresentation input = new DomRepresentation(requestRepr); |
d1b8c91b4506
Added url '/service/{service}' to REST server to offer the actual service over HTTP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
59 input.setNamespaceAware(true); |
d1b8c91b4506
Added url '/service/{service}' to REST server to offer the actual service over HTTP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
60 inputDocument = input.getDocument(); |
d1b8c91b4506
Added url '/service/{service}' to REST server to offer the actual service over HTTP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
61 } |
d1b8c91b4506
Added url '/service/{service}' to REST server to offer the actual service over HTTP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
62 catch (IOException ioe) { |
d1b8c91b4506
Added url '/service/{service}' to REST server to offer the actual service over HTTP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
63 logger.error(ioe.getMessage()); |
d1b8c91b4506
Added url '/service/{service}' to REST server to offer the actual service over HTTP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
64 Response response = getResponse(); |
d1b8c91b4506
Added url '/service/{service}' to REST server to offer the actual service over HTTP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
65 response.setStatus(Status.CLIENT_ERROR_BAD_REQUEST, ioe); |
d1b8c91b4506
Added url '/service/{service}' to REST server to offer the actual service over HTTP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
66 return new EmptyRepresentation(); |
d1b8c91b4506
Added url '/service/{service}' to REST server to offer the actual service over HTTP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
67 } |
d1b8c91b4506
Added url '/service/{service}' to REST server to offer the actual service over HTTP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
68 |
d1b8c91b4506
Added url '/service/{service}' to REST server to offer the actual service over HTTP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
69 Request request = getRequest(); |
d1b8c91b4506
Added url '/service/{service}' to REST server to offer the actual service over HTTP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
70 |
d1b8c91b4506
Added url '/service/{service}' to REST server to offer the actual service over HTTP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
71 String service = (String)request.getAttributes().get("service"); |
d1b8c91b4506
Added url '/service/{service}' to REST server to offer the actual service over HTTP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
72 |
d1b8c91b4506
Added url '/service/{service}' to REST server to offer the actual service over HTTP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
73 ArtifactDatabase db = (ArtifactDatabase)getContext() |
d1b8c91b4506
Added url '/service/{service}' to REST server to offer the actual service over HTTP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
74 .getAttributes().get("database"); |
d1b8c91b4506
Added url '/service/{service}' to REST server to offer the actual service over HTTP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
75 |
d1b8c91b4506
Added url '/service/{service}' to REST server to offer the actual service over HTTP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
76 try { |
380
9798e4d83681
Services are now able to return more than just XML documents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
101
diff
changeset
|
77 return guessRepresentation( |
73
d1b8c91b4506
Added url '/service/{service}' to REST server to offer the actual service over HTTP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
78 db.process(service, inputDocument, getCallMeta())); |
d1b8c91b4506
Added url '/service/{service}' to REST server to offer the actual service over HTTP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
79 } |
d1b8c91b4506
Added url '/service/{service}' to REST server to offer the actual service over HTTP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
80 catch (ArtifactDatabaseException adbe) { |
d1b8c91b4506
Added url '/service/{service}' to REST server to offer the actual service over HTTP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
81 logger.warn(adbe.getLocalizedMessage(), adbe); |
d1b8c91b4506
Added url '/service/{service}' to REST server to offer the actual service over HTTP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
82 Response response = getResponse(); |
d1b8c91b4506
Added url '/service/{service}' to REST server to offer the actual service over HTTP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
83 response.setStatus( |
d1b8c91b4506
Added url '/service/{service}' to REST server to offer the actual service over HTTP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
84 Status.CLIENT_ERROR_BAD_REQUEST, adbe.getMessage()); |
d1b8c91b4506
Added url '/service/{service}' to REST server to offer the actual service over HTTP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
85 return new EmptyRepresentation(); |
d1b8c91b4506
Added url '/service/{service}' to REST server to offer the actual service over HTTP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
86 } |
d1b8c91b4506
Added url '/service/{service}' to REST server to offer the actual service over HTTP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
87 } |
380
9798e4d83681
Services are now able to return more than just XML documents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
101
diff
changeset
|
88 |
9798e4d83681
Services are now able to return more than just XML documents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
101
diff
changeset
|
89 protected static Representation guessRepresentation(Service.Output output) { |
9798e4d83681
Services are now able to return more than just XML documents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
101
diff
changeset
|
90 |
9798e4d83681
Services are now able to return more than just XML documents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
101
diff
changeset
|
91 MediaType mediaType = new MediaType(output.getMIMEType()); |
9798e4d83681
Services are now able to return more than just XML documents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
101
diff
changeset
|
92 Object data = output.getData(); |
9798e4d83681
Services are now able to return more than just XML documents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
101
diff
changeset
|
93 |
9798e4d83681
Services are now able to return more than just XML documents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
101
diff
changeset
|
94 if (data instanceof Document) { |
9798e4d83681
Services are now able to return more than just XML documents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
101
diff
changeset
|
95 return new DomRepresentation(mediaType, (Document)data); |
9798e4d83681
Services are now able to return more than just XML documents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
101
diff
changeset
|
96 } |
9798e4d83681
Services are now able to return more than just XML documents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
101
diff
changeset
|
97 |
9798e4d83681
Services are now able to return more than just XML documents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
101
diff
changeset
|
98 if (data instanceof byte []) { |
9798e4d83681
Services are now able to return more than just XML documents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
101
diff
changeset
|
99 return new ByteArrayRepresentation(mediaType, (byte [])data); |
9798e4d83681
Services are now able to return more than just XML documents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
101
diff
changeset
|
100 } |
9798e4d83681
Services are now able to return more than just XML documents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
101
diff
changeset
|
101 |
9798e4d83681
Services are now able to return more than just XML documents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
101
diff
changeset
|
102 return new EmptyRepresentation(); |
9798e4d83681
Services are now able to return more than just XML documents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
101
diff
changeset
|
103 } |
73
d1b8c91b4506
Added url '/service/{service}' to REST server to offer the actual service over HTTP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
104 } |
d1b8c91b4506
Added url '/service/{service}' to REST server to offer the actual service over HTTP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
105 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |