Search in sources :

Example 81 with DatabaseSession

use of org.bimserver.database.DatabaseSession in project BIMserver by opensourceBIM.

the class NewServicesImpl method listAvailableOutputFormats.

@Override
public List<SFormatSerializerMap> listAvailableOutputFormats(Long poid) throws ServerException, UserException {
    Map<String, SFormatSerializerMap> outputs = new HashMap<>();
    try (DatabaseSession session = getBimServer().getDatabase().createSession()) {
        Project project = session.get(poid, OldQuery.getDefault());
        try {
            List<SSerializerPluginConfiguration> allSerializersForPoids = getServiceMap().get(PluginInterface.class).getAllSerializersForPoids(true, Collections.singleton(poid));
            for (SSerializerPluginConfiguration pluginConfiguration : allSerializersForPoids) {
                PluginDescriptor pluginDescriptor = session.get(pluginConfiguration.getPluginDescriptorId(), OldQuery.getDefault());
                Plugin plugin = getBimServer().getPluginManager().getPlugin(pluginDescriptor.getIdentifier(), true);
                String outputFormat = null;
                // TODO For now only streaming serializers
                if (plugin instanceof StreamingSerializerPlugin) {
                    outputFormat = ((StreamingSerializerPlugin) plugin).getOutputFormat(Schema.valueOf(project.getSchema().toUpperCase()));
                }
                if (outputFormat != null) {
                    SFormatSerializerMap map = outputs.get(outputFormat);
                    if (map == null) {
                        map = new SFormatSerializerMap();
                        map.setFormat(outputFormat);
                        outputs.put(outputFormat, map);
                    }
                    map.getSerializers().add(pluginConfiguration);
                }
            }
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (UserException e) {
            e.printStackTrace();
        }
        return new ArrayList<>(outputs.values());
    } catch (BimserverDatabaseException e) {
        return handleException(e);
    }
}
Also used : ServerException(org.bimserver.shared.exceptions.ServerException) HashMap(java.util.HashMap) DatabaseSession(org.bimserver.database.DatabaseSession) PluginInterface(org.bimserver.shared.interfaces.PluginInterface) ArrayList(java.util.ArrayList) SFormatSerializerMap(org.bimserver.interfaces.objects.SFormatSerializerMap) StreamingSerializerPlugin(org.bimserver.plugins.serializers.StreamingSerializerPlugin) Project(org.bimserver.models.store.Project) PluginDescriptor(org.bimserver.models.store.PluginDescriptor) BimserverDatabaseException(org.bimserver.BimserverDatabaseException) SSerializerPluginConfiguration(org.bimserver.interfaces.objects.SSerializerPluginConfiguration) UserException(org.bimserver.shared.exceptions.UserException) StreamingSerializerPlugin(org.bimserver.plugins.serializers.StreamingSerializerPlugin) Plugin(org.bimserver.plugins.Plugin)

Example 82 with DatabaseSession

use of org.bimserver.database.DatabaseSession in project BIMserver by opensourceBIM.

the class OAuthServiceImpl method listRegisteredServersLocal.

@Override
public List<SOAuthServer> listRegisteredServersLocal() throws ServerException, UserException {
    try (DatabaseSession session = getBimServer().getDatabase().createSession()) {
        List<OAuthServer> allOfType = session.getAllOfType(StorePackage.eINSTANCE.getOAuthServer(), OAuthServer.class, OldQuery.getDefault());
        Iterator<OAuthServer> iterator = allOfType.iterator();
        while (iterator.hasNext()) {
            OAuthServer next = iterator.next();
            if (!next.isIncoming()) {
                iterator.remove();
            }
        }
        return getBimServer().getSConverter().convertToSListOAuthServer(allOfType);
    } catch (BimserverDatabaseException e) {
        return handleException(e);
    }
}
Also used : BimserverDatabaseException(org.bimserver.BimserverDatabaseException) DatabaseSession(org.bimserver.database.DatabaseSession) SOAuthServer(org.bimserver.interfaces.objects.SOAuthServer) OAuthServer(org.bimserver.models.store.OAuthServer)

Example 83 with DatabaseSession

use of org.bimserver.database.DatabaseSession in project BIMserver by opensourceBIM.

the class OAuthServiceImpl method listRegisteredServers.

@Override
public List<SOAuthServer> listRegisteredServers() throws ServerException, UserException {
    try (DatabaseSession session = getBimServer().getDatabase().createSession()) {
        List<OAuthServer> allOfType = session.getAllOfType(StorePackage.eINSTANCE.getOAuthServer(), OAuthServer.class, OldQuery.getDefault());
        Iterator<OAuthServer> iterator = allOfType.iterator();
        while (iterator.hasNext()) {
            OAuthServer next = iterator.next();
            if (next.isIncoming()) {
                iterator.remove();
            }
        }
        return getBimServer().getSConverter().convertToSListOAuthServer(allOfType);
    } catch (BimserverDatabaseException e) {
        return handleException(e);
    }
}
Also used : BimserverDatabaseException(org.bimserver.BimserverDatabaseException) DatabaseSession(org.bimserver.database.DatabaseSession) SOAuthServer(org.bimserver.interfaces.objects.SOAuthServer) OAuthServer(org.bimserver.models.store.OAuthServer)

Example 84 with DatabaseSession

use of org.bimserver.database.DatabaseSession in project BIMserver by opensourceBIM.

the class OAuthServiceImpl method authorize.

@Override
public String authorize(Long oAuthServerOid, SAuthorization authorization) throws ServerException, UserException {
    try (DatabaseSession session = getBimServer().getDatabase().createSession()) {
        if (authorization instanceof SSingleProjectAuthorization) {
            User user = session.get(getCurrentUser().getOid(), OldQuery.getDefault());
            SSingleProjectAuthorization sSingleProjectAuthorization = (SSingleProjectAuthorization) authorization;
            SingleProjectAuthorization singleProjectAuthorization = session.create(SingleProjectAuthorization.class);
            Project project = session.get(sSingleProjectAuthorization.getProjectId(), OldQuery.getDefault());
            if (project == null) {
                throw new UserException("No project found with poid " + sSingleProjectAuthorization.getProjectId());
            }
            singleProjectAuthorization.setProject(project);
            OAuthAuthorizationCode code = session.create(OAuthAuthorizationCode.class);
            org.bimserver.webservices.authorization.Authorization auth = new org.bimserver.webservices.authorization.SingleProjectAuthorization(getBimServer(), user.getOid(), project.getOid());
            String asHexToken = auth.asHexToken(getBimServer().getEncryptionKey());
            code.setCode(asHexToken);
            code.setOauthServer(session.get(oAuthServerOid, OldQuery.getDefault()));
            code.setAuthorization(singleProjectAuthorization);
            code.setUser(user);
            user.getOAuthIssuedAuthorizationCodes().add(code);
            session.store(user);
            session.store(singleProjectAuthorization);
            session.commit();
            return code.getCode();
        } else if (authorization instanceof SRunServiceAuthorization) {
            SRunServiceAuthorization serviceAuthorization = (SRunServiceAuthorization) authorization;
            User user = session.get(getCurrentUser().getOid(), OldQuery.getDefault());
            RunServiceAuthorization runServiceAuth = session.create(RunServiceAuthorization.class);
            InternalServicePluginConfiguration conf = session.get(serviceAuthorization.getServiceId(), OldQuery.getDefault());
            if (conf == null) {
                throw new UserException("No service found with soid " + serviceAuthorization.getServiceId());
            }
            runServiceAuth.setService(conf);
            OAuthAuthorizationCode code = session.create(OAuthAuthorizationCode.class);
            org.bimserver.webservices.authorization.Authorization auth = new org.bimserver.webservices.authorization.RunServiceAuthorization(getBimServer(), user.getOid(), conf.getOid());
            String asHexToken = auth.asHexToken(getBimServer().getEncryptionKey());
            code.setCode(asHexToken);
            code.setOauthServer(session.get(oAuthServerOid, OldQuery.getDefault()));
            code.setAuthorization(runServiceAuth);
            code.setUser(user);
            user.getOAuthIssuedAuthorizationCodes().add(code);
            session.store(user);
            session.store(code);
            session.store(runServiceAuth);
            session.commit();
            return code.getCode();
        } else {
            throw new UserException("Unimplemented type of authorization " + authorization.getClass().getSimpleName());
        }
    } catch (Exception e) {
        return handleException(e);
    }
}
Also used : User(org.bimserver.models.store.User) DatabaseSession(org.bimserver.database.DatabaseSession) SSingleProjectAuthorization(org.bimserver.interfaces.objects.SSingleProjectAuthorization) SingleProjectAuthorization(org.bimserver.models.store.SingleProjectAuthorization) SRunServiceAuthorization(org.bimserver.interfaces.objects.SRunServiceAuthorization) RunServiceAuthorization(org.bimserver.models.store.RunServiceAuthorization) UserException(org.bimserver.shared.exceptions.UserException) ServerException(org.bimserver.shared.exceptions.ServerException) BimserverDatabaseException(org.bimserver.BimserverDatabaseException) SAuthorization(org.bimserver.interfaces.objects.SAuthorization) Authorization(org.bimserver.models.store.Authorization) SRunServiceAuthorization(org.bimserver.interfaces.objects.SRunServiceAuthorization) RunServiceAuthorization(org.bimserver.models.store.RunServiceAuthorization) SSingleProjectAuthorization(org.bimserver.interfaces.objects.SSingleProjectAuthorization) SingleProjectAuthorization(org.bimserver.models.store.SingleProjectAuthorization) SSingleProjectAuthorization(org.bimserver.interfaces.objects.SSingleProjectAuthorization) Project(org.bimserver.models.store.Project) InternalServicePluginConfiguration(org.bimserver.models.store.InternalServicePluginConfiguration) UserException(org.bimserver.shared.exceptions.UserException) SRunServiceAuthorization(org.bimserver.interfaces.objects.SRunServiceAuthorization) OAuthAuthorizationCode(org.bimserver.models.store.OAuthAuthorizationCode) SOAuthAuthorizationCode(org.bimserver.interfaces.objects.SOAuthAuthorizationCode)

Example 85 with DatabaseSession

use of org.bimserver.database.DatabaseSession in project BIMserver by opensourceBIM.

the class OAuthServiceImpl method generateForwardUrl.

public String generateForwardUrl(String registrationEndpoint, String authorizeUrl, String returnUrl) throws ServerException, UserException {
    try (DatabaseSession session = getBimServer().getDatabase().createSession()) {
        OAuthServer oAuthServer = session.querySingle(StorePackage.eINSTANCE.getOAuthServer_RegistrationEndpoint(), registrationEndpoint);
        if (oAuthServer == null) {
            throw new UserException("Application not registered");
        }
        OAuthClientRequest request2 = OAuthClientRequest.authorizationLocation(authorizeUrl).setParameter("auth_type", "service").setClientId(oAuthServer.getClientId()).setRedirectURI(returnUrl).setResponseType(ResponseType.CODE.toString()).setState("state").buildQueryMessage();
        return request2.getLocationUri();
    } catch (Exception e) {
        return handleException(e);
    }
}
Also used : DatabaseSession(org.bimserver.database.DatabaseSession) UserException(org.bimserver.shared.exceptions.UserException) OAuthClientRequest(org.apache.oltu.oauth2.client.request.OAuthClientRequest) SOAuthServer(org.bimserver.interfaces.objects.SOAuthServer) OAuthServer(org.bimserver.models.store.OAuthServer) UserException(org.bimserver.shared.exceptions.UserException) ServerException(org.bimserver.shared.exceptions.ServerException) BimserverDatabaseException(org.bimserver.BimserverDatabaseException)

Aggregations

DatabaseSession (org.bimserver.database.DatabaseSession)279 BimserverDatabaseException (org.bimserver.BimserverDatabaseException)258 UserException (org.bimserver.shared.exceptions.UserException)228 ServerException (org.bimserver.shared.exceptions.ServerException)217 IOException (java.io.IOException)194 DeserializeException (org.bimserver.plugins.deserializers.DeserializeException)105 SerializerException (org.bimserver.plugins.serializers.SerializerException)105 UnsupportedEncodingException (java.io.UnsupportedEncodingException)103 MalformedURLException (java.net.MalformedURLException)103 MessagingException (javax.mail.MessagingException)103 AddressException (javax.mail.internet.AddressException)103 CannotBeScheduledException (org.bimserver.longaction.CannotBeScheduledException)103 BcfException (org.opensourcebim.bcf.BcfException)103 User (org.bimserver.models.store.User)34 ArrayList (java.util.ArrayList)29 UserSettings (org.bimserver.models.store.UserSettings)29 Project (org.bimserver.models.store.Project)25 ServerSettings (org.bimserver.models.store.ServerSettings)21 ServerSettingsSetter (org.bimserver.database.actions.ServerSettingsSetter)18 SetServerSettingDatabaseAction (org.bimserver.database.actions.SetServerSettingDatabaseAction)18