use of javax.activation.MimeTypeParseException in project ddf by codice.
the class ResourceImplTest method setUp.
@Before
public void setUp() {
content = new File("src/test/resources/data/i4ce.png");
MimetypesFileTypeMap mimeMapper = new MimetypesFileTypeMap();
try {
mimeType = new MimeType(mimeMapper.getContentType(content));
} catch (MimeTypeParseException e) {
LOGGER.error("Mime parser Failure", e);
new Failure(null, e);
}
}
use of javax.activation.MimeTypeParseException in project ddf by codice.
the class TestXmlResponseQueueTransformer method testMimeTypeInitException.
@Test(expected = ExceptionInInitializerError.class)
public void testMimeTypeInitException() throws IOException, CatalogTransformerException, XmlPullParserException, MimeTypeParseException {
SourceResponse response = givenSourceResponse(new MetacardStub("source1", "id1"));
PrintWriterProvider pwp = new PrintWriterProviderImpl();
MetacardMarshaller mockMetacardMarshaller = mock(MetacardMarshaller.class);
MimeType mockMimeType = mock(MimeType.class);
doThrow(new MimeTypeParseException("")).when(mockMimeType).setSubType(anyString());
XmlResponseQueueTransformer xrqt = new XmlResponseQueueTransformer(parser, FJP, pwp, mockMetacardMarshaller, mockMimeType);
xrqt.setThreshold(2);
BinaryContent bc = xrqt.transform(response, null);
// then exception
}
use of javax.activation.MimeTypeParseException in project Xponents by OpenSextant.
the class XText method convertChildren.
/**
* Save children objects for a given ConvertedDocument to a location....
* convert those items immediately, saving the Parent metadata along with
* them. You should have setParent already
*
* @param parentDoc
* parent conversion
* @throws IOException
* on err
*/
public void convertChildren(ConvertedDocument parentDoc) throws IOException {
if (parentDoc.is_webArchive) {
//
return;
}
parentDoc.evalParentChildContainer();
FileUtility.makeDirectory(parentDoc.parentContainer);
String targetPath = parentDoc.parentContainer.getAbsolutePath();
for (Content child : parentDoc.getRawChildren()) {
if (child.content == null) {
log.error("Attempted to write out child object with no content {}", child.id);
continue;
}
OutputStream io = null;
try {
// We just assume for now Child ID is filename.
// Alternatively, child.meta.getProperty(
// ConvertedDocument.CHILD_ENTRY_KEY )
// same result, just more verbose.
//
File childFile = new File(FilenameUtils.concat(targetPath, child.id));
io = new FileOutputStream(childFile);
IOUtils.write(child.content, io);
ConvertedDocument childConv = convertFile(childFile, parentDoc);
if (childConv != null) {
if (childConv.is_converted) {
// Push down all child metadata down to ConvertedDoc
for (String k : child.meta.stringPropertyNames()) {
String val = child.meta.getProperty(k);
childConv.addUserProperty(k, val);
}
// Save cached version once again.
childConv.saveBuffer(new File(childConv.textpath));
}
if (child.mimeType != null) {
try {
childConv.setMimeType(new MimeType(child.mimeType));
} catch (MimeTypeParseException e) {
log.warn("Invalid mime type encountered: {} ignoring.", child.mimeType);
}
}
parentDoc.addChild(childConv);
}
} catch (Exception err) {
log.error("Failed to write out child {}, but will continue with others", child.id, err);
} finally {
if (io != null) {
io.close();
}
}
}
}
use of javax.activation.MimeTypeParseException 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.MimeTypeParseException in project ddf by codice.
the class ResourceMetacardTransformer method transform.
@Override
public BinaryContent transform(Metacard metacard, Map<String, Serializable> arguments) throws CatalogTransformerException {
LOGGER.trace("Entering resource ResourceMetacardTransformer.transform");
if (!isValid(metacard)) {
throw new CatalogTransformerException("Could not transform metacard to a resource because the metacard is not valid.");
}
if (StringUtils.isNotEmpty(metacard.getResourceSize())) {
arguments.put(Metacard.RESOURCE_SIZE, metacard.getResourceSize());
}
String id = metacard.getId();
LOGGER.debug("executing resource request with id '{}'", id);
final ResourceRequest resourceRequest = new ResourceRequestById(id, arguments);
ResourceResponse resourceResponse = null;
String sourceName = metacard.getSourceId();
if (StringUtils.isBlank(sourceName)) {
sourceName = catalogFramework.getId();
}
String resourceUriAscii = "";
if (metacard.getResourceURI() != null) {
resourceUriAscii = metacard.getResourceURI().toASCIIString();
}
try {
resourceResponse = catalogFramework.getResource(resourceRequest, sourceName);
} catch (IOException e) {
throw new CatalogTransformerException(retrieveResourceFailureMessage(id, sourceName, resourceUriAscii, e.getMessage()), e);
} catch (ResourceNotFoundException e) {
throw new CatalogTransformerException(retrieveResourceFailureMessage(id, sourceName, resourceUriAscii, e.getMessage()), e);
} catch (ResourceNotSupportedException e) {
throw new CatalogTransformerException(retrieveResourceFailureMessage(id, sourceName, resourceUriAscii, e.getMessage()), e);
}
if (resourceResponse == null) {
throw new CatalogTransformerException(retrieveResourceFailureMessage(id, sourceName, resourceUriAscii));
}
Resource transformedContent = resourceResponse.getResource();
MimeType mimeType = transformedContent.getMimeType();
if (mimeType == null) {
try {
mimeType = new MimeType(DEFAULT_MIME_TYPE_STR);
// There is no method to set the MIME type, so in order to set it to our default
// one, we need to create a new object.
transformedContent = new ResourceImpl(transformedContent.getInputStream(), mimeType, transformedContent.getName());
} catch (MimeTypeParseException e) {
throw new CatalogTransformerException("Could not create default mime type upon null mimeType, for default mime type '" + DEFAULT_MIME_TYPE_STR + "'.", e);
}
}
LOGGER.debug("Found mime type: '{}' for product of metacard with id: '{}'.\nGetting associated resource from input stream. \n", mimeType, id);
LOGGER.trace("Exiting resource transform for metacard id: '{}'", id);
return transformedContent;
}
Aggregations