Search in sources :

Example 41 with FileItemStream

use of org.apache.commons.fileupload.FileItemStream in project BIMserver by opensourceBIM.

the class UploadServlet method service.

@Override
public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    if (request.getHeader("Origin") != null && !getBimServer().getServerSettingsCache().isHostAllowed(request.getHeader("Origin"))) {
        response.setStatus(403);
        return;
    }
    response.setHeader("Access-Control-Allow-Origin", request.getHeader("Origin"));
    response.setHeader("Access-Control-Allow-Headers", "Content-Type");
    String token = (String) request.getSession().getAttribute("token");
    ObjectNode result = OBJECT_MAPPER.createObjectNode();
    response.setContentType("text/json");
    try {
        boolean isMultipart = ServletFileUpload.isMultipartContent(request);
        long poid = -1;
        String comment = null;
        if (isMultipart) {
            ServletFileUpload upload = new ServletFileUpload();
            FileItemIterator iter = upload.getItemIterator(request);
            InputStream in = null;
            String name = "";
            long deserializerOid = -1;
            boolean merge = false;
            boolean sync = false;
            String compression = null;
            String action = null;
            long topicId = -1;
            try {
                while (iter.hasNext()) {
                    FileItemStream item = iter.next();
                    if (item.isFormField()) {
                        if ("action".equals(item.getFieldName())) {
                            action = Streams.asString(item.openStream());
                        } else if ("token".equals(item.getFieldName())) {
                            token = Streams.asString(item.openStream());
                        } else if ("poid".equals(item.getFieldName())) {
                            poid = Long.parseLong(Streams.asString(item.openStream()));
                        } else if ("comment".equals(item.getFieldName())) {
                            comment = Streams.asString(item.openStream());
                        } else if ("topicId".equals(item.getFieldName())) {
                            topicId = Long.parseLong(Streams.asString(item.openStream()));
                        } else if ("sync".equals(item.getFieldName())) {
                            sync = Streams.asString(item.openStream()).equals("true");
                        } else if ("merge".equals(item.getFieldName())) {
                            merge = Streams.asString(item.openStream()).equals("true");
                        } else if ("compression".equals(item.getFieldName())) {
                            compression = Streams.asString(item.openStream());
                        } else if ("deserializerOid".equals(item.getFieldName())) {
                            deserializerOid = Long.parseLong(Streams.asString(item.openStream()));
                        }
                    } else {
                        name = item.getName();
                        in = item.openStream();
                        if ("file".equals(action)) {
                            ServiceInterface serviceInterface = getBimServer().getServiceFactory().get(token, AccessMethod.INTERNAL).get(ServiceInterface.class);
                            SFile file = new SFile();
                            byte[] data = IOUtils.toByteArray(in);
                            file.setData(data);
                            file.setSize(data.length);
                            file.setFilename(name);
                            file.setMime(item.getContentType());
                            result.put("fileId", serviceInterface.uploadFile(file));
                        } else if (poid != -1) {
                            InputStream realStream = null;
                            if ("gzip".equals(compression)) {
                                realStream = new GZIPInputStream(in);
                            } else if ("deflate".equals(compression)) {
                                realStream = new InflaterInputStream(in);
                            } else {
                                realStream = in;
                            }
                            // When uploading in async mode, we want to return as soon as the whole stream has been read (that's not when the checkin process has finished!)
                            TriggerOnCloseInputStream triggerOnCloseInputStream = new TriggerOnCloseInputStream(realStream);
                            InputStreamDataSource inputStreamDataSource = new InputStreamDataSource(triggerOnCloseInputStream);
                            inputStreamDataSource.setName(name);
                            DataHandler ifcFile = new DataHandler(inputStreamDataSource);
                            if (token != null) {
                                ServiceInterface service = getBimServer().getServiceFactory().get(token, AccessMethod.INTERNAL).get(ServiceInterface.class);
                                if (topicId == -1) {
                                    if (sync) {
                                        SLongCheckinActionState checkinSync = service.checkinSync(poid, comment, deserializerOid, -1L, name, ifcFile, merge);
                                        result = (ObjectNode) getBimServer().getJsonHandler().getJsonConverter().toJson(checkinSync);
                                        service.cleanupLongAction(checkinSync.getTopicId());
                                    } else {
                                        // When async, we can return as soon as all the data has been read
                                        long newTopicId = service.checkinAsync(poid, comment, deserializerOid, -1L, name, ifcFile, merge);
                                        triggerOnCloseInputStream.await();
                                        result.put("topicId", newTopicId);
                                    }
                                } else {
                                    if (sync) {
                                        SLongCheckinActionState checkinSync = service.checkinInitiatedSync(topicId, poid, comment, deserializerOid, -1L, name, ifcFile, merge);
                                        result = (ObjectNode) getBimServer().getJsonHandler().getJsonConverter().toJson(checkinSync);
                                        service.cleanupLongAction(checkinSync.getTopicId());
                                    } else {
                                        service.checkinInitiatedAsync(topicId, poid, comment, deserializerOid, -1L, name, ifcFile, merge);
                                        triggerOnCloseInputStream.await();
                                        result.put("topicId", topicId);
                                    }
                                }
                            }
                        } else {
                            result.put("exception", "No poid");
                        }
                    }
                }
            } catch (MalformedStreamException e) {
                LOGGER.error(comment);
                LOGGER.error("", e);
            }
        }
    } catch (Exception e) {
        LOGGER.error("", e);
        sendException(response, e);
        return;
    }
    response.getWriter().write(result.toString());
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) GZIPInputStream(java.util.zip.GZIPInputStream) InflaterInputStream(java.util.zip.InflaterInputStream) InputStream(java.io.InputStream) InflaterInputStream(java.util.zip.InflaterInputStream) SLongCheckinActionState(org.bimserver.interfaces.objects.SLongCheckinActionState) DataHandler(javax.activation.DataHandler) ServletException(javax.servlet.ServletException) MalformedStreamException(org.apache.commons.fileupload.MultipartStream.MalformedStreamException) IOException(java.io.IOException) GZIPInputStream(java.util.zip.GZIPInputStream) InputStreamDataSource(org.bimserver.utils.InputStreamDataSource) ServletFileUpload(org.apache.commons.fileupload.servlet.ServletFileUpload) FileItemStream(org.apache.commons.fileupload.FileItemStream) ServiceInterface(org.bimserver.shared.interfaces.ServiceInterface) SFile(org.bimserver.interfaces.objects.SFile) MalformedStreamException(org.apache.commons.fileupload.MultipartStream.MalformedStreamException) FileItemIterator(org.apache.commons.fileupload.FileItemIterator)

Example 42 with FileItemStream

use of org.apache.commons.fileupload.FileItemStream in project flow by vaadin.

the class StreamReceiverHandler method handleMultipartFileUploadFromInputStream.

private boolean handleMultipartFileUploadFromInputStream(VaadinSession session, VaadinRequest request, StreamReceiver streamReceiver, StateNode owner) throws IOException {
    boolean success = true;
    long contentLength = getContentLength(request);
    // Parse the request
    FileItemIterator iter;
    try {
        iter = getItemIterator(request);
        while (iter.hasNext()) {
            FileItemStream item = iter.next();
            boolean itemSuccess = handleStream(session, streamReceiver, owner, contentLength, item);
            success = success && itemSuccess;
        }
    } catch (FileUploadException e) {
        success = false;
        getLogger().warn("File upload failed.", e);
    }
    return success;
}
Also used : FileItemStream(org.apache.commons.fileupload.FileItemStream) FileItemIterator(org.apache.commons.fileupload.FileItemIterator) FileUploadException(org.apache.commons.fileupload.FileUploadException)

Example 43 with FileItemStream

use of org.apache.commons.fileupload.FileItemStream in project application by collectionspace.

the class WebUIRequest method initRequest.

private void initRequest(UIUmbrella umbrella, HttpServletRequest request, HttpServletResponse response, List<String> p) throws IOException, UIException {
    this.request = request;
    this.response = response;
    boolean isMultipart = ServletFileUpload.isMultipartContent(request);
    if (isMultipart) {
        // Create a new file upload handler
        ServletFileUpload upload = new ServletFileUpload();
        // Parse the request
        FileItemIterator iter;
        try {
            iter = upload.getItemIterator(request);
            while (iter.hasNext()) {
                FileItemStream item = iter.next();
                String name = item.getFieldName();
                // InputStream stream = item.openStream();
                if (item.isFormField()) {
                // System.out.println("Form field " + name + " with value "
                // + Streams.asString(stream) + " detected.");
                } else {
                    // System.out.println("File field " + name + " with file name "
                    // + item.getName() + " detected.");
                    // Process the input stream
                    contentHeaders = item.getHeaders();
                    uploadName = item.getName();
                    ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
                    if (item != null) {
                        InputStream stream = item.openStream();
                        IOUtils.copy(stream, byteOut);
                        new TeeInputStream(stream, byteOut);
                    }
                    bytebody = byteOut.toByteArray();
                }
            }
        } catch (FileUploadException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    } else {
        body = IOUtils.toString(request.getInputStream(), "UTF-8");
    }
    this.ppath = p.toArray(new String[0]);
    if (!(umbrella instanceof WebUIUmbrella))
        throw new UIException("Bad umbrella");
    this.umbrella = (WebUIUmbrella) umbrella;
    session = calculateSessionId();
}
Also used : FileItemStream(org.apache.commons.fileupload.FileItemStream) TeeInputStream(org.apache.commons.io.input.TeeInputStream) InputStream(java.io.InputStream) UIException(org.collectionspace.csp.api.ui.UIException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) TeeInputStream(org.apache.commons.io.input.TeeInputStream) FileItemIterator(org.apache.commons.fileupload.FileItemIterator) FileUploadException(org.apache.commons.fileupload.FileUploadException)

Example 44 with FileItemStream

use of org.apache.commons.fileupload.FileItemStream in project tutorials by eugenp.

the class UploadController method handleUpload.

@RequestMapping(value = "/upload", method = RequestMethod.POST)
public String handleUpload(HttpServletRequest request) {
    System.out.println(System.getProperty("java.io.tmpdir"));
    boolean isMultipart = ServletFileUpload.isMultipartContent(request);
    // Create a factory for disk-based file items
    DiskFileItemFactory factory = new DiskFileItemFactory();
    factory.setRepository(new File(System.getProperty("java.io.tmpdir")));
    factory.setSizeThreshold(DiskFileItemFactory.DEFAULT_SIZE_THRESHOLD);
    factory.setFileCleaningTracker(null);
    // Configure a repository (to ensure a secure temp location is used)
    ServletFileUpload upload = new ServletFileUpload(factory);
    try {
        // Parse the request
        List<FileItem> items = upload.parseRequest(request);
        // Process the uploaded items
        Iterator<FileItem> iter = items.iterator();
        while (iter.hasNext()) {
            FileItem item = iter.next();
            if (!item.isFormField()) {
                try (InputStream uploadedStream = item.getInputStream();
                    OutputStream out = new FileOutputStream("file.mov")) {
                    IOUtils.copy(uploadedStream, out);
                    out.close();
                }
            }
        }
        // Parse the request with Streaming API
        upload = new ServletFileUpload();
        FileItemIterator iterStream = upload.getItemIterator(request);
        while (iterStream.hasNext()) {
            FileItemStream item = iterStream.next();
            String name = item.getFieldName();
            InputStream stream = item.openStream();
            if (!item.isFormField()) {
            // Process the InputStream
            } else {
                // process form fields
                String formFieldValue = Streams.asString(stream);
            }
        }
        return "success!";
    } catch (IOException | FileUploadException ex) {
        return "failed: " + ex.getMessage();
    }
}
Also used : InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) DiskFileItemFactory(org.apache.commons.fileupload.disk.DiskFileItemFactory) FileItem(org.apache.commons.fileupload.FileItem) ServletFileUpload(org.apache.commons.fileupload.servlet.ServletFileUpload) FileItemStream(org.apache.commons.fileupload.FileItemStream) FileOutputStream(java.io.FileOutputStream) File(java.io.File) FileItemIterator(org.apache.commons.fileupload.FileItemIterator) FileUploadException(org.apache.commons.fileupload.FileUploadException) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 45 with FileItemStream

use of org.apache.commons.fileupload.FileItemStream in project structr by structr.

the class UploadServlet method doPost.

@Override
protected void doPost(final HttpServletRequest request, final HttpServletResponse response) throws ServletException {
    try {
        if (!ServletFileUpload.isMultipartContent(request)) {
            response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
            response.getOutputStream().write("ERROR (400): Request does not contain multipart content.\n".getBytes("UTF-8"));
            return;
        }
    } catch (IOException ioex) {
        logger.warn("Unable to send response", ioex);
    }
    SecurityContext securityContext = null;
    String redirectUrl = null;
    boolean appendUuidOnRedirect = false;
    String path = null;
    // isolate request authentication in a transaction
    try (final Tx tx = StructrApp.getInstance().tx()) {
        try {
            securityContext = getConfig().getAuthenticator().initializeAndExamineRequest(request, response);
        } catch (AuthenticationException ae) {
            response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
            response.getOutputStream().write("ERROR (401): Invalid user or password.\n".getBytes("UTF-8"));
            return;
        }
        tx.success();
    } catch (FrameworkException fex) {
        logger.warn("Unable to examine request", fex);
    } catch (IOException ioex) {
        logger.warn("Unable to send response", ioex);
    }
    // something went wrong, but we don't know what...
    if (securityContext == null) {
        logger.warn("No SecurityContext, aborting.");
        return;
    }
    try {
        if (securityContext.getUser(false) == null && !Settings.UploadAllowAnonymous.getValue()) {
            response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
            response.getOutputStream().write("ERROR (401): Anonymous uploads forbidden.\n".getBytes("UTF-8"));
            return;
        }
        // Ensure access mode is frontend
        securityContext.setAccessMode(AccessMode.Frontend);
        request.setCharacterEncoding("UTF-8");
        // Important: Set character encoding before calling response.getWriter() !!, see Servlet Spec 5.4
        response.setCharacterEncoding("UTF-8");
        // don't continue on redirects
        if (response.getStatus() == 302) {
            return;
        }
        final String pathInfo = request.getPathInfo();
        String type = null;
        if (StringUtils.isNotBlank(pathInfo)) {
            type = SchemaHelper.normalizeEntityName(StringUtils.stripStart(pathInfo.trim(), "/"));
        }
        uploader.setFileSizeMax((long) MEGABYTE * Settings.UploadMaxFileSize.getValue());
        uploader.setSizeMax((long) MEGABYTE * Settings.UploadMaxRequestSize.getValue());
        response.setContentType("text/html");
        FileItemIterator fileItemsIterator = uploader.getItemIterator(request);
        final Map<String, Object> params = new HashMap<>();
        while (fileItemsIterator.hasNext()) {
            final FileItemStream item = fileItemsIterator.next();
            if (item.isFormField()) {
                final String fieldName = item.getFieldName();
                final String fieldValue = IOUtils.toString(item.openStream(), "UTF-8");
                if (REDIRECT_AFTER_UPLOAD_PARAMETER.equals(fieldName)) {
                    redirectUrl = fieldValue;
                } else if (APPEND_UUID_ON_REDIRECT_PARAMETER.equals(fieldName)) {
                    appendUuidOnRedirect = "true".equalsIgnoreCase(fieldValue);
                } else if (UPLOAD_FOLDER_PATH_PARAMETER.equals(fieldName)) {
                    path = fieldValue;
                } else {
                    params.put(fieldName, fieldValue);
                }
            } else {
                try {
                    final String contentType = item.getContentType();
                    boolean isImage = (contentType != null && contentType.startsWith("image"));
                    boolean isVideo = (contentType != null && contentType.startsWith("video"));
                    // Override type from path info
                    if (params.containsKey(NodeInterface.type.jsonName())) {
                        type = (String) params.get(NodeInterface.type.jsonName());
                    }
                    Class cls = null;
                    if (type != null) {
                        cls = SchemaHelper.getEntityClassForRawType(type);
                    }
                    if (cls == null) {
                        if (isImage) {
                            cls = Image.class;
                        } else if (isVideo) {
                            cls = SchemaHelper.getEntityClassForRawType("VideoFile");
                            if (cls == null) {
                                logger.warn("Unable to create entity of type VideoFile, class is not defined.");
                            }
                        } else {
                            cls = File.class;
                        }
                    }
                    if (cls != null) {
                        type = cls.getSimpleName();
                    }
                    final String name = item.getName().replaceAll("\\\\", "/");
                    File newFile = null;
                    String uuid = null;
                    boolean retry = true;
                    while (retry) {
                        retry = false;
                        Folder uploadFolder = null;
                        final String defaultUploadFolderConfigValue = Settings.DefaultUploadFolder.getValue();
                        // If a path attribute was sent, create all folders on the fly.
                        if (path != null) {
                            uploadFolder = getOrCreateFolderPath(securityContext, path);
                        } else if (StringUtils.isNotBlank(defaultUploadFolderConfigValue)) {
                            uploadFolder = getOrCreateFolderPath(SecurityContext.getSuperUserInstance(), defaultUploadFolderConfigValue);
                        }
                        try (final Tx tx = StructrApp.getInstance(securityContext).tx()) {
                            try (final InputStream is = item.openStream()) {
                                newFile = FileHelper.createFile(securityContext, is, contentType, cls, name, uploadFolder);
                                AbstractFile.validateAndRenameFileOnce(newFile, securityContext, null);
                                final PropertyMap changedProperties = new PropertyMap();
                                changedProperties.putAll(PropertyMap.inputTypeToJavaType(securityContext, cls, params));
                                // Update type as it could have changed
                                changedProperties.put(AbstractNode.type, type);
                                newFile.unlockSystemPropertiesOnce();
                                newFile.setProperties(securityContext, changedProperties);
                                uuid = newFile.getUuid();
                            }
                            tx.success();
                        } catch (RetryException rex) {
                            retry = true;
                        }
                    }
                    // only the actual existing file creates a UUID output
                    if (newFile != null) {
                        // upload trigger
                        newFile.notifyUploadCompletion();
                        // send redirect to allow form-based file upload without JavaScript..
                        if (StringUtils.isNotBlank(redirectUrl)) {
                            if (appendUuidOnRedirect) {
                                response.sendRedirect(redirectUrl + uuid);
                            } else {
                                response.sendRedirect(redirectUrl);
                            }
                        } else {
                            // Just write out the uuids of the new files
                            response.getWriter().write(uuid);
                        }
                    }
                } catch (IOException ex) {
                    logger.warn("Could not upload file", ex);
                }
            }
        }
    } catch (Throwable t) {
        final String content;
        if (t instanceof FrameworkException) {
            final FrameworkException fex = (FrameworkException) t;
            logger.error(fex.toString());
            content = errorPage(fex);
        } else {
            logger.error("Exception while processing upload request", t);
            content = errorPage(t);
        }
        try {
            final ServletOutputStream out = response.getOutputStream();
            IOUtils.write(content, out);
        } catch (IOException ex) {
            logger.error("Could not write to response", ex);
        }
    }
}
Also used : Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) AuthenticationException(org.structr.core.auth.exception.AuthenticationException) HashMap(java.util.HashMap) ServletOutputStream(javax.servlet.ServletOutputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) Folder(org.structr.web.entity.Folder) RetryException(org.structr.api.RetryException) PropertyMap(org.structr.core.property.PropertyMap) FileItemStream(org.apache.commons.fileupload.FileItemStream) SecurityContext(org.structr.common.SecurityContext) GraphObject(org.structr.core.GraphObject) FileItemIterator(org.apache.commons.fileupload.FileItemIterator) AbstractFile(org.structr.web.entity.AbstractFile) File(org.structr.web.entity.File)

Aggregations

FileItemStream (org.apache.commons.fileupload.FileItemStream)45 FileItemIterator (org.apache.commons.fileupload.FileItemIterator)44 ServletFileUpload (org.apache.commons.fileupload.servlet.ServletFileUpload)36 IOException (java.io.IOException)28 InputStream (java.io.InputStream)22 FileUploadException (org.apache.commons.fileupload.FileUploadException)19 NotFoundException (org.opencastproject.util.NotFoundException)7 HashMap (java.util.HashMap)6 DiskFileItemFactory (org.apache.commons.fileupload.disk.DiskFileItemFactory)6 IngestException (org.opencastproject.ingest.api.IngestException)6 MediaPackageException (org.opencastproject.mediapackage.MediaPackageException)6 ByteArrayInputStream (java.io.ByteArrayInputStream)5 GZIPInputStream (java.util.zip.GZIPInputStream)5 HttpServletRequest (javax.servlet.http.HttpServletRequest)5 POST (javax.ws.rs.POST)5 Path (javax.ws.rs.Path)5 Produces (javax.ws.rs.Produces)5 WebApplicationException (javax.ws.rs.WebApplicationException)5 SchedulerException (org.opencastproject.scheduler.api.SchedulerException)5 UnauthorizedException (org.opencastproject.security.api.UnauthorizedException)5