Search in sources :

Example 1 with QueryResponseTransformer

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;
}
Also used : Serializable(java.io.Serializable) SourceResponse(ddf.catalog.operation.SourceResponse) QueryResponseTransformer(ddf.catalog.transform.QueryResponseTransformer) HashMap(java.util.HashMap) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) MimeType(javax.activation.MimeType)

Example 2 with QueryResponseTransformer

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>());
}
Also used : Serializable(java.io.Serializable) SourceResponse(ddf.catalog.operation.SourceResponse) QueryResponseTransformer(ddf.catalog.transform.QueryResponseTransformer) SourceResponseImpl(ddf.catalog.operation.impl.SourceResponseImpl) CatalogFramework(ddf.catalog.CatalogFramework) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) Matchers.anyString(org.mockito.Matchers.anyString) Map(java.util.Map) Matchers.anyMap(org.mockito.Matchers.anyMap) HashMap(java.util.HashMap) BundleContext(org.osgi.framework.BundleContext) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test)

Example 3 with QueryResponseTransformer

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);
}
Also used : Serializable(java.io.Serializable) SourceResponse(ddf.catalog.operation.SourceResponse) SourceResponseImpl(ddf.catalog.operation.impl.SourceResponseImpl) BinaryContentImpl(ddf.catalog.data.impl.BinaryContentImpl) Matchers.anyString(org.mockito.Matchers.anyString) BinaryContent(ddf.catalog.data.BinaryContent) ServiceReference(org.osgi.framework.ServiceReference) QueryResponseTransformer(ddf.catalog.transform.QueryResponseTransformer) CatalogFramework(ddf.catalog.CatalogFramework) Map(java.util.Map) Matchers.anyMap(org.mockito.Matchers.anyMap) HashMap(java.util.HashMap) BundleContext(org.osgi.framework.BundleContext) Test(org.junit.Test)

Example 4 with QueryResponseTransformer

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."));
    }
}
Also used : Serializable(java.io.Serializable) WebApplicationException(javax.ws.rs.WebApplicationException) QueryResponseTransformer(ddf.catalog.transform.QueryResponseTransformer) HashMap(java.util.HashMap) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Resource(ddf.catalog.resource.Resource) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) BinaryContent(ddf.catalog.data.BinaryContent)

Example 5 with QueryResponseTransformer

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;
}
Also used : Serializable(java.io.Serializable) HashMap(java.util.HashMap) BinaryContent(ddf.catalog.data.BinaryContent) Result(ddf.catalog.data.Result) QueryImpl(ddf.catalog.operation.impl.QueryImpl) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) CatalogFacade(org.codice.ddf.commands.catalog.facade.CatalogFacade) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) ArrayList(java.util.ArrayList) List(java.util.List) SourceResponse(ddf.catalog.operation.SourceResponse) RejectedExecutionHandler(java.util.concurrent.RejectedExecutionHandler) Period(org.joda.time.Period) AtomicLong(java.util.concurrent.atomic.AtomicLong) Metacard(ddf.catalog.data.Metacard) QueryResponseTransformer(ddf.catalog.transform.QueryResponseTransformer) QueryRequestImpl(ddf.catalog.operation.impl.QueryRequestImpl) ExecutorService(java.util.concurrent.ExecutorService) AtomicLong(java.util.concurrent.atomic.AtomicLong) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) File(java.io.File)

Aggregations

QueryResponseTransformer (ddf.catalog.transform.QueryResponseTransformer)5 Serializable (java.io.Serializable)5 HashMap (java.util.HashMap)5 SourceResponse (ddf.catalog.operation.SourceResponse)4 BinaryContent (ddf.catalog.data.BinaryContent)3 CatalogTransformerException (ddf.catalog.transform.CatalogTransformerException)3 CatalogFramework (ddf.catalog.CatalogFramework)2 SourceResponseImpl (ddf.catalog.operation.impl.SourceResponseImpl)2 Map (java.util.Map)2 Test (org.junit.Test)2 Matchers.anyMap (org.mockito.Matchers.anyMap)2 Matchers.anyString (org.mockito.Matchers.anyString)2 BundleContext (org.osgi.framework.BundleContext)2 ServiceReference (org.osgi.framework.ServiceReference)2 Metacard (ddf.catalog.data.Metacard)1 Result (ddf.catalog.data.Result)1 BinaryContentImpl (ddf.catalog.data.impl.BinaryContentImpl)1 QueryImpl (ddf.catalog.operation.impl.QueryImpl)1 QueryRequestImpl (ddf.catalog.operation.impl.QueryRequestImpl)1 Resource (ddf.catalog.resource.Resource)1