Search in sources :

Example 6 with ContentDisposition

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

the class TestRestEndpoint 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.
     *
     * @throws Exception
     */
@Test
public void testGetMetacardAsXml() throws Exception {
    String filename = "src/test/resources/ValidGeojson.json";
    CatalogFramework framework = givenCatalogFramework(SAMPLE_ID);
    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(Map.class))).thenAnswer(new Answer<BinaryContent>() {

        @Override
        public BinaryContent answer(InvocationOnMock invocation) throws Throwable {
            Object[] args = invocation.getArguments();
            Metacard metacard = (Metacard) args[0];
            return content;
        }
    });
    RESTEndpoint restEndpoint = new RESTEndpoint(framework);
    // Add a MimeTypeToINputTransformer that the REST endpoint will call to create the metacard
    addMatchingService(restEndpoint, Arrays.asList(getSimpleTransformer()));
    restEndpoint.setTikaMimeTypeResolver(new TikaMimeTypeResolver());
    FilterBuilder filterBuilder = new GeotoolsFilterBuilder();
    restEndpoint.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);
    UriInfo uriInfo = createSpecificUriInfo(LOCAL_RETRIEVE_ADDRESS);
    Response response = restEndpoint.createMetacard(multipartBody, uriInfo, RESTEndpoint.DEFAULT_METACARD_TRANSFORMER);
    assertEquals(OK, response.getStatus());
    InputStream responseEntity = (InputStream) response.getEntity();
    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) Matchers.anyString(org.mockito.Matchers.anyString) BinaryContent(ddf.catalog.data.BinaryContent) QueryResponse(ddf.catalog.operation.QueryResponse) Response(javax.ws.rs.core.Response) SourceInfoResponse(ddf.catalog.operation.SourceInfoResponse) Metacard(ddf.catalog.data.Metacard) ContentDisposition(org.apache.cxf.jaxrs.ext.multipart.ContentDisposition) ByteArrayInputStream(java.io.ByteArrayInputStream) InvocationOnMock(org.mockito.invocation.InvocationOnMock) 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) Map(java.util.Map) MultivaluedMap(javax.ws.rs.core.MultivaluedMap) UriInfo(javax.ws.rs.core.UriInfo) Test(org.junit.Test)

Example 7 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)

Aggregations

ContentDisposition (org.apache.cxf.jaxrs.ext.multipart.ContentDisposition)7 Attachment (org.apache.cxf.jaxrs.ext.multipart.Attachment)5 MultipartBody (org.apache.cxf.jaxrs.ext.multipart.MultipartBody)4 Test (org.junit.Test)4 CatalogFramework (ddf.catalog.CatalogFramework)3 ByteArrayInputStream (java.io.ByteArrayInputStream)3 ArrayList (java.util.ArrayList)3 Response (javax.ws.rs.core.Response)3 MetacardImpl (ddf.catalog.data.impl.MetacardImpl)2 QueryResponse (ddf.catalog.operation.QueryResponse)2 SourceInfoResponse (ddf.catalog.operation.SourceInfoResponse)2 InputTransformer (ddf.catalog.transform.InputTransformer)2 MimeTypeMapper (ddf.mime.MimeTypeMapper)2 UriInfo (javax.ws.rs.core.UriInfo)2 BundleContext (org.osgi.framework.BundleContext)2 ServiceReference (org.osgi.framework.ServiceReference)2 BinaryContent (ddf.catalog.data.BinaryContent)1 Metacard (ddf.catalog.data.Metacard)1 FilterBuilder (ddf.catalog.filter.FilterBuilder)1 GeotoolsFilterBuilder (ddf.catalog.filter.proxy.builder.GeotoolsFilterBuilder)1