use of org.apache.commons.fileupload.FileItemIterator in project jena by apache.
the class Upload method fileUploadWorker.
/** Process an HTTP upload of RDF files (triples or quads)
* Stream straight into a graph or dataset -- unlike SPARQL_Upload the destination
* is known at the start of the multipart file body
*/
public static UploadDetails fileUploadWorker(HttpAction action, StreamRDF dest) {
String base = ActionLib.wholeRequestURL(action.request);
ServletFileUpload upload = new ServletFileUpload();
//log.info(format("[%d] Upload: Field=%s ignored", action.id, fieldName)) ;
// Overall counting.
StreamRDFCounting countingDest = StreamRDFLib.count(dest);
try {
FileItemIterator iter = upload.getItemIterator(action.request);
while (iter.hasNext()) {
FileItemStream fileStream = iter.next();
if (fileStream.isFormField()) {
// Ignore?
String fieldName = fileStream.getFieldName();
InputStream stream = fileStream.openStream();
String value = Streams.asString(stream, "UTF-8");
ServletOps.errorBadRequest(format("Only files accepted in multipart file upload (got %s=%s)", fieldName, value));
}
//Ignore the field name.
//String fieldName = fileStream.getFieldName();
InputStream stream = fileStream.openStream();
// Process the input stream
String contentTypeHeader = fileStream.getContentType();
ContentType ct = ContentType.create(contentTypeHeader);
Lang lang = null;
if (!matchContentType(ctTextPlain, ct))
lang = RDFLanguages.contentTypeToLang(ct.getContentType());
if (lang == null) {
String name = fileStream.getName();
if (name == null || name.equals(""))
ServletOps.errorBadRequest("No name for content - can't determine RDF syntax");
lang = RDFLanguages.filenameToLang(name);
if (name.endsWith(".gz"))
stream = new GZIPInputStream(stream);
}
if (lang == null)
// Desperate.
lang = RDFLanguages.RDFXML;
String printfilename = fileStream.getName();
if (printfilename == null || printfilename.equals(""))
printfilename = "<none>";
// Before
// action.log.info(format("[%d] Filename: %s, Content-Type=%s, Charset=%s => %s",
// action.id, printfilename, ct.getContentType(), ct.getCharset(), lang.getName())) ;
// count just this step
StreamRDFCounting countingDest2 = StreamRDFLib.count(countingDest);
try {
ActionSPARQL.parse(action, countingDest2, stream, lang, base);
UploadDetails details1 = new UploadDetails(countingDest2.count(), countingDest2.countTriples(), countingDest2.countQuads());
action.log.info(format("[%d] Filename: %s, Content-Type=%s, Charset=%s => %s : %s", action.id, printfilename, ct.getContentType(), ct.getCharset(), lang.getName(), details1.detailsStr()));
} catch (RiotParseException ex) {
action.log.info(format("[%d] Filename: %s, Content-Type=%s, Charset=%s => %s : %s", action.id, printfilename, ct.getContentType(), ct.getCharset(), lang.getName(), ex.getMessage()));
throw ex;
}
}
} catch (ActionErrorException ex) {
throw ex;
} catch (Exception ex) {
ServletOps.errorOccurred(ex.getMessage());
}
// Overall results.
UploadDetails details = new UploadDetails(countingDest.count(), countingDest.countTriples(), countingDest.countQuads());
return details;
}
use of org.apache.commons.fileupload.FileItemIterator in project stanbol by apache.
the class ContentItemReader method createContentItem.
/**
* Creates a ContentItem
* @param id the ID or <code>null</code> if not known
* @param metadata the metadata or <code>null</code> if not parsed. NOTE that
* if <code>id == null</code> also <code>metadata == null</code> and
* <code>id != null</code> also <code>metadata != null</code>.
* @param content the {@link FileItemStream} of the MIME part representing
* the content. If {@link FileItemStream#getContentType()} is compatible with
* "multipart/*" than this will further parse for multiple parsed content
* version. In any other case the contents of the parsed {@link FileItemStream}
* will be directly add as content for the {@link ContentItem} created by
* this method.
* @param parsedContentParts used to add the IDs of parsed contentParts
* @return the created content item
* @throws IOException on any error while accessing the contents of the parsed
* {@link FileItemStream}
* @throws FileUploadException if the parsed contents are not correctly
* encoded Multipart MIME
*/
private ContentItem createContentItem(IRI id, Graph metadata, FileItemStream content, Set<String> parsedContentParts) throws IOException, FileUploadException {
MediaType partContentType = MediaType.valueOf(content.getContentType());
ContentItem contentItem = null;
ContentItemFactory ciFactory = getContentItemFactory();
if (MULTIPART.isCompatible(partContentType)) {
log.debug(" - multiple (alternate) ContentParts");
//multiple contentParts are parsed
FileItemIterator contentPartIterator = fu.getItemIterator(new MessageBodyReaderContext(content.openStream(), partContentType));
while (contentPartIterator.hasNext()) {
FileItemStream fis = contentPartIterator.next();
if (contentItem == null) {
log.debug(" - create ContentItem {} for content (type:{})", id, fis.getContentType());
contentItem = ciFactory.createContentItem(id, new StreamSource(fis.openStream(), fis.getContentType()), metadata);
} else {
log.debug(" - create Blob for content (type:{})", fis.getContentType());
Blob blob = ciFactory.createBlob(new StreamSource(fis.openStream(), fis.getContentType()));
IRI contentPartId = null;
if (fis.getFieldName() != null && !fis.getFieldName().isEmpty()) {
contentPartId = new IRI(fis.getFieldName());
} else {
//generating a random ID might break metadata
//TODO maybe we should throw an exception instead
contentPartId = new IRI("urn:contentpart:" + randomUUID());
}
log.debug(" ... add Blob {} to ContentItem {} with content (type:{})", new Object[] { contentPartId, id, fis.getContentType() });
contentItem.addPart(contentPartId, blob);
parsedContentParts.add(contentPartId.getUnicodeString());
}
}
} else {
log.debug(" - create ContentItem {} for content (type:{})", id, content.getContentType());
contentItem = ciFactory.createContentItem(id, new StreamSource(content.openStream(), content.getContentType()), metadata);
}
//add the URI of the main content to the parsed contentParts
parsedContentParts.add(contentItem.getPartUri(0).getUnicodeString());
return contentItem;
}
Aggregations