use of org.eclipse.kapua.locator.KapuaLocator in project kapua by eclipse.
the class AuthenticationServiceTest method testLoginAndLogout.
/**
* We should ignore this test until schema loading feature is provided.
*/
@Ignore
@Test
public void testLoginAndLogout() throws Exception {
String username = "kapua-sys";
char[] password = "kapua-password".toCharArray();
KapuaLocator locator = KapuaLocator.getInstance();
UsernamePasswordTokenFactory usernamePasswordTokenFactory = locator.getFactory(UsernamePasswordTokenFactory.class);
UsernamePasswordToken credentialsToken = usernamePasswordTokenFactory.newInstance(username, password);
AuthenticationService authenticationService = locator.getService(AuthenticationService.class);
authenticationService.login(credentialsToken);
Subject currentSubject = SecurityUtils.getSubject();
assertTrue(currentSubject.isAuthenticated());
assertEquals(username, currentSubject.getPrincipal());
authenticationService.logout();
assertFalse(currentSubject.isAuthenticated());
assertNull(currentSubject.getPrincipal());
}
use of org.eclipse.kapua.locator.KapuaLocator in project kapua by eclipse.
the class UserPermissionServiceImpl method query.
@Override
public UserPermissionListResult query(KapuaQuery<UserPermission> query) throws KapuaException {
ArgumentValidator.notNull(query, "query");
ArgumentValidator.notNull(query.getScopeId(), "query.scopeId");
//
// Check Access
KapuaLocator locator = KapuaLocator.getInstance();
AuthorizationService authorizationService = locator.getService(AuthorizationService.class);
PermissionFactory permissionFactory = locator.getFactory(PermissionFactory.class);
authorizationService.checkPermission(permissionFactory.newPermission(UserPermissionDomain.USER_PERMISSION, Actions.read, query.getScopeId()));
//
// Do query
UserPermissionListResult result = null;
EntityManager em = AuthorizationEntityManagerFactory.getEntityManager();
try {
result = UserPermissionDAO.query(em, query);
} catch (Exception e) {
throw KapuaExceptionUtils.convertPersistenceException(e);
} finally {
em.close();
}
return result;
}
use of org.eclipse.kapua.locator.KapuaLocator in project kapua by eclipse.
the class UserPermissionServiceImpl method find.
@Override
public UserPermission find(KapuaId scopeId, KapuaId permissionId) throws KapuaException {
ArgumentValidator.notNull(scopeId, "accountId");
ArgumentValidator.notNull(permissionId, "permissionId");
//
// Check Access
KapuaLocator locator = KapuaLocator.getInstance();
AuthorizationService authorizationService = locator.getService(AuthorizationService.class);
PermissionFactory permissionFactory = locator.getFactory(PermissionFactory.class);
authorizationService.checkPermission(permissionFactory.newPermission(UserPermissionDomain.USER_PERMISSION, Actions.read, scopeId));
//
// Do find
UserPermission permission = null;
EntityManager em = AuthorizationEntityManagerFactory.getEntityManager();
try {
permission = UserPermissionDAO.find(em, permissionId);
} catch (Exception e) {
throw KapuaExceptionUtils.convertPersistenceException(e);
} finally {
em.close();
}
return permission;
}
use of org.eclipse.kapua.locator.KapuaLocator in project kapua by eclipse.
the class UserPermissionServiceImpl method create.
@Override
public UserPermission create(UserPermissionCreator userPermissionCreator) throws KapuaException {
ArgumentValidator.notNull(userPermissionCreator, "userPermissionCreator");
ArgumentValidator.notNull(userPermissionCreator.getPermission(), "userPermissionCreator.permission");
//
// Check Access
KapuaLocator locator = KapuaLocator.getInstance();
AuthorizationService authorizationService = locator.getService(AuthorizationService.class);
PermissionFactory permissionFactory = locator.getFactory(PermissionFactory.class);
authorizationService.checkPermission(permissionFactory.newPermission(UserPermissionDomain.USER_PERMISSION, Actions.write, userPermissionCreator.getScopeId()));
//
// Do create
UserPermission permission = null;
EntityManager em = AuthorizationEntityManagerFactory.getEntityManager();
try {
em.beginTransaction();
permission = UserPermissionDAO.create(em, userPermissionCreator);
em.commit();
} catch (Exception e) {
em.rollback();
throw KapuaExceptionUtils.convertPersistenceException(e);
} finally {
em.close();
}
return permission;
}
use of org.eclipse.kapua.locator.KapuaLocator in project kapua by eclipse.
the class TranslatorAppBundleKapuaKura method translate.
@Override
public KuraRequestMessage translate(BundleRequestMessage kapuaMessage) throws KapuaException {
//
// Kura channel
KapuaLocator locator = KapuaLocator.getInstance();
AccountService accountService = locator.getService(AccountService.class);
Account account = accountService.find(kapuaMessage.getScopeId());
DeviceRegistryService deviceService = locator.getService(DeviceRegistryService.class);
Device device = deviceService.find(kapuaMessage.getScopeId(), kapuaMessage.getDeviceId());
KuraRequestChannel kuraRequestChannel = translate(kapuaMessage.getChannel());
kuraRequestChannel.setScope(account.getName());
kuraRequestChannel.setClientId(device.getClientId());
//
// Kura payload
KuraRequestPayload kuraPayload = translate(kapuaMessage.getPayload());
// Return Kura Message
return new KuraRequestMessage(kuraRequestChannel, kapuaMessage.getReceivedOn(), kuraPayload);
}
Aggregations