use of org.apache.cxf.jaxrs.ext.multipart.Attachment in project ddf by codice.
the class RESTEndpoint method parseAttachments.
CreateInfo parseAttachments(List<Attachment> contentParts, String transformerParam) {
if (contentParts.size() == 1) {
Attachment contentPart = contentParts.get(0);
return parseAttachment(contentPart);
}
List<Attribute> attributes = new ArrayList<>(contentParts.size());
Metacard metacard = null;
CreateInfo createInfo = null;
for (Attachment attachment : contentParts) {
String name = attachment.getContentDisposition().getParameter("name");
String parsedName = (name.startsWith("parse.")) ? name.substring(6) : name;
try {
InputStream inputStream = attachment.getDataHandler().getInputStream();
if (name.equals("parse.resource")) {
createInfo = parseAttachment(attachment);
} else if (name.equals("parse.metadata")) {
metacard = parseMetadata(transformerParam, metacard, attachment, inputStream);
} else {
parseOverrideAttributes(attributes, parsedName, inputStream);
}
} catch (IOException e) {
LOGGER.debug("Unable to get input stream for mime attachment. Ignoring override attribute: {}", name, e);
}
}
if (createInfo == null) {
throw new IllegalArgumentException("No parse.resource specified in request.");
}
if (metacard == null) {
metacard = new MetacardImpl();
}
for (Attribute attribute : attributes) {
metacard.setAttribute(attribute);
}
createInfo.setMetacard(metacard);
return createInfo;
}
use of org.apache.cxf.jaxrs.ext.multipart.Attachment in project ddf by codice.
the class RESTEndpoint method updateDocument.
/**
* REST Put. Updates the specified entry with the provided document.
*
* @param id
* @param message
* @return
*/
@PUT
@Path("/{id}")
@Consumes("multipart/*")
public Response updateDocument(@PathParam("id") String id, @Context HttpHeaders headers, @Context HttpServletRequest httpRequest, MultipartBody multipartBody, @QueryParam("transform") String transformerParam, InputStream message) {
LOGGER.trace("PUT");
Response response;
try {
if (id != null && message != null) {
MimeType mimeType = getMimeType(headers);
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");
}
}
if (createInfo == null) {
UpdateRequest updateRequest = new UpdateRequestImpl(id, generateMetacard(mimeType, id, message, transformerParam));
catalogFramework.update(updateRequest);
} else {
UpdateStorageRequest streamUpdateRequest = new UpdateStorageRequestImpl(Collections.singletonList(new IncomingContentItem(id, createInfo.getStream(), createInfo.getContentType(), createInfo.getFilename(), 0, createInfo.getMetacard())), null);
catalogFramework.update(streamUpdateRequest);
}
LOGGER.debug("Metacard {} updated.", id);
response = Response.ok().build();
} else {
String errorResponseString = "Both ID and content are needed to perform UPDATE.";
LOGGER.info(errorResponseString);
throw new ServerErrorException(errorResponseString, Status.BAD_REQUEST);
}
} catch (SourceUnavailableException e) {
String exceptionMessage = "Cannot update catalog entry: Source is unavailable: ";
LOGGER.info(exceptionMessage, e);
throw new ServerErrorException(exceptionMessage, Status.INTERNAL_SERVER_ERROR);
} catch (InternalIngestException e) {
String exceptionMessage = "Error cataloging updated metadata: ";
LOGGER.info(exceptionMessage, e);
throw new ServerErrorException(exceptionMessage, Status.INTERNAL_SERVER_ERROR);
} catch (MetacardCreationException | IngestException e) {
String exceptionMessage = "Error cataloging updated metadata: ";
LOGGER.info(exceptionMessage, e);
throw new ServerErrorException(exceptionMessage, Status.BAD_REQUEST);
}
return response;
}
Aggregations