Search in sources :

Example 1 with MultiplexingInputStream

use of org.bimserver.utils.MultiplexingInputStream in project BIMserver by opensourceBIM.

the class ServiceImpl method checkinInternal.

private Long checkinInternal(Long topicId, final Long poid, final String comment, Long deserializerOid, Long fileSize, String fileName, InputStream originalInputStream, Boolean merge, Boolean sync, final DatabaseSession session, String username, String userUsername, Project project, Path file, long newServiceId) throws BimserverDatabaseException, UserException, IOException, DeserializeException, CannotBeScheduledException {
    if (getBimServer().getCheckinsInProgress().containsKey(poid)) {
        throw new UserException("Checkin in progress on this project, please try again later");
    }
    getBimServer().getCheckinsInProgress().put(poid, getAuthorization().getUoid());
    DeserializerPluginConfiguration deserializerPluginConfiguration = session.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;
                ObjectType settings = deserializerPluginConfiguration.getSettings();
                Deserializer deserializer = deserializerPlugin.createDeserializer(new PluginConfiguration(settings));
                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);
                LongCheckinAction longAction = new LongCheckinAction(topicId, getBimServer(), username, userUsername, getAuthorization(), checkinDatabaseAction);
                getBimServer().getLongActionManager().start(longAction);
                if (sync) {
                    longAction.waitForCompletion();
                }
                return longAction.getProgressTopic().getKey().getId();
            } else if (plugin instanceof StreamingDeserializerPlugin) {
                StreamingDeserializerPlugin streaminDeserializerPlugin = (StreamingDeserializerPlugin) plugin;
                ObjectType settings = deserializerPluginConfiguration.getSettings();
                StreamingDeserializer streamingDeserializer = streaminDeserializerPlugin.createDeserializer(new PluginConfiguration(settings));
                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);
                LongStreamingCheckinAction longAction = new LongStreamingCheckinAction(topicId, getBimServer(), username, userUsername, getAuthorization(), checkinDatabaseAction);
                getBimServer().getLongActionManager().start(longAction);
                if (sync) {
                    longAction.waitForCompletion();
                }
                return longAction.getProgressTopic().getKey().getId();
            } 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) 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) ObjectType(org.bimserver.models.store.ObjectType) 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) SDeserializerPluginConfiguration(org.bimserver.interfaces.objects.SDeserializerPluginConfiguration) SerializerPluginConfiguration(org.bimserver.models.store.SerializerPluginConfiguration) InternalServicePluginConfiguration(org.bimserver.models.store.InternalServicePluginConfiguration) SQueryEnginePluginConfiguration(org.bimserver.interfaces.objects.SQueryEnginePluginConfiguration) PluginConfiguration(org.bimserver.plugins.PluginConfiguration) DeserializerPluginConfiguration(org.bimserver.models.store.DeserializerPluginConfiguration) SSerializerPluginConfiguration(org.bimserver.interfaces.objects.SSerializerPluginConfiguration) 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 MultiplexingInputStream

use of org.bimserver.utils.MultiplexingInputStream in project BIMserver by opensourceBIM.

the class ServiceImpl method checkinFromUrl.

@Override
public Long checkinFromUrl(Long poid, String comment, Long deserializerOid, String fileName, String urlString, Boolean merge, Boolean sync) throws ServerException, UserException {
    requireAuthenticationAndRunningServer();
    final DatabaseSession session = getBimServer().getDatabase().createSession();
    String username = "Unknown";
    String userUsername = "Unknown";
    try {
        if (getBimServer().getCheckinsInProgress().containsKey(poid)) {
            throw new UserException("Checkin in progress on this project, please try again later");
        }
        User user = (User) session.get(StorePackage.eINSTANCE.getUser(), getAuthorization().getUoid(), OldQuery.getDefault());
        username = user.getName();
        userUsername = user.getUsername();
        Path homeDirIncoming = getBimServer().getHomeDir().resolve("incoming");
        if (!Files.isDirectory(homeDirIncoming)) {
            Files.createDirectory(homeDirIncoming);
        }
        Path userDirIncoming = homeDirIncoming.resolve(userUsername);
        if (!Files.exists(userDirIncoming)) {
            Files.createDirectory(userDirIncoming);
        }
        Project project = session.get(poid, OldQuery.getDefault());
        if (project == null) {
            throw new UserException("No project found with poid " + poid);
        }
        Long topicId = initiateCheckin(poid, deserializerOid);
        URL url = new URL(urlString);
        URLConnection openConnection = url.openConnection();
        InputStream input = openConnection.getInputStream();
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
        if (fileName == null) {
            if (urlString.contains("/")) {
                fileName = urlString.substring(urlString.lastIndexOf("/") + 1);
            } else {
                fileName = urlString;
            }
            if (fileName.contains("?")) {
                fileName = fileName.substring(0, fileName.indexOf("?"));
            }
            fileName = URLDecoder.decode(fileName, Charsets.UTF_8.name());
        } else {
            fileName = dateFormat.format(new Date()) + "-" + fileName;
        }
        Path file = userDirIncoming.resolve(fileName);
        if (fileName.contains(" ")) {
            fileName = fileName.replace(" ", "_");
        }
        return checkinInternal(topicId, poid, comment, deserializerOid, (long) openConnection.getContentLength(), fileName, input, merge, sync, session, username, userUsername, project, file, -1);
    // DeserializerPluginConfiguration deserializerPluginConfiguration = session.get(StorePackage.eINSTANCE.getDeserializerPluginConfiguration(), deserializerOid, OldQuery.getDefault());
    // if (deserializerPluginConfiguration == null) {
    // throw new UserException("Deserializer with oid " + deserializerOid + " not found");
    // }
    // OutputStream outputStream = Files.newOutputStream(file);
    // InputStream inputStream = new MultiplexingInputStream(input, outputStream);
    // DeserializerPlugin deserializerPlugin = (DeserializerPlugin) getBimServer().getPluginManager().getPlugin(deserializerPluginConfiguration.getPluginDescriptor().getPluginClassName(), true);
    // ObjectType settings = deserializerPluginConfiguration.getSettings();
    // 
    // Deserializer deserializer = deserializerPlugin.createDeserializer(new PluginConfiguration(settings));
    // deserializer.init(getBimServer().getDatabase().getMetaDataManager().getPackageMetaData("ifc2x3tc1"));
    // 
    // IfcModelInterface model = deserializer.read(inputStream, fileName, 0, null);
    // 
    // CheckinDatabaseAction checkinDatabaseAction = new CheckinDatabaseAction(getBimServer(), null, getInternalAccessMethod(), poid, getAuthorization(), model, comment, fileName, merge);
    // LongCheckinAction longAction = new LongCheckinAction(-1L, getBimServer(), username, userUsername, getAuthorization(), checkinDatabaseAction);
    // getBimServer().getLongActionManager().start(longAction);
    // if (sync) {
    // longAction.waitForCompletion();
    // }
    // return longAction.getProgressTopic().getKey().getId();
    } catch (UserException e) {
        throw e;
    } catch (Throwable e) {
        LOGGER.error("", e);
        throw new ServerException(e);
    } finally {
        session.close();
    }
}
Also used : Path(java.nio.file.Path) SUser(org.bimserver.interfaces.objects.SUser) User(org.bimserver.models.store.User) ServerException(org.bimserver.shared.exceptions.ServerException) DatabaseSession(org.bimserver.database.DatabaseSession) ByteArrayInputStream(java.io.ByteArrayInputStream) MultiplexingInputStream(org.bimserver.utils.MultiplexingInputStream) InputStream(java.io.InputStream) URL(java.net.URL) URLConnection(java.net.URLConnection) Date(java.util.Date) Project(org.bimserver.models.store.Project) SProject(org.bimserver.interfaces.objects.SProject) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) UserException(org.bimserver.shared.exceptions.UserException) SimpleDateFormat(java.text.SimpleDateFormat)

Aggregations

ByteArrayInputStream (java.io.ByteArrayInputStream)2 InputStream (java.io.InputStream)2 UserException (org.bimserver.shared.exceptions.UserException)2 MultiplexingInputStream (org.bimserver.utils.MultiplexingInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 OutputStream (java.io.OutputStream)1 URL (java.net.URL)1 URLConnection (java.net.URLConnection)1 Path (java.nio.file.Path)1 DateFormat (java.text.DateFormat)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Date (java.util.Date)1 DatabaseSession (org.bimserver.database.DatabaseSession)1 CheckinDatabaseAction (org.bimserver.database.actions.CheckinDatabaseAction)1 StreamingCheckinDatabaseAction (org.bimserver.database.actions.StreamingCheckinDatabaseAction)1 IfcModelInterface (org.bimserver.emf.IfcModelInterface)1 SDeserializerPluginConfiguration (org.bimserver.interfaces.objects.SDeserializerPluginConfiguration)1 SProject (org.bimserver.interfaces.objects.SProject)1 SQueryEnginePluginConfiguration (org.bimserver.interfaces.objects.SQueryEnginePluginConfiguration)1 SSerializerPluginConfiguration (org.bimserver.interfaces.objects.SSerializerPluginConfiguration)1