Search in sources :

Example 1 with ProgressTopic

use of org.bimserver.notifications.ProgressTopic in project BIMserver by opensourceBIM.

the class ServiceImpl method checkinInternal.

private SLongCheckinActionState checkinInternal(Long topicId, final Long poid, final String comment, Long deserializerOid, Long fileSize, String fileName, InputStream originalInputStream, Boolean merge, Boolean sync, final DatabaseSession readOnlySession, String username, String userUsername, Project project, Path file, long newServiceId) throws BimserverDatabaseException, IOException, DeserializeException, CannotBeScheduledException, ServiceException {
    DeserializerPluginConfiguration deserializerPluginConfiguration = readOnlySession.get(StorePackage.eINSTANCE.getDeserializerPluginConfiguration(), deserializerOid, OldQuery.getDefault());
    if (deserializerPluginConfiguration == null) {
        throw new UserException("Deserializer with oid " + deserializerOid + " not found");
    } else {
        PluginBundleVersion pluginBundleVersion = deserializerPluginConfiguration.getPluginDescriptor().getPluginBundleVersion();
        Plugin plugin = getBimServer().getPluginManager().getPlugin(deserializerPluginConfiguration.getPluginDescriptor().getPluginClassName(), true);
        if (plugin != null) {
            if (plugin instanceof DeserializerPlugin) {
                DeserializerPlugin deserializerPlugin = (DeserializerPlugin) plugin;
                Deserializer deserializer = deserializerPlugin.createDeserializer(getBimServer().getPluginSettingsCache().getPluginSettings(deserializerOid));
                OutputStream outputStream = Files.newOutputStream(file);
                InputStream inputStream = new MultiplexingInputStream(originalInputStream, outputStream);
                deserializer.init(getBimServer().getDatabase().getMetaDataManager().getPackageMetaData(project.getSchema()));
                IfcModelInterface model = null;
                try {
                    model = deserializer.read(inputStream, fileName, fileSize, null);
                } finally {
                    inputStream.close();
                }
                CheckinDatabaseAction checkinDatabaseAction = new CheckinDatabaseAction(getBimServer(), null, getInternalAccessMethod(), poid, getAuthorization(), model, comment, fileName, merge, newServiceId, topicId);
                LongCheckinAction longAction = new LongCheckinAction(topicId, getBimServer(), username, userUsername, getAuthorization(), checkinDatabaseAction);
                getBimServer().getLongActionManager().start(longAction);
                if (sync) {
                    longAction.waitForCompletion();
                    clearCheckinInProgress(poid);
                }
                ProgressTopic progressTopic = getBimServer().getNotificationsManager().getProgressTopic(topicId);
                return (SLongCheckinActionState) getBimServer().getSConverter().convertToSObject(progressTopic.getLastProgress());
            } else if (plugin instanceof StreamingDeserializerPlugin) {
                StreamingDeserializerPlugin streaminDeserializerPlugin = (StreamingDeserializerPlugin) plugin;
                StreamingDeserializer streamingDeserializer = streaminDeserializerPlugin.createDeserializer(getBimServer().getPluginSettingsCache().getPluginSettings(deserializerPluginConfiguration.getOid()));
                streamingDeserializer.init(getBimServer().getDatabase().getMetaDataManager().getPackageMetaData(project.getSchema()));
                RestartableInputStream restartableInputStream = new RestartableInputStream(originalInputStream, file);
                StreamingCheckinDatabaseAction checkinDatabaseAction = new StreamingCheckinDatabaseAction(getBimServer(), null, getInternalAccessMethod(), poid, getAuthorization(), comment, fileName, restartableInputStream, streamingDeserializer, fileSize, newServiceId, pluginBundleVersion, topicId);
                LongStreamingCheckinAction longAction = new LongStreamingCheckinAction(topicId, getBimServer(), username, userUsername, getAuthorization(), checkinDatabaseAction);
                getBimServer().getLongActionManager().start(longAction);
                ProgressTopic progressTopic = null;
                if (sync) {
                    longAction.waitForCompletion();
                    progressTopic = longAction.getProgressTopic();
                    clearCheckinInProgress(poid);
                    SLongCheckinActionState convertToSObject = (SLongCheckinActionState) getBimServer().getSConverter().convertToSObject(progressTopic.getLastProgress());
                    getBimServer().getLongActionManager().remove(progressTopic.getKey().getId());
                    return convertToSObject;
                } else {
                    return (SLongCheckinActionState) getBimServer().getSConverter().convertToSObject(longAction.getState());
                }
            } else {
                throw new UserException("No (enabled) (streaming) deserializer found with oid " + deserializerOid);
            }
        } else {
            throw new UserException("No (enabled) (streaming) deserializer found with oid " + deserializerOid);
        }
    }
}
Also used : MultiplexingInputStream(org.bimserver.utils.MultiplexingInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) MultiplexingInputStream(org.bimserver.utils.MultiplexingInputStream) InputStream(java.io.InputStream) IfcModelInterface(org.bimserver.emf.IfcModelInterface) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) StreamingDeserializerPlugin(org.bimserver.plugins.deserializers.StreamingDeserializerPlugin) DeserializerPlugin(org.bimserver.plugins.deserializers.DeserializerPlugin) SLongCheckinActionState(org.bimserver.interfaces.objects.SLongCheckinActionState) CheckinDatabaseAction(org.bimserver.database.actions.CheckinDatabaseAction) StreamingCheckinDatabaseAction(org.bimserver.database.actions.StreamingCheckinDatabaseAction) SDeserializerPluginConfiguration(org.bimserver.interfaces.objects.SDeserializerPluginConfiguration) DeserializerPluginConfiguration(org.bimserver.models.store.DeserializerPluginConfiguration) StreamingDeserializer(org.bimserver.plugins.deserializers.StreamingDeserializer) ProgressTopic(org.bimserver.notifications.ProgressTopic) Deserializer(org.bimserver.plugins.deserializers.Deserializer) StreamingDeserializer(org.bimserver.plugins.deserializers.StreamingDeserializer) LongStreamingCheckinAction(org.bimserver.longaction.LongStreamingCheckinAction) StreamingDeserializerPlugin(org.bimserver.plugins.deserializers.StreamingDeserializerPlugin) LongCheckinAction(org.bimserver.longaction.LongCheckinAction) UserException(org.bimserver.shared.exceptions.UserException) PluginBundleVersion(org.bimserver.models.store.PluginBundleVersion) StreamingCheckinDatabaseAction(org.bimserver.database.actions.StreamingCheckinDatabaseAction) StreamingSerializerPlugin(org.bimserver.plugins.serializers.StreamingSerializerPlugin) QueryEnginePlugin(org.bimserver.plugins.queryengine.QueryEnginePlugin) MessagingSerializerPlugin(org.bimserver.plugins.serializers.MessagingSerializerPlugin) StreamingDeserializerPlugin(org.bimserver.plugins.deserializers.StreamingDeserializerPlugin) DeserializerPlugin(org.bimserver.plugins.deserializers.DeserializerPlugin) MessagingStreamingSerializerPlugin(org.bimserver.plugins.serializers.MessagingStreamingSerializerPlugin) SerializerPlugin(org.bimserver.plugins.serializers.SerializerPlugin) Plugin(org.bimserver.plugins.Plugin)

Example 2 with ProgressTopic

use of org.bimserver.notifications.ProgressTopic in project BIMserver by opensourceBIM.

the class BimBotRunner method runBimBot.

public BimBotsOutput runBimBot() throws UserException, IOException {
    BimBotContext bimBotContext = new BimBotContext() {

        @Override
        public void updateProgress(String label, int percentage) {
            if (streamingSocketInterface != null) {
                ObjectNode message = objectMapper.createObjectNode();
                message.put("type", "progress");
                message.put("topicId", topicId);
                ObjectNode payload = objectMapper.createObjectNode();
                payload.put("progress", percentage);
                payload.put("label", label);
                message.set("payload", payload);
                streamingSocketInterface.send(message);
            }
        }

        public String getCurrentUser() {
            return authorization.getUsername();
        }

        public String getContextId() {
            return contextId;
        }
    };
    try (DatabaseSession session = bimServer.getDatabase().createSession(OperationType.READ_ONLY)) {
        ServiceMap serviceMap = bimServer.getServiceFactory().get(authorization, AccessMethod.INTERNAL);
        ServiceInterface serviceInterface = serviceMap.get(ServiceInterface.class);
        if (bimServer.getServerSettingsCache().getServerSettings().isStoreServiceRuns()) {
            long start = System.nanoTime();
            // When we store service runs, we can just use the streaming deserializer to stream directly to the database, after that we'll trigger the actual service
            // Create or find project and link user and service to project
            // Checkin stream into project
            // Trigger service
            byte[] data = null;
            if (bimBotsServiceInterface.needsRawInput()) {
                // We need the raw input later on, lets just get it now
                data = ByteStreams.toByteArray(inputStream);
                // Make a clean inputstream that uses the data as the original won't be available after this
                inputStream = new ByteArrayInputStream(data);
            }
            SchemaName schema = SchemaName.valueOf(inputType);
            String projectSchema = getProjectSchema(serviceInterface, schema);
            SProject project = null;
            String uuid = contextId;
            if (uuid != null) {
                project = serviceInterface.getProjectByUuid(uuid);
            } else {
                project = serviceInterface.addProject("tmp-" + new Random().nextInt(), projectSchema);
            }
            SDeserializerPluginConfiguration deserializer = serviceInterface.getSuggestedDeserializerForExtension("ifc", project.getOid());
            if (deserializer == null) {
                throw new BimBotsException("No deserializer found", BimBotDefaultErrorCode.NO_DESERIALIZER);
            }
            Long topicId = serviceInterface.initiateCheckin(project.getOid(), deserializer.getOid());
            ProgressTopic progressTopic = null;
            VirtualEndPoint virtualEndpoint = null;
            try {
                progressTopic = bimServer.getNotificationsManager().getProgressTopic(topicId);
                virtualEndpoint = new VirtualEndPoint() {

                    @Override
                    public NotificationInterface getNotificationInterface() {
                        return new NotificationInterfaceAdaptor() {

                            @Override
                            public void progress(Long topicId, SLongActionState state) throws UserException, ServerException {
                                bimBotContext.updateProgress(state.getTitle(), state.getProgress());
                            }
                        };
                    }
                };
                progressTopic.register(virtualEndpoint);
            } catch (TopicRegisterException e1) {
                e1.printStackTrace();
            }
            serviceInterface.checkinInitiatedSync(topicId, project.getOid(), "Auto checkin", deserializer.getOid(), -1L, "s", new DataHandler(new InputStreamDataSource(inputStream)), false);
            project = serviceInterface.getProjectByPoid(project.getOid());
            PackageMetaData packageMetaData = bimServer.getMetaDataManager().getPackageMetaData(project.getSchema());
            BasicIfcModel model = new BasicIfcModel(packageMetaData, null);
            model.setPluginClassLoaderProvider(bimServer.getPluginManager());
            try {
                Revision revision = session.get(project.getLastRevisionId(), OldQuery.getDefault());
                session.getMap(model, new OldQuery(packageMetaData, project.getId(), revision.getId(), revision.getOid(), Deep.NO));
                model.getModelMetaData().setIfcHeader(revision.getLastConcreteRevision().getIfcHeader());
            } catch (BimserverDatabaseException e) {
                e.printStackTrace();
            }
            BimServerBimBotsInput input = new BimServerBimBotsInput(bimServer, authorization.getUoid(), inputType, data, model, false);
            BimBotsOutput output = bimBotsServiceInterface.runBimBot(input, bimBotContext, settings);
            long end = System.nanoTime();
            if (output.getModel() != null) {
                SerializerPlugin plugin = bimServer.getPluginManager().getSerializerPlugin("org.bimserver.ifc.step.serializer.Ifc2x3tc1StepSerializerPlugin");
                Serializer serializer = plugin.createSerializer(new PluginConfiguration());
                serializer.init(output.getModel(), new ProjectInfo(), true);
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                serializer.writeToOutputStream(baos, null);
                output.setData(baos.toByteArray());
                output.setContentType("application/ifc");
            }
            SExtendedData extendedData = new SExtendedData();
            SFile file = new SFile();
            file.setData(output.getData());
            file.setFilename(output.getContentDisposition());
            file.setMime(output.getContentType());
            file.setSize(output.getData().length);
            Long fileId = serviceInterface.uploadFile(file);
            long ms = (end - start) / 1000000;
            extendedData.setTimeToGenerate(ms);
            extendedData.setFileId(fileId);
            extendedData.setTitle(output.getTitle());
            SExtendedDataSchema extendedDataSchema = null;
            try {
                extendedDataSchema = serviceInterface.getExtendedDataSchemaByName(output.getSchemaName());
            } catch (UserException e) {
                extendedDataSchema = new SExtendedDataSchema();
                extendedDataSchema.setContentType(output.getContentType());
                extendedDataSchema.setName(output.getSchemaName());
                serviceInterface.addExtendedDataSchema(extendedDataSchema);
            }
            extendedData.setSchemaId(extendedDataSchema.getOid());
            serviceInterface.addExtendedDataToRevision(project.getLastRevisionId(), extendedData);
            output.setContextId(project.getUuid().toString());
            if (progressTopic != null) {
                try {
                    progressTopic.unregister(virtualEndpoint);
                } catch (TopicRegisterException e) {
                    e.printStackTrace();
                }
            }
            return output;
        } else {
            // When we don't store the service runs, there is no other way than to just use the old deserializer and run the service from the EMF model
            LOGGER.info("NOT Storing intermediate results");
            String projectSchema = getProjectSchema(serviceInterface, SchemaName.valueOf(inputType));
            Schema schema = Schema.valueOf(projectSchema.toUpperCase());
            DeserializerPlugin deserializerPlugin = bimServer.getPluginManager().getFirstDeserializer("ifc", schema, true);
            if (deserializerPlugin == null) {
                throw new BimBotsException("No deserializer plugin found", BimBotDefaultErrorCode.NO_DESERIALIZER);
            }
            byte[] data = IOUtils.toByteArray(inputStream);
            Deserializer deserializer = deserializerPlugin.createDeserializer(new PluginConfiguration());
            PackageMetaData packageMetaData = bimServer.getMetaDataManager().getPackageMetaData(schema.name());
            deserializer.init(packageMetaData);
            IfcModelInterface model = deserializer.read(new ByteArrayInputStream(data), inputType, data.length, null);
            BimServerBimBotsInput input = new BimServerBimBotsInput(bimServer, authorization.getUoid(), inputType, data, model, true);
            BimBotsOutput output = bimBotsServiceInterface.runBimBot(input, bimBotContext, new PluginConfiguration(foundService.getSettings()));
            return output;
        }
    } catch (BimBotsException e) {
        ObjectNode errorNode = OBJECT_MAPPER.createObjectNode();
        errorNode.put("code", e.getErrorCode());
        errorNode.put("message", e.getMessage());
        BimBotsOutput bimBotsOutput = new BimBotsOutput("ERROR", errorNode.toString().getBytes(Charsets.UTF_8));
        return bimBotsOutput;
    } catch (Throwable e) {
        LOGGER.error("", e);
        ObjectNode errorNode = OBJECT_MAPPER.createObjectNode();
        errorNode.put("code", 500);
        // TODO should not leak this much info to called, but for now this is useful debugging info
        errorNode.put("message", "Unknown error: " + e.getMessage());
        BimBotsOutput bimBotsOutput = new BimBotsOutput("ERROR", errorNode.toString().getBytes(Charsets.UTF_8));
        return bimBotsOutput;
    }
}
Also used : SDeserializerPluginConfiguration(org.bimserver.interfaces.objects.SDeserializerPluginConfiguration) ServiceMap(org.bimserver.webservices.ServiceMap) DatabaseSession(org.bimserver.database.DatabaseSession) IfcModelInterface(org.bimserver.emf.IfcModelInterface) SExtendedDataSchema(org.bimserver.interfaces.objects.SExtendedDataSchema) Schema(org.bimserver.emf.Schema) SLongActionState(org.bimserver.interfaces.objects.SLongActionState) DataHandler(javax.activation.DataHandler) SProject(org.bimserver.interfaces.objects.SProject) SExtendedDataSchema(org.bimserver.interfaces.objects.SExtendedDataSchema) InputStreamDataSource(org.bimserver.utils.InputStreamDataSource) BimserverDatabaseException(org.bimserver.BimserverDatabaseException) TopicRegisterException(org.bimserver.notifications.TopicRegisterException) Random(java.util.Random) BimBotsServiceInterface(org.bimserver.bimbots.BimBotsServiceInterface) ServiceInterface(org.bimserver.shared.interfaces.ServiceInterface) ProjectInfo(org.bimserver.plugins.serializers.ProjectInfo) InternalServicePluginConfiguration(org.bimserver.models.store.InternalServicePluginConfiguration) SDeserializerPluginConfiguration(org.bimserver.interfaces.objects.SDeserializerPluginConfiguration) PluginConfiguration(org.bimserver.plugins.PluginConfiguration) UserException(org.bimserver.shared.exceptions.UserException) VirtualEndPoint(org.bimserver.endpoints.VirtualEndPoint) SFile(org.bimserver.interfaces.objects.SFile) Serializer(org.bimserver.plugins.serializers.Serializer) ServerException(org.bimserver.shared.exceptions.ServerException) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) PackageMetaData(org.bimserver.emf.PackageMetaData) DeserializerPlugin(org.bimserver.plugins.deserializers.DeserializerPlugin) BimBotsException(org.bimserver.bimbots.BimBotsException) SerializerPlugin(org.bimserver.plugins.serializers.SerializerPlugin) ByteArrayOutputStream(org.apache.commons.io.output.ByteArrayOutputStream) BasicIfcModel(org.bimserver.ifc.BasicIfcModel) BimServerBimBotsInput(org.bimserver.bimbots.BimServerBimBotsInput) OldQuery(org.bimserver.database.OldQuery) ProgressTopic(org.bimserver.notifications.ProgressTopic) Revision(org.bimserver.models.store.Revision) SExtendedData(org.bimserver.interfaces.objects.SExtendedData) ByteArrayInputStream(java.io.ByteArrayInputStream) BimBotContext(org.bimserver.bimbots.BimBotContext) Deserializer(org.bimserver.plugins.deserializers.Deserializer) BimBotsOutput(org.bimserver.bimbots.BimBotsOutput) NotificationInterface(org.bimserver.shared.interfaces.NotificationInterface) NotificationInterfaceAdaptor(org.bimserver.shared.interfaces.NotificationInterfaceAdaptor) SchemaName(org.bimserver.plugins.SchemaName)

Example 3 with ProgressTopic

use of org.bimserver.notifications.ProgressTopic in project BIMserver by opensourceBIM.

the class DownloadServlet method service.

@Override
public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    try {
        String acceptEncoding = request.getHeader("Accept-Encoding");
        boolean useGzip = false;
        if (acceptEncoding != null && acceptEncoding.contains("gzip")) {
            useGzip = true;
        }
        OutputStream outputStream = response.getOutputStream();
        boolean zip = request.getParameter("zip") != null && request.getParameter("zip").equals("on");
        if (useGzip && !zip) {
            response.setHeader("Content-Encoding", "gzip");
            outputStream = new GZIPOutputStream(response.getOutputStream());
        }
        String token = (String) request.getSession().getAttribute("token");
        if (token == null) {
            token = request.getParameter("token");
        }
        long topicId = -1;
        if (request.getParameter("topicId") != null) {
            topicId = Long.parseLong(request.getParameter("topicId"));
        }
        ServiceMap serviceMap = getBimServer().getServiceFactory().get(token, AccessMethod.INTERNAL);
        String action = request.getParameter("action");
        if (action != null) {
            if (action.equals("extendeddata")) {
                SExtendedData sExtendedData = serviceMap.getServiceInterface().getExtendedData(Long.parseLong(request.getParameter("edid")));
                SFile file = serviceMap.getServiceInterface().getFile(sExtendedData.getFileId());
                if (file.getMime() != null) {
                    response.setContentType(file.getMime());
                }
                if (file.getFilename() != null) {
                    response.setHeader("Content-Disposition", "inline; filename=\"" + file.getFilename() + "\"");
                }
                outputStream.write(file.getData());
                if (outputStream instanceof GZIPOutputStream) {
                    ((GZIPOutputStream) outputStream).finish();
                }
                outputStream.flush();
                return;
            } else if (action.equals("getfile")) {
                String type = request.getParameter("type");
                if (type.equals("proto")) {
                    try {
                        String protocolBuffersFile = serviceMap.getAdminInterface().getProtocolBuffersFile(request.getParameter("name"));
                        outputStream.write(protocolBuffersFile.getBytes(Charsets.UTF_8));
                        outputStream.flush();
                    } catch (ServiceException e) {
                        LOGGER.error("", e);
                    }
                } else if (type.equals("serverlog")) {
                    try {
                        OutputStreamWriter writer = new OutputStreamWriter(outputStream);
                        writer.write(serviceMap.getAdminInterface().getServerLog());
                        writer.flush();
                    } catch (ServerException e) {
                        LOGGER.error("", e);
                    } catch (UserException e) {
                        LOGGER.error("", e);
                    }
                }
            } else if (action.equals("getBcfImage")) {
                long extendedDataId = Long.parseLong(request.getParameter("extendedDataId"));
                String topicUuid = request.getParameter("topicUuid");
                String name = request.getParameter("name");
                BcfFile bcfFile = BcfCache.INSTANCE.get(extendedDataId);
                if (bcfFile == null) {
                    SExtendedData extendedData = serviceMap.getServiceInterface().getExtendedData(extendedDataId);
                    long fileId = extendedData.getFileId();
                    SFile file = serviceMap.getServiceInterface().getFile(fileId);
                    try {
                        bcfFile = BcfFile.read(new ByteArrayInputStream(file.getData()), new ReadOptions(false));
                        BcfCache.INSTANCE.put(extendedDataId, bcfFile);
                    } catch (BcfException e) {
                        e.printStackTrace();
                    }
                }
                TopicFolder topicFolder = bcfFile.getTopicFolder(topicUuid);
                if (topicFolder != null) {
                    byte[] data = topicFolder.getSnapshot(topicUuid + "/" + name);
                    if (data != null) {
                        response.setContentType("image/png");
                        IOUtils.write(data, outputStream);
                        if (outputStream instanceof GZIPOutputStream) {
                            ((GZIPOutputStream) outputStream).finish();
                        }
                        outputStream.flush();
                        return;
                    }
                }
            }
        } else {
            if (request.getParameter("topicId") != null) {
                topicId = Long.parseLong(request.getParameter("topicId"));
            }
            if (topicId == -1) {
                response.getWriter().println("No valid topicId");
                return;
            }
            SDownloadResult checkoutResult = serviceMap.getServiceInterface().getDownloadData(topicId);
            if (checkoutResult == null) {
                LOGGER.error("Invalid topicId: " + topicId);
            } else {
                DataSource dataSource = checkoutResult.getFile().getDataSource();
                PluginConfiguration pluginConfiguration = getBimServer().getPluginSettingsCache().getPluginSettings(checkoutResult.getSerializerOid());
                final ProgressTopic progressTopic = getBimServer().getNotificationsManager().getProgressTopic(topicId);
                ProgressReporter progressReporter = new ProgressReporter() {

                    private long lastMax;

                    private long lastProgress;

                    private int stage = 3;

                    private Date start = new Date();

                    private String title = "Downloading...";

                    @Override
                    public void update(long progress, long max) {
                        if (progressTopic != null) {
                            LongActionState ds = StoreFactory.eINSTANCE.createLongActionState();
                            ds.setStart(start);
                            ds.setState(progress == max ? ActionState.FINISHED : ActionState.STARTED);
                            ds.setTitle(title);
                            ds.setStage(stage);
                            ds.setProgress((int) Math.round(100.0 * progress / max));
                            progressTopic.stageProgressUpdate(ds);
                            this.lastMax = max;
                            this.lastProgress = progress;
                        }
                    }

                    @Override
                    public void setTitle(String title) {
                        if (progressTopic != null) {
                            stage++;
                            this.title = title;
                            LongActionState ds = StoreFactory.eINSTANCE.createLongActionState();
                            ds.setStart(new Date());
                            ds.setState(lastProgress == lastMax ? ActionState.FINISHED : ActionState.STARTED);
                            ds.setTitle(title);
                            ds.setStage(stage);
                            ds.setProgress((int) Math.round(100.0 * lastProgress / lastMax));
                            progressTopic.stageProgressUpdate(ds);
                        }
                    }
                };
                try {
                    if (zip) {
                        if (pluginConfiguration.getString("ZipExtension") != null) {
                            response.setHeader("Content-Disposition", "inline; filename=\"" + dataSource.getName() + "." + pluginConfiguration.getString(SerializerPlugin.ZIP_EXTENSION) + "\"");
                        } else {
                            response.setHeader("Content-Disposition", "inline; filename=\"" + dataSource.getName() + ".zip" + "\"");
                        }
                        response.setContentType("application/zip");
                        String nameInZip = dataSource.getName() + "." + pluginConfiguration.getString(SerializerPlugin.EXTENSION);
                        ZipOutputStream zipOutputStream = new ZipOutputStream(outputStream);
                        zipOutputStream.putNextEntry(new ZipEntry(nameInZip));
                        processDataSource(zipOutputStream, dataSource, progressReporter);
                        try {
                            zipOutputStream.finish();
                        } catch (IOException e) {
                        // Sometimes it's already closed, that's no problem
                        }
                    } else {
                        if (request.getParameter("mime") == null) {
                            response.setContentType(pluginConfiguration.getString(SerializerPlugin.CONTENT_TYPE));
                            response.setHeader("Content-Disposition", "inline; filename=\"" + dataSource.getName() + "." + pluginConfiguration.getString(SerializerPlugin.EXTENSION) + "\"");
                        } else {
                            response.setContentType(request.getParameter("mime"));
                        }
                        processDataSource(outputStream, dataSource, progressReporter);
                    }
                } catch (SerializerException s) {
                    if (s.getCause() != null && s.getCause() instanceof IOException) {
                    } else {
                        LOGGER.error("", s);
                    }
                    LongActionState ds = StoreFactory.eINSTANCE.createLongActionState();
                    ds.setStart(new Date());
                    ds.setState(ActionState.AS_ERROR);
                    ds.setTitle(s.getMessage());
                    ds.setProgress(-1);
                    ds.setStage(3);
                    ds.getErrors().add(s.getMessage());
                    progressTopic.stageProgressUpdate(ds);
                }
            }
        }
        if (outputStream instanceof GZIPOutputStream) {
            ((GZIPOutputStream) outputStream).finish();
        }
        outputStream.flush();
    } catch (NumberFormatException e) {
        LOGGER.error("", e);
        response.getWriter().println("Some number was incorrectly formatted");
    } catch (ServiceException e) {
        LOGGER.error("", e);
        response.getWriter().println(e.getUserMessage());
    } catch (EOFException e) {
    } catch (Exception e) {
        LOGGER.error("", e);
    }
}
Also used : ServiceMap(org.bimserver.webservices.ServiceMap) LongActionState(org.bimserver.models.store.LongActionState) ZipOutputStream(java.util.zip.ZipOutputStream) OutputStream(java.io.OutputStream) GZIPOutputStream(java.util.zip.GZIPOutputStream) ZipEntry(java.util.zip.ZipEntry) BcfFile(org.opensourcebim.bcf.BcfFile) GZIPOutputStream(java.util.zip.GZIPOutputStream) ReadOptions(org.opensourcebim.bcf.ReadOptions) EOFException(java.io.EOFException) PluginConfiguration(org.bimserver.plugins.PluginConfiguration) UserException(org.bimserver.shared.exceptions.UserException) SFile(org.bimserver.interfaces.objects.SFile) ServerException(org.bimserver.shared.exceptions.ServerException) TopicFolder(org.opensourcebim.bcf.TopicFolder) BcfException(org.opensourcebim.bcf.BcfException) SDownloadResult(org.bimserver.interfaces.objects.SDownloadResult) ProgressReporter(org.bimserver.plugins.serializers.ProgressReporter) IOException(java.io.IOException) SerializerException(org.bimserver.plugins.serializers.SerializerException) Date(java.util.Date) ServletException(javax.servlet.ServletException) ServiceException(org.bimserver.shared.exceptions.ServiceException) SerializerException(org.bimserver.plugins.serializers.SerializerException) IOException(java.io.IOException) EOFException(java.io.EOFException) BcfException(org.opensourcebim.bcf.BcfException) UserException(org.bimserver.shared.exceptions.UserException) ServerException(org.bimserver.shared.exceptions.ServerException) ExtendedDataSource(org.bimserver.plugins.serializers.ExtendedDataSource) DataSource(javax.activation.DataSource) ProgressTopic(org.bimserver.notifications.ProgressTopic) SExtendedData(org.bimserver.interfaces.objects.SExtendedData) ServiceException(org.bimserver.shared.exceptions.ServiceException) ByteArrayInputStream(java.io.ByteArrayInputStream) ZipOutputStream(java.util.zip.ZipOutputStream) OutputStreamWriter(java.io.OutputStreamWriter)

Example 4 with ProgressTopic

use of org.bimserver.notifications.ProgressTopic in project BIMserver by opensourceBIM.

the class NotificationRegistryServiceImpl method registerProgressHandler.

@Override
public void registerProgressHandler(Long topicId, Long endPointId) throws UserException {
    if (endPointId == null) {
        throw new UserException("No EndpointId given", DefaultErrorCode.ENDPOINT_NOT_FOUND);
    }
    EndPoint endPoint = getEndPoint(endPointId);
    if (endPoint == null) {
        throw new UserException("Endpoint with id " + endPointId + " not found", DefaultErrorCode.ENDPOINT_NOT_FOUND);
    }
    ProgressTopic progressTopic = getBimServer().getNotificationsManager().getProgressTopic(topicId);
    if (progressTopic == null) {
        throw new UserException("Topic with id " + topicId + " not found");
    }
    try {
        progressTopic.register(endPoint);
        LongActionState lastProgress = progressTopic.getLastProgress();
        if (lastProgress != null && lastProgress.getState() == ActionState.FINISHED) {
            LoggerFactory.getLogger(NotificationRegistryInterface.class).debug("Sending update directly for topic " + progressTopic.getKey().getId());
        // TODO!!
        // progressTopic.stageProgressUpdate(ate, title)updateProgress(lastProgress);
        } else {
            LoggerFactory.getLogger(NotificationRegistryInterface.class).debug("NOT Sending update directly for topic " + progressTopic.getKey().getId());
        }
    } catch (TopicRegisterException e) {
        throw new UserException(e);
    }
}
Also used : ProgressTopic(org.bimserver.notifications.ProgressTopic) TopicRegisterException(org.bimserver.notifications.TopicRegisterException) SLongActionState(org.bimserver.interfaces.objects.SLongActionState) LongActionState(org.bimserver.models.store.LongActionState) UserException(org.bimserver.shared.exceptions.UserException) EndPoint(org.bimserver.endpoints.EndPoint) NotificationRegistryInterface(org.bimserver.shared.interfaces.NotificationRegistryInterface)

Example 5 with ProgressTopic

use of org.bimserver.notifications.ProgressTopic in project BIMserver by opensourceBIM.

the class NotificationRegistryServiceImpl method unregisterProgressHandler.

@Override
public void unregisterProgressHandler(Long topicId, Long endPointId) throws UserException, ServerException {
    EndPoint endPoint = getEndPoint(endPointId);
    ProgressTopic progressTopic = getBimServer().getNotificationsManager().getProgressTopic(topicId);
    if (progressTopic == null) {
        throw new UserException("Topic with id " + topicId + " not found");
    }
    try {
        progressTopic.unregister(endPoint);
    } catch (TopicRegisterException e) {
        handleException(e);
    }
}
Also used : ProgressTopic(org.bimserver.notifications.ProgressTopic) TopicRegisterException(org.bimserver.notifications.TopicRegisterException) EndPoint(org.bimserver.endpoints.EndPoint) UserException(org.bimserver.shared.exceptions.UserException)

Aggregations

ProgressTopic (org.bimserver.notifications.ProgressTopic)7 UserException (org.bimserver.shared.exceptions.UserException)6 TopicRegisterException (org.bimserver.notifications.TopicRegisterException)4 ServerException (org.bimserver.shared.exceptions.ServerException)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 BimserverDatabaseException (org.bimserver.BimserverDatabaseException)3 OutputStream (java.io.OutputStream)2 DatabaseSession (org.bimserver.database.DatabaseSession)2 IfcModelInterface (org.bimserver.emf.IfcModelInterface)2 EndPoint (org.bimserver.endpoints.EndPoint)2 SExtendedData (org.bimserver.interfaces.objects.SExtendedData)2 SFile (org.bimserver.interfaces.objects.SFile)2 SLongActionState (org.bimserver.interfaces.objects.SLongActionState)2 LongActionState (org.bimserver.models.store.LongActionState)2 Revision (org.bimserver.models.store.Revision)2 PluginConfiguration (org.bimserver.plugins.PluginConfiguration)2 ServiceMap (org.bimserver.webservices.ServiceMap)2 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 EOFException (java.io.EOFException)1