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);
}
}
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);
}
}
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);
}
}
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);
}
}
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);
}
}
Aggregations