use of ddf.catalog.transform.CatalogTransformerException in project ddf by codice.
the class CswQueryResponseTransformer method multiThreadedMarshal.
/**
* Multi-threaded marshal of metacard assumes that the query size is unbounded to guard against
* resource exhaustion with fixed thread-pool and fixed work-queue. CPU-bound for optimum
* utilization from availableProcessors()+1 thread pool.
*
* @param results - the list of results to marshal
* @param recordSchema - the schema
* @param arguments - additional args
* @return - the marshaled results
* @throws CatalogTransformerException
*/
private String multiThreadedMarshal(List<Result> results, AtomicLong numResults, String recordSchema, final Map<String, Serializable> arguments) throws CatalogTransformerException {
CompletionService<BinaryContent> completionService = new ExecutorCompletionService<>(queryExecutor);
final MetacardTransformer transformer = metacardTransformerManager.getTransformerBySchema(recordSchema);
if (transformer == null) {
throw new CatalogTransformerException("Cannot find transformer for schema: " + recordSchema);
}
Map<Future<BinaryContent>, Result> futures = new HashMap<>(results.size());
for (Result result : results) {
final Metacard mc = result.getMetacard();
// the "current" thread will run submitted task when queueSize exceeded; effectively
// blocking enqueue of more tasks.
futures.put(completionService.submit(() -> {
BinaryContent content = transformer.transform(mc, arguments);
return content;
}), result);
}
InputStream[] contents = new InputStream[results.size()];
while (!futures.isEmpty()) {
try {
Future<BinaryContent> completedFuture = completionService.take();
int index = results.indexOf(futures.get(completedFuture));
try {
contents[index] = completedFuture.get().getInputStream();
} catch (ExecutionException | CancellationException e) {
LOGGER.debug("Error transforming Metacard", e);
numResults.decrementAndGet();
} catch (InterruptedException e) {
numResults.decrementAndGet();
Thread.currentThread().interrupt();
throw new CatalogTransformerException("Metacard transform interrupted", e);
} finally {
futures.remove(completedFuture);
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new CatalogTransformerException("Metacard transform interrupted", e);
}
}
CharArrayWriter accum = new CharArrayWriter(ACCUM_INITIAL_SIZE);
for (InputStream is : contents) {
try {
if (is != null) {
IOUtils.copy(is, accum);
}
} catch (IOException e) {
LOGGER.debug("Error copying Metacard Binary content", e);
}
}
return accum.toString();
}
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
* @return
* @throws IOException
* @throws CatalogTransformerException
*/
private Metacard transformLogic(InputStream input) 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());
try {
extractThumbnail(metacard, fileBackedOutputStream.asByteSource().openStream());
} catch (EncryptedDocumentException e) {
LOGGER.debug("Unable to generate thumbnail", e);
}
return metacard;
}
}
use of ddf.catalog.transform.CatalogTransformerException in project ddf by codice.
the class PropertyJsonMetacardTransformer method convertToJSON.
public static Map<String, Object> convertToJSON(Metacard metacard, List<AttributeType.AttributeFormat> dontInclude) throws CatalogTransformerException {
if (metacard == null) {
throw new CatalogTransformerException("Cannot transform null metacard.");
}
Map<String, Object> rootObject = new HashMap<>();
Map<String, Object> properties = new HashMap<>();
for (AttributeDescriptor ad : metacard.getMetacardType().getAttributeDescriptors()) {
Attribute attribute = metacard.getAttribute(ad.getName());
if (attribute != null) {
Object value = convertAttribute(attribute, ad, dontInclude);
if (value != null) {
properties.put(attribute.getName(), value);
}
}
}
properties.put(METACARD_TYPE_PROPERTY_KEY, metacard.getMetacardType().getName());
if (StringUtils.isNotBlank(metacard.getSourceId())) {
properties.put(SOURCE_ID_PROPERTY, metacard.getSourceId());
}
rootObject.put("properties", properties);
return rootObject;
}
use of ddf.catalog.transform.CatalogTransformerException in project ddf by codice.
the class PptxInputTransformerTest method testBadCopy.
@Test(expected = CatalogTransformerException.class)
public void testBadCopy() throws IOException, CatalogTransformerException {
IOException ioe = new IOException();
try {
PptxInputTransformer t = new PptxInputTransformer(inputTransformer);
InputStream is = mock(InputStream.class);
when(is.read(any())).thenThrow(ioe);
t.transform(is);
} catch (CatalogTransformerException e) {
assertThat(e.getCause(), is(ioe));
throw e;
}
}
use of ddf.catalog.transform.CatalogTransformerException in project ddf by codice.
the class ResourceMetacardTransformer method transform.
@Override
public BinaryContent transform(Metacard metacard, Map<String, Serializable> arguments) throws CatalogTransformerException {
LOGGER.trace("Entering resource ResourceMetacardTransformer.transform");
if (!isValid(metacard)) {
throw new CatalogTransformerException("Could not transform metacard to a resource because the metacard is not valid.");
}
if (StringUtils.isNotEmpty(metacard.getResourceSize())) {
arguments.put(Metacard.RESOURCE_SIZE, metacard.getResourceSize());
}
String id = metacard.getId();
LOGGER.debug("executing resource request with id '{}'", id);
final ResourceRequest resourceRequest = new ResourceRequestById(id, arguments);
ResourceResponse resourceResponse = null;
String sourceName = metacard.getSourceId();
if (StringUtils.isBlank(sourceName)) {
sourceName = catalogFramework.getId();
}
String resourceUriAscii = "";
if (metacard.getResourceURI() != null) {
resourceUriAscii = metacard.getResourceURI().toASCIIString();
}
try {
resourceResponse = catalogFramework.getResource(resourceRequest, sourceName);
} catch (IOException | ResourceNotFoundException | ResourceNotSupportedException e) {
throw new CatalogTransformerException(retrieveResourceFailureMessage(id, sourceName, resourceUriAscii, e.getMessage()), e);
}
if (resourceResponse == null) {
throw new CatalogTransformerException(retrieveResourceFailureMessage(id, sourceName, resourceUriAscii));
}
Resource transformedContent = resourceResponse.getResource();
MimeType mimeType = transformedContent.getMimeType();
if (mimeType == null) {
try {
mimeType = new MimeType(DEFAULT_MIME_TYPE_STR);
// There is no method to set the MIME type, so in order to set it to our default
// one, we need to create a new object.
transformedContent = new ResourceImpl(transformedContent.getInputStream(), mimeType, transformedContent.getName());
} catch (MimeTypeParseException e) {
throw new CatalogTransformerException("Could not create default mime type upon null mimeType, for default mime type '" + DEFAULT_MIME_TYPE_STR + "'.", e);
}
}
LOGGER.debug("Found mime type: '{}' for product of metacard with id: '{}'.\nGetting associated resource from input stream. \n", mimeType, id);
LOGGER.trace("Exiting resource transform for metacard id: '{}'", id);
return transformedContent;
}
Aggregations