comparison artifact-database/src/main/java/org/dive4elements/artifactdatabase/rest/ServiceResource.java @ 473:d0ac790a6c89 dive4elements-move

Moved directories to org.dive4elements
author Sascha L. Teichmann <teichmann@intevation.de>
date Thu, 25 Apr 2013 10:57:18 +0200
parents artifact-database/src/main/java/de/intevation/artifactdatabase/rest/ServiceResource.java@9798e4d83681
children 415df0fc4fa1
comparison
equal deleted inserted replaced
472:783cc1b6b615 473:d0ac790a6c89
1 /*
2 * Copyright (c) 2010 by Intevation GmbH
3 *
4 * This program is free software under the LGPL (>=v2.1)
5 * Read the file LGPL.txt coming with the software for details
6 * or visit http://www.gnu.org/licenses/ if it does not exist.
7 */
8
9 package de.intevation.artifactdatabase.rest;
10
11 import de.intevation.artifacts.ArtifactDatabase;
12 import de.intevation.artifacts.ArtifactDatabaseException;
13
14 import java.io.IOException;
15
16 import org.apache.log4j.Logger;
17
18 import org.restlet.Request;
19 import org.restlet.Response;
20 import org.restlet.data.MediaType;
21 import org.restlet.data.Status;
22
23 import org.restlet.ext.xml.DomRepresentation;
24
25 import org.restlet.representation.EmptyRepresentation;
26 import org.restlet.representation.Representation;
27
28 import org.w3c.dom.Document;
29
30 import de.intevation.artifacts.Service;
31
32 /**
33 * Resource to process incoming XML documents with a given service.
34 *
35 * @author <a href="mailto:sascha.teichmann@intevation">Sascha L. Teichmann</a>
36 */
37 public class ServiceResource
38 extends BaseResource
39 {
40 private static Logger logger = Logger.getLogger(ServiceResource.class);
41
42 /**
43 * server URL where to reach the resource.
44 */
45 public static final String PATH = "/service/{service}";
46
47 /**
48 * Error message if no corresponing service is provided by
49 * the artifact database.
50 */
51 public static final String NO_SUCH_ACTION_MESSAGE = "no such service";
52
53 @Override
54 protected Representation innerPost(Representation requestRepr) {
55
56 Document inputDocument = null;
57 try {
58 DomRepresentation input = new DomRepresentation(requestRepr);
59 input.setNamespaceAware(true);
60 inputDocument = input.getDocument();
61 }
62 catch (IOException ioe) {
63 logger.error(ioe.getMessage());
64 Response response = getResponse();
65 response.setStatus(Status.CLIENT_ERROR_BAD_REQUEST, ioe);
66 return new EmptyRepresentation();
67 }
68
69 Request request = getRequest();
70
71 String service = (String)request.getAttributes().get("service");
72
73 ArtifactDatabase db = (ArtifactDatabase)getContext()
74 .getAttributes().get("database");
75
76 try {
77 return guessRepresentation(
78 db.process(service, inputDocument, getCallMeta()));
79 }
80 catch (ArtifactDatabaseException adbe) {
81 logger.warn(adbe.getLocalizedMessage(), adbe);
82 Response response = getResponse();
83 response.setStatus(
84 Status.CLIENT_ERROR_BAD_REQUEST, adbe.getMessage());
85 return new EmptyRepresentation();
86 }
87 }
88
89 protected static Representation guessRepresentation(Service.Output output) {
90
91 MediaType mediaType = new MediaType(output.getMIMEType());
92 Object data = output.getData();
93
94 if (data instanceof Document) {
95 return new DomRepresentation(mediaType, (Document)data);
96 }
97
98 if (data instanceof byte []) {
99 return new ByteArrayRepresentation(mediaType, (byte [])data);
100 }
101
102 return new EmptyRepresentation();
103 }
104 }
105 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org