comparison artifact-database/src/main/java/org/dive4elements/artifactdatabase/rest/CreateResource.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/CreateResource.java@b2115f484edb
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.common.utils.XMLUtils;
12
13 import de.intevation.artifacts.ArtifactDatabase;
14 import de.intevation.artifacts.ArtifactDatabaseException;
15 import de.intevation.artifacts.ArtifactNamespaceContext;
16
17 import java.io.IOException;
18
19 import org.apache.log4j.Logger;
20
21 import org.restlet.Response;
22
23 import org.restlet.data.MediaType;
24 import org.restlet.data.Status;
25
26 import org.restlet.ext.xml.DomRepresentation;
27
28 import org.restlet.representation.EmptyRepresentation;
29 import org.restlet.representation.Representation;
30
31 import org.restlet.resource.ResourceException;
32
33 import org.w3c.dom.Document;
34
35 /**
36 * Resource to create a new artifact within artifact database.
37 * @author <a href="mailto:sascha.teichmann@intevation">Sascha L. Teichmann</a>
38 */
39 public class CreateResource
40 extends BaseResource
41 {
42 private static Logger logger = Logger.getLogger(CreateResource.class);
43
44 /**
45 * server URL where to reach the resource.
46 */
47 public static final String PATH = "/create";
48
49 /**
50 * XPATH to figure out the name of the factory which should be used
51 * to create the new artifact.
52 */
53 public static final String XPATH_FACTORY = "/art:action/art:factory/@name";
54
55 /**
56 * Error message if no factory was given.
57 */
58 public static final String NO_FACTORY_MESSAGE = "No factory given";
59
60 /**
61 * Error message if no artifact was created.
62 */
63 public static final String NO_ARTIFACT_CREATED = "No artifact created";
64
65 @Override
66 protected Representation innerPost(Representation requestRepr)
67 throws ResourceException
68 {
69 Document inputDocument = null;
70 try {
71 DomRepresentation input = new DomRepresentation(requestRepr);
72 input.setNamespaceAware(true);
73 inputDocument = input.getDocument();
74 }
75 catch (IOException ioe) {
76 logger.error(ioe.getMessage());
77 Response response = getResponse();
78 response.setStatus(Status.CLIENT_ERROR_BAD_REQUEST, ioe);
79 return new EmptyRepresentation();
80 }
81
82 String factory = XMLUtils.xpathString(
83 inputDocument,
84 XPATH_FACTORY,
85 ArtifactNamespaceContext.INSTANCE);
86
87 if (factory == null || factory.length() == 0) {
88 Response response = getResponse();
89 response.setStatus(
90 Status.CLIENT_ERROR_BAD_REQUEST, NO_FACTORY_MESSAGE);
91 return new EmptyRepresentation();
92 }
93
94 if (logger.isDebugEnabled()) {
95 logger.debug("Create artifact with factory '" + factory + "'");
96 }
97
98 ArtifactDatabase db = getArtifactDatabase();
99
100 try {
101 return new DomRepresentation(
102 MediaType.APPLICATION_XML,
103 db.createArtifactWithFactory(factory,
104 getCallMeta(),
105 inputDocument));
106 }
107 catch (ArtifactDatabaseException adbe) {
108 Response response = getResponse();
109 response.setStatus(
110 Status.CLIENT_ERROR_UNPROCESSABLE_ENTITY, adbe.getMessage());
111 return new EmptyRepresentation();
112 }
113 }
114 }
115 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org