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;
}
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();
}
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;
}
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);
}
}
}
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;
}
Aggregations