Search in sources :

Example 1 with FakeClosingInputStream

use of org.opensourcebim.bcf.utils.FakeClosingInputStream in project BIMserver by opensourceBIM.

the class BulkUploadServlet 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 = "";
            while (iter.hasNext()) {
                FileItemStream item = iter.next();
                if (item.isFormField()) {
                    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 {
                    name = item.getName();
                    in = item.openStream();
                    if (poid != -1) {
                        ServiceInterface service = getBimServer().getServiceFactory().get(token, AccessMethod.INTERNAL).get(ServiceInterface.class);
                        SProject mainProject = service.getProjectByPoid(poid);
                        ZipInputStream zipInputStream = new ZipInputStream(in);
                        ZipEntry nextEntry = zipInputStream.getNextEntry();
                        while (nextEntry != null) {
                            String fullfilename = nextEntry.getName();
                            if (fullfilename.toLowerCase().endsWith(".ifc") || fullfilename.toLowerCase().endsWith("ifcxml") || fullfilename.toLowerCase().endsWith(".ifczip")) {
                                InputStreamDataSource inputStreamDataSource = new InputStreamDataSource(new FakeClosingInputStream(zipInputStream));
                                inputStreamDataSource.setName(name);
                                DataHandler ifcFile = new DataHandler(inputStreamDataSource);
                                if (fullfilename.contains("/")) {
                                    String path = fullfilename.substring(0, fullfilename.lastIndexOf("/"));
                                    String filename = fullfilename.substring(fullfilename.lastIndexOf("/") + 1);
                                    String extension = filename.substring(filename.lastIndexOf(".") + 1);
                                    SProject project = getOrCreatePath(service, mainProject, mainProject, path);
                                    SDeserializerPluginConfiguration deserializer = service.getSuggestedDeserializerForExtension(extension, project.getOid());
                                    long topicId = -1;
                                    try {
                                        topicId = service.checkin(project.getOid(), comment, deserializer.getOid(), -1L, filename, ifcFile, false, true);
                                    } finally {
                                        if (topicId != -1) {
                                            service.cleanupLongAction(topicId);
                                        }
                                    }
                                }
                            } else {
                                if (!nextEntry.isDirectory()) {
                                    LOGGER.info("Unknown fileextenstion " + fullfilename);
                                }
                            }
                            nextEntry = zipInputStream.getNextEntry();
                        }
                    // DataHandler ifcFile = new DataHandler(inputStreamDataSource);
                    // 
                    // if (token != null) {
                    // if (topicId == -1) {
                    // long newTopicId = service.checkin(poid, comment, deserializerOid, -1L, name, ifcFile, merge, sync);
                    // result.put("topicId", newTopicId);
                    // } else {
                    // ServiceInterface service = getBimServer().getServiceFactory().get(token, AccessMethod.INTERNAL).get(ServiceInterface.class);
                    // long newTopicId = service.checkinInitiated(topicId, poid, comment, deserializerOid, -1L, name, ifcFile, merge, true);
                    // result.put("topicId", newTopicId);
                    // }
                    // }
                    } else {
                        result.put("exception", "No poid");
                    }
                }
            }
        }
    } catch (Exception e) {
        LOGGER.error("", e);
        // sendException(response, e);
        return;
    }
    response.getWriter().write(result.toString());
}
Also used : SDeserializerPluginConfiguration(org.bimserver.interfaces.objects.SDeserializerPluginConfiguration) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) ZipInputStream(java.util.zip.ZipInputStream) FakeClosingInputStream(org.opensourcebim.bcf.utils.FakeClosingInputStream) InputStream(java.io.InputStream) ZipEntry(java.util.zip.ZipEntry) DataHandler(javax.activation.DataHandler) SProject(org.bimserver.interfaces.objects.SProject) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) UserException(org.bimserver.shared.exceptions.UserException) ServerException(org.bimserver.shared.exceptions.ServerException) InputStreamDataSource(org.bimserver.utils.InputStreamDataSource) ZipInputStream(java.util.zip.ZipInputStream) ServletFileUpload(org.apache.commons.fileupload.servlet.ServletFileUpload) FileItemStream(org.apache.commons.fileupload.FileItemStream) ServiceInterface(org.bimserver.shared.interfaces.ServiceInterface) FileItemIterator(org.apache.commons.fileupload.FileItemIterator) FakeClosingInputStream(org.opensourcebim.bcf.utils.FakeClosingInputStream)

Aggregations

ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 ZipEntry (java.util.zip.ZipEntry)1 ZipInputStream (java.util.zip.ZipInputStream)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 ServletFileUpload (org.apache.commons.fileupload.servlet.ServletFileUpload)1 SDeserializerPluginConfiguration (org.bimserver.interfaces.objects.SDeserializerPluginConfiguration)1 SProject (org.bimserver.interfaces.objects.SProject)1 ServerException (org.bimserver.shared.exceptions.ServerException)1 UserException (org.bimserver.shared.exceptions.UserException)1 ServiceInterface (org.bimserver.shared.interfaces.ServiceInterface)1 InputStreamDataSource (org.bimserver.utils.InputStreamDataSource)1 FakeClosingInputStream (org.opensourcebim.bcf.utils.FakeClosingInputStream)1