comparison artifact-database/src/main/java/de/intevation/artifactdatabase/rest/CreateCollectionResource.java @ 140:f141ae401a00

Added a Rest resource that might be used to create new collections owned by a specific user. artifacts/trunk@1365 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Wed, 02 Mar 2011 11:48:34 +0000
parents
children
comparison
equal deleted inserted replaced
139:320a81983c8d 140:f141ae401a00
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
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.data.MediaType;
19 import org.restlet.data.Status;
20 import org.restlet.ext.xml.DomRepresentation;
21 import org.restlet.representation.EmptyRepresentation;
22 import org.restlet.representation.Representation;
23 import org.restlet.resource.ResourceException;
24 import org.restlet.Response;
25 import org.restlet.Request;
26
27 import org.w3c.dom.Document;
28
29 /**
30 * Resource to create a new collections within the artifact database.
31 *
32 * @author <a href="mailto:ingo.weinzierl@intevation">Ingo Weinzierl</a>
33 */
34 public class CreateCollectionResource
35 extends BaseResource
36 {
37 /** The logger used in this class.*/
38 private static Logger logger =
39 Logger.getLogger(CreateCollectionResource.class);
40
41 /** The URL part for this resource.*/
42 public static final String PATH = "/create-collection/{ownerid}";
43
44
45 @Override
46 protected Representation innerPost(Representation requestRepr)
47 throws ResourceException
48 {
49 Document input = null;
50
51 try {
52 DomRepresentation in = new DomRepresentation(requestRepr);
53 in.setNamespaceAware(true);
54 input = in.getDocument();
55 }
56 catch (IOException ioe) {
57 logger.error(ioe.getLocalizedMessage(), ioe);
58
59 Response response = getResponse();
60 response.setStatus(Status.CLIENT_ERROR_BAD_REQUEST, ioe);
61 return new EmptyRepresentation();
62 }
63
64 ArtifactDatabase db = getArtifactDatabase();
65
66 Request request = getRequest();
67
68 String ownerId = (String) request.getAttributes().get("ownerid");
69
70 logger.info("Create new collection owned by: " + ownerId);
71
72 try {
73 return new DomRepresentation(
74 MediaType.APPLICATION_XML,
75 db.createCollection(ownerId, input, getCallMeta()));
76 }
77 catch (ArtifactDatabaseException adbe) {
78 logger.warn(adbe.getLocalizedMessage(), adbe);
79
80 Response response = getResponse();
81 response.setStatus(
82 Status.CLIENT_ERROR_UNPROCESSABLE_ENTITY, adbe.getMessage());
83 return new EmptyRepresentation();
84 }
85 }
86 }
87 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org