sascha@21: package de.intevation.artifactdatabase.rest; sascha@21: sascha@21: import org.w3c.dom.Document; sascha@21: sascha@21: import org.restlet.ext.xml.DomRepresentation; sascha@21: sascha@40: import org.restlet.resource.ResourceException; sascha@21: sascha@21: import org.restlet.data.Response; sascha@21: import org.restlet.data.Status; sascha@21: sascha@21: import org.restlet.data.MediaType; sascha@21: sascha@21: import org.apache.log4j.Logger; sascha@21: sascha@21: import de.intevation.artifactdatabase.XMLUtils; sascha@21: sascha@21: import org.restlet.representation.Representation; sascha@21: import org.restlet.representation.EmptyRepresentation; sascha@21: sascha@21: import java.io.IOException; sascha@21: sascha@26: import de.intevation.artifacts.ArtifactNamespaceContext; sascha@32: import de.intevation.artifacts.ArtifactDatabaseException; sascha@26: import de.intevation.artifacts.ArtifactDatabase; sascha@26: sascha@21: /** sascha@21: * @author Sascha L. Teichmann (sascha.teichmann@intevation) sascha@21: */ sascha@21: public class CreateResource sascha@40: extends BaseResource sascha@21: { sascha@21: private static Logger logger = Logger.getLogger(CreateResource.class); sascha@21: sascha@21: public static final String PATH = "/create"; sascha@21: sascha@26: public static final String XPATH_FACTORY = "/action/factory/@name"; sascha@26: sascha@26: public static final String NO_FACTORY_MESSAGE = "No factory given"; sascha@26: sascha@26: public static final String NO_ARTIFACT_CREATED = "No artifact created"; sascha@26: sascha@40: protected Representation innerPost(Representation requestRepr) sascha@40: throws ResourceException sascha@40: { sascha@21: Document inputDocument = null; sascha@21: try { sascha@21: DomRepresentation input = new DomRepresentation(requestRepr); sascha@21: inputDocument = input.getDocument(); sascha@21: } sascha@21: catch (IOException ioe) { sascha@21: logger.error(ioe.getMessage()); sascha@21: Response response = getResponse(); sascha@21: response.setStatus(Status.CLIENT_ERROR_BAD_REQUEST, ioe); sascha@21: return new EmptyRepresentation(); sascha@21: } sascha@26: sascha@26: String factory = XMLUtils.xpathString( sascha@26: inputDocument, sascha@26: XPATH_FACTORY, sascha@26: ArtifactNamespaceContext.INSTANCE); sascha@26: sascha@26: if (factory == null || factory.length() == 0) { sascha@26: Response response = getResponse(); sascha@32: response.setStatus( sascha@32: Status.CLIENT_ERROR_BAD_REQUEST, NO_FACTORY_MESSAGE); sascha@26: return new EmptyRepresentation(); sascha@26: } sascha@26: sascha@26: if (logger.isDebugEnabled()) { sascha@26: logger.debug("Create artifact with factory '" + factory + "'"); sascha@26: } sascha@26: sascha@26: ArtifactDatabase db = (ArtifactDatabase)getContext() sascha@26: .getAttributes().get("database"); sascha@26: sascha@32: try { sascha@32: return new DomRepresentation( sascha@32: MediaType.APPLICATION_XML, sascha@32: db.createArtifactWithFactory(factory)); sascha@32: } sascha@32: catch (ArtifactDatabaseException adbe) { sascha@26: Response response = getResponse(); sascha@32: response.setStatus( sascha@32: Status.CLIENT_ERROR_UNPROCESSABLE_ENTITY, adbe.getMessage()); sascha@26: return new EmptyRepresentation(); sascha@26: } sascha@21: } sascha@21: } sascha@21: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8: