use of javax.activation.MimeType in project ddf by codice.
the class CswEndpoint method queryProductById.
private CswRecordCollection queryProductById(String id, String rangeValue) throws CswException, UnsupportedQueryException {
final ResourceRequestById resourceRequest = new ResourceRequestById(id);
long bytesToSkip = getRange(rangeValue);
if (bytesToSkip > 0) {
LOGGER.debug("Bytes to skip: {}", String.valueOf(bytesToSkip));
resourceRequest.getProperties().put(CswConstants.BYTES_TO_SKIP, bytesToSkip);
}
ResourceResponse resourceResponse;
try {
resourceResponse = framework.getLocalResource(resourceRequest);
} catch (IOException | ResourceNotFoundException | ResourceNotSupportedException e) {
throw new CswException(String.format(ERROR_ID_PRODUCT_RETRIEVAL, id), e);
}
Resource resource = resourceResponse.getResource();
MimeType mimeType = resource.getMimeType();
if (mimeType == null) {
try {
mimeType = new MimeType(MediaType.APPLICATION_OCTET_STREAM);
resource = new ResourceImpl(resource.getInputStream(), mimeType, resource.getName());
} catch (MimeTypeParseException e) {
throw new CswException(String.format("Could not create mime type upon null mimeType, for mime %s.", MediaType.APPLICATION_OCTET_STREAM), e);
}
}
CswRecordCollection cswRecordCollection = new CswRecordCollection();
cswRecordCollection.setResource(resource);
cswRecordCollection.setOutputSchema(OCTET_STREAM_OUTPUT_SCHEMA);
LOGGER.debug("{} successfully retrieved product for ID: {}", id);
return cswRecordCollection;
}
use of javax.activation.MimeType in project ddf by codice.
the class Configuration method setMimeType.
public void setMimeType(String mimeType) {
MimeType mime = null;
try {
mime = new MimeType(mimeType);
} catch (MimeTypeParseException e) {
LOGGER.info("Failed to create mimetype: {}.", mimeType);
}
this.mimeType = mime;
}
use of javax.activation.MimeType in project ddf by codice.
the class RESTEndpoint method addDocument.
/**
* REST Post. Creates a new metadata entry in the catalog.
*
* @param message
* @return
*/
@POST
@Consumes("multipart/*")
public Response addDocument(@Context HttpHeaders headers, @Context UriInfo requestUriInfo, @Context HttpServletRequest httpRequest, MultipartBody multipartBody, @QueryParam("transform") String transformerParam, InputStream message) {
LOGGER.debug("POST");
Response response;
MimeType mimeType = getMimeType(headers);
try {
if (message != null) {
CreateInfo createInfo = null;
if (multipartBody != null) {
List<Attachment> contentParts = multipartBody.getAllAttachments();
if (contentParts != null && contentParts.size() > 0) {
createInfo = parseAttachments(contentParts, transformerParam);
} else {
LOGGER.debug("No file contents attachment found");
}
}
CreateResponse createResponse;
if (createInfo == null) {
CreateRequest createRequest = new CreateRequestImpl(generateMetacard(mimeType, null, message, transformerParam));
createResponse = catalogFramework.create(createRequest);
} else {
CreateStorageRequest streamCreateRequest = new CreateStorageRequestImpl(Collections.singletonList(new IncomingContentItem(uuidGenerator, createInfo.getStream(), createInfo.getContentType(), createInfo.getFilename(), createInfo.getMetacard())), null);
createResponse = catalogFramework.create(streamCreateRequest);
}
String id = createResponse.getCreatedMetacards().get(0).getId();
LOGGER.debug("Create Response id [{}]", id);
UriBuilder uriBuilder = requestUriInfo.getAbsolutePathBuilder().path("/" + id);
ResponseBuilder responseBuilder = Response.created(uriBuilder.build());
responseBuilder.header(Metacard.ID, id);
response = responseBuilder.build();
LOGGER.debug("Entry successfully saved, id: {}", id);
if (INGEST_LOGGER.isInfoEnabled()) {
INGEST_LOGGER.info("Entry successfully saved, id: {}", id);
}
} else {
String errorMessage = "No content found, cannot do CREATE.";
LOGGER.info(errorMessage);
throw new ServerErrorException(errorMessage, Status.BAD_REQUEST);
}
} catch (SourceUnavailableException e) {
String exceptionMessage = "Cannot create catalog entry because source is unavailable: ";
LOGGER.info(exceptionMessage, e);
// Catalog framework logs these exceptions to the ingest logger so we don't have to.
throw new ServerErrorException(exceptionMessage, Status.INTERNAL_SERVER_ERROR);
} catch (InternalIngestException e) {
String exceptionMessage = "Error while storing entry in catalog: ";
LOGGER.info(exceptionMessage, e);
// Catalog framework logs these exceptions to the ingest logger so we don't have to.
throw new ServerErrorException(exceptionMessage, Status.INTERNAL_SERVER_ERROR);
} catch (MetacardCreationException | IngestException e) {
String exceptionMessage = "Error while storing entry in catalog: ";
LOGGER.info(exceptionMessage, e);
// Catalog framework logs these exceptions to the ingest logger so we don't have to.
throw new ServerErrorException(exceptionMessage, Status.BAD_REQUEST);
} finally {
IOUtils.closeQuietly(message);
}
return response;
}
use of javax.activation.MimeType in project ddf by codice.
the class RESTEndpoint method getMimeType.
private MimeType getMimeType(HttpHeaders headers) {
List<String> contentTypeList = headers.getRequestHeader(HttpHeaders.CONTENT_TYPE);
String singleMimeType = null;
if (contentTypeList != null && !contentTypeList.isEmpty()) {
singleMimeType = contentTypeList.get(0);
LOGGER.debug("Encountered [{}] {}", singleMimeType, HttpHeaders.CONTENT_TYPE);
}
MimeType mimeType = null;
// Sending a null argument to MimeType causes NPE
if (singleMimeType != null) {
try {
mimeType = new MimeType(singleMimeType);
} catch (MimeTypeParseException e) {
LOGGER.debug("Could not parse mime type from headers.", e);
}
}
return mimeType;
}
use of javax.activation.MimeType in project ddf by codice.
the class RESTEndpoint method parseMetadata.
private Metacard parseMetadata(String transformerParam, Metacard metacard, Attachment attachment, InputStream inputStream) {
String transformer = DEFAULT_METACARD_TRANSFORMER;
if (transformerParam != null) {
transformer = transformerParam;
}
try {
MimeType mimeType = new MimeType(attachment.getContentType().toString());
metacard = generateMetacard(mimeType, "assigned-when-ingested", inputStream, transformer);
} catch (MimeTypeParseException | MetacardCreationException e) {
LOGGER.debug("Unable to parse metadata {}", attachment.getContentType().toString());
} finally {
IOUtils.closeQuietly(inputStream);
}
return metacard;
}
Aggregations