Search in sources :

Example 11 with FileItemStream

use of org.apache.commons.fileupload.FileItemStream 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

FileItemStream (org.apache.commons.fileupload.FileItemStream)11 FileItemIterator (org.apache.commons.fileupload.FileItemIterator)10 IOException (java.io.IOException)7 ServletFileUpload (org.apache.commons.fileupload.servlet.ServletFileUpload)7 InputStream (java.io.InputStream)6 FileUploadException (org.apache.commons.fileupload.FileUploadException)5 GZIPInputStream (java.util.zip.GZIPInputStream)3 MediaType (javax.ws.rs.core.MediaType)2 IRI (org.apache.clerezza.commons.rdf.IRI)2 ContentType (org.apache.jena.atlas.web.ContentType)2 Gson (com.google.gson.Gson)1 JsonObject (com.google.gson.JsonObject)1 JsonSyntaxException (com.google.gson.JsonSyntaxException)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