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