comparison artifact-database/src/main/java/de/intevation/artifactdatabase/rest/FindUserResource.java @ 416:733e76413bf8

Add REST service to be able to find a user by its account name artifacts/trunk@5263 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Bjoern Ricks <bjoern.ricks@intevation.de>
date Fri, 24 Aug 2012 14:09:53 +0000
parents
children
comparison
equal deleted inserted replaced
415:d4a846ee35b8 416:733e76413bf8
1 /*
2 * Copyright (c) 2011 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 package de.intevation.artifactdatabase.rest;
9
10 import java.io.IOException;
11
12 import org.apache.log4j.Logger;
13
14 import org.restlet.data.MediaType;
15 import org.restlet.data.Status;
16 import org.restlet.ext.xml.DomRepresentation;
17 import org.restlet.representation.EmptyRepresentation;
18 import org.restlet.representation.Representation;
19 import org.restlet.resource.ResourceException;
20 import org.restlet.Response;
21
22 import org.w3c.dom.Document;
23
24 import de.intevation.artifacts.ArtifactDatabase;
25 import de.intevation.artifacts.ArtifactDatabaseException;
26
27 /**
28 * A Rest resource that finds the user provided by the artifact database.
29 *
30 */
31 public class FindUserResource
32 extends BaseResource
33 {
34 /** The logger that is used in this class.*/
35 private static Logger logger = Logger.getLogger(FindUserResource.class);
36
37 /** server URL where to reach the resource.*/
38 public static final String PATH = "/find-user";
39
40
41 @Override
42 protected Representation innerPost(Representation requestRepr)
43 throws ResourceException
44 {
45 Document input = null;
46
47 try {
48 DomRepresentation in = new DomRepresentation(requestRepr);
49 in.setNamespaceAware(true);
50 input = in.getDocument();
51 }
52 catch (IOException ioe) {
53 logger.error(ioe.getLocalizedMessage(), ioe);
54
55 Response response = getResponse();
56 response.setStatus(Status.CLIENT_ERROR_BAD_REQUEST, ioe);
57 return new EmptyRepresentation();
58 }
59
60 ArtifactDatabase db = getArtifactDatabase();
61
62 try {
63 logger.info(PATH);
64
65 return new DomRepresentation(
66 MediaType.APPLICATION_XML,
67 db.findUser(input, getCallMeta()));
68 }
69 catch (ArtifactDatabaseException adbe) {
70 logger.warn(adbe.getLocalizedMessage(), adbe);
71
72 Response response = getResponse();
73 response.setStatus(
74 Status.CLIENT_ERROR_UNPROCESSABLE_ENTITY, adbe.getMessage());
75 return new EmptyRepresentation();
76 }
77 }
78 }
79 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org