annotate artifact-database/src/main/java/org/dive4elements/artifactdatabase/rest/ServiceResource.java @ 570:584591f8203c 3.2.x

Upgrade to Log4j 2
author Tom Gottfried <tom@intevation.de>
date Mon, 28 Feb 2022 17:41:14 +0100
parents 415df0fc4fa1
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
475
415df0fc4fa1 Fixed maven group ids
Sascha L. Teichmann <teichmann@intevation.de>
parents: 473
diff changeset
9 package org.dive4elements.artifactdatabase.rest;
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
10
475
415df0fc4fa1 Fixed maven group ids
Sascha L. Teichmann <teichmann@intevation.de>
parents: 473
diff changeset
11 import org.dive4elements.artifacts.ArtifactDatabase;
415df0fc4fa1 Fixed maven group ids
Sascha L. Teichmann <teichmann@intevation.de>
parents: 473
diff changeset
12 import org.dive4elements.artifacts.ArtifactDatabaseException;
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
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
570
584591f8203c Upgrade to Log4j 2
Tom Gottfried <tom@intevation.de>
parents: 475
diff changeset
16 import org.apache.logging.log4j.Logger;
584591f8203c Upgrade to Log4j 2
Tom Gottfried <tom@intevation.de>
parents: 475
diff changeset
17 import org.apache.logging.log4j.LogManager;
93
e27cf9c84eb8 Unified imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 89
diff changeset
18
101
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.Request;
7fc0650f194c Upgraded the Restlet version to 2.0.4 (current stable).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 100
diff changeset
20 import org.restlet.Response;
93
e27cf9c84eb8 Unified imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 89
diff changeset
21 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
22 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
23
93
e27cf9c84eb8 Unified imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 89
diff changeset
24 import org.restlet.ext.xml.DomRepresentation;
e27cf9c84eb8 Unified imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 89
diff changeset
25
e27cf9c84eb8 Unified imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 89
diff changeset
26 import org.restlet.representation.EmptyRepresentation;
e27cf9c84eb8 Unified imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 89
diff changeset
27 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
28
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 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
30
475
415df0fc4fa1 Fixed maven group ids
Sascha L. Teichmann <teichmann@intevation.de>
parents: 473
diff changeset
31 import org.dive4elements.artifacts.Service;
380
9798e4d83681 Services are now able to return more than just XML documents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 101
diff changeset
32
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
33 /**
88
69c84cf7c5d7 Added javadoc to the REST package of the artifact database (complete).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 77
diff changeset
34 * 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
35 *
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
36 * @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
37 */
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 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
39 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
40 {
570
584591f8203c Upgrade to Log4j 2
Tom Gottfried <tom@intevation.de>
parents: 475
diff changeset
41 private static Logger logger = LogManager.getLogger(ServiceResource.class);
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
42
88
69c84cf7c5d7 Added javadoc to the REST package of the artifact database (complete).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 77
diff changeset
43 /**
69c84cf7c5d7 Added javadoc to the REST package of the artifact database (complete).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 77
diff changeset
44 * 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
45 */
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
46 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
47
88
69c84cf7c5d7 Added javadoc to the REST package of the artifact database (complete).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 77
diff changeset
48 /**
69c84cf7c5d7 Added javadoc to the REST package of the artifact database (complete).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 77
diff changeset
49 * 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
50 * 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
51 */
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
52 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
53
89
d348fe1fd822 More javadoc (fixes small glitches, too).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 88
diff changeset
54 @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
55 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
56
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 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
58 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
59 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
60 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
61 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
62 }
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 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
64 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
65 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
66 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
67 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
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
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 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
71
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 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
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
74 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
75 .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
76
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
77 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
78 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
79 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
80 }
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 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
82 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
83 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
84 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
85 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
86 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
87 }
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
88 }
380
9798e4d83681 Services are now able to return more than just XML documents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 101
diff changeset
89
9798e4d83681 Services are now able to return more than just XML documents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 101
diff changeset
90 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
91
9798e4d83681 Services are now able to return more than just XML documents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 101
diff changeset
92 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
93 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
94
9798e4d83681 Services are now able to return more than just XML documents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 101
diff changeset
95 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
96 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
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
9798e4d83681 Services are now able to return more than just XML documents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 101
diff changeset
99 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
100 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
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
9798e4d83681 Services are now able to return more than just XML documents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 101
diff changeset
103 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
104 }
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
105 }
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
106 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org