Search in sources :

Example 21 with KapuaException

use of org.eclipse.kapua.KapuaException in project kapua by eclipse.

the class RoleServiceImpl method query.

@Override
public RoleListResult query(KapuaQuery<Role> 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(RoleDomain.ROLE, Actions.read, query.getScopeId()));
    // 
    // Do query
    RoleListResult result = null;
    EntityManager em = AuthorizationEntityManagerFactory.getEntityManager();
    try {
        result = RoleDAO.query(em, query);
    } catch (Exception e) {
        throw KapuaExceptionUtils.convertPersistenceException(e);
    } finally {
        em.close();
    }
    return result;
}
Also used : KapuaLocator(org.eclipse.kapua.locator.KapuaLocator) EntityManager(org.eclipse.kapua.commons.jpa.EntityManager) RoleListResult(org.eclipse.kapua.service.authorization.role.RoleListResult) AuthorizationService(org.eclipse.kapua.service.authorization.AuthorizationService) PermissionFactory(org.eclipse.kapua.service.authorization.permission.PermissionFactory) KapuaEntityNotFoundException(org.eclipse.kapua.KapuaEntityNotFoundException) KapuaException(org.eclipse.kapua.KapuaException)

Example 22 with KapuaException

use of org.eclipse.kapua.KapuaException in project kapua by eclipse.

the class AbstractUserServiceTest method scriptSession.

public static void scriptSession(String path, String fileFilter) {
    EntityManager em = null;
    try {
        logger.info("Running database scripts...");
        em = UserEntityManagerFactory.getInstance().createEntityManager();
        em.beginTransaction();
        SimpleSqlScriptExecutor sqlScriptExecutor = new SimpleSqlScriptExecutor();
        sqlScriptExecutor.scanScripts(path, fileFilter);
        sqlScriptExecutor.executeUpdate(em);
        em.commit();
        logger.info("...database scripts done!");
    } catch (KapuaException e) {
        logger.error("Database scripts failed: {}", e.getMessage());
        if (em != null)
            em.rollback();
    } finally {
        if (em != null)
            em.close();
    }
}
Also used : EntityManager(org.eclipse.kapua.commons.jpa.EntityManager) SimpleSqlScriptExecutor(org.eclipse.kapua.commons.jpa.SimpleSqlScriptExecutor) KapuaException(org.eclipse.kapua.KapuaException)

Example 23 with KapuaException

use of org.eclipse.kapua.KapuaException in project kapua by eclipse.

the class KapuaTest method setUp.

@Before
public void setUp() {
    LOG.debug("Setting up test...");
    if (!isInitialized) {
        LOG.debug("Kapua test context is not initialized. Initializing...");
        try {
            // 
            // Login
            String username = "kapua-sys";
            String password = "kapua-password";
            AuthenticationService authenticationService = locator.getService(AuthenticationService.class);
            UsernamePasswordTokenFactory credentialsFactory = locator.getFactory(UsernamePasswordTokenFactory.class);
            authenticationService.login(credentialsFactory.newInstance(username, password.toCharArray()));
            // 
            // Get current user Id
            adminUserId = KapuaSecurityUtils.getSession().getUserId();
            adminScopeId = KapuaSecurityUtils.getSession().getScopeId();
        } catch (KapuaException exc) {
            exc.printStackTrace();
        }
        isInitialized = true;
    }
}
Also used : KapuaException(org.eclipse.kapua.KapuaException) UsernamePasswordTokenFactory(org.eclipse.kapua.service.authentication.UsernamePasswordTokenFactory) AuthenticationService(org.eclipse.kapua.service.authentication.AuthenticationService) Before(org.junit.Before)

Example 24 with KapuaException

use of org.eclipse.kapua.KapuaException in project kapua by eclipse.

the class KapuaTest method tearDown.

@AfterClass
public static void tearDown() {
    LOG.debug("Stopping Kapua test context.");
    isInitialized = false;
    try {
        KapuaLocator locator = KapuaLocator.getInstance();
        AuthenticationService authenticationService = locator.getService(AuthenticationService.class);
        authenticationService.logout();
    } catch (KapuaException exc) {
        exc.printStackTrace();
    }
}
Also used : KapuaLocator(org.eclipse.kapua.locator.KapuaLocator) KapuaException(org.eclipse.kapua.KapuaException) AuthenticationService(org.eclipse.kapua.service.authentication.AuthenticationService) AfterClass(org.junit.AfterClass)

Example 25 with KapuaException

use of org.eclipse.kapua.KapuaException in project kapua by eclipse.

the class UserRolesServiceImpl method count.

@Override
public long count(KapuaQuery<UserRoles> 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(RoleDomain.ROLE, Actions.read, query.getScopeId()));
    // 
    // Do count
    long count = 0;
    EntityManager em = AuthorizationEntityManagerFactory.getEntityManager();
    try {
        count = UserRolesDAO.count(em, query);
    } catch (Exception e) {
        throw KapuaExceptionUtils.convertPersistenceException(e);
    } finally {
        em.close();
    }
    return count;
}
Also used : KapuaLocator(org.eclipse.kapua.locator.KapuaLocator) EntityManager(org.eclipse.kapua.commons.jpa.EntityManager) AuthorizationService(org.eclipse.kapua.service.authorization.AuthorizationService) PermissionFactory(org.eclipse.kapua.service.authorization.permission.PermissionFactory) KapuaEntityNotFoundException(org.eclipse.kapua.KapuaEntityNotFoundException) KapuaException(org.eclipse.kapua.KapuaException)

Aggregations

KapuaException (org.eclipse.kapua.KapuaException)99 EntityManager (org.eclipse.kapua.commons.jpa.EntityManager)53 AuthorizationService (org.eclipse.kapua.service.authorization.AuthorizationService)50 PermissionFactory (org.eclipse.kapua.service.authorization.permission.PermissionFactory)50 KapuaEntityNotFoundException (org.eclipse.kapua.KapuaEntityNotFoundException)48 KapuaLocator (org.eclipse.kapua.locator.KapuaLocator)48 KapuaIllegalArgumentException (org.eclipse.kapua.KapuaIllegalArgumentException)19 LocalServicePlan (org.eclipse.kapua.service.datastore.internal.elasticsearch.LocalServicePlan)17 DeviceManagementSetting (org.eclipse.kapua.service.device.management.commons.setting.DeviceManagementSetting)12 Date (java.util.Date)11 KapuaIllegalAccessException (org.eclipse.kapua.KapuaIllegalAccessException)10 Account (org.eclipse.kapua.service.account.Account)9 DeviceCallExecutor (org.eclipse.kapua.service.device.management.commons.call.DeviceCallExecutor)8 DeviceManagementException (org.eclipse.kapua.service.device.management.commons.exception.DeviceManagementException)8 DeviceEventCreator (org.eclipse.kapua.service.device.registry.event.DeviceEventCreator)8 DeviceEventFactory (org.eclipse.kapua.service.device.registry.event.DeviceEventFactory)8 DeviceEventService (org.eclipse.kapua.service.device.registry.event.DeviceEventService)8 IOException (java.io.IOException)7 TranslatorException (org.eclipse.kapua.translator.exception.TranslatorException)7 UnknownHostException (java.net.UnknownHostException)6