Search in sources :

Example 1 with SLongActionState

use of org.bimserver.interfaces.objects.SLongActionState in project BIMserver by opensourceBIM.

the class MultiCheckinAndDownload method test.

@Test
public void test() {
    try {
        // Create a new BimServerClient with authentication
        BimServerClientInterface bimServerClient = getFactory().create(new UsernamePasswordAuthenticationInfo("admin@bimserver.org", "admin"));
        long s = System.nanoTime();
        for (int i = 0; i < 3; i++) {
            // Create a new project
            SProject newProject = bimServerClient.getServiceInterface().addProject("test" + Math.random(), "ifc2x3tc1");
            // This is the file we will be checking in
            File ifcFile = new File("../TestData/data/AC11-FZK-Haus-IFC.ifc");
            // Find a deserializer to use
            SDeserializerPluginConfiguration deserializer = bimServerClient.getServiceInterface().getSuggestedDeserializerForExtension("ifc", newProject.getOid());
            // Checkin
            Long progressId = bimServerClient.getServiceInterface().checkin(newProject.getOid(), "test", deserializer.getOid(), ifcFile.length(), ifcFile.getName(), new DataHandler(new FileDataSource(ifcFile)), false, true);
            // Get the status
            SLongActionState longActionState = bimServerClient.getRegistry().getProgress(progressId);
            if (longActionState.getState() == SActionState.FINISHED) {
                // Find a serializer
                SSerializerPluginConfiguration serializer = bimServerClient.getServiceInterface().getSerializerByContentType("application/ifc");
                // Get the project details
                newProject = bimServerClient.getServiceInterface().getProjectByPoid(newProject.getOid());
                // Download the latest revision  (the one we just checked in)
                Long topicId = bimServerClient.getServiceInterface().download(Collections.singleton(newProject.getLastRevisionId()), DefaultQueries.allAsString(), serializer.getOid(), true);
                SLongActionState downloadState = bimServerClient.getRegistry().getProgress(topicId);
                if (downloadState.getState() == SActionState.FINISHED) {
                    // Success
                    System.out.println("Success");
                }
            }
        }
        long e = System.nanoTime();
        System.out.println(((e - s) / 1000000) + " ms");
    } catch (Exception e) {
        fail(e.getMessage());
    }
}
Also used : SDeserializerPluginConfiguration(org.bimserver.interfaces.objects.SDeserializerPluginConfiguration) UsernamePasswordAuthenticationInfo(org.bimserver.shared.UsernamePasswordAuthenticationInfo) SLongActionState(org.bimserver.interfaces.objects.SLongActionState) DataHandler(javax.activation.DataHandler) SProject(org.bimserver.interfaces.objects.SProject) FileDataSource(javax.activation.FileDataSource) BimServerClientInterface(org.bimserver.plugins.services.BimServerClientInterface) SSerializerPluginConfiguration(org.bimserver.interfaces.objects.SSerializerPluginConfiguration) File(java.io.File) Test(org.junit.Test)

Example 2 with SLongActionState

use of org.bimserver.interfaces.objects.SLongActionState in project BIMserver by opensourceBIM.

the class SingleCheckinAndDownload method test.

@Test
public void test() {
    try {
        // Create a new BimServerClient with authentication
        BimServerClientInterface bimServerClient = getFactory().create(new UsernamePasswordAuthenticationInfo("admin@bimserver.org", "admin"));
        // When you use the channel for checkin/download calls, all data will have to be converted to the used channel's format, for example for JSON, binary data will be Base64 encoded, which will make things slower and larger
        // The alternative is to use the Servlets, those will also use compression where possible
        // Using the channel is slower
        boolean useChannel = false;
        // Create a new project
        SProject newProject = bimServerClient.getServiceInterface().addProject("test" + Math.random(), "ifc2x3tc1");
        // Find a deserializer to use
        SDeserializerPluginConfiguration deserializer = bimServerClient.getServiceInterface().getSuggestedDeserializerForExtension("ifc", newProject.getOid());
        // Checkin
        Long progressId = -1L;
        // if (useChannel) {
        // progressId = bimServerClient.getServiceInterface().checkin(newProject.getOid(), "test", deserializer.getOid(), ifcFile.toFile().length(), ifcFile.getFileName().toString(), new DataHandler(new FileDataSource(ifcFile.toFile())), true, true);
        // } else {
        progressId = bimServerClient.checkin(newProject.getOid(), "test", deserializer.getOid(), false, Flow.SYNC, new URL("https://github.com/opensourceBIM/TestFiles/raw/master/TestData/data/AC11-Institute-Var-2-IFC.ifc"));
        // }
        // Get the status
        SLongActionState longActionState = bimServerClient.getRegistry().getProgress(progressId);
        if (longActionState.getState() == SActionState.FINISHED) {
            // Find a serializer
            SSerializerPluginConfiguration serializer = bimServerClient.getServiceInterface().getSerializerByContentType("application/ifc");
            // Get the project details
            newProject = bimServerClient.getServiceInterface().getProjectByPoid(newProject.getOid());
            // Download the latest revision  (the one we just checked in)
            if (useChannel) {
                Long topicId = bimServerClient.getServiceInterface().download(Collections.singleton(newProject.getLastRevisionId()), DefaultQueries.allAsString(), serializer.getOid(), true);
                SLongActionState downloadState = bimServerClient.getRegistry().getProgress(topicId);
                if (downloadState.getState() == SActionState.FINISHED) {
                    InputStream inputStream = bimServerClient.getServiceInterface().getDownloadData(topicId).getFile().getInputStream();
                    IOUtils.copy(inputStream, new ByteArrayOutputStream());
                    System.out.println("Success");
                }
            } else {
                // Note: sync: false
                Long topicId = bimServerClient.getServiceInterface().download(Collections.singleton(newProject.getLastRevisionId()), DefaultQueries.allAsString(), serializer.getOid(), false);
                InputStream downloadData = bimServerClient.getDownloadData(topicId);
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                IOUtils.copy(downloadData, baos);
                System.out.println(baos.size() + " bytes downloaded");
            }
        } else {
            System.out.println(longActionState.getState());
        }
    } catch (Exception e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
}
Also used : SDeserializerPluginConfiguration(org.bimserver.interfaces.objects.SDeserializerPluginConfiguration) UsernamePasswordAuthenticationInfo(org.bimserver.shared.UsernamePasswordAuthenticationInfo) InputStream(java.io.InputStream) BimServerClientInterface(org.bimserver.plugins.services.BimServerClientInterface) SLongActionState(org.bimserver.interfaces.objects.SLongActionState) SSerializerPluginConfiguration(org.bimserver.interfaces.objects.SSerializerPluginConfiguration) ByteArrayOutputStream(java.io.ByteArrayOutputStream) SProject(org.bimserver.interfaces.objects.SProject) URL(java.net.URL) Test(org.junit.Test)

Example 3 with SLongActionState

use of org.bimserver.interfaces.objects.SLongActionState in project BIMserver by opensourceBIM.

the class AbstractService method register.

@Override
public void register(long uoid, SInternalServicePluginConfiguration internalService, final PluginConfiguration pluginConfiguration) {
    name = internalService.getName();
    ServiceDescriptor serviceDescriptor = StoreFactory.eINSTANCE.createServiceDescriptor();
    serviceDescriptor.setProviderName("BIMserver");
    serviceDescriptor.setIdentifier("" + internalService.getOid());
    serviceDescriptor.setName(internalService.getName());
    serviceDescriptor.setDescription(internalService.getDescription());
    serviceDescriptor.setNotificationProtocol(AccessMethod.INTERNAL);
    serviceDescriptor.setTrigger(Trigger.NEW_REVISION);
    addRequiredRights(serviceDescriptor);
    serviceDescriptor.setReadRevision(true);
    registerNewRevisionHandler(uoid, serviceDescriptor, new NewRevisionHandler() {

        @Override
        public void newRevision(BimServerClientInterface bimServerClientInterface, long poid, long roid, String userToken, long soid, SObjectType settings) throws ServerException, UserException {
            try {
                Long topicId = bimServerClientInterface.getRegistry().registerProgressOnRevisionTopic(SProgressTopicType.RUNNING_SERVICE, poid, roid, "Running " + name);
                RunningService runningService = new RunningService(topicId, bimServerClientInterface, pluginConfiguration);
                try {
                    SLongActionState state = new SLongActionState();
                    state.setProgress(getProgressType() == ProgressType.KNOWN ? 0 : -1);
                    state.setTitle(name);
                    state.setState(SActionState.STARTED);
                    state.setStart(runningService.getStartDate());
                    bimServerClientInterface.getRegistry().updateProgressTopic(topicId, state);
                    AbstractService.this.newRevision(runningService, bimServerClientInterface, poid, roid, userToken, soid, settings);
                    state = new SLongActionState();
                    state.setProgress(100);
                    state.setTitle(name);
                    state.setState(SActionState.FINISHED);
                    state.setStart(runningService.getStartDate());
                    state.setEnd(new Date());
                    bimServerClientInterface.getRegistry().updateProgressTopic(topicId, state);
                } catch (BimServerClientException e) {
                    LOGGER.error("", e);
                } catch (Exception e) {
                    LOGGER.error("", e);
                } finally {
                    bimServerClientInterface.getRegistry().unregisterProgressTopic(topicId);
                }
            } catch (PublicInterfaceNotFoundException e) {
                LOGGER.error("", e);
            }
        }
    });
}
Also used : SObjectType(org.bimserver.interfaces.objects.SObjectType) ServerException(org.bimserver.shared.exceptions.ServerException) SLongActionState(org.bimserver.interfaces.objects.SLongActionState) BimServerClientException(org.bimserver.shared.exceptions.BimServerClientException) Date(java.util.Date) PluginException(org.bimserver.shared.exceptions.PluginException) PublicInterfaceNotFoundException(org.bimserver.shared.exceptions.PublicInterfaceNotFoundException) BimServerClientException(org.bimserver.shared.exceptions.BimServerClientException) UserException(org.bimserver.shared.exceptions.UserException) ServerException(org.bimserver.shared.exceptions.ServerException) ServiceDescriptor(org.bimserver.models.store.ServiceDescriptor) PublicInterfaceNotFoundException(org.bimserver.shared.exceptions.PublicInterfaceNotFoundException) UserException(org.bimserver.shared.exceptions.UserException)

Example 4 with SLongActionState

use of org.bimserver.interfaces.objects.SLongActionState in project BIMserver by opensourceBIM.

the class BimServerClient method checkin.

public long checkin(long poid, String comment, long deserializerOid, boolean merge, Flow flow, URL url) throws UserException, ServerException {
    try {
        InputStream openStream = url.openStream();
        if (flow == Flow.SYNC) {
            try {
                long topicId = channel.checkin(baseAddress, token, poid, comment, deserializerOid, merge, flow, -1, url.toString(), openStream);
                SLongActionState progress = getNotificationRegistryInterface().getProgress(topicId);
                if (progress.getState() == SActionState.AS_ERROR) {
                    throw new UserException(Joiner.on(", ").join(progress.getErrors()));
                } else {
                    return topicId;
                }
            } finally {
                openStream.close();
            }
        } else {
            long topicId = channel.checkin(baseAddress, token, poid, comment, deserializerOid, merge, flow, -1, url.toString(), openStream);
            return topicId;
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return -1;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) SLongActionState(org.bimserver.interfaces.objects.SLongActionState) UserException(org.bimserver.shared.exceptions.UserException) IOException(java.io.IOException)

Example 5 with SLongActionState

use of org.bimserver.interfaces.objects.SLongActionState in project BIMserver by opensourceBIM.

the class BimServerClient method download.

public void download(long roid, long serializerOid, OutputStream outputStream) throws BimServerClientException {
    try {
        Long topicId = getServiceInterface().download(Collections.singleton(roid), DefaultQueries.allAsString(), serializerOid, false);
        SLongActionState progress = getNotificationRegistryInterface().getProgress(topicId);
        if (progress != null && progress.getState() == SActionState.AS_ERROR) {
            throw new BimServerClientException(Joiner.on(", ").join(progress.getErrors()));
        } else {
            InputStream inputStream = getDownloadData(topicId);
            try {
                IOUtils.copy(inputStream, outputStream);
                getServiceInterface().cleanupLongAction(topicId);
            } finally {
                inputStream.close();
            }
        }
    } catch (ServerException e) {
        LOGGER.error("", e);
    } catch (UserException e) {
        LOGGER.error("", e);
    } catch (IOException e) {
        LOGGER.error("", e);
    } catch (PublicInterfaceNotFoundException e) {
        LOGGER.error("", e);
    }
}
Also used : ServerException(org.bimserver.shared.exceptions.ServerException) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) PublicInterfaceNotFoundException(org.bimserver.shared.exceptions.PublicInterfaceNotFoundException) SLongActionState(org.bimserver.interfaces.objects.SLongActionState) UserException(org.bimserver.shared.exceptions.UserException) IOException(java.io.IOException) BimServerClientException(org.bimserver.shared.exceptions.BimServerClientException)

Aggregations

SLongActionState (org.bimserver.interfaces.objects.SLongActionState)5 InputStream (java.io.InputStream)3 UserException (org.bimserver.shared.exceptions.UserException)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 FileInputStream (java.io.FileInputStream)2 IOException (java.io.IOException)2 SDeserializerPluginConfiguration (org.bimserver.interfaces.objects.SDeserializerPluginConfiguration)2 SProject (org.bimserver.interfaces.objects.SProject)2 SSerializerPluginConfiguration (org.bimserver.interfaces.objects.SSerializerPluginConfiguration)2 BimServerClientInterface (org.bimserver.plugins.services.BimServerClientInterface)2 UsernamePasswordAuthenticationInfo (org.bimserver.shared.UsernamePasswordAuthenticationInfo)2 BimServerClientException (org.bimserver.shared.exceptions.BimServerClientException)2 PublicInterfaceNotFoundException (org.bimserver.shared.exceptions.PublicInterfaceNotFoundException)2 ServerException (org.bimserver.shared.exceptions.ServerException)2 Test (org.junit.Test)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 File (java.io.File)1 URL (java.net.URL)1 Date (java.util.Date)1 DataHandler (javax.activation.DataHandler)1