use of ddf.catalog.transform.InputTransformer in project ddf by codice.
the class TestOpenSearchSource method testQueryBySearchPhraseContentTypeSetRss.
@Test
public void testQueryBySearchPhraseContentTypeSetRss() throws UnsupportedQueryException, URISyntaxException, IOException {
WebClient client = mock(WebClient.class);
Response clientResponse = mock(Response.class);
when(client.get()).thenReturn(clientResponse);
when(clientResponse.getStatus()).thenReturn(Response.Status.OK.getStatusCode());
when(clientResponse.getEntity()).thenReturn(getSampleRssStream());
SecureCxfClientFactory factory = getMockFactory(client);
OverriddenOpenSearchSource source = new OverriddenOpenSearchSource(FILTER_ADAPTER, encryptionService);
InputTransformer inputTransformer = mock(InputTransformer.class);
MetacardImpl generatedMetacard = new MetacardImpl();
generatedMetacard.setMetadata(getSample());
generatedMetacard.setId(SAMPLE_ID);
generatedMetacard.setContentTypeName("myType");
try {
when(inputTransformer.transform(isA(InputStream.class))).thenReturn(generatedMetacard);
when(inputTransformer.transform(isA(InputStream.class), isA(String.class))).thenReturn(generatedMetacard);
} catch (IOException e) {
fail();
} catch (CatalogTransformerException e) {
fail();
}
source.setInputTransformer(inputTransformer);
source.setEndpointUrl("http://localhost:8181/services/catalog/query");
source.init();
source.setParameters(DEFAULT_PARAMETERS);
source.factory = factory;
Filter filter = filterBuilder.attribute(Metacard.METADATA).like().text(SAMPLE_SEARCH_PHRASE);
SourceResponse response = source.query(new QueryRequestImpl(new QueryImpl(filter)));
Assert.assertEquals(1, response.getHits());
List<Result> results = response.getResults();
Assert.assertTrue(results.size() == 1);
Result result = results.get(0);
Metacard metacard = result.getMetacard();
Assert.assertNotNull(metacard);
Assert.assertEquals("myType", metacard.getContentTypeName());
}
use of ddf.catalog.transform.InputTransformer in project ddf by codice.
the class TestOpenSearchSource method testQueryBySearchPhraseContentTypeSet.
@Test
public void testQueryBySearchPhraseContentTypeSet() throws UnsupportedQueryException, URISyntaxException, IOException {
WebClient client = mock(WebClient.class);
Response clientResponse = mock(Response.class);
when(client.get()).thenReturn(clientResponse);
when(clientResponse.getStatus()).thenReturn(Response.Status.OK.getStatusCode());
when(clientResponse.getEntity()).thenReturn(getSampleAtomStream());
SecureCxfClientFactory factory = getMockFactory(client);
OverriddenOpenSearchSource source = new OverriddenOpenSearchSource(FILTER_ADAPTER, encryptionService);
InputTransformer inputTransformer = mock(InputTransformer.class);
MetacardImpl generatedMetacard = new MetacardImpl();
generatedMetacard.setMetadata(getSample());
generatedMetacard.setId(SAMPLE_ID);
generatedMetacard.setContentTypeName("myType");
try {
when(inputTransformer.transform(isA(InputStream.class))).thenReturn(generatedMetacard);
when(inputTransformer.transform(isA(InputStream.class), isA(String.class))).thenReturn(generatedMetacard);
} catch (IOException e) {
fail();
} catch (CatalogTransformerException e) {
fail();
}
source.setInputTransformer(inputTransformer);
source.setEndpointUrl("http://localhost:8181/services/catalog/query");
source.init();
source.setParameters(DEFAULT_PARAMETERS);
source.factory = factory;
Filter filter = filterBuilder.attribute(Metacard.METADATA).like().text(SAMPLE_SEARCH_PHRASE);
SourceResponse response = source.query(new QueryRequestImpl(new QueryImpl(filter)));
Assert.assertEquals(1, response.getHits());
List<Result> results = response.getResults();
Assert.assertTrue(results.size() == 1);
Result result = results.get(0);
Metacard metacard = result.getMetacard();
Assert.assertNotNull(metacard);
Assert.assertEquals("myType", metacard.getContentTypeName());
}
use of ddf.catalog.transform.InputTransformer in project ddf by codice.
the class CswSubscriptionEndpoint method getMetacards.
private List<Metacard> getMetacards(GetRecordsResponseType recordsResponse) throws CswException {
try {
InputTransformer transformer = inputTransformerManager.getTransformerBySchema(recordsResponse.getSearchResults().getRecordSchema());
List<Metacard> metacards = new ArrayList<>();
for (Object result : recordsResponse.getSearchResults().getAny()) {
if (result instanceof Node) {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
XMLUtils.transform((Node) result, new TransformerProperties(), new StreamResult(outputStream));
InputStream is = new ByteArrayInputStream(outputStream.toByteArray());
metacards.add(transformer.transform(is));
}
}
return metacards;
} catch (IOException | CatalogTransformerException e) {
String msg = "Could not parse SearchResults in getRecordsResponse";
LOGGER.debug(msg, e);
throw new CswException(msg, e);
}
}
use of ddf.catalog.transform.InputTransformer in project ddf by codice.
the class IntegrationTest method testInputAndOutput.
@Test
public void testInputAndOutput() throws CatalogTransformerException, IOException {
Parser parser = new XmlParser();
InputTransformer inputTransformer = new XmlInputTransformer(parser);
MetacardMarshaller metacardMarshaller = new MetacardMarshallerImpl(parser, new PrintWriterProviderImpl());
MetacardTransformer outputTransformer = new XmlMetacardTransformer(metacardMarshaller);
InputStream input = getClass().getResourceAsStream("/extensibleMetacard.xml");
Metacard metacard = inputTransformer.transform(input);
LOGGER.info("Attributes: ");
for (AttributeDescriptor descriptor : metacard.getMetacardType().getAttributeDescriptors()) {
Attribute attribute = metacard.getAttribute(descriptor.getName());
LOGGER.info("\t" + descriptor.getName() + ": " + ((attribute == null) ? attribute : attribute.getValue()));
}
BinaryContent output = outputTransformer.transform(metacard, mockArguments);
String outputString = new String(output.getByteArray());
// TODO test equivalence with XMLUnit.
LOGGER.info(outputString);
}
use of ddf.catalog.transform.InputTransformer in project ddf by codice.
the class RESTEndpoint method generateMetacard.
private Metacard generateMetacard(MimeType mimeType, String id, InputStream message, String transformerId) throws MetacardCreationException {
Metacard generatedMetacard = null;
List<InputTransformer> listOfCandidates = mimeTypeToTransformerMapper.findMatches(InputTransformer.class, mimeType);
List<String> stackTraceList = new ArrayList<>();
LOGGER.trace("Entering generateMetacard.");
LOGGER.debug("List of matches for mimeType [{}]: {}", mimeType, listOfCandidates);
try (TemporaryFileBackedOutputStream fileBackedOutputStream = new TemporaryFileBackedOutputStream()) {
try {
if (null != message) {
IOUtils.copy(message, fileBackedOutputStream);
} else {
throw new MetacardCreationException("Could not copy bytes of content message. Message was NULL.");
}
} catch (IOException e) {
throw new MetacardCreationException("Could not copy bytes of content message.", e);
}
Iterator<InputTransformer> it = listOfCandidates.iterator();
if (StringUtils.isNotEmpty(transformerId)) {
BundleContext bundleContext = getBundleContext();
Collection<ServiceReference<InputTransformer>> serviceReferences = bundleContext.getServiceReferences(InputTransformer.class, "(id=" + transformerId + ")");
it = serviceReferences.stream().map(bundleContext::getService).iterator();
}
while (it.hasNext()) {
InputTransformer transformer = it.next();
try (InputStream inputStreamMessageCopy = fileBackedOutputStream.asByteSource().openStream()) {
generatedMetacard = transformer.transform(inputStreamMessageCopy);
} catch (CatalogTransformerException | IOException e) {
List<String> stackTraces = Arrays.asList(ExceptionUtils.getRootCauseStackTrace(e));
stackTraceList.add(String.format("Transformer [%s] could not create metacard.", transformer));
stackTraceList.addAll(stackTraces);
LOGGER.debug("Transformer [{}] could not create metacard.", transformer, e);
}
if (generatedMetacard != null) {
break;
}
}
if (generatedMetacard == null) {
throw new MetacardCreationException(String.format("Could not create metacard with mimeType %s : %s", mimeType, StringUtils.join(stackTraceList, "\n")));
}
if (id != null) {
generatedMetacard.setAttribute(new AttributeImpl(Metacard.ID, id));
} else {
LOGGER.debug("Metacard had a null id");
}
} catch (IOException e) {
throw new MetacardCreationException("Could not create metacard.", e);
} catch (InvalidSyntaxException e) {
throw new MetacardCreationException("Could not determine transformer", e);
}
return generatedMetacard;
}
Aggregations