Annotation[] annotations,
MediaType mediaType,
MultivaluedMap<String,String> httpHeaders,
InputStream entityStream) throws IOException, WebApplicationException {
//boolean withMetadata = withMetadata(httpHeaders);
ContentItem contentItem = null;
UriRef contentItemId = getContentItemId();
Set<String> parsedContentIds = new HashSet<String>();
if(mediaType.isCompatible(MULTIPART)){
//try to read ContentItem from "multipart/from-data"
MGraph metadata = null;
FileItemIterator fileItemIterator;
try {
fileItemIterator = fu.getItemIterator(new MessageBodyReaderContext(entityStream, mediaType));
while(fileItemIterator.hasNext()){
FileItemStream fis = fileItemIterator.next();
if(fis.getFieldName().equals("metadata")){
if(contentItem != null){
throw new WebApplicationException(
Response.status(Response.Status.BAD_REQUEST)
.entity("The Multipart MIME part with the 'metadata' " +
"MUST BE before the MIME part containing the " +
"'content'!").build());
}
//the metadata may define the ID for the contentItem
//only used if not parsed as query param
if(contentItemId == null && fis.getName() != null && !fis.getName().isEmpty()){
contentItemId = new UriRef(fis.getName());
}
metadata = new IndexedMGraph();
try {
getParser().parse(metadata, fis.openStream(), fis.getContentType());
} catch (Exception e) {
throw new WebApplicationException(e,
Response.status(Response.Status.BAD_REQUEST)
.entity(String.format("Unable to parse Metadata " +
"from Multipart MIME part '%s' (" +
"contentItem: %s| contentType: %s)",
fis.getFieldName(),fis.getName(),fis.getContentType()))
.build());
}
} else if(fis.getFieldName().equals("content")){
contentItem = createContentItem(contentItemId, metadata, fis, parsedContentIds);
} else if(fis.getFieldName().equals("properties") ||
fis.getFieldName().equals(ENHANCEMENT_PROPERTIES_URI.getUnicodeString())){
//parse the enhancementProperties
if(contentItem == null){
throw new WebApplicationException(
Response.status(Response.Status.BAD_REQUEST)
.entity("Multipart MIME parts for " +
"EnhancementProperties MUST BE after the " +
"MIME parts for 'metadata' AND 'content'")
.build());
}
MediaType propMediaType = MediaType.valueOf(fis.getContentType());
if(!APPLICATION_JSON_TYPE.isCompatible(propMediaType)){
throw new WebApplicationException(
Response.status(Response.Status.BAD_REQUEST)
.entity("EnhancementProperties (Multipart MIME parts" +
"with the name '"+fis.getFieldName()+"') MUST " +
"BE encoded as 'appicaltion/json' (encountered: '" +
fis.getContentType()+"')!")
.build());
}
String propCharset = propMediaType.getParameters().get("charset");
if(propCharset == null){
propCharset = "UTF-8";
}
Map<String,Object> enhancementProperties = getEnhancementProperties(contentItem);
try {
enhancementProperties.putAll(toMap(new JSONObject(
IOUtils.toString(fis.openStream(),propCharset))));
} catch (JSONException e) {
throw new WebApplicationException(e,
Response.status(Response.Status.BAD_REQUEST)
.entity("Unable to parse EnhancementProperties from" +
"Multipart MIME parts with the name 'properties'!")
.build());
}
} else { //additional metadata as serialised RDF
if(contentItem == null){
throw new WebApplicationException(
Response.status(Response.Status.BAD_REQUEST)
.entity("Multipart MIME parts for additional " +
"contentParts MUST BE after the MIME " +
"parts for 'metadata' AND 'content'")
.build());
}
if(fis.getFieldName() == null || fis.getFieldName().isEmpty()){
throw new WebApplicationException(
Response.status(Response.Status.BAD_REQUEST)
.entity("Multipart MIME parts representing " +
"ContentParts for additional RDF metadata" +
"MUST define the contentParts URI as" +
"'name' of the MIME part!").build());
}
MGraph graph = new IndexedMGraph();
try {
getParser().parse(graph, fis.openStream(), fis.getContentType());
} catch (Exception e) {
throw new WebApplicationException(e,
Response.status(Response.Status.BAD_REQUEST)
.entity(String.format("Unable to parse RDF " +
"for ContentPart '%s' ( contentType: %s)",
fis.getName(),fis.getContentType()))
.build());
}
UriRef contentPartId = new UriRef(fis.getFieldName());
contentItem.addPart(contentPartId, graph);
}
}
if(contentItem == null){
throw new WebApplicationException(
Response.status(Response.Status.BAD_REQUEST)
.entity("The parsed multipart content item does not contain "
+ "any content. The content is expected to be contained "
+ "in a MIME part with the name 'content'. This part can "
+ " be also a 'multipart/alternate' if multiple content "
+ "parts need to be included in requests.").build());
}
} catch (FileUploadException e) {
throw new WebApplicationException(e, Response.Status.BAD_REQUEST);
}
} else { //normal content
ContentItemFactory ciFactory = getContentItemFactory();
contentItem = ciFactory.createContentItem(contentItemId,
new StreamSource(entityStream, mediaType.toString()));
//add the URI of the main content
parsedContentIds.add(contentItem.getPartUri(0).getUnicodeString());
}
//set the parsed contentIDs to the EnhancementProperties
getEnhancementProperties(contentItem).put(PARSED_CONTENT_URIS,
Collections.unmodifiableSet(parsedContentIds));
return contentItem;