Search in sources :

Example 1 with MimeTypeResolutionException

use of ddf.mime.MimeTypeResolutionException in project ddf by codice.

the class URLResourceReader method retrieveHttpProduct.

private ResourceResponse retrieveHttpProduct(URI resourceURI, String productName, String bytesToSkip, Map<String, Serializable> properties) throws ResourceNotFoundException {
    try {
        LOGGER.debug("Opening connection to: {}", resourceURI);
        WebClient client = getWebClient(resourceURI, properties);
        Response response = client.get();
        MultivaluedMap<String, Object> headers = response.getHeaders();
        List<Object> cdHeaders = headers.get(HttpHeaders.CONTENT_DISPOSITION);
        if (cdHeaders != null && !cdHeaders.isEmpty()) {
            String contentHeader = (String) cdHeaders.get(0);
            productName = StringUtils.defaultIfBlank(handleContentDispositionHeader(contentHeader), productName);
        }
        String mimeType = getMimeType(resourceURI, productName);
        Response clientResponse = client.get();
        InputStream is;
        Object entityObj = clientResponse.getEntity();
        if (entityObj instanceof InputStream) {
            is = (InputStream) entityObj;
            if (Response.Status.OK.getStatusCode() != clientResponse.getStatus() && Response.Status.PARTIAL_CONTENT.getStatusCode() != clientResponse.getStatus()) {
                String error = getResponseErrorMessage(is);
                String errorMsg = "Received error code while retrieving resource (status " + clientResponse.getStatus() + "): " + error;
                throw new ResourceNotFoundException(errorMsg);
            }
        } else {
            throw new ResourceNotFoundException("Received null response while retrieving resource.");
        }
        long responseBytesSkipped = 0L;
        if (headers.getFirst(HttpHeaders.CONTENT_RANGE) != null) {
            String contentRangeHeader = String.valueOf(headers.getFirst(HttpHeaders.CONTENT_RANGE));
            responseBytesSkipped = Long.parseLong(StringUtils.substringBetween(contentRangeHeader.toLowerCase(), "bytes ", "-"));
        }
        alignStream(is, Long.parseLong(bytesToSkip), responseBytesSkipped);
        return new ResourceResponseImpl(new ResourceImpl(new BufferedInputStream(is), mimeType, FilenameUtils.getName(productName)));
    } catch (MimeTypeResolutionException | IOException | WebApplicationException e) {
        LOGGER.info("Error retrieving resource", e);
        throw new ResourceNotFoundException("Unable to retrieve resource at: " + resourceURI.toString(), e);
    }
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) BufferedInputStream(java.io.BufferedInputStream) InputStream(java.io.InputStream) ResourceResponseImpl(ddf.catalog.operation.impl.ResourceResponseImpl) IOException(java.io.IOException) WebClient(org.apache.cxf.jaxrs.client.WebClient) ResourceResponse(ddf.catalog.operation.ResourceResponse) Response(javax.ws.rs.core.Response) MimeTypeResolutionException(ddf.mime.MimeTypeResolutionException) BufferedInputStream(java.io.BufferedInputStream) ResourceNotFoundException(ddf.catalog.resource.ResourceNotFoundException)

Example 2 with MimeTypeResolutionException

use of ddf.mime.MimeTypeResolutionException in project ddf by codice.

the class OperationsMetacardSupport method guessMimeType.

// package-private for unit testing
String guessMimeType(String mimeTypeRaw, String fileName, Path tmpContentPath) throws IOException {
    if (ContentItem.DEFAULT_MIME_TYPE.equals(mimeTypeRaw)) {
        try (InputStream inputStreamMessageCopy = com.google.common.io.Files.asByteSource(tmpContentPath.toFile()).openStream()) {
            String mimeTypeGuess = frameworkProperties.getMimeTypeMapper().guessMimeType(inputStreamMessageCopy, FilenameUtils.getExtension(fileName));
            if (StringUtils.isNotEmpty(mimeTypeGuess)) {
                mimeTypeRaw = mimeTypeGuess;
            }
        } catch (MimeTypeResolutionException e) {
            LOGGER.debug(MIME_TYPE_MSG, e);
        }
        if (ContentItem.DEFAULT_MIME_TYPE.equals(mimeTypeRaw)) {
            Detector detector = new DefaultProbDetector();
            try (InputStream inputStreamMessageCopy = TikaInputStream.get(tmpContentPath)) {
                MediaType mediaType = detector.detect(inputStreamMessageCopy, new Metadata());
                mimeTypeRaw = mediaType.toString();
            } catch (IOException e) {
                LOGGER.debug(MIME_TYPE_MSG, e);
            }
        }
        if (mimeTypeRaw.equals("text/plain")) {
            try (InputStream inputStreamMessageCopy = com.google.common.io.Files.asByteSource(tmpContentPath.toFile()).openStream();
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStreamMessageCopy, Charset.forName("UTF-8")))) {
                String line = bufferedReader.lines().map(String::trim).filter(StringUtils::isNotEmpty).findFirst().orElse("");
                if (line.startsWith("<")) {
                    mimeTypeRaw = "text/xml";
                } else if (line.startsWith("{") || line.startsWith("[")) {
                    mimeTypeRaw = "application/json";
                }
            } catch (IOException e) {
                LOGGER.debug(MIME_TYPE_MSG, e);
            }
        }
    }
    return mimeTypeRaw;
}
Also used : MimeTypeResolutionException(ddf.mime.MimeTypeResolutionException) DefaultProbDetector(org.apache.tika.detect.DefaultProbDetector) Detector(org.apache.tika.detect.Detector) InputStreamReader(java.io.InputStreamReader) TikaInputStream(org.apache.tika.io.TikaInputStream) InputStream(java.io.InputStream) StringUtils(org.apache.commons.lang.StringUtils) Metadata(org.apache.tika.metadata.Metadata) BufferedReader(java.io.BufferedReader) MediaType(org.apache.tika.mime.MediaType) DefaultProbDetector(org.apache.tika.detect.DefaultProbDetector) IOException(java.io.IOException)

Example 3 with MimeTypeResolutionException

use of ddf.mime.MimeTypeResolutionException in project ddf by codice.

the class MimeTypeMapperImpl method getFileExtensionForMimeType.

@Override
public String getFileExtensionForMimeType(String mimeType) throws MimeTypeResolutionException {
    LOGGER.trace(ENTERING_STR, "getFileExtensionForMimeType()");
    String extension = null;
    LOGGER.debug("Looping through {} MimeTypeResolvers", mimeTypeResolvers.size());
    // Sort the mime type resolvers in descending order of priority. This should
    // insure custom mime type resolvers are called before the (default) Apache Tika
    // mime type resolver.
    List<MimeTypeResolver> sortedResolvers = sortResolvers(mimeTypeResolvers);
    // Once a file extension is find for the given mime type, exit the loop.
    for (MimeTypeResolver resolver : sortedResolvers) {
        LOGGER.debug(CALLING_RESOLVER_MSG, resolver.getName());
        try {
            extension = resolver.getFileExtensionForMimeType(mimeType);
        } catch (Exception e) {
            LOGGER.debug("Error resolving file extension for mime type: {}", mimeType);
            throw new MimeTypeResolutionException(e);
        }
        if (StringUtils.isNotEmpty(extension)) {
            LOGGER.debug("extension [{}] retrieved from MimeTypeResolver:  {}", extension, resolver.getName());
            break;
        }
    }
    LOGGER.debug(MIME_TYPE_FILE_EXT_MSG, mimeType, extension);
    LOGGER.trace(EXITING_STR, "getFileExtensionForMimeType()");
    return extension;
}
Also used : MimeTypeResolver(ddf.mime.MimeTypeResolver) MimeTypeResolutionException(ddf.mime.MimeTypeResolutionException) IOException(java.io.IOException) MimeTypeResolutionException(ddf.mime.MimeTypeResolutionException)

Example 4 with MimeTypeResolutionException

use of ddf.mime.MimeTypeResolutionException in project ddf by codice.

the class MimeTypeMapperImpl method getMimeTypeForFileExtension.

@Override
public String getMimeTypeForFileExtension(String fileExtension) throws MimeTypeResolutionException {
    LOGGER.trace(ENTERING_STR, "getMimeTypeForFileExtension()");
    String mimeType = null;
    LOGGER.debug("Looping through {} MimeTypeResolvers", mimeTypeResolvers.size());
    // This is to force the TikaMimeTypeResolver to be called
    // after the CustomMimeTypeResolvers to prevent Tika default mapping
    // from being used when a CustomMimeTypeResolver may be more appropriate.
    List<MimeTypeResolver> sortedResolvers = sortResolvers(mimeTypeResolvers);
    // Once a file extension is find for the given mime type, exit the loop.
    for (MimeTypeResolver resolver : sortedResolvers) {
        LOGGER.debug(CALLING_RESOLVER_MSG, resolver.getName());
        try {
            mimeType = resolver.getMimeTypeForFileExtension(fileExtension);
        } catch (Exception e) {
            LOGGER.debug("Error resolving mime type for file extension: {}", fileExtension);
            throw new MimeTypeResolutionException(e);
        }
        if (StringUtils.isNotEmpty(mimeType)) {
            LOGGER.debug("mimeType [{}] retrieved from MimeTypeResolver: {}", mimeType, resolver.getName());
            break;
        }
    }
    LOGGER.debug(MIME_TYPE_FILE_EXT_MSG, mimeType, fileExtension);
    LOGGER.trace(EXITING_STR, "getMimeTypeForFileExtension()");
    return mimeType;
}
Also used : MimeTypeResolver(ddf.mime.MimeTypeResolver) MimeTypeResolutionException(ddf.mime.MimeTypeResolutionException) IOException(java.io.IOException) MimeTypeResolutionException(ddf.mime.MimeTypeResolutionException)

Example 5 with MimeTypeResolutionException

use of ddf.mime.MimeTypeResolutionException in project ddf by codice.

the class RESTEndpoint method parseAttachment.

CreateInfo parseAttachment(Attachment contentPart) {
    CreateInfo createInfo = new CreateInfo();
    InputStream stream = null;
    FileBackedOutputStream fbos = null;
    String filename = null;
    String contentType = null;
    // at the beginning
    try {
        stream = contentPart.getDataHandler().getInputStream();
        if (stream != null && stream.available() == 0) {
            stream.reset();
        }
        createInfo.setStream(stream);
    } catch (IOException e) {
        LOGGER.info("IOException reading stream from file attachment in multipart body", e);
    }
    // Content-Type: application/json;id=geojson
    if (contentPart.getContentType() != null) {
        contentType = contentPart.getContentType().toString();
    }
    if (contentPart.getContentDisposition() != null) {
        filename = contentPart.getContentDisposition().getParameter(FILENAME_CONTENT_DISPOSITION_PARAMETER_NAME);
    }
    // specified content type.
    if (StringUtils.isEmpty(filename)) {
        LOGGER.debug("No filename parameter provided - generating default filename");
        String fileExtension = DEFAULT_FILE_EXTENSION;
        try {
            // DDF-2307
            fileExtension = mimeTypeMapper.getFileExtensionForMimeType(contentType);
            if (StringUtils.isEmpty(fileExtension)) {
                fileExtension = DEFAULT_FILE_EXTENSION;
            }
        } catch (MimeTypeResolutionException e) {
            LOGGER.debug("Exception getting file extension for contentType = {}", contentType);
        }
        // DDF-2263
        filename = DEFAULT_FILE_NAME + "." + fileExtension;
        LOGGER.debug("No filename parameter provided - default to {}", filename);
    } else {
        filename = FilenameUtils.getName(filename);
        // by determining the mime type based on the filename's extension.
        if (StringUtils.isEmpty(contentType) || REFINEABLE_MIME_TYPES.contains(contentType)) {
            String fileExtension = FilenameUtils.getExtension(filename);
            LOGGER.debug("fileExtension = {}, contentType before refinement = {}", fileExtension, contentType);
            try {
                contentType = mimeTypeMapper.getMimeTypeForFileExtension(fileExtension);
            } catch (MimeTypeResolutionException e) {
                LOGGER.debug("Unable to refine contentType {} based on filename extension {}", contentType, fileExtension);
            }
            LOGGER.debug("Refined contentType = {}", contentType);
        }
    }
    createInfo.setContentType(contentType);
    createInfo.setFilename(filename);
    return createInfo;
}
Also used : MimeTypeResolutionException(ddf.mime.MimeTypeResolutionException) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) FileBackedOutputStream(com.google.common.io.FileBackedOutputStream) TemporaryFileBackedOutputStream(org.codice.ddf.platform.util.TemporaryFileBackedOutputStream) IOException(java.io.IOException)

Aggregations

MimeTypeResolutionException (ddf.mime.MimeTypeResolutionException)9 IOException (java.io.IOException)8 InputStream (java.io.InputStream)7 MimeTypeResolver (ddf.mime.MimeTypeResolver)3 TemporaryFileBackedOutputStream (org.codice.ddf.platform.util.TemporaryFileBackedOutputStream)3 ResourceResponseImpl (ddf.catalog.operation.impl.ResourceResponseImpl)2 ResourceNotFoundException (ddf.catalog.resource.ResourceNotFoundException)2 BufferedInputStream (java.io.BufferedInputStream)2 Detector (org.apache.tika.detect.Detector)2 Metadata (org.apache.tika.metadata.Metadata)2 MediaType (org.apache.tika.mime.MediaType)2 FileBackedOutputStream (com.google.common.io.FileBackedOutputStream)1 ResourceResponse (ddf.catalog.operation.ResourceResponse)1 MimeTypeMapper (ddf.mime.MimeTypeMapper)1 BufferedReader (java.io.BufferedReader)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStreamReader (java.io.InputStreamReader)1 URLConnection (java.net.URLConnection)1 PrivilegedAction (java.security.PrivilegedAction)1 WebApplicationException (javax.ws.rs.WebApplicationException)1