Search in sources :

Example 51 with MetacardTransformer

use of ddf.catalog.transform.MetacardTransformer in project ddf by codice.

the class CatalogFrameworkImplTest method testMetacardTransformWithTransformException.

@Test(expected = CatalogTransformerException.class)
public void testMetacardTransformWithTransformException() throws Exception {
    BundleContext context = mock(BundleContext.class);
    MetacardTransformer transformer = mock(MetacardTransformer.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(Metacard.class), isA(Map.class))).thenThrow(new CatalogTransformerException("Could not transform"));
    CatalogFramework framework = this.createDummyCatalogFramework(provider, storageProvider, context, eventAdmin, true);
    MetacardImpl newCard = new MetacardImpl();
    newCard.setId(null);
    framework.transform(newCard, "NONE", new HashMap<String, Serializable>());
}
Also used : Metacard(ddf.catalog.data.Metacard) Serializable(java.io.Serializable) MetacardTransformer(ddf.catalog.transform.MetacardTransformer) CatalogFramework(ddf.catalog.CatalogFramework) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Map(java.util.Map) ArgumentMatchers.anyMap(org.mockito.ArgumentMatchers.anyMap) HashMap(java.util.HashMap) MetacardImpl(ddf.catalog.data.impl.MetacardImpl) BundleContext(org.osgi.framework.BundleContext) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test)

Example 52 with MetacardTransformer

use of ddf.catalog.transform.MetacardTransformer in project ddf by codice.

the class CatalogFrameworkImplTest method testMetacardTransform.

@Test
public void testMetacardTransform() throws Exception {
    BundleContext context = mock(BundleContext.class);
    MetacardTransformer transformer = mock(MetacardTransformer.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(Metacard.class), isA(Map.class))).thenReturn(new BinaryContentImpl(null));
    CatalogFramework framework = this.createDummyCatalogFramework(provider, storageProvider, context, eventAdmin, true);
    MetacardImpl newCard = new MetacardImpl();
    newCard.setId(null);
    BinaryContent content = framework.transform(newCard, "NONE", new HashMap<String, Serializable>());
    assertNotNull(content);
}
Also used : Serializable(java.io.Serializable) MetacardTransformer(ddf.catalog.transform.MetacardTransformer) BinaryContentImpl(ddf.catalog.data.impl.BinaryContentImpl) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) BinaryContent(ddf.catalog.data.BinaryContent) MetacardImpl(ddf.catalog.data.impl.MetacardImpl) ServiceReference(org.osgi.framework.ServiceReference) Metacard(ddf.catalog.data.Metacard) CatalogFramework(ddf.catalog.CatalogFramework) Map(java.util.Map) ArgumentMatchers.anyMap(org.mockito.ArgumentMatchers.anyMap) HashMap(java.util.HashMap) BundleContext(org.osgi.framework.BundleContext) Test(org.junit.Test)

Example 53 with MetacardTransformer

use of ddf.catalog.transform.MetacardTransformer in project ddf by codice.

the class CswTransformProvider method marshal.

/**
 * Marshals Metacards to an xml. This method is not typically be called directly, instead it is
 * called by another XStream Converter using MarshallingContext.convertAnother();
 *
 * @param o - metacard to transform.
 * @param writer - writes the XML.
 * @param context - the marshalling context. Should contain a map entry for {@link
 *     org.codice.ddf.spatial.ogc.csw.catalog.common.CswConstants#TRANSFORMER_LOOKUP_KEY} {@link
 *     org.codice.ddf.spatial.ogc.csw.catalog.common.CswConstants#TRANSFORMER_LOOKUP_VALUE} to
 *     identify which transformer to use. Also contains properties for any arguments to provide
 *     the transformer.
 */
@Override
public void marshal(Object o, HierarchicalStreamWriter writer, MarshallingContext context) {
    if (o == null) {
        return;
    }
    Metacard metacard = (Metacard) o;
    String keyArg = (String) context.get(CswConstants.TRANSFORMER_LOOKUP_KEY);
    String valArg = (String) context.get(CswConstants.TRANSFORMER_LOOKUP_VALUE);
    MetacardTransformer transformer;
    if (StringUtils.isNotBlank(keyArg) && StringUtils.isNotBlank(valArg)) {
        transformer = metacardTransformerManager.getTransformerByProperty(keyArg, valArg);
    } else {
        transformer = metacardTransformerManager.getTransformerBySchema(CswConstants.CSW_OUTPUT_SCHEMA);
    }
    if (transformer == null) {
        throw new ConversionException(String.format("Unable to locate a transformer for %s = %s", keyArg, valArg));
    }
    BinaryContent content;
    try {
        content = transformer.transform(metacard, getArguments(context));
    } catch (CatalogTransformerException e) {
        throw new ConversionException("Unable to transform Metacard", e);
    }
    writeXml(content, writer);
}
Also used : ConversionException(com.thoughtworks.xstream.converters.ConversionException) Metacard(ddf.catalog.data.Metacard) MetacardTransformer(ddf.catalog.transform.MetacardTransformer) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) BinaryContent(ddf.catalog.data.BinaryContent)

Example 54 with MetacardTransformer

use of ddf.catalog.transform.MetacardTransformer 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();
}
Also used : MetacardTransformer(ddf.catalog.transform.MetacardTransformer) HashMap(java.util.HashMap) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ExecutorCompletionService(java.util.concurrent.ExecutorCompletionService) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) IOException(java.io.IOException) BinaryContent(ddf.catalog.data.BinaryContent) CharArrayWriter(java.io.CharArrayWriter) Result(ddf.catalog.data.Result) Metacard(ddf.catalog.data.Metacard) CancellationException(java.util.concurrent.CancellationException) Future(java.util.concurrent.Future) ExecutionException(java.util.concurrent.ExecutionException)

Aggregations

MetacardTransformer (ddf.catalog.transform.MetacardTransformer)54 BinaryContent (ddf.catalog.data.BinaryContent)48 Test (org.junit.Test)40 SourceResponse (ddf.catalog.operation.SourceResponse)38 Metacard (ddf.catalog.data.Metacard)28 Result (ddf.catalog.data.Result)25 ResultImpl (ddf.catalog.data.impl.ResultImpl)20 FileOutputStream (java.io.FileOutputStream)18 File (java.io.File)17 HashMap (java.util.HashMap)15 Map (java.util.Map)10 CatalogTransformerException (ddf.catalog.transform.CatalogTransformerException)7 MetacardImpl (ddf.catalog.data.impl.MetacardImpl)5 Action (ddf.action.Action)4 ActionProvider (ddf.action.ActionProvider)4 BinaryContentImpl (ddf.catalog.data.impl.BinaryContentImpl)4 QueryImpl (ddf.catalog.operation.impl.QueryImpl)4 QueryRequestImpl (ddf.catalog.operation.impl.QueryRequestImpl)4 Serializable (java.io.Serializable)4 URL (java.net.URL)4