sascha@21: package de.intevation.artifactdatabase.rest; sascha@21: sascha@93: import de.intevation.artifactdatabase.XMLUtils; sascha@93: sascha@93: import de.intevation.artifacts.ArtifactDatabase; sascha@93: import de.intevation.artifacts.ArtifactDatabaseException; sascha@93: import de.intevation.artifacts.ArtifactNamespaceContext; sascha@93: sascha@21: import java.io.IOException; sascha@21: tim@75: import org.apache.log4j.Logger; sascha@93: tim@75: import org.restlet.data.MediaType; tim@75: import org.restlet.data.Response; tim@75: import org.restlet.data.Status; sascha@93: tim@75: import org.restlet.ext.xml.DomRepresentation; sascha@93: tim@75: import org.restlet.representation.EmptyRepresentation; tim@75: import org.restlet.representation.Representation; tim@75: sascha@93: import org.restlet.resource.ResourceException; sascha@93: sascha@93: import org.w3c.dom.Document; sascha@26: sascha@21: /** sascha@88: * Resource to create a new artifact within artifact database. sascha@77: * @author Sascha L. Teichmann 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@88: /** sascha@88: * server URL where to reach the resource. sascha@88: */ sascha@21: public static final String PATH = "/create"; sascha@21: sascha@88: /** sascha@88: * XPATH to figure out the name of the factory which should be used sascha@88: * to create the new artifact. sascha@88: */ ingo@65: public static final String XPATH_FACTORY = "/art:action/art:factory/@name"; sascha@26: sascha@88: /** sascha@88: * Error message if no factory was given. sascha@88: */ sascha@26: public static final String NO_FACTORY_MESSAGE = "No factory given"; sascha@26: sascha@88: /** sascha@88: * Error message if no artifact was created. sascha@88: */ sascha@26: public static final String NO_ARTIFACT_CREATED = "No artifact created"; sascha@26: sascha@89: @Override 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); ingo@65: input.setNamespaceAware(true); 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@86: db.createArtifactWithFactory(factory, tim@75: getCallMeta(), tim@75: inputDocument)); 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@91: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :