Search in sources :

Example 16 with Attachment

use of org.apache.cxf.jaxrs.ext.multipart.Attachment in project ddf by codice.

the class RESTEndpoint method parseAttachments.

CreateInfo parseAttachments(List<Attachment> contentParts, String transformerParam) {
    if (contentParts.size() == 1) {
        Attachment contentPart = contentParts.get(0);
        return parseAttachment(contentPart);
    }
    List<Attribute> attributes = new ArrayList<>(contentParts.size());
    Metacard metacard = null;
    CreateInfo createInfo = null;
    for (Attachment attachment : contentParts) {
        String name = attachment.getContentDisposition().getParameter("name");
        String parsedName = (name.startsWith("parse.")) ? name.substring(6) : name;
        try {
            InputStream inputStream = attachment.getDataHandler().getInputStream();
            if (name.equals("parse.resource")) {
                createInfo = parseAttachment(attachment);
            } else if (name.equals("parse.metadata")) {
                metacard = parseMetadata(transformerParam, metacard, attachment, inputStream);
            } else {
                parseOverrideAttributes(attributes, parsedName, inputStream);
            }
        } catch (IOException e) {
            LOGGER.debug("Unable to get input stream for mime attachment. Ignoring override attribute: {}", name, e);
        }
    }
    if (createInfo == null) {
        throw new IllegalArgumentException("No parse.resource specified in request.");
    }
    if (metacard == null) {
        metacard = new MetacardImpl();
    }
    for (Attribute attribute : attributes) {
        metacard.setAttribute(attribute);
    }
    createInfo.setMetacard(metacard);
    return createInfo;
}
Also used : Metacard(ddf.catalog.data.Metacard) Attribute(ddf.catalog.data.Attribute) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) Attachment(org.apache.cxf.jaxrs.ext.multipart.Attachment) IOException(java.io.IOException) MetacardImpl(ddf.catalog.data.impl.MetacardImpl)

Example 17 with Attachment

use of org.apache.cxf.jaxrs.ext.multipart.Attachment in project ddf by codice.

the class RESTEndpoint method updateDocument.

/**
     * REST Put. Updates the specified entry with the provided document.
     *
     * @param id
     * @param message
     * @return
     */
@PUT
@Path("/{id}")
@Consumes("multipart/*")
public Response updateDocument(@PathParam("id") String id, @Context HttpHeaders headers, @Context HttpServletRequest httpRequest, MultipartBody multipartBody, @QueryParam("transform") String transformerParam, InputStream message) {
    LOGGER.trace("PUT");
    Response response;
    try {
        if (id != null && message != null) {
            MimeType mimeType = getMimeType(headers);
            CreateInfo createInfo = null;
            if (multipartBody != null) {
                List<Attachment> contentParts = multipartBody.getAllAttachments();
                if (contentParts != null && contentParts.size() > 0) {
                    createInfo = parseAttachments(contentParts, transformerParam);
                } else {
                    LOGGER.debug("No file contents attachment found");
                }
            }
            if (createInfo == null) {
                UpdateRequest updateRequest = new UpdateRequestImpl(id, generateMetacard(mimeType, id, message, transformerParam));
                catalogFramework.update(updateRequest);
            } else {
                UpdateStorageRequest streamUpdateRequest = new UpdateStorageRequestImpl(Collections.singletonList(new IncomingContentItem(id, createInfo.getStream(), createInfo.getContentType(), createInfo.getFilename(), 0, createInfo.getMetacard())), null);
                catalogFramework.update(streamUpdateRequest);
            }
            LOGGER.debug("Metacard {} updated.", id);
            response = Response.ok().build();
        } else {
            String errorResponseString = "Both ID and content are needed to perform UPDATE.";
            LOGGER.info(errorResponseString);
            throw new ServerErrorException(errorResponseString, Status.BAD_REQUEST);
        }
    } catch (SourceUnavailableException e) {
        String exceptionMessage = "Cannot update catalog entry: Source is unavailable: ";
        LOGGER.info(exceptionMessage, e);
        throw new ServerErrorException(exceptionMessage, Status.INTERNAL_SERVER_ERROR);
    } catch (InternalIngestException e) {
        String exceptionMessage = "Error cataloging updated metadata: ";
        LOGGER.info(exceptionMessage, e);
        throw new ServerErrorException(exceptionMessage, Status.INTERNAL_SERVER_ERROR);
    } catch (MetacardCreationException | IngestException e) {
        String exceptionMessage = "Error cataloging updated metadata: ";
        LOGGER.info(exceptionMessage, e);
        throw new ServerErrorException(exceptionMessage, Status.BAD_REQUEST);
    }
    return response;
}
Also used : SourceUnavailableException(ddf.catalog.source.SourceUnavailableException) MetacardCreationException(ddf.catalog.data.MetacardCreationException) UpdateStorageRequestImpl(ddf.catalog.content.operation.impl.UpdateStorageRequestImpl) InternalIngestException(ddf.catalog.source.InternalIngestException) UpdateRequest(ddf.catalog.operation.UpdateRequest) Attachment(org.apache.cxf.jaxrs.ext.multipart.Attachment) MimeType(javax.activation.MimeType) SourceInfoResponse(ddf.catalog.operation.SourceInfoResponse) QueryResponse(ddf.catalog.operation.QueryResponse) Response(javax.ws.rs.core.Response) CreateResponse(ddf.catalog.operation.CreateResponse) UpdateStorageRequest(ddf.catalog.content.operation.UpdateStorageRequest) IngestException(ddf.catalog.source.IngestException) InternalIngestException(ddf.catalog.source.InternalIngestException) UpdateRequestImpl(ddf.catalog.operation.impl.UpdateRequestImpl) Path(javax.ws.rs.Path) Consumes(javax.ws.rs.Consumes) PUT(javax.ws.rs.PUT)

Aggregations

Attachment (org.apache.cxf.jaxrs.ext.multipart.Attachment)17 Response (javax.ws.rs.core.Response)13 Test (org.junit.Test)9 QueryResponse (ddf.catalog.operation.QueryResponse)5 SourceInfoResponse (ddf.catalog.operation.SourceInfoResponse)5 ByteArrayInputStream (java.io.ByteArrayInputStream)5 ContentDisposition (org.apache.cxf.jaxrs.ext.multipart.ContentDisposition)5 InputStream (java.io.InputStream)4 ArrayList (java.util.ArrayList)4 POST (javax.ws.rs.POST)4 Path (javax.ws.rs.Path)4 MultipartBody (org.apache.cxf.jaxrs.ext.multipart.MultipartBody)4 CatalogFramework (ddf.catalog.CatalogFramework)3 Metacard (ddf.catalog.data.Metacard)3 MetacardCreationException (ddf.catalog.data.MetacardCreationException)3 MetacardImpl (ddf.catalog.data.impl.MetacardImpl)3 CreateResponse (ddf.catalog.operation.CreateResponse)3 File (java.io.File)3 IOException (java.io.IOException)3 MimeType (javax.activation.MimeType)3