use of ddf.catalog.transform.QueryResponseTransformer in project ddf by codice.
the class QueryResponseTransformerProducer method transform.
protected Object transform(Message in, Object obj, String mimeType, String transformerId, MimeTypeToTransformerMapper mapper) throws MimeTypeParseException, CatalogTransformerException {
// Look up the QueryResponseTransformer for the request's mime type.
// If a transformer is found, then transform the request's payload into a BinaryContent
// Otherwise, throw an exception.
MimeType derivedMimeType = new MimeType(mimeType);
if (transformerId != null) {
derivedMimeType = new MimeType(mimeType + ";" + MimeTypeToTransformerMapper.ID_KEY + "=" + transformerId);
}
List<QueryResponseTransformer> matches = mapper.findMatches(QueryResponseTransformer.class, derivedMimeType);
Object binaryContent = null;
if (matches != null && matches.size() == 1) {
Map<String, Serializable> arguments = new HashMap<String, Serializable>();
for (Entry<String, Object> entry : in.getHeaders().entrySet()) {
if (entry.getValue() instanceof Serializable) {
arguments.put(entry.getKey(), (Serializable) entry.getValue());
}
}
LOGGER.debug("Found a matching QueryResponseTransformer for [{}]", transformerId);
QueryResponseTransformer transformer = matches.get(0);
SourceResponse srcResp = in.getBody(SourceResponse.class);
if (null != srcResp) {
binaryContent = transformer.transform(srcResp, arguments);
}
} else {
LOGGER.debug("Did not find an QueryResponseTransformer for [{}]", transformerId);
throw new CatalogTransformerException("Did not find an QueryResponseTransformer for [" + transformerId + "]");
}
return binaryContent;
}
use of ddf.catalog.transform.QueryResponseTransformer in project ddf by codice.
the class CatalogFrameworkImplTest method testQueryTransformWithTransformException.
@Test(expected = CatalogTransformerException.class)
public void testQueryTransformWithTransformException() throws Exception {
BundleContext context = mock(BundleContext.class);
QueryResponseTransformer transformer = mock(QueryResponseTransformer.class);
ServiceReference reference = mock(ServiceReference.class);
ServiceReference[] serviceReferences = new ServiceReference[] { reference };
when(context.getServiceReferences(anyString(), anyString())).thenReturn(serviceReferences);
when(context.getService(isA(ServiceReference.class))).thenReturn(transformer);
when(transformer.transform(isA(SourceResponse.class), isA(Map.class))).thenThrow(new CatalogTransformerException("Could not transform"));
CatalogFramework framework = this.createDummyCatalogFramework(provider, storageProvider, context, eventAdmin, true);
SourceResponse response = new SourceResponseImpl(null, null);
framework.transform(response, "NONE", new HashMap<String, Serializable>());
}
use of ddf.catalog.transform.QueryResponseTransformer in project ddf by codice.
the class CatalogFrameworkImplTest method testQueryTransform.
@Test
public void testQueryTransform() throws Exception {
BundleContext context = mock(BundleContext.class);
QueryResponseTransformer transformer = mock(QueryResponseTransformer.class);
ServiceReference reference = mock(ServiceReference.class);
ServiceReference[] serviceReferences = new ServiceReference[] { reference };
when(context.getServiceReferences(anyString(), anyString())).thenReturn(serviceReferences);
when(context.getService(isA(ServiceReference.class))).thenReturn(transformer);
when(transformer.transform(isA(SourceResponse.class), isA(Map.class))).thenReturn(new BinaryContentImpl(null));
CatalogFramework framework = this.createDummyCatalogFramework(provider, storageProvider, context, eventAdmin, true);
SourceResponse response = new SourceResponseImpl(null, null);
BinaryContent content = framework.transform(response, "NONE", new HashMap<String, Serializable>());
assertNotNull(content);
}
use of ddf.catalog.transform.QueryResponseTransformer in project ddf by codice.
the class CswRecordCollectionMessageBodyWriter method writeTo.
@Override
public void writeTo(CswRecordCollection recordCollection, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream outStream) throws IOException, WebApplicationException {
final String mimeType = recordCollection.getMimeType();
LOGGER.debug("Attempting to transform RecordCollection with mime-type: {} & outputSchema: {}", mimeType, recordCollection.getOutputSchema());
QueryResponseTransformer transformer;
Map<String, Serializable> arguments = new HashMap<String, Serializable>();
if (StringUtils.isBlank(recordCollection.getOutputSchema()) && StringUtils.isNotBlank(mimeType) && !XML_MIME_TYPES.contains(mimeType)) {
transformer = transformerManager.getTransformerByMimeType(mimeType);
} else if (OCTET_STREAM_OUTPUT_SCHEMA.equals(recordCollection.getOutputSchema())) {
Resource resource = recordCollection.getResource();
httpHeaders.put(HttpHeaders.CONTENT_TYPE, Arrays.asList(resource.getMimeType()));
httpHeaders.put(HttpHeaders.CONTENT_DISPOSITION, Arrays.asList(String.format("inline; filename=\"%s\"", resource.getName())));
// Custom HTTP header to represent that the product data will be returned in the response.
httpHeaders.put(CswConstants.PRODUCT_RETRIEVAL_HTTP_HEADER, Arrays.asList("true"));
// Accept-ranges header to represent that ranges in bytes are accepted.
httpHeaders.put(CswConstants.ACCEPT_RANGES_HEADER, Arrays.asList(CswConstants.BYTES));
ByteArrayInputStream in = new ByteArrayInputStream(resource.getByteArray());
IOUtils.copy(in, outStream);
return;
} else {
transformer = transformerManager.getTransformerBySchema(CswConstants.CSW_OUTPUT_SCHEMA);
if (recordCollection.getElementName() != null) {
arguments.put(CswConstants.ELEMENT_NAMES, recordCollection.getElementName().toArray());
}
arguments.put(CswConstants.OUTPUT_SCHEMA_PARAMETER, recordCollection.getOutputSchema());
arguments.put(CswConstants.ELEMENT_SET_TYPE, recordCollection.getElementSetType());
arguments.put(CswConstants.IS_BY_ID_QUERY, recordCollection.isById());
arguments.put(CswConstants.GET_RECORDS, recordCollection.getRequest());
arguments.put(CswConstants.RESULT_TYPE_PARAMETER, recordCollection.getResultType());
arguments.put(CswConstants.WRITE_NAMESPACES, false);
}
if (transformer == null) {
throw new WebApplicationException(new CatalogTransformerException("Unable to locate Transformer."));
}
BinaryContent content = null;
try {
content = transformer.transform(recordCollection.getSourceResponse(), arguments);
} catch (CatalogTransformerException e) {
throw new WebApplicationException(e);
}
if (content != null) {
try (InputStream inputStream = content.getInputStream()) {
IOUtils.copy(inputStream, outStream);
}
} else {
throw new WebApplicationException(new CatalogTransformerException("Transformer returned null."));
}
}
use of ddf.catalog.transform.QueryResponseTransformer in project ddf by codice.
the class DumpCommand method executeWithSubject.
@Override
protected Object executeWithSubject() throws Exception {
if (FilenameUtils.getExtension(dirPath).equals("") && !dirPath.endsWith(File.separator)) {
dirPath += File.separator;
}
final File dumpDir = new File(dirPath);
if (!dumpDir.exists()) {
printErrorMessage("Directory [" + dirPath + "] must exist.");
console.println("If the directory does indeed exist, try putting the path in quotes.");
return null;
}
if (!dumpDir.isDirectory()) {
printErrorMessage("Path [" + dirPath + "] must be a directory.");
return null;
}
if (!SERIALIZED_OBJECT_ID.matches(transformerId)) {
transformers = getTransformers();
if (transformers == null) {
console.println(transformerId + " is an invalid metacard transformer.");
return null;
}
}
if (StringUtils.isNotBlank(zipFileName) && new File(dirPath + zipFileName).exists()) {
console.println("Cannot dump Catalog. Zip file " + zipFileName + " already exists.");
return null;
}
SecurityLogger.audit("Called catalog:dump command with path : {}", dirPath);
CatalogFacade catalog = getCatalog();
if (StringUtils.isNotBlank(zipFileName)) {
zipArgs = new HashMap<>();
zipArgs.put(FILE_PATH, dirPath + zipFileName);
}
QueryImpl query = new QueryImpl(getFilter());
query.setRequestsTotalResultsCount(false);
query.setPageSize(pageSize);
Map<String, Serializable> props = new HashMap<>();
// Avoid caching all results while dumping with native query mode
props.put("mode", "native");
final AtomicLong resultCount = new AtomicLong(0);
long start = System.currentTimeMillis();
SourceResponse response = catalog.query(new QueryRequestImpl(query, props));
BlockingQueue<Runnable> blockingQueue = new ArrayBlockingQueue<>(multithreaded);
RejectedExecutionHandler rejectedExecutionHandler = new ThreadPoolExecutor.CallerRunsPolicy();
final ExecutorService executorService = new ThreadPoolExecutor(multithreaded, multithreaded, 0L, TimeUnit.MILLISECONDS, blockingQueue, rejectedExecutionHandler);
while (response.getResults().size() > 0) {
response = catalog.query(new QueryRequestImpl(query, props));
if (StringUtils.isNotBlank(zipFileName)) {
try {
Optional<QueryResponseTransformer> zipCompression = getZipCompression();
if (zipCompression.isPresent()) {
BinaryContent binaryContent = zipCompression.get().transform(response, zipArgs);
if (binaryContent != null) {
IOUtils.closeQuietly(binaryContent.getInputStream());
}
Long resultSize = (long) response.getResults().size();
printStatus(resultCount.addAndGet(resultSize));
}
} catch (InvalidSyntaxException e) {
LOGGER.info("No Zip Transformer found. Unable export metacards to a zip file.");
}
} else if (multithreaded > 1) {
final List<Result> results = new ArrayList<>(response.getResults());
executorService.submit(() -> {
boolean transformationFailed = false;
for (final Result result : results) {
Metacard metacard = result.getMetacard();
try {
exportMetacard(dumpDir, metacard);
} catch (IOException | CatalogTransformerException e) {
transformationFailed = true;
LOGGER.debug("Failed to dump metacard {}", metacard.getId(), e);
executorService.shutdownNow();
}
printStatus(resultCount.incrementAndGet());
}
if (transformationFailed) {
LOGGER.info("One or more metacards failed to transform. Enable debug log for more details.");
}
});
} else {
for (final Result result : response.getResults()) {
Metacard metacard = result.getMetacard();
exportMetacard(dumpDir, metacard);
printStatus(resultCount.incrementAndGet());
}
}
if (response.getResults().size() < pageSize || pageSize == -1) {
break;
}
if (pageSize > 0) {
query.setStartIndex(query.getStartIndex() + pageSize);
}
}
executorService.shutdown();
while (!executorService.isTerminated()) {
try {
TimeUnit.MILLISECONDS.sleep(100);
} catch (InterruptedException e) {
// ignore
}
}
long end = System.currentTimeMillis();
String elapsedTime = timeFormatter.print(new Period(start, end).withMillis(0));
console.printf(" %d file(s) dumped in %s\t%n", resultCount.get(), elapsedTime);
LOGGER.debug("{} file(s) dumped in {}", resultCount.get(), elapsedTime);
console.println();
SecurityLogger.audit("Exported {} files to {}", resultCount.get(), dirPath);
return null;
}
Aggregations