Search in sources :

Example 11 with MimeTypeParseException

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);
    }
}
Also used : MimetypesFileTypeMap(javax.activation.MimetypesFileTypeMap) MimeTypeParseException(javax.activation.MimeTypeParseException) File(java.io.File) MimeType(javax.activation.MimeType) Failure(org.junit.runner.notification.Failure) Before(org.junit.Before)

Example 12 with MimeTypeParseException

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
}
Also used : MimeTypeParseException(javax.activation.MimeTypeParseException) PrintWriterProvider(ddf.catalog.transformer.api.PrintWriterProvider) SourceResponse(ddf.catalog.operation.SourceResponse) MetacardMarshaller(ddf.catalog.transformer.api.MetacardMarshaller) PrintWriterProviderImpl(ddf.catalog.transformer.xml.PrintWriterProviderImpl) XmlResponseQueueTransformer(ddf.catalog.transformer.xml.XmlResponseQueueTransformer) BinaryContent(ddf.catalog.data.BinaryContent) MimeType(javax.activation.MimeType) Test(org.junit.Test)

Example 13 with MimeTypeParseException

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();
            }
        }
    }
}
Also used : MimeTypeParseException(javax.activation.MimeTypeParseException) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) File(java.io.File) MimeType(javax.activation.MimeType) ConfigException(org.opensextant.ConfigException) IOException(java.io.IOException) MimeTypeParseException(javax.activation.MimeTypeParseException)

Example 14 with MimeTypeParseException

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;
}
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) Matchers.anyLong(org.mockito.Matchers.anyLong) ResourceNotFoundException(ddf.catalog.resource.ResourceNotFoundException) Map(java.util.Map) HashMap(java.util.HashMap)

Example 15 with MimeTypeParseException

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;
}
Also used : MimeTypeParseException(javax.activation.MimeTypeParseException) ResourceImpl(ddf.catalog.resource.impl.ResourceImpl) ResourceResponse(ddf.catalog.operation.ResourceResponse) ResourceNotSupportedException(ddf.catalog.resource.ResourceNotSupportedException) ResourceRequestById(ddf.catalog.operation.impl.ResourceRequestById) Resource(ddf.catalog.resource.Resource) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) ResourceRequest(ddf.catalog.operation.ResourceRequest) IOException(java.io.IOException) ResourceNotFoundException(ddf.catalog.resource.ResourceNotFoundException) MimeType(javax.activation.MimeType)

Aggregations

MimeTypeParseException (javax.activation.MimeTypeParseException)17 MimeType (javax.activation.MimeType)14 IOException (java.io.IOException)8 File (java.io.File)4 ResourceNotFoundException (ddf.catalog.resource.ResourceNotFoundException)3 MimetypesFileTypeMap (javax.activation.MimetypesFileTypeMap)3 Before (org.junit.Before)3 Failure (org.junit.runner.notification.Failure)3 RestResponse (com.linkedin.r2.message.rest.RestResponse)2 RestLiDecodingException (com.linkedin.restli.client.RestLiDecodingException)2 IndividualResponse (com.linkedin.restli.common.multiplexer.IndividualResponse)2 BinaryContent (ddf.catalog.data.BinaryContent)2 MetacardCreationException (ddf.catalog.data.MetacardCreationException)2 ResourceResponse (ddf.catalog.operation.ResourceResponse)2 ResourceRequestById (ddf.catalog.operation.impl.ResourceRequestById)2 Resource (ddf.catalog.resource.Resource)2 ResourceNotSupportedException (ddf.catalog.resource.ResourceNotSupportedException)2 ResourceImpl (ddf.catalog.resource.impl.ResourceImpl)2 CatalogTransformerException (ddf.catalog.transform.CatalogTransformerException)2 InputStream (java.io.InputStream)2