Search in sources :

Example 21 with ContentDisposition

use of org.apache.cxf.jaxrs.ext.multipart.ContentDisposition in project tika by apache.

the class GrobidRESTParser method parse.

public void parse(String filePath, ContentHandler handler, Metadata metadata, ParseContext context) throws FileNotFoundException {
    File pdfFile = new File(filePath);
    ContentDisposition cd = new ContentDisposition("form-data; name=\"input\"; filename=\"" + pdfFile.getName() + "\"");
    Attachment att = new Attachment("input", new FileInputStream(pdfFile), cd);
    MultipartBody body = new MultipartBody(att);
    Response response = WebClient.create(restHostUrlStr + GROBID_PROCESSHEADER_PATH).accept(MediaType.APPLICATION_XML).type(MediaType.MULTIPART_FORM_DATA).post(body);
    try {
        String resp = response.readEntity(String.class);
        Metadata teiMet = new TEIParser().parse(resp);
        for (String key : teiMet.names()) {
            metadata.add("grobid:header_" + key, teiMet.get(key));
        }
    } catch (Exception e) {
        LOG.warn("Couldn't read response", e);
    }
}
Also used : Response(javax.ws.rs.core.Response) ContentDisposition(org.apache.cxf.jaxrs.ext.multipart.ContentDisposition) MultipartBody(org.apache.cxf.jaxrs.ext.multipart.MultipartBody) Metadata(org.apache.tika.metadata.Metadata) Attachment(org.apache.cxf.jaxrs.ext.multipart.Attachment) File(java.io.File) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException)

Example 22 with ContentDisposition

use of org.apache.cxf.jaxrs.ext.multipart.ContentDisposition in project opencast by opencast.

the class IngestServiceImpl method getContentDispositionFileName.

private String getContentDispositionFileName(HttpResponse response) {
    if (response == null)
        return null;
    Header header = response.getFirstHeader("Content-Disposition");
    ContentDisposition contentDisposition = new ContentDisposition(header.getValue());
    return contentDisposition.getParameter("filename");
}
Also used : Header(org.apache.http.Header) ContentDisposition(org.apache.cxf.jaxrs.ext.multipart.ContentDisposition)

Example 23 with ContentDisposition

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

the class AbstractCatalogService method parseParts.

@Override
public Map.Entry<AttachmentInfo, Metacard> parseParts(Collection<Part> contentParts, String transformerParam) {
    if (contentParts.size() == 1) {
        Part part = Iterables.get(contentParts, 0);
        try (InputStream inputStream = part.getInputStream()) {
            ContentDisposition contentDisposition = new ContentDisposition(part.getHeader(HEADER_CONTENT_DISPOSITION));
            return new ImmutablePair<>(attachmentParser.generateAttachmentInfo(inputStream, part.getContentType(), contentDisposition.getParameter(FILENAME_CONTENT_DISPOSITION_PARAMETER_NAME)), null);
        } catch (IOException e) {
            LOGGER.debug("IOException reading stream from file attachment in multipart body.", e);
        }
    }
    Metacard metacard = null;
    AttachmentInfo attachmentInfo = null;
    Map<String, AttributeImpl> attributeMap = new HashMap<>();
    for (Part part : contentParts) {
        String name = part.getName();
        String parsedName = (name.startsWith("parse.")) ? name.substring(6) : name;
        try (InputStream inputStream = part.getInputStream()) {
            ContentDisposition contentDisposition = new ContentDisposition(part.getHeader(HEADER_CONTENT_DISPOSITION));
            switch(name) {
                case "parse.resource":
                    attachmentInfo = attachmentParser.generateAttachmentInfo(inputStream, part.getContentType(), contentDisposition.getParameter(FILENAME_CONTENT_DISPOSITION_PARAMETER_NAME));
                    break;
                case "parse.metadata":
                    metacard = parseMetacard(transformerParam, metacard, part, inputStream);
                    break;
                default:
                    parseOverrideAttributes(attributeMap, parsedName, inputStream);
                    break;
            }
        } catch (IOException e) {
            LOGGER.debug("Unable to get input stream for mime attachment. Ignoring override attribute: {}", name, e);
        }
    }
    if (attachmentInfo == null) {
        throw new IllegalArgumentException("No parse.resource specified in request.");
    }
    if (metacard == null) {
        metacard = new MetacardImpl();
    }
    Set<AttributeDescriptor> missingDescriptors = new HashSet<>();
    for (Attribute attribute : attributeMap.values()) {
        if (metacard.getMetacardType().getAttributeDescriptor(attribute.getName()) == null) {
            attributeRegistry.lookup(attribute.getName()).ifPresent(missingDescriptors::add);
        }
        metacard.setAttribute(attribute);
    }
    if (!missingDescriptors.isEmpty()) {
        MetacardType original = metacard.getMetacardType();
        MetacardImpl newMetacard = new MetacardImpl(metacard);
        newMetacard.setType(new MetacardTypeImpl(original.getName(), original, missingDescriptors));
        metacard = newMetacard;
    }
    return new ImmutablePair<>(attachmentInfo, metacard);
}
Also used : HashMap(java.util.HashMap) Attribute(ddf.catalog.data.Attribute) BoundedInputStream(org.apache.commons.io.input.BoundedInputStream) InputStream(java.io.InputStream) AttributeImpl(ddf.catalog.data.impl.AttributeImpl) AttachmentInfo(org.codice.ddf.attachment.AttachmentInfo) AttributeDescriptor(ddf.catalog.data.AttributeDescriptor) MetacardTypeImpl(ddf.catalog.data.impl.MetacardTypeImpl) IOException(java.io.IOException) MetacardImpl(ddf.catalog.data.impl.MetacardImpl) MetacardType(ddf.catalog.data.MetacardType) Metacard(ddf.catalog.data.Metacard) ContentDisposition(org.apache.cxf.jaxrs.ext.multipart.ContentDisposition) ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair) Part(javax.servlet.http.Part) HashSet(java.util.HashSet)

Example 24 with ContentDisposition

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

the class CatalogServiceImplTest method testGetMetacardAsXml.

/**
 * Tests that a geojson input has its InputTransformer invoked by the REST endpoint to create a
 * metacard that is then converted to XML and returned from the REST endpoint.
 */
@Test
@SuppressWarnings({ "unchecked" })
public void testGetMetacardAsXml() throws Exception {
    CatalogFramework framework = givenCatalogFramework();
    String metacardXml = "<metacard ns2:id=\"assigned-when-ingested\">\r\n" + "<type>type.metacard</type>\r\n" + "<string name=\"title\">\r\n" + "<value>Title goes here ...</value>\r\n" + "</string>\r\n" + "<string name=\"metadata\">\r\n" + "<value>metadata goes here ...</value>\r\n" + "</metacard>";
    // Mock XmlMetacardTransformer that CatalogFramework will call to convert generated
    // metacard into XML to be returned from REST endpoint.
    final BinaryContent content = mock(BinaryContent.class);
    InputStream inputStream = new ByteArrayInputStream(metacardXml.getBytes(GET_OUTPUT_TYPE));
    when(content.getInputStream()).thenReturn(inputStream);
    when(content.getMimeTypeValue()).thenReturn("application/json;id=geojson");
    when(framework.transform(isA(Metacard.class), anyString(), isNull())).thenReturn(content);
    CatalogServiceImpl catalogService = new CatalogServiceImpl(framework, attachmentParser, attributeRegistry);
    // Add a MimeTypeToINputTransformer that the REST endpoint will call to create the metacard
    addMatchingService(catalogService, Collections.singletonList(getSimpleTransformer()));
    catalogService.setTikaMimeTypeResolver(new TikaMimeTypeResolver());
    FilterBuilder filterBuilder = new GeotoolsFilterBuilder();
    catalogService.setFilterBuilder(filterBuilder);
    String json = "{\r\n" + "    \"properties\": {\r\n" + "        \"title\": \"myTitle\",\r\n" + "        \"thumbnail\": \"CA==\",\r\n" + "        \"resource-uri\": \"http://example.com\",\r\n" + "        \"created\": \"2012-09-01T00:09:19.368+0000\",\r\n" + "        \"metadata-content-type-version\": \"myVersion\",\r\n" + "        \"metadata-content-type\": \"myType\",\r\n" + "        \"metadata\": \"<xml>metadata goes here ...</xml>\",\r\n" + "        \"modified\": \"2012-09-01T00:09:19.368+0000\"\r\n" + "    },\r\n" + "    \"type\": \"Feature\",\r\n" + "    \"geometry\": {\r\n" + "        \"type\": \"Point\",\r\n" + "        \"coordinates\": [\r\n" + "            30.0,\r\n" + "            10.0\r\n" + "        ]\r\n" + "    }\r\n" + "} ";
    // Sample headers for a multipart body specifying a geojson file to have a metacard created for:
    // Content-Disposition: form-data; name="file"; filename="C:\DDF\geojson_valid.json"
    // Content-Type: application/json;id=geojson
    InputStream is = IOUtils.toInputStream(json);
    List<Attachment> attachments = new ArrayList<>();
    ContentDisposition contentDisposition = new ContentDisposition("form-data; name=file; filename=C:\\DDF\\geojson_valid.json");
    Attachment attachment = new Attachment("file_part", is, contentDisposition);
    attachments.add(attachment);
    MediaType mediaType = new MediaType(MediaType.APPLICATION_JSON, "id=geojson");
    MultipartBody multipartBody = new MultipartBody(attachments, mediaType, true);
    BinaryContent binaryContent = catalogService.createMetacard(multipartBody, AbstractCatalogService.DEFAULT_METACARD_TRANSFORMER);
    InputStream responseEntity = binaryContent.getInputStream();
    String responseXml = IOUtils.toString(responseEntity);
    assertEquals(metacardXml, responseXml);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) TikaMimeTypeResolver(ddf.mime.tika.TikaMimeTypeResolver) ArrayList(java.util.ArrayList) Attachment(org.apache.cxf.jaxrs.ext.multipart.Attachment) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) BinaryContent(ddf.catalog.data.BinaryContent) Metacard(ddf.catalog.data.Metacard) ContentDisposition(org.apache.cxf.jaxrs.ext.multipart.ContentDisposition) ByteArrayInputStream(java.io.ByteArrayInputStream) GeotoolsFilterBuilder(ddf.catalog.filter.proxy.builder.GeotoolsFilterBuilder) FilterBuilder(ddf.catalog.filter.FilterBuilder) MultipartBody(org.apache.cxf.jaxrs.ext.multipart.MultipartBody) GeotoolsFilterBuilder(ddf.catalog.filter.proxy.builder.GeotoolsFilterBuilder) CatalogFramework(ddf.catalog.CatalogFramework) MediaType(javax.ws.rs.core.MediaType) Test(org.junit.Test)

Example 25 with ContentDisposition

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

the class CatalogServiceImplTest method testParseAttachmentsWithAttributeOverrides.

@Test
public void testParseAttachmentsWithAttributeOverrides() throws IngestException, SourceUnavailableException {
    CatalogFramework framework = givenCatalogFramework();
    CatalogServiceImpl catalogServiceImpl = new CatalogServiceImpl(framework, attachmentParser, attributeRegistry);
    when(attributeRegistry.lookup(Topic.KEYWORD)).thenReturn(Optional.of(new TopicAttributes().getAttributeDescriptor(Topic.KEYWORD)));
    when(attributeRegistry.lookup(Core.LOCATION)).thenReturn(Optional.of(new CoreAttributes().getAttributeDescriptor(Core.LOCATION)));
    List<Attachment> attachments = new ArrayList<>();
    ContentDisposition contentDisposition = new ContentDisposition("form-data; name=parse.resource; filename=/path/to/metacard.txt");
    Attachment attachment = new Attachment("parse.resource", new ByteArrayInputStream("Some Text".getBytes()), contentDisposition);
    ContentDisposition contentDisposition1 = new ContentDisposition("form-data; name=parse.location");
    Attachment attachment1 = new Attachment("parse.location", new ByteArrayInputStream("POINT(0 0)".getBytes()), contentDisposition1);
    ContentDisposition contentDisposition2 = new ContentDisposition("form-data; name=parse.topic.keyword");
    Attachment attachment2 = new Attachment("parse.topic.keyword", new ByteArrayInputStream("keyword1".getBytes()), contentDisposition2);
    ContentDisposition contentDisposition3 = new ContentDisposition("form-data; name=parse.topic.keyword");
    Attachment attachment3 = new Attachment("parse.topic.keyword", new ByteArrayInputStream("keyword2".getBytes()), contentDisposition3);
    attachments.add(attachment);
    attachments.add(attachment1);
    attachments.add(attachment2);
    attachments.add(attachment3);
    Pair<AttachmentInfo, Metacard> attachmentInfoAndMetacard = catalogServiceImpl.parseAttachments(attachments, null);
    Metacard metacard = attachmentInfoAndMetacard.getValue();
    assertThat(metacard.getAttribute(Core.LOCATION).getValues(), hasItem("POINT(0 0)"));
    assertThat(metacard.getAttribute(Topic.KEYWORD).getValues(), hasItems("keyword1", "keyword2"));
}
Also used : Metacard(ddf.catalog.data.Metacard) ContentDisposition(org.apache.cxf.jaxrs.ext.multipart.ContentDisposition) ByteArrayInputStream(java.io.ByteArrayInputStream) CoreAttributes(ddf.catalog.data.impl.types.CoreAttributes) CatalogFramework(ddf.catalog.CatalogFramework) ArrayList(java.util.ArrayList) AttachmentInfo(org.codice.ddf.attachment.AttachmentInfo) Attachment(org.apache.cxf.jaxrs.ext.multipart.Attachment) TopicAttributes(ddf.catalog.data.impl.types.TopicAttributes) Test(org.junit.Test)

Aggregations

ContentDisposition (org.apache.cxf.jaxrs.ext.multipart.ContentDisposition)37 Attachment (org.apache.cxf.jaxrs.ext.multipart.Attachment)25 Test (org.junit.Test)18 MultipartBody (org.apache.cxf.jaxrs.ext.multipart.MultipartBody)15 ByteArrayInputStream (java.io.ByteArrayInputStream)13 ArrayList (java.util.ArrayList)11 CatalogFramework (ddf.catalog.CatalogFramework)10 IOException (java.io.IOException)10 InputStream (java.io.InputStream)9 MetacardImpl (ddf.catalog.data.impl.MetacardImpl)7 InputTransformer (ddf.catalog.transform.InputTransformer)7 BundleContext (org.osgi.framework.BundleContext)7 ServiceReference (org.osgi.framework.ServiceReference)7 Metacard (ddf.catalog.data.Metacard)6 URI (java.net.URI)6 APIProvider (org.wso2.carbon.apimgt.api.APIProvider)6 CoreAttributes (ddf.catalog.data.impl.types.CoreAttributes)5 Response (javax.ws.rs.core.Response)5 File (java.io.File)4 WebClient (org.apache.cxf.jaxrs.client.WebClient)4