Search in sources :

Example 16 with KapuaException

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

the class AccountServiceImpl method query.

@Override
public KapuaListResultImpl<Account> query(KapuaQuery<Account> 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(AccountDomain.ACCOUNT, Actions.read, query.getScopeId()));
    // 
    // Do count
    KapuaListResultImpl<Account> result = null;
    EntityManager em = AccountEntityManagerFactory.getInstance().createEntityManager();
    try {
        result = AccountDAO.query(em, query);
    } catch (Exception e) {
        throw KapuaExceptionUtils.convertPersistenceException(e);
    } finally {
        em.close();
    }
    return result;
}
Also used : KapuaLocator(org.eclipse.kapua.locator.KapuaLocator) Account(org.eclipse.kapua.service.account.Account) 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) KapuaIllegalArgumentException(org.eclipse.kapua.KapuaIllegalArgumentException) KapuaIllegalAccessException(org.eclipse.kapua.KapuaIllegalAccessException) KapuaException(org.eclipse.kapua.KapuaException)

Example 17 with KapuaException

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

the class TranslatorAppBundleKuraKapua method translate.

private BundleResponsePayload translate(KuraResponsePayload kuraPayload) throws KapuaException {
    BundleResponsePayload bundleResponsePayload = new BundleResponsePayload();
    bundleResponsePayload.setExceptionMessage((String) kuraPayload.getMetrics().get(ResponseMetrics.RESP_METRIC_EXCEPTION_MESSAGE.getValue()));
    bundleResponsePayload.setExceptionStack((String) kuraPayload.getMetrics().get(ResponseMetrics.RESP_METRIC_EXCEPTION_STACK.getValue()));
    if (kuraPayload.getBody() != null) {
        DeviceManagementSetting config = DeviceManagementSetting.getInstance();
        String charEncoding = config.getString(DeviceManagementSettingKey.CHAR_ENCODING);
        String body = null;
        try {
            body = new String(kuraPayload.getBody(), charEncoding);
        } catch (Exception e) {
            throw new TranslatorException(TranslatorErrorCodes.INVALID_PAYLOAD, e, kuraPayload.getBody());
        }
        KuraBundles kuraBundles = null;
        try {
            kuraBundles = XmlUtil.unmarshal(body, KuraBundles.class);
        } catch (Exception e) {
            throw new TranslatorException(TranslatorErrorCodes.INVALID_PAYLOAD, e, body);
        }
        translate(bundleResponsePayload, charEncoding, kuraBundles);
    }
    // Return Kapua Payload
    return bundleResponsePayload;
}
Also used : KuraBundles(org.eclipse.kapua.service.device.call.kura.model.bundle.KuraBundles) BundleResponsePayload(org.eclipse.kapua.service.device.management.bundle.message.internal.BundleResponsePayload) DeviceManagementSetting(org.eclipse.kapua.service.device.management.commons.setting.DeviceManagementSetting) TranslatorException(org.eclipse.kapua.translator.exception.TranslatorException) TranslatorException(org.eclipse.kapua.translator.exception.TranslatorException) KapuaException(org.eclipse.kapua.KapuaException)

Example 18 with KapuaException

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

the class TranslatorAppConfigurationKuraKapua method translate.

private ConfigurationResponsePayload translate(KuraResponsePayload kuraPayload) throws KapuaException {
    ConfigurationResponsePayload configurationResponsePayload = new ConfigurationResponsePayload();
    configurationResponsePayload.setExceptionMessage((String) kuraPayload.getMetrics().get(ResponseMetrics.RESP_METRIC_EXCEPTION_MESSAGE.getValue()));
    configurationResponsePayload.setExceptionStack((String) kuraPayload.getMetrics().get(ResponseMetrics.RESP_METRIC_EXCEPTION_STACK.getValue()));
    DeviceManagementSetting config = DeviceManagementSetting.getInstance();
    String charEncoding = config.getString(DeviceManagementSettingKey.CHAR_ENCODING);
    if (kuraPayload.getBody() != null) {
        String body = null;
        try {
            body = new String(kuraPayload.getBody(), charEncoding);
        } catch (Exception e) {
            throw new TranslatorException(TranslatorErrorCodes.INVALID_PAYLOAD, e, configurationResponsePayload.getBody());
        }
        KuraDeviceConfiguration kuraDeviceConfiguration = null;
        try {
            kuraDeviceConfiguration = XmlUtil.unmarshal(body, KuraDeviceConfiguration.class);
        } catch (Exception e) {
            throw new TranslatorException(TranslatorErrorCodes.INVALID_PAYLOAD, e, body);
        }
        translateBody(configurationResponsePayload, charEncoding, kuraDeviceConfiguration);
    }
    // Return Kapua Payload
    return configurationResponsePayload;
}
Also used : KuraDeviceConfiguration(org.eclipse.kapua.service.device.call.kura.model.configuration.KuraDeviceConfiguration) DeviceManagementSetting(org.eclipse.kapua.service.device.management.commons.setting.DeviceManagementSetting) ConfigurationResponsePayload(org.eclipse.kapua.service.device.management.configuration.message.internal.ConfigurationResponsePayload) TranslatorException(org.eclipse.kapua.translator.exception.TranslatorException) TranslatorException(org.eclipse.kapua.translator.exception.TranslatorException) KapuaException(org.eclipse.kapua.KapuaException)

Example 19 with KapuaException

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

the class TranslatorAppPackageKuraKapua method translate.

private void translate(PackageResponsePayload packageResponsePayload, String charEncoding, KuraDeploymentPackages kuraDeploymentPackages) throws KapuaException {
    try {
        KuraDeploymentPackage[] deploymentPackageArray = kuraDeploymentPackages.getDeploymentPackages();
        if (deploymentPackageArray != null) {
            KapuaLocator locator = KapuaLocator.getInstance();
            DevicePackageFactory deviceDeploymentFactory = locator.getFactory(DevicePackageFactory.class);
            DevicePackages deviceDeploymentPackages = deviceDeploymentFactory.newDeviceDeploymentPackages();
            for (KuraDeploymentPackage deploymentPackage : deploymentPackageArray) {
                DevicePackage deviceDeploymentPackage = deviceDeploymentFactory.newDeviceDeploymentPackage();
                deviceDeploymentPackage.setName(deploymentPackage.getName());
                deviceDeploymentPackage.setVersion(deploymentPackage.getVersion());
                DevicePackageBundleInfos devicePackageBundleInfos = deviceDeploymentPackage.getBundleInfos();
                KuraBundleInfo[] bundleInfoArray = deploymentPackage.getBundleInfos();
                for (KuraBundleInfo bundleInfo : bundleInfoArray) {
                    DevicePackageBundleInfo devicePackageBundleInfo = deviceDeploymentFactory.newDevicePackageBundleInfo();
                    devicePackageBundleInfo.setName(bundleInfo.getName());
                    devicePackageBundleInfo.setVersion(bundleInfo.getVersion());
                    // Add the new DevicePackageBundleInfo object to the corresponding list
                    devicePackageBundleInfos.getBundlesInfos().add(devicePackageBundleInfo);
                }
                // Add the new DeviceDeploymentPackage object to the corresponding list
                deviceDeploymentPackages.getPackages().add(deviceDeploymentPackage);
            }
            StringWriter sw = new StringWriter();
            XmlUtil.marshal(deviceDeploymentPackages, sw);
            byte[] requestBody = sw.toString().getBytes(charEncoding);
            packageResponsePayload.setBody(requestBody);
        }
    } catch (Exception e) {
        throw new TranslatorException(TranslatorErrorCodes.INVALID_BODY, e, kuraDeploymentPackages);
    }
}
Also used : KapuaLocator(org.eclipse.kapua.locator.KapuaLocator) DevicePackageBundleInfo(org.eclipse.kapua.service.device.management.packages.model.DevicePackageBundleInfo) DevicePackage(org.eclipse.kapua.service.device.management.packages.model.DevicePackage) KuraBundleInfo(org.eclipse.kapua.service.device.call.kura.model.deploy.KuraBundleInfo) TranslatorException(org.eclipse.kapua.translator.exception.TranslatorException) KapuaException(org.eclipse.kapua.KapuaException) DevicePackages(org.eclipse.kapua.service.device.management.packages.model.DevicePackages) DevicePackageFactory(org.eclipse.kapua.service.device.management.packages.DevicePackageFactory) StringWriter(java.io.StringWriter) DevicePackageBundleInfos(org.eclipse.kapua.service.device.management.packages.model.DevicePackageBundleInfos) TranslatorException(org.eclipse.kapua.translator.exception.TranslatorException) KuraDeploymentPackage(org.eclipse.kapua.service.device.call.kura.model.deploy.KuraDeploymentPackage)

Example 20 with KapuaException

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

the class RoleServiceImpl method count.

@Override
public long count(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 count
    long count = 0;
    EntityManager em = AuthorizationEntityManagerFactory.getEntityManager();
    try {
        count = RoleDAO.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