Search in sources :

Example 1 with TikaMimeTypeResolver

use of ddf.mime.tika.TikaMimeTypeResolver 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 2 with TikaMimeTypeResolver

use of ddf.mime.tika.TikaMimeTypeResolver in project ddf by codice.

the class ResourceReaderTest method setUp.

@Before
public void setUp() {
    MimeTypeResolver tikaResolver = new TikaMimeTypeResolver();
    this.customResolver = new CustomMimeTypeResolver();
    List<MimeTypeResolver> resolvers = new ArrayList<MimeTypeResolver>();
    resolvers.add(tikaResolver);
    resolvers.add(this.customResolver);
    this.mimeTypeMapper = new MimeTypeMapperImpl(resolvers);
}
Also used : TikaMimeTypeResolver(ddf.mime.tika.TikaMimeTypeResolver) MimeTypeResolver(ddf.mime.MimeTypeResolver) CustomMimeTypeResolver(ddf.mime.custom.CustomMimeTypeResolver) TikaMimeTypeResolver(ddf.mime.tika.TikaMimeTypeResolver) ArrayList(java.util.ArrayList) MimeTypeMapperImpl(ddf.mime.mapper.MimeTypeMapperImpl) CustomMimeTypeResolver(ddf.mime.custom.CustomMimeTypeResolver) Before(org.junit.Before)

Example 3 with TikaMimeTypeResolver

use of ddf.mime.tika.TikaMimeTypeResolver in project ddf by codice.

the class TestRestEndpoint method executeTest.

private Response executeTest(CatalogFramework framework, String transformer, boolean local, HttpServletRequest request) throws URISyntaxException {
    RESTEndpoint restEndpoint = new RESTEndpoint(framework);
    restEndpoint.setTikaMimeTypeResolver(new TikaMimeTypeResolver());
    FilterBuilder filterBuilder = new GeotoolsFilterBuilder();
    restEndpoint.setFilterBuilder(filterBuilder);
    UriInfo uriInfo;
    Response response;
    if (local) {
        uriInfo = createSpecificUriInfo(LOCAL_RETRIEVE_ADDRESS);
        response = restEndpoint.getDocument(GET_ID, transformer, uriInfo, request);
    } else {
        uriInfo = createSpecificUriInfo(FED_RETRIEVE_ADDRESS);
        response = restEndpoint.getDocument(GET_SITENAME, GET_ID, transformer, uriInfo, request);
    }
    return response;
}
Also used : QueryResponse(ddf.catalog.operation.QueryResponse) Response(javax.ws.rs.core.Response) SourceInfoResponse(ddf.catalog.operation.SourceInfoResponse) GeotoolsFilterBuilder(ddf.catalog.filter.proxy.builder.GeotoolsFilterBuilder) FilterBuilder(ddf.catalog.filter.FilterBuilder) GeotoolsFilterBuilder(ddf.catalog.filter.proxy.builder.GeotoolsFilterBuilder) TikaMimeTypeResolver(ddf.mime.tika.TikaMimeTypeResolver) UriInfo(javax.ws.rs.core.UriInfo)

Example 4 with TikaMimeTypeResolver

use of ddf.mime.tika.TikaMimeTypeResolver in project ddf by codice.

the class TestRestEndpoint method headTest.

private Response headTest(boolean local) throws CatalogTransformerException, URISyntaxException, UnsupportedEncodingException, UnsupportedQueryException, SourceUnavailableException, FederationException, IngestException {
    MetacardImpl metacard = null;
    List<Result> list = new ArrayList<Result>();
    Result result = mock(Result.class);
    InputStream inputStream = null;
    UriInfo uriInfo;
    Response response;
    CatalogFramework framework = givenCatalogFramework(SAMPLE_ID);
    list.add(result);
    QueryResponse queryResponse = mock(QueryResponse.class);
    when(queryResponse.getResults()).thenReturn(list);
    when(framework.query(isA(QueryRequest.class), isNull(FederationStrategy.class))).thenReturn(queryResponse);
    metacard = new MetacardImpl();
    metacard.setSourceId(GET_SITENAME);
    when(result.getMetacard()).thenReturn(metacard);
    Resource resource = mock(Resource.class);
    inputStream = new ByteArrayInputStream(GET_STREAM.getBytes(GET_OUTPUT_TYPE));
    when(resource.getInputStream()).thenReturn(inputStream);
    when(resource.getMimeTypeValue()).thenReturn(GET_MIME_TYPE);
    when(resource.getName()).thenReturn(GET_FILENAME);
    when(framework.transform(isA(Metacard.class), anyString(), isA(Map.class))).thenReturn(resource);
    RESTEndpoint restEndpoint = new RESTEndpoint(framework);
    restEndpoint.setTikaMimeTypeResolver(new TikaMimeTypeResolver());
    FilterBuilder filterBuilder = new GeotoolsFilterBuilder();
    restEndpoint.setFilterBuilder(filterBuilder);
    uriInfo = createSpecificUriInfo(LOCAL_RETRIEVE_ADDRESS);
    if (local) {
        response = restEndpoint.getHeaders(GET_ID, uriInfo, null);
    } else {
        response = restEndpoint.getHeaders(null, GET_ID, uriInfo, null);
    }
    return response;
}
Also used : QueryRequest(ddf.catalog.operation.QueryRequest) FederationStrategy(ddf.catalog.federation.FederationStrategy) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) Resource(ddf.catalog.resource.Resource) TikaMimeTypeResolver(ddf.mime.tika.TikaMimeTypeResolver) MetacardImpl(ddf.catalog.data.impl.MetacardImpl) Result(ddf.catalog.data.Result) QueryResponse(ddf.catalog.operation.QueryResponse) Response(javax.ws.rs.core.Response) SourceInfoResponse(ddf.catalog.operation.SourceInfoResponse) Metacard(ddf.catalog.data.Metacard) ByteArrayInputStream(java.io.ByteArrayInputStream) GeotoolsFilterBuilder(ddf.catalog.filter.proxy.builder.GeotoolsFilterBuilder) FilterBuilder(ddf.catalog.filter.FilterBuilder) QueryResponse(ddf.catalog.operation.QueryResponse) GeotoolsFilterBuilder(ddf.catalog.filter.proxy.builder.GeotoolsFilterBuilder) CatalogFramework(ddf.catalog.CatalogFramework) Map(java.util.Map) MultivaluedMap(javax.ws.rs.core.MultivaluedMap) UriInfo(javax.ws.rs.core.UriInfo)

Aggregations

TikaMimeTypeResolver (ddf.mime.tika.TikaMimeTypeResolver)4 FilterBuilder (ddf.catalog.filter.FilterBuilder)3 GeotoolsFilterBuilder (ddf.catalog.filter.proxy.builder.GeotoolsFilterBuilder)3 QueryResponse (ddf.catalog.operation.QueryResponse)3 SourceInfoResponse (ddf.catalog.operation.SourceInfoResponse)3 ArrayList (java.util.ArrayList)3 Response (javax.ws.rs.core.Response)3 UriInfo (javax.ws.rs.core.UriInfo)3 CatalogFramework (ddf.catalog.CatalogFramework)2 Metacard (ddf.catalog.data.Metacard)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 InputStream (java.io.InputStream)2 Map (java.util.Map)2 MultivaluedMap (javax.ws.rs.core.MultivaluedMap)2 BinaryContent (ddf.catalog.data.BinaryContent)1 Result (ddf.catalog.data.Result)1 MetacardImpl (ddf.catalog.data.impl.MetacardImpl)1 FederationStrategy (ddf.catalog.federation.FederationStrategy)1 QueryRequest (ddf.catalog.operation.QueryRequest)1 Resource (ddf.catalog.resource.Resource)1