use of ddf.catalog.transform.CatalogTransformerException in project ddf by codice.
the class TestRestEndpoint method getMockInputTransformer.
protected InputTransformer getMockInputTransformer() {
InputTransformer inputTransformer = mock(InputTransformer.class);
Metacard generatedMetacard = getSimpleMetacard();
try {
when(inputTransformer.transform(isA(InputStream.class))).thenReturn(generatedMetacard);
when(inputTransformer.transform(isA(InputStream.class), isA(String.class))).thenReturn(generatedMetacard);
} catch (IOException e) {
LOGGER.debug("Exception occurred during test", e);
} catch (CatalogTransformerException e) {
LOGGER.debug("Exception occurred during test", e);
}
return inputTransformer;
}
use of ddf.catalog.transform.CatalogTransformerException in project ddf by codice.
the class PptxInputTransformer method transformLogic.
/**
* This is a three step process. First, create a FileBackedOutputStream because we need to
* consume the stream twice. Once for the injected inputTransformer and once for Apache POI.
* Next, extract the metadata with the injected input transformer. And last, use Apache POI
* to create the thumbnail.
*
* @param input
* @param id
* @return
* @throws IOException
* @throws CatalogTransformerException
*/
private Metacard transformLogic(InputStream input, String id) throws IOException, CatalogTransformerException {
try (TemporaryFileBackedOutputStream fileBackedOutputStream = new TemporaryFileBackedOutputStream()) {
try {
int c = IOUtils.copy(input, fileBackedOutputStream);
LOGGER.debug("copied {} bytes from input stream to file backed output stream", c);
} catch (IOException e) {
throw new CatalogTransformerException("Could not copy bytes of content message.", e);
}
Metacard metacard = extractInitialMetadata(fileBackedOutputStream.asByteSource().openStream());
extractThumbnail(metacard, fileBackedOutputStream.asByteSource().openStream());
if (!usePptTitleAsTitle) {
// Allow the metacard title to be set as the filename
metacard.setAttribute(new AttributeImpl(Core.TITLE, (Serializable) null));
}
return metacard;
}
}
use of ddf.catalog.transform.CatalogTransformerException in project ddf by codice.
the class PdfInputTransformer method transformWithExtractors.
private Metacard transformWithExtractors(InputStream input, String id) throws IOException, CatalogTransformerException {
try (TemporaryFileBackedOutputStream fbos = new TemporaryFileBackedOutputStream()) {
try {
IOUtils.copy(input, fbos);
} catch (IOException e) {
throw new CatalogTransformerException("Could not copy bytes of content message.", e);
}
String plainText = null;
try (InputStream isCopy = fbos.asByteSource().openStream()) {
Parser parser = new AutoDetectParser();
ContentHandler contentHandler = new ToTextContentHandler();
TikaMetadataExtractor tikaMetadataExtractor = new TikaMetadataExtractor(parser, contentHandler);
tikaMetadataExtractor.parseMetadata(isCopy, new ParseContext());
plainText = contentHandler.toString();
} catch (CatalogTransformerException e) {
LOGGER.warn("Cannot extract metadata from pdf", e);
}
try (InputStream isCopy = fbos.asByteSource().openStream();
PDDocument pdfDocument = pdDocumentGenerator.apply(isCopy)) {
return transformPdf(id, pdfDocument, plainText);
} catch (InvalidPasswordException e) {
LOGGER.debug("Cannot transform encrypted pdf", e);
return initializeMetacard(id);
}
}
}
use of ddf.catalog.transform.CatalogTransformerException in project ddf by codice.
the class TestOpenSearchSource method getMockInputTransformer.
protected InputTransformer getMockInputTransformer() {
InputTransformer inputTransformer = mock(InputTransformer.class);
Metacard generatedMetacard = getSimpleMetacard();
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();
}
return inputTransformer;
}
use of ddf.catalog.transform.CatalogTransformerException in project ddf by codice.
the class TestCswQueryResponseTransformer method testMarshalAcknowledgementWithFailedTransforms.
@Test
public void testMarshalAcknowledgementWithFailedTransforms() throws WebApplicationException, IOException, JAXBException, CatalogTransformerException {
GetRecordsType query = new GetRecordsType();
query.setResultType(ResultType.RESULTS);
query.setMaxRecords(BigInteger.valueOf(6));
query.setStartPosition(BigInteger.valueOf(0));
SourceResponse sourceResponse = createSourceResponse(query, 6);
Map<String, Serializable> args = new HashMap<>();
args.put(CswConstants.RESULT_TYPE_PARAMETER, ResultType.RESULTS);
args.put(CswConstants.GET_RECORDS, query);
PrintWriter printWriter = getSimplePrintWriter();
MetacardTransformer mockMetacardTransformer = mock(MetacardTransformer.class);
final AtomicLong atomicLong = new AtomicLong(0);
when(mockMetacardTransformer.transform(any(Metacard.class), anyMap())).then(invocationOnMock -> {
if (atomicLong.incrementAndGet() == 2) {
throw new CatalogTransformerException("");
}
Metacard metacard = (Metacard) invocationOnMock.getArguments()[0];
BinaryContentImpl bci = new BinaryContentImpl(IOUtils.toInputStream(metacard.getId() + ","), new MimeType("application/xml"));
return bci;
});
when(mockPrintWriterProvider.build((Class<Metacard>) notNull())).thenReturn(printWriter);
when(mockTransformerManager.getTransformerBySchema(anyString())).thenReturn(mockMetacardTransformer);
CswQueryResponseTransformer cswQueryResponseTransformer = new CswQueryResponseTransformer(mockTransformerManager, mockPrintWriterProvider);
cswQueryResponseTransformer.init();
BinaryContent content = cswQueryResponseTransformer.transform(sourceResponse, args);
cswQueryResponseTransformer.destroy();
String xml = new String(content.getByteArray());
assertThat(xml, containsString(CswQueryResponseTransformer.NUMBER_OF_RECORDS_MATCHED_ATTRIBUTE + " 6"));
assertThat(xml, containsString(CswQueryResponseTransformer.NUMBER_OF_RECORDS_RETURNED_ATTRIBUTE + " 5"));
assertThat(xml, containsString(CswQueryResponseTransformer.NEXT_RECORD_ATTRIBUTE + " 0"));
}
Aggregations