Search in sources :

Example 6 with FileItemIterator

use of org.apache.commons.fileupload.FileItemIterator in project symmetric-ds by JumpMind.

the class FileSyncPushUriHandler method handle.

public void handle(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException, FileUploadException {
    String nodeId = ServletUtils.getParameter(req, WebConstants.NODE_ID);
    if (StringUtils.isBlank(nodeId)) {
        ServletUtils.sendError(res, HttpServletResponse.SC_BAD_REQUEST, "Node must be specified");
        return;
    } else if (!ServletFileUpload.isMultipartContent(req)) {
        ServletUtils.sendError(res, HttpServletResponse.SC_BAD_REQUEST, "We only handle multipart requests");
        return;
    } else {
        log.debug("File sync push request received from {}", nodeId);
    }
    ServletFileUpload upload = new ServletFileUpload();
    // Parse the request
    FileItemIterator iter = upload.getItemIterator(req);
    while (iter.hasNext()) {
        FileItemStream item = iter.next();
        String name = item.getFieldName();
        if (!item.isFormField()) {
            log.debug("Processing upload file field " + name + " with file name " + item.getName() + " detected.");
            engine.getFileSyncService().loadFilesFromPush(nodeId, item.openStream(), res.getOutputStream());
        }
    }
    res.flushBuffer();
}
Also used : ServletFileUpload(org.apache.commons.fileupload.servlet.ServletFileUpload) FileItemStream(org.apache.commons.fileupload.FileItemStream) FileItemIterator(org.apache.commons.fileupload.FileItemIterator)

Example 7 with FileItemIterator

use of org.apache.commons.fileupload.FileItemIterator in project pratilipi by Pratilipi.

the class GenericApi method executeApi.

final Object executeApi(GenericApi api, Method apiMethod, JsonObject requestPayloadJson, Class<? extends GenericRequest> apiMethodParameterType, HttpServletRequest request) {
    try {
        GenericRequest apiRequest = new Gson().fromJson(requestPayloadJson, apiMethodParameterType);
        if (apiRequest instanceof GenericFileUploadRequest) {
            GenericFileUploadRequest gfuRequest = (GenericFileUploadRequest) apiRequest;
            try {
                ServletFileUpload upload = new ServletFileUpload();
                FileItemIterator iterator = upload.getItemIterator(request);
                while (iterator.hasNext()) {
                    FileItemStream fileItemStream = iterator.next();
                    if (!fileItemStream.isFormField()) {
                        gfuRequest.setName(fileItemStream.getName());
                        gfuRequest.setData(IOUtils.toByteArray(fileItemStream.openStream()));
                        gfuRequest.setMimeType(fileItemStream.getContentType());
                        break;
                    }
                }
            } catch (IOException | FileUploadException e) {
                throw new UnexpectedServerException();
            }
        }
        JsonObject errorMessages = apiRequest.validate();
        if (errorMessages.entrySet().size() > 0)
            return new InvalidArgumentException(errorMessages);
        else
            return apiMethod.invoke(api, apiRequest);
    } catch (JsonSyntaxException e) {
        logger.log(Level.SEVERE, "Invalid JSON in request body.", e);
        return new InvalidArgumentException("Invalid JSON in request body.");
    } catch (UnexpectedServerException e) {
        return e;
    } catch (InvocationTargetException e) {
        Throwable te = e.getTargetException();
        if (te instanceof InvalidArgumentException || te instanceof InsufficientAccessException || te instanceof UnexpectedServerException) {
            return te;
        } else {
            logger.log(Level.SEVERE, "Failed to execute API.", te);
            return new UnexpectedServerException();
        }
    } catch (IllegalAccessException | IllegalArgumentException e) {
        logger.log(Level.SEVERE, "Failed to execute API.", e);
        return new UnexpectedServerException();
    }
}
Also used : GenericFileUploadRequest(com.pratilipi.api.shared.GenericFileUploadRequest) Gson(com.google.gson.Gson) JsonObject(com.google.gson.JsonObject) IOException(java.io.IOException) InsufficientAccessException(com.pratilipi.common.exception.InsufficientAccessException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ServletFileUpload(org.apache.commons.fileupload.servlet.ServletFileUpload) UnexpectedServerException(com.pratilipi.common.exception.UnexpectedServerException) InvalidArgumentException(com.pratilipi.common.exception.InvalidArgumentException) JsonSyntaxException(com.google.gson.JsonSyntaxException) FileItemStream(org.apache.commons.fileupload.FileItemStream) GenericRequest(com.pratilipi.api.shared.GenericRequest) FileItemIterator(org.apache.commons.fileupload.FileItemIterator) FileUploadException(org.apache.commons.fileupload.FileUploadException)

Example 8 with FileItemIterator

use of org.apache.commons.fileupload.FileItemIterator in project zm-mailbox by Zimbra.

the class UserServletContext method getRequestInputStream.

public InputStream getRequestInputStream(long limit) throws IOException, ServiceException, UserServletException {
    String contentType = MimeConstants.CT_APPLICATION_OCTET_STREAM;
    String filename = null;
    InputStream is = null;
    final long DEFAULT_MAX_SIZE = 10 * 1024 * 1024;
    if (limit == 0) {
        if (req.getParameter("lbfums") != null) {
            limit = Provisioning.getInstance().getLocalServer().getLongAttr(Provisioning.A_zimbraFileUploadMaxSize, DEFAULT_MAX_SIZE);
        } else {
            limit = Provisioning.getInstance().getConfig().getLongAttr(Provisioning.A_zimbraMtaMaxMessageSize, DEFAULT_MAX_SIZE);
        }
    }
    boolean doCsrfCheck = false;
    if (req.getAttribute(CsrfFilter.CSRF_TOKEN_CHECK) != null) {
        doCsrfCheck = (Boolean) req.getAttribute(CsrfFilter.CSRF_TOKEN_CHECK);
    }
    if (ServletFileUpload.isMultipartContent(req)) {
        ServletFileUpload sfu = new ServletFileUpload();
        try {
            FileItemIterator iter = sfu.getItemIterator(req);
            while (iter.hasNext()) {
                FileItemStream fis = iter.next();
                if (fis.isFormField()) {
                    is = fis.openStream();
                    params.put(fis.getFieldName(), new String(ByteUtil.getContent(is, -1), "UTF-8"));
                    if (doCsrfCheck && !this.csrfAuthSucceeded) {
                        String csrfToken = params.get(FileUploadServlet.PARAM_CSRF_TOKEN);
                        if (UserServlet.log.isDebugEnabled()) {
                            String paramValue = req.getParameter(UserServlet.QP_AUTH);
                            UserServlet.log.debug("CSRF check is: %s, CSRF token is: %s, Authentication recd with request is: %s", doCsrfCheck, csrfToken, paramValue);
                        }
                        if (!CsrfUtil.isValidCsrfToken(csrfToken, authToken)) {
                            setCsrfAuthSucceeded(Boolean.FALSE);
                            UserServlet.log.debug("CSRF token validation failed for account: %s" + ", Auth token is CSRF enabled:  %s" + "CSRF token is: %s", authToken, authToken.isCsrfTokenEnabled(), csrfToken);
                            throw new UserServletException(HttpServletResponse.SC_UNAUTHORIZED, L10nUtil.getMessage(MsgKey.errMustAuthenticate));
                        } else {
                            setCsrfAuthSucceeded(Boolean.TRUE);
                        }
                    }
                    is.close();
                    is = null;
                } else {
                    is = new UploadInputStream(fis.openStream(), limit);
                    break;
                }
            }
        } catch (UserServletException e) {
            throw new UserServletException(e.getHttpStatusCode(), e.getMessage(), e);
        } catch (Exception e) {
            throw new UserServletException(HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE, e.toString());
        }
        if (is == null)
            throw new UserServletException(HttpServletResponse.SC_NO_CONTENT, "No file content");
    } else {
        ContentType ctype = new ContentType(req.getContentType());
        String contentEncoding = req.getHeader("Content-Encoding");
        contentType = ctype.getContentType();
        filename = ctype.getParameter("name");
        if (filename == null || filename.trim().equals(""))
            filename = new ContentDisposition(req.getHeader("Content-Disposition")).getParameter("filename");
        is = new UploadInputStream(contentEncoding != null && contentEncoding.indexOf("gzip") != -1 ? new GZIPInputStream(req.getInputStream()) : req.getInputStream(), limit);
    }
    if (filename == null || filename.trim().equals(""))
        filename = "unknown";
    else
        params.put(UserServlet.UPLOAD_NAME, filename);
    params.put(UserServlet.UPLOAD_TYPE, contentType);
    ZimbraLog.mailbox.info("UserServlet received file %s - %d request bytes", filename, req.getContentLength());
    return is;
}
Also used : ContentType(com.zimbra.common.mime.ContentType) GZIPInputStream(java.util.zip.GZIPInputStream) InputStream(java.io.InputStream) ServiceException(com.zimbra.common.service.ServiceException) IOException(java.io.IOException) GZIPInputStream(java.util.zip.GZIPInputStream) ServletFileUpload(org.apache.commons.fileupload.servlet.ServletFileUpload) ContentDisposition(com.zimbra.common.mime.ContentDisposition) FileItemStream(org.apache.commons.fileupload.FileItemStream) FileItemIterator(org.apache.commons.fileupload.FileItemIterator)

Example 9 with FileItemIterator

use of org.apache.commons.fileupload.FileItemIterator in project jena by apache.

the class SPARQL_Upload method uploadWorker.

/** Process an HTTP file upload of RDF with additiona name field for the graph name.
     *  We can't stream straight into a dataset because the graph name can be after the data.
     *  @return graph name and count
     */
// ?? Combine with Upload.fileUploadWorker
// Difference is the handling of names for graphs.
private static UploadDetails uploadWorker(HttpAction action, String base) {
    DatasetGraph dsgTmp = DatasetGraphFactory.create();
    ServletFileUpload upload = new ServletFileUpload();
    String graphName = null;
    boolean isQuads = false;
    long count = -1;
    String name = null;
    ContentType ct = null;
    Lang lang = null;
    try {
        FileItemIterator iter = upload.getItemIterator(action.request);
        while (iter.hasNext()) {
            FileItemStream item = iter.next();
            String fieldName = item.getFieldName();
            InputStream stream = item.openStream();
            if (item.isFormField()) {
                // Graph name.
                String value = Streams.asString(stream, "UTF-8");
                if (fieldName.equals(HttpNames.paramGraph)) {
                    graphName = value;
                    if (graphName != null && !graphName.equals("") && !graphName.equals(HttpNames.valueDefault)) {
                        IRI iri = IRIResolver.parseIRI(value);
                        if (iri.hasViolation(false))
                            ServletOps.errorBadRequest("Bad IRI: " + graphName);
                        if (iri.getScheme() == null)
                            ServletOps.errorBadRequest("Bad IRI: no IRI scheme name: " + graphName);
                        if (iri.getScheme().equalsIgnoreCase("http") || iri.getScheme().equalsIgnoreCase("https")) {
                            // Redundant??
                            if (iri.getRawHost() == null)
                                ServletOps.errorBadRequest("Bad IRI: no host name: " + graphName);
                            if (iri.getRawPath() == null || iri.getRawPath().length() == 0)
                                ServletOps.errorBadRequest("Bad IRI: no path: " + graphName);
                            if (iri.getRawPath().charAt(0) != '/')
                                ServletOps.errorBadRequest("Bad IRI: Path does not start '/': " + graphName);
                        }
                    }
                } else if (fieldName.equals(HttpNames.paramDefaultGraphURI))
                    graphName = null;
                else
                    // Add file type?
                    action.log.info(format("[%d] Upload: Field=%s ignored", action.id, fieldName));
            } else {
                // Process the input stream
                name = item.getName();
                if (name == null || name.equals("") || name.equals("UNSET FILE NAME"))
                    ServletOps.errorBadRequest("No name for content - can't determine RDF syntax");
                String contentTypeHeader = item.getContentType();
                ct = ContentType.create(contentTypeHeader);
                lang = RDFLanguages.contentTypeToLang(ct.getContentType());
                if (lang == null) {
                    lang = RDFLanguages.filenameToLang(name);
                    // present we wrap the stream accordingly
                    if (name.endsWith(".gz"))
                        stream = new GZIPInputStream(stream);
                }
                if (lang == null)
                    // Desperate.
                    lang = RDFLanguages.RDFXML;
                isQuads = RDFLanguages.isQuads(lang);
                action.log.info(format("[%d] Upload: Filename: %s, Content-Type=%s, Charset=%s => %s", action.id, name, ct.getContentType(), ct.getCharset(), lang.getName()));
                StreamRDF x = StreamRDFLib.dataset(dsgTmp);
                StreamRDFCounting dest = StreamRDFLib.count(x);
                ActionSPARQL.parse(action, dest, stream, lang, base);
                count = dest.count();
            }
        }
        if (graphName == null || graphName.equals(""))
            graphName = HttpNames.valueDefault;
        if (isQuads)
            graphName = null;
        return new UploadDetails(graphName, dsgTmp, count);
    } catch (ActionErrorException ex) {
        throw ex;
    } catch (Exception ex) {
        ServletOps.errorOccurred(ex);
        return null;
    }
}
Also used : IRI(org.apache.jena.iri.IRI) ContentType(org.apache.jena.atlas.web.ContentType) GZIPInputStream(java.util.zip.GZIPInputStream) InputStream(java.io.InputStream) Lang(org.apache.jena.riot.Lang) DatasetGraph(org.apache.jena.sparql.core.DatasetGraph) GZIPInputStream(java.util.zip.GZIPInputStream) ServletFileUpload(org.apache.commons.fileupload.servlet.ServletFileUpload) FileItemStream(org.apache.commons.fileupload.FileItemStream) StreamRDF(org.apache.jena.riot.system.StreamRDF) StreamRDFCounting(org.apache.jena.riot.lang.StreamRDFCounting) FileItemIterator(org.apache.commons.fileupload.FileItemIterator)

Example 10 with FileItemIterator

use of org.apache.commons.fileupload.FileItemIterator in project stanbol by apache.

the class ContentItemReader method readFrom.

@Override
public ContentItem readFrom(Class<ContentItem> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, String> httpHeaders, InputStream entityStream) throws IOException, WebApplicationException {
    //boolean withMetadata = withMetadata(httpHeaders);
    ContentItem contentItem = null;
    IRI contentItemId = getContentItemId();
    if (log.isTraceEnabled()) {
        //NOTE: enabling TRACE level logging will copy the parsed content
        //      into a BYTE array
        log.trace("Parse ContentItem from");
        log.trace("  - MediaType: {}", mediaType);
        log.trace("  - Headers:");
        for (Entry<String, List<String>> header : httpHeaders.entrySet()) {
            log.trace("      {}: {}", header.getKey(), header.getValue());
        }
        byte[] content = IOUtils.toByteArray(entityStream);
        log.trace("content: \n{}", new String(content, "UTF-8"));
        IOUtils.closeQuietly(entityStream);
        entityStream = new ByteArrayInputStream(content);
    }
    Set<String> parsedContentIds = new HashSet<String>();
    if (mediaType.isCompatible(MULTIPART)) {
        log.debug(" - parse Multipart MIME ContentItem");
        //try to read ContentItem from "multipart/from-data"
        Graph 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());
                    }
                    //only used if not parsed as query param
                    if (contentItemId == null && fis.getName() != null && !fis.getName().isEmpty()) {
                        contentItemId = new IRI(fis.getName());
                    }
                    metadata = new IndexedGraph();
                    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(REQUEST_PROPERTIES_URI.getUnicodeString())) {
                    //parse the RequestProperties
                    if (contentItem == null) {
                        throw new WebApplicationException(Response.status(Response.Status.BAD_REQUEST).entity("Multipart MIME parts for " + "Request Properties 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("Request Properties (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> reqProp = ContentItemHelper.initRequestPropertiesContentPart(contentItem);
                    try {
                        reqProp.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 Request Properties 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());
                    }
                    Graph graph = new IndexedGraph();
                    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());
                    }
                    IRI contentPartId = new IRI(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
    Map<String, Object> ep = ContentItemHelper.initRequestPropertiesContentPart(contentItem);
    parseEnhancementPropertiesFromParameters(ep);
    ep.put(PARSED_CONTENT_URIS, Collections.unmodifiableSet(parsedContentIds));
    //STANBOL-660: set the language of the content if explicitly parsed in the request
    String contentLanguage = getContentLanguage();
    if (!StringUtils.isBlank(contentLanguage)) {
        //language codes are case insensitive ... so we convert to lower case
        contentLanguage = contentLanguage.toLowerCase(Locale.ROOT);
        createParsedLanguageAnnotation(contentItem, contentLanguage);
    // previously only the dc:language property was set to the contentItem. However this
    // information is only used as fallback if no Language annotation is present. However
    // if a user explicitly parses the language he expects this language to be used
    // so this was change with STANBOL-1417
    //            EnhancementEngineHelper.set(contentItem.getMetadata(), contentItem.getUri(), 
    //                DC_LANGUAGE, new PlainLiteralImpl(contentLanguage));
    }
    return contentItem;
}
Also used : IRI(org.apache.clerezza.commons.rdf.IRI) ContentItemFactory(org.apache.stanbol.enhancer.servicesapi.ContentItemFactory) WebApplicationException(javax.ws.rs.WebApplicationException) StreamSource(org.apache.stanbol.enhancer.servicesapi.impl.StreamSource) JSONException(org.codehaus.jettison.json.JSONException) URISyntaxException(java.net.URISyntaxException) WebApplicationException(javax.ws.rs.WebApplicationException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) JSONException(org.codehaus.jettison.json.JSONException) FileUploadException(org.apache.commons.fileupload.FileUploadException) IndexedGraph(org.apache.stanbol.commons.indexedgraph.IndexedGraph) Graph(org.apache.clerezza.commons.rdf.Graph) JSONObject(org.codehaus.jettison.json.JSONObject) ByteArrayInputStream(java.io.ByteArrayInputStream) FileItemStream(org.apache.commons.fileupload.FileItemStream) MediaType(javax.ws.rs.core.MediaType) List(java.util.List) ArrayList(java.util.ArrayList) JSONObject(org.codehaus.jettison.json.JSONObject) IndexedGraph(org.apache.stanbol.commons.indexedgraph.IndexedGraph) FileItemIterator(org.apache.commons.fileupload.FileItemIterator) ContentItem(org.apache.stanbol.enhancer.servicesapi.ContentItem) FileUploadException(org.apache.commons.fileupload.FileUploadException) HashSet(java.util.HashSet)

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