Search in sources :

Example 1 with MalformedStreamException

use of org.apache.commons.fileupload.MultipartStream.MalformedStreamException 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)

Aggregations

ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 GZIPInputStream (java.util.zip.GZIPInputStream)1 InflaterInputStream (java.util.zip.InflaterInputStream)1 DataHandler (javax.activation.DataHandler)1 ServletException (javax.servlet.ServletException)1 FileItemIterator (org.apache.commons.fileupload.FileItemIterator)1 FileItemStream (org.apache.commons.fileupload.FileItemStream)1 MalformedStreamException (org.apache.commons.fileupload.MultipartStream.MalformedStreamException)1 ServletFileUpload (org.apache.commons.fileupload.servlet.ServletFileUpload)1 SFile (org.bimserver.interfaces.objects.SFile)1 SLongCheckinActionState (org.bimserver.interfaces.objects.SLongCheckinActionState)1 ServiceInterface (org.bimserver.shared.interfaces.ServiceInterface)1 InputStreamDataSource (org.bimserver.utils.InputStreamDataSource)1