Search in sources :

Example 81 with MimeType

use of javax.activation.MimeType in project ddf by codice.

the class QueryResponseTransformerProducer method transform.

@Override
protected Object transform(Message in, 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<>();
        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 82 with MimeType

use of javax.activation.MimeType in project ddf by codice.

the class CatalogComponentTest method testTransformMetacard.

@Test
public void testTransformMetacard() throws Exception {
    LOGGER.debug("Running testTransformMetacard()");
    MockEndpoint mock = getMockEndpoint("mock:result");
    mock.expectedMinimumMessageCount(1);
    // Mock a XML InputTransformer and register it in the OSGi Registry
    // (PojoSR)
    InputTransformer mockTransformer = getMockInputTransformer();
    Hashtable<String, String> props = new Hashtable<String, String>();
    props.put(MimeTypeToTransformerMapper.ID_KEY, "xml");
    props.put(MimeTypeToTransformerMapper.MIME_TYPE_KEY, "text/xml");
    bundleContext.registerService(InputTransformer.class.getName(), mockTransformer, props);
    // Mock the MimeTypeToTransformerMapper and register it in the OSGi
    // Registry (PojoSR)
    MimeTypeToTransformerMapper matchingService = mock(MimeTypeToTransformerMapper.class);
    // HUGH bundleContext.registerService(
    // MimeTypeToTransformerMapper.class.getName(), matchingService, null );
    catalogComponent.setMimeTypeToTransformerMapper(matchingService);
    // Mock the MimeTypeToTransformerMapper returning the mock XML
    // InputTransformer
    when(matchingService.findMatches(eq(InputTransformer.class), isA(MimeType.class))).thenReturn((List) Arrays.asList(mockTransformer));
    // Send in sample XML as InputStream to InputTransformer
    InputStream input = IOUtils.toInputStream(xmlInput);
    // Get the InputTransformer registered with the ID associated with the
    // <from> node in the Camel route
    InputTransformer transformer = getTransformer("text/xml", "identity");
    assertNotNull("InputTransformer for mimeType=text/xml&id=identity not found", transformer);
    // Transform the XML input into a Metacard
    Metacard metacard = transformer.transform(input);
    assertNotNull(metacard);
    assertMockEndpointsSatisfied();
}
Also used : MimeTypeToTransformerMapper(ddf.mime.MimeTypeToTransformerMapper) Metacard(ddf.catalog.data.Metacard) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) Hashtable(java.util.Hashtable) InputStream(java.io.InputStream) InputTransformer(ddf.catalog.transform.InputTransformer) MimeType(javax.activation.MimeType) Test(org.junit.Test)

Example 83 with MimeType

use of javax.activation.MimeType in project ddf by codice.

the class ReliableResourceDownloadManagerTest method getMockResourceRetrieverWithRetryCapability.

private ResourceRetriever getMockResourceRetrieverWithRetryCapability(final RetryType retryType, final boolean readSlow) throws Exception {
    // Mocking to support re-retrieval of product when error encountered
    // during caching.
    ResourceRetriever retriever = mock(ResourceRetriever.class);
    when(retriever.retrieveResource()).thenAnswer(new Answer<Object>() {

        int invocationCount = 0;

        public Object answer(InvocationOnMock invocation) throws ResourceNotFoundException {
            // Create new InputStream for retrieving the same product. This
            // simulates re-retrieving the product from the remote source.
            invocationCount++;
            if (readSlow) {
                mis = new MockInputStream(productInputFilename, true);
                mis.setReadDelay(MONITOR_PERIOD - 2, TimeUnit.MILLISECONDS);
            } else {
                mis = new MockInputStream(productInputFilename);
            }
            if (retryType == RetryType.INPUT_STREAM_IO_EXCEPTION) {
                if (invocationCount == 1) {
                    mis.setInvocationCountToThrowIOException(5);
                } else {
                    mis.setInvocationCountToThrowIOException(-1);
                }
            } else if (retryType == RetryType.TIMEOUT_EXCEPTION) {
                if (invocationCount == 1) {
                    mis.setInvocationCountToTimeout(3);
                    mis.setReadDelay(MONITOR_PERIOD * 2, TimeUnit.SECONDS);
                } else {
                    mis.setInvocationCountToTimeout(-1);
                    mis.setReadDelay(0, TimeUnit.SECONDS);
                }
            } else if (retryType == RetryType.NETWORK_CONNECTION_UP_AND_DOWN) {
                mis.setInvocationCountToThrowIOException(2);
            } else if (retryType == RetryType.NETWORK_CONNECTION_DROPPED) {
                if (invocationCount == 1) {
                    mis.setInvocationCountToThrowIOException(2);
                } else {
                    throw new ResourceNotFoundException();
                }
            }
            // Reset the mock Resource so that it can be reconfigured to return
            // the new InputStream
            reset(resource);
            when(resource.getInputStream()).thenReturn(mis);
            when(resource.getName()).thenReturn("test-resource");
            try {
                when(resource.getMimeType()).thenReturn(new MimeType("text/plain"));
            } catch (MimeTypeParseException e) {
            }
            // Reset the mock ResourceResponse so that it can be reconfigured to return
            // the new Resource
            reset(resourceResponse);
            when(resourceResponse.getRequest()).thenReturn(resourceRequest);
            when(resourceResponse.getResource()).thenReturn(resource);
            when(resourceResponse.getProperties()).thenReturn(new HashMap<String, Serializable>());
            return resourceResponse;
        }
    });
    ArgumentCaptor<Long> bytesReadArg = ArgumentCaptor.forClass(Long.class);
    // Mocking to support re-retrieval of product when error encountered
    // during caching. This resource retriever supports skipping.
    when(retriever.retrieveResource(anyLong())).thenAnswer(new Answer<Object>() {

        int invocationCount = 0;

        public Object answer(InvocationOnMock invocation) throws ResourceNotFoundException, IOException {
            // Create new InputStream for retrieving the same product. This
            // simulates re-retrieving the product from the remote source.
            invocationCount++;
            if (readSlow) {
                mis = new MockInputStream(productInputFilename, true);
                mis.setReadDelay(MONITOR_PERIOD - 2, TimeUnit.MILLISECONDS);
            } else {
                mis = new MockInputStream(productInputFilename);
            }
            // Skip the number of bytes that have already been read
            Object[] args = invocation.getArguments();
            long bytesToSkip = (Long) args[0];
            mis.skip(bytesToSkip);
            if (retryType == RetryType.INPUT_STREAM_IO_EXCEPTION) {
                if (invocationCount == 1) {
                    mis.setInvocationCountToThrowIOException(5);
                } else {
                    mis.setInvocationCountToThrowIOException(-1);
                }
            } else if (retryType == RetryType.TIMEOUT_EXCEPTION) {
                if (invocationCount == 1) {
                    mis.setInvocationCountToTimeout(3);
                    mis.setReadDelay(MONITOR_PERIOD * 2, TimeUnit.SECONDS);
                } else {
                    mis.setInvocationCountToTimeout(-1);
                    mis.setReadDelay(0, TimeUnit.MILLISECONDS);
                }
            } else if (retryType == RetryType.NETWORK_CONNECTION_UP_AND_DOWN) {
                mis.setInvocationCountToThrowIOException(2);
            } else if (retryType == RetryType.NETWORK_CONNECTION_DROPPED) {
                if (invocationCount == 1) {
                    mis.setInvocationCountToThrowIOException(2);
                } else {
                    throw new ResourceNotFoundException();
                }
            }
            // Reset the mock Resource so that it can be reconfigured to return
            // the new InputStream
            reset(resource);
            when(resource.getInputStream()).thenReturn(mis);
            when(resource.getName()).thenReturn("test-resource");
            try {
                when(resource.getMimeType()).thenReturn(new MimeType("text/plain"));
            } catch (MimeTypeParseException e) {
            }
            // Reset the mock ResourceResponse so that it can be reconfigured to return
            // the new Resource
            reset(resourceResponse);
            when(resourceResponse.getRequest()).thenReturn(resourceRequest);
            when(resourceResponse.getResource()).thenReturn(resource);
            Map<String, Serializable> responseProperties = new HashMap<>();
            responseProperties.put("BytesSkipped", true);
            when(resourceResponse.getProperties()).thenReturn(responseProperties);
            when(resourceResponse.containsPropertyName("BytesSkipped")).thenReturn(true);
            when(resourceResponse.getPropertyValue("BytesSkipped")).thenReturn(true);
            return resourceResponse;
        }
    });
    return retriever;
}
Also used : MockInputStream(ddf.catalog.cache.MockInputStream) MimeTypeParseException(javax.activation.MimeTypeParseException) HashMap(java.util.HashMap) ResourceRetriever(ddf.catalog.resourceretriever.ResourceRetriever) IOException(java.io.IOException) MimeType(javax.activation.MimeType) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ArgumentMatchers.anyLong(org.mockito.ArgumentMatchers.anyLong) ResourceNotFoundException(ddf.catalog.resource.ResourceNotFoundException) Map(java.util.Map) HashMap(java.util.HashMap)

Example 84 with MimeType

use of javax.activation.MimeType in project ddf by codice.

the class AbstractCatalogService method createMetacard.

private BinaryContent createMetacard(InputStream stream, String contentType, String transformerParam) throws CatalogServiceException {
    String transformer = DEFAULT_METACARD_TRANSFORMER;
    if (transformerParam != null) {
        transformer = transformerParam;
    }
    MimeType mimeType = null;
    if (contentType != null) {
        try {
            mimeType = new MimeType(contentType);
        } catch (MimeTypeParseException e) {
            LOGGER.debug("Unable to create MimeType from raw data {}", contentType);
        }
    } else {
        LOGGER.debug("No content type specified in request");
    }
    try {
        Metacard metacard = generateMetacard(mimeType, null, stream, null);
        String metacardId = metacard.getId();
        LOGGER.debug("Metacard {} created", metacardId);
        LOGGER.debug("Transforming metacard {} to {} to be able to return it to client", metacardId, LogSanitizer.sanitize(transformer));
        final BinaryContent content = catalogFramework.transform(metacard, transformer, null);
        LOGGER.debug("Metacard to {} transform complete for {}, preparing response.", LogSanitizer.sanitize(transformer), metacardId);
        LOGGER.trace("EXITING: createMetacard");
        return content;
    } catch (MetacardCreationException | CatalogTransformerException e) {
        throw new CatalogServiceException("Unable to create metacard");
    } finally {
        try {
            if (stream != null) {
                stream.close();
            }
        } catch (IOException e) {
            LOGGER.debug("Unexpected error closing stream", e);
        }
    }
}
Also used : MimeTypeParseException(javax.activation.MimeTypeParseException) Metacard(ddf.catalog.data.Metacard) CatalogServiceException(org.codice.ddf.rest.api.CatalogServiceException) MetacardCreationException(ddf.catalog.data.MetacardCreationException) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) IOException(java.io.IOException) BinaryContent(ddf.catalog.data.BinaryContent) MimeType(javax.activation.MimeType)

Example 85 with MimeType

use of javax.activation.MimeType in project ddf by codice.

the class AbstractCatalogService method parseMetacard.

private Metacard parseMetacard(String transformerParam, Metacard metacard, Part part, InputStream inputStream) {
    String transformer = "xml";
    if (transformerParam != null) {
        transformer = transformerParam;
    }
    try {
        MimeType mimeType = new MimeType(part.getContentType());
        metacard = generateMetacard(mimeType, null, inputStream, transformer);
    } catch (MimeTypeParseException | MetacardCreationException e) {
        LOGGER.debug("Unable to parse metadata {}", part.getContentType());
    }
    return metacard;
}
Also used : MimeTypeParseException(javax.activation.MimeTypeParseException) MetacardCreationException(ddf.catalog.data.MetacardCreationException) MimeType(javax.activation.MimeType)

Aggregations

MimeType (javax.activation.MimeType)146 Test (org.junit.Test)74 Metacard (ddf.catalog.data.Metacard)36 MimeTypeParseException (javax.activation.MimeTypeParseException)34 URI (java.net.URI)28 MimeTypeToTransformerMapper (ddf.mime.MimeTypeToTransformerMapper)25 HashMap (java.util.HashMap)25 BundleContext (org.osgi.framework.BundleContext)25 ServiceReference (org.osgi.framework.ServiceReference)25 CatalogFramework (ddf.catalog.CatalogFramework)20 Serializable (java.io.Serializable)16 ResourceResponse (ddf.catalog.operation.ResourceResponse)15 IOException (java.io.IOException)14 BinaryContentImpl (ddf.catalog.data.impl.BinaryContentImpl)11 InputStream (java.io.InputStream)10 StringWriter (java.io.StringWriter)10 MetacardCreationException (ddf.catalog.data.MetacardCreationException)9 Resource (ddf.catalog.resource.Resource)9 CatalogTransformerException (ddf.catalog.transform.CatalogTransformerException)9 File (java.io.File)9