Mercurial > dive4elements > framework
annotate artifact-database/src/main/java/de/intevation/artifactdatabase/rest/FindUserResource.java @ 417:410a1bfb9590
Fix for NPE on empty user database
artifacts/trunk@5270 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Christian Lins <christian.lins@intevation.de> |
---|---|
date | Mon, 27 Aug 2012 08:33:05 +0000 |
parents | 733e76413bf8 |
children |
rev | line source |
---|---|
416
733e76413bf8
Add REST service to be able to find a user by its account name
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
1 /* |
733e76413bf8
Add REST service to be able to find a user by its account name
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
2 * Copyright (c) 2011 by Intevation GmbH |
733e76413bf8
Add REST service to be able to find a user by its account name
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
3 * |
733e76413bf8
Add REST service to be able to find a user by its account name
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
4 * This program is free software under the LGPL (>=v2.1) |
733e76413bf8
Add REST service to be able to find a user by its account name
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
5 * Read the file LGPL.txt coming with the software for details |
733e76413bf8
Add REST service to be able to find a user by its account name
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
6 * or visit http://www.gnu.org/licenses/ if it does not exist. |
733e76413bf8
Add REST service to be able to find a user by its account name
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
7 */ |
733e76413bf8
Add REST service to be able to find a user by its account name
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
8 package de.intevation.artifactdatabase.rest; |
733e76413bf8
Add REST service to be able to find a user by its account name
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
9 |
733e76413bf8
Add REST service to be able to find a user by its account name
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
10 import java.io.IOException; |
733e76413bf8
Add REST service to be able to find a user by its account name
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
11 |
733e76413bf8
Add REST service to be able to find a user by its account name
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
12 import org.apache.log4j.Logger; |
733e76413bf8
Add REST service to be able to find a user by its account name
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
13 |
733e76413bf8
Add REST service to be able to find a user by its account name
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
14 import org.restlet.data.MediaType; |
733e76413bf8
Add REST service to be able to find a user by its account name
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
15 import org.restlet.data.Status; |
733e76413bf8
Add REST service to be able to find a user by its account name
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
16 import org.restlet.ext.xml.DomRepresentation; |
733e76413bf8
Add REST service to be able to find a user by its account name
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
17 import org.restlet.representation.EmptyRepresentation; |
733e76413bf8
Add REST service to be able to find a user by its account name
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
18 import org.restlet.representation.Representation; |
733e76413bf8
Add REST service to be able to find a user by its account name
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
19 import org.restlet.resource.ResourceException; |
733e76413bf8
Add REST service to be able to find a user by its account name
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
20 import org.restlet.Response; |
733e76413bf8
Add REST service to be able to find a user by its account name
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
21 |
733e76413bf8
Add REST service to be able to find a user by its account name
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
22 import org.w3c.dom.Document; |
733e76413bf8
Add REST service to be able to find a user by its account name
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
23 |
733e76413bf8
Add REST service to be able to find a user by its account name
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
24 import de.intevation.artifacts.ArtifactDatabase; |
733e76413bf8
Add REST service to be able to find a user by its account name
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
25 import de.intevation.artifacts.ArtifactDatabaseException; |
733e76413bf8
Add REST service to be able to find a user by its account name
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
26 |
733e76413bf8
Add REST service to be able to find a user by its account name
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
27 /** |
733e76413bf8
Add REST service to be able to find a user by its account name
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
28 * A Rest resource that finds the user provided by the artifact database. |
733e76413bf8
Add REST service to be able to find a user by its account name
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
29 * |
733e76413bf8
Add REST service to be able to find a user by its account name
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
30 */ |
733e76413bf8
Add REST service to be able to find a user by its account name
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
31 public class FindUserResource |
733e76413bf8
Add REST service to be able to find a user by its account name
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
32 extends BaseResource |
733e76413bf8
Add REST service to be able to find a user by its account name
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
33 { |
733e76413bf8
Add REST service to be able to find a user by its account name
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
34 /** The logger that is used in this class.*/ |
733e76413bf8
Add REST service to be able to find a user by its account name
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
35 private static Logger logger = Logger.getLogger(FindUserResource.class); |
733e76413bf8
Add REST service to be able to find a user by its account name
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
36 |
733e76413bf8
Add REST service to be able to find a user by its account name
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
37 /** server URL where to reach the resource.*/ |
733e76413bf8
Add REST service to be able to find a user by its account name
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
38 public static final String PATH = "/find-user"; |
733e76413bf8
Add REST service to be able to find a user by its account name
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
39 |
733e76413bf8
Add REST service to be able to find a user by its account name
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
40 |
733e76413bf8
Add REST service to be able to find a user by its account name
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
41 @Override |
733e76413bf8
Add REST service to be able to find a user by its account name
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
42 protected Representation innerPost(Representation requestRepr) |
733e76413bf8
Add REST service to be able to find a user by its account name
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
43 throws ResourceException |
733e76413bf8
Add REST service to be able to find a user by its account name
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
44 { |
733e76413bf8
Add REST service to be able to find a user by its account name
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
45 Document input = null; |
733e76413bf8
Add REST service to be able to find a user by its account name
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
46 |
733e76413bf8
Add REST service to be able to find a user by its account name
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
47 try { |
733e76413bf8
Add REST service to be able to find a user by its account name
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
48 DomRepresentation in = new DomRepresentation(requestRepr); |
733e76413bf8
Add REST service to be able to find a user by its account name
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
49 in.setNamespaceAware(true); |
733e76413bf8
Add REST service to be able to find a user by its account name
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
50 input = in.getDocument(); |
733e76413bf8
Add REST service to be able to find a user by its account name
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
51 } |
733e76413bf8
Add REST service to be able to find a user by its account name
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
52 catch (IOException ioe) { |
733e76413bf8
Add REST service to be able to find a user by its account name
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
53 logger.error(ioe.getLocalizedMessage(), ioe); |
733e76413bf8
Add REST service to be able to find a user by its account name
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
54 |
733e76413bf8
Add REST service to be able to find a user by its account name
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
55 Response response = getResponse(); |
733e76413bf8
Add REST service to be able to find a user by its account name
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
56 response.setStatus(Status.CLIENT_ERROR_BAD_REQUEST, ioe); |
733e76413bf8
Add REST service to be able to find a user by its account name
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
57 return new EmptyRepresentation(); |
733e76413bf8
Add REST service to be able to find a user by its account name
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
58 } |
733e76413bf8
Add REST service to be able to find a user by its account name
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
59 |
733e76413bf8
Add REST service to be able to find a user by its account name
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
60 ArtifactDatabase db = getArtifactDatabase(); |
733e76413bf8
Add REST service to be able to find a user by its account name
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
61 |
733e76413bf8
Add REST service to be able to find a user by its account name
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
62 try { |
733e76413bf8
Add REST service to be able to find a user by its account name
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
63 logger.info(PATH); |
733e76413bf8
Add REST service to be able to find a user by its account name
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
64 |
733e76413bf8
Add REST service to be able to find a user by its account name
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
65 return new DomRepresentation( |
733e76413bf8
Add REST service to be able to find a user by its account name
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
66 MediaType.APPLICATION_XML, |
733e76413bf8
Add REST service to be able to find a user by its account name
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
67 db.findUser(input, getCallMeta())); |
733e76413bf8
Add REST service to be able to find a user by its account name
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
68 } |
733e76413bf8
Add REST service to be able to find a user by its account name
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
69 catch (ArtifactDatabaseException adbe) { |
733e76413bf8
Add REST service to be able to find a user by its account name
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
70 logger.warn(adbe.getLocalizedMessage(), adbe); |
733e76413bf8
Add REST service to be able to find a user by its account name
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
71 |
733e76413bf8
Add REST service to be able to find a user by its account name
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
72 Response response = getResponse(); |
733e76413bf8
Add REST service to be able to find a user by its account name
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
73 response.setStatus( |
733e76413bf8
Add REST service to be able to find a user by its account name
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
74 Status.CLIENT_ERROR_UNPROCESSABLE_ENTITY, adbe.getMessage()); |
733e76413bf8
Add REST service to be able to find a user by its account name
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
75 return new EmptyRepresentation(); |
733e76413bf8
Add REST service to be able to find a user by its account name
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
76 } |
733e76413bf8
Add REST service to be able to find a user by its account name
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
77 } |
733e76413bf8
Add REST service to be able to find a user by its account name
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
78 } |
733e76413bf8
Add REST service to be able to find a user by its account name
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
79 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |