Search in sources :

Example 11 with FileItemIterator

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;
}
Also used : RiotParseException(org.apache.jena.riot.RiotParseException) WebContent.matchContentType(org.apache.jena.riot.WebContent.matchContentType) ContentType(org.apache.jena.atlas.web.ContentType) GZIPInputStream(java.util.zip.GZIPInputStream) InputStream(java.io.InputStream) Lang(org.apache.jena.riot.Lang) IOException(java.io.IOException) RiotParseException(org.apache.jena.riot.RiotParseException) GZIPInputStream(java.util.zip.GZIPInputStream) ServletFileUpload(org.apache.commons.fileupload.servlet.ServletFileUpload) FileItemStream(org.apache.commons.fileupload.FileItemStream) StreamRDFCounting(org.apache.jena.riot.lang.StreamRDFCounting) FileItemIterator(org.apache.commons.fileupload.FileItemIterator)

Example 12 with FileItemIterator

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;
}
Also used : IRI(org.apache.clerezza.commons.rdf.IRI) Blob(org.apache.stanbol.enhancer.servicesapi.Blob) ContentItemFactory(org.apache.stanbol.enhancer.servicesapi.ContentItemFactory) FileItemStream(org.apache.commons.fileupload.FileItemStream) StreamSource(org.apache.stanbol.enhancer.servicesapi.impl.StreamSource) MediaType(javax.ws.rs.core.MediaType) FileItemIterator(org.apache.commons.fileupload.FileItemIterator) ContentItem(org.apache.stanbol.enhancer.servicesapi.ContentItem)

Aggregations

FileItemIterator (org.apache.commons.fileupload.FileItemIterator)12 FileItemStream (org.apache.commons.fileupload.FileItemStream)10 IOException (java.io.IOException)7 ServletFileUpload (org.apache.commons.fileupload.servlet.ServletFileUpload)7 InputStream (java.io.InputStream)5 FileUploadException (org.apache.commons.fileupload.FileUploadException)5 GZIPInputStream (java.util.zip.GZIPInputStream)3 MediaType (javax.ws.rs.core.MediaType)2 Gson (com.google.gson.Gson)1 JsonObject (com.google.gson.JsonObject)1 JsonSyntaxException (com.google.gson.JsonSyntaxException)1 AbstractModule (com.google.inject.AbstractModule)1 Injector (com.google.inject.Injector)1 GenericFileUploadRequest (com.pratilipi.api.shared.GenericFileUploadRequest)1 GenericRequest (com.pratilipi.api.shared.GenericRequest)1 InsufficientAccessException (com.pratilipi.common.exception.InsufficientAccessException)1 InvalidArgumentException (com.pratilipi.common.exception.InvalidArgumentException)1 UnexpectedServerException (com.pratilipi.common.exception.UnexpectedServerException)1 ContentDisposition (com.zimbra.common.mime.ContentDisposition)1 ContentType (com.zimbra.common.mime.ContentType)1