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);
}
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);
}
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;
}
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;
}
Aggregations