Search in sources :

Example 96 with KapuaException

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

the class DeviceConnectionServiceImpl method find.

@Override
public DeviceConnection find(KapuaId scopeId, KapuaId entityId) throws KapuaException {
    // 
    // Argument Validation
    ArgumentValidator.notNull(scopeId, "scopeId");
    ArgumentValidator.notNull(entityId, "entityId");
    // 
    // Check Access
    KapuaLocator locator = KapuaLocator.getInstance();
    AuthorizationService authorizationService = locator.getService(AuthorizationService.class);
    PermissionFactory permissionFactory = locator.getFactory(PermissionFactory.class);
    authorizationService.checkPermission(permissionFactory.newPermission(DeviceConnectionDomain.DEVICE_CONNECTION, Actions.read, scopeId));
    // 
    // Do find
    DeviceConnection deviceConnection = null;
    EntityManager em = DeviceEntityManagerFactory.getEntityManager();
    try {
        deviceConnection = DeviceConnectionDAO.find(em, entityId);
    } catch (Exception e) {
        throw KapuaExceptionUtils.convertPersistenceException(e);
    } finally {
        em.close();
    }
    return deviceConnection;
}
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) DeviceConnection(org.eclipse.kapua.service.device.registry.connection.DeviceConnection) KapuaEntityNotFoundException(org.eclipse.kapua.KapuaEntityNotFoundException) KapuaException(org.eclipse.kapua.KapuaException)

Example 97 with KapuaException

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

the class DeviceConnectionServiceImpl method delete.

@Override
public void delete(KapuaId scopeId, KapuaId deviceConnectionId) throws KapuaException {
    // 
    // Argument Validation
    ArgumentValidator.notNull(deviceConnectionId, "deviceConnection.id");
    ArgumentValidator.notNull(scopeId, "deviceConnection.scopeId");
    // 
    // Check Access
    KapuaLocator locator = KapuaLocator.getInstance();
    AuthorizationService authorizationService = locator.getService(AuthorizationService.class);
    PermissionFactory permissionFactory = locator.getFactory(PermissionFactory.class);
    authorizationService.checkPermission(permissionFactory.newPermission(DeviceConnectionDomain.DEVICE_CONNECTION, Actions.write, scopeId));
    // 
    // Do delete
    EntityManager em = DeviceEntityManagerFactory.getEntityManager();
    try {
        if (DeviceConnectionDAO.find(em, deviceConnectionId) == null) {
            throw new KapuaEntityNotFoundException(DeviceConnection.TYPE, deviceConnectionId);
        }
        em.beginTransaction();
        DeviceConnectionDAO.delete(em, deviceConnectionId);
        em.commit();
    } catch (Exception e) {
        em.rollback();
        throw KapuaExceptionUtils.convertPersistenceException(e);
    } finally {
        em.close();
    }
}
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) KapuaEntityNotFoundException(org.eclipse.kapua.KapuaEntityNotFoundException) KapuaException(org.eclipse.kapua.KapuaException)

Example 98 with KapuaException

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

the class DeviceConnectionServiceImpl method query.

@Override
public DeviceConnectionListResult query(KapuaQuery<DeviceConnection> query) throws KapuaException {
    // 
    // Argument Validation
    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(DeviceConnectionDomain.DEVICE_CONNECTION, Actions.read, query.getScopeId()));
    // 
    // Do Query
    DeviceConnectionListResult result = null;
    EntityManager em = DeviceEntityManagerFactory.getEntityManager();
    try {
        result = DeviceConnectionDAO.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) DeviceConnectionListResult(org.eclipse.kapua.service.device.registry.connection.DeviceConnectionListResult) 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 99 with KapuaException

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

the class DeviceConfigurationManagementServiceImpl method put.

@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
public void put(KapuaId scopeId, KapuaId deviceId, DeviceConfiguration deviceConfiguration, Long timeout) throws KapuaException {
    // 
    // Argument Validation
    ArgumentValidator.notNull(scopeId, "scopeId");
    ArgumentValidator.notNull(deviceId, "deviceId");
    ArgumentValidator.notNull(deviceConfiguration, "componentConfiguration");
    // 
    // Check Access
    KapuaLocator locator = KapuaLocator.getInstance();
    AuthorizationService authorizationService = locator.getService(AuthorizationService.class);
    PermissionFactory permissionFactory = locator.getFactory(PermissionFactory.class);
    authorizationService.checkPermission(permissionFactory.newPermission(DeviceManagementDomain.DEVICE_MANAGEMENT, Actions.write, scopeId));
    // 
    // Prepare the request
    ConfigurationRequestChannel configurationRequestChannel = new ConfigurationRequestChannel();
    configurationRequestChannel.setAppName(DeviceConfigurationAppProperties.APP_NAME);
    configurationRequestChannel.setVersion(DeviceConfigurationAppProperties.APP_VERSION);
    configurationRequestChannel.setMethod(KapuaMethod.WRITE);
    ConfigurationRequestPayload configurationRequestPayload = new ConfigurationRequestPayload();
    try {
        DeviceManagementSetting deviceManagementConfig = DeviceManagementSetting.getInstance();
        String charEncoding = deviceManagementConfig.getString(DeviceManagementSettingKey.CHAR_ENCODING);
        StringWriter sw = new StringWriter();
        XmlUtil.marshal(deviceConfiguration, sw);
        byte[] requestBody = sw.toString().getBytes(charEncoding);
        configurationRequestPayload.setBody(requestBody);
    } catch (Exception e) {
        throw new DeviceManagementException(DeviceManagementErrorCodes.REQUEST_EXCEPTION, e, deviceConfiguration);
    }
    ConfigurationRequestMessage configurationRequestMessage = new ConfigurationRequestMessage();
    configurationRequestMessage.setScopeId(scopeId);
    configurationRequestMessage.setDeviceId(deviceId);
    configurationRequestMessage.setCapturedOn(new Date());
    configurationRequestMessage.setPayload(configurationRequestPayload);
    configurationRequestMessage.setChannel(configurationRequestChannel);
    // 
    // Do put
    DeviceCallExecutor deviceApplicationCall = new DeviceCallExecutor(configurationRequestMessage, timeout);
    ConfigurationResponseMessage responseMessage = (ConfigurationResponseMessage) deviceApplicationCall.send();
    // 
    // Create event
    DeviceEventService deviceEventService = locator.getService(DeviceEventService.class);
    DeviceEventFactory deviceEventFactory = locator.getFactory(DeviceEventFactory.class);
    DeviceEventCreator deviceEventCreator = deviceEventFactory.newCreator(scopeId, deviceId, responseMessage.getReceivedOn(), DeviceConfigurationAppProperties.APP_NAME.getValue());
    deviceEventCreator.setPosition(responseMessage.getPosition());
    deviceEventCreator.setSentOn(responseMessage.getSentOn());
    deviceEventCreator.setAction(KapuaMethod.WRITE);
    deviceEventCreator.setResponseCode(responseMessage.getResponseCode());
    deviceEventCreator.setEventMessage(responseMessage.getPayload().toDisplayString());
    deviceEventService.create(deviceEventCreator);
}
Also used : KapuaLocator(org.eclipse.kapua.locator.KapuaLocator) PermissionFactory(org.eclipse.kapua.service.authorization.permission.PermissionFactory) DeviceEventFactory(org.eclipse.kapua.service.device.registry.event.DeviceEventFactory) DeviceManagementException(org.eclipse.kapua.service.device.management.commons.exception.DeviceManagementException) XMLStreamException(javax.xml.stream.XMLStreamException) JAXBException(javax.xml.bind.JAXBException) KapuaIllegalArgumentException(org.eclipse.kapua.KapuaIllegalArgumentException) SAXException(org.xml.sax.SAXException) KapuaException(org.eclipse.kapua.KapuaException) Date(java.util.Date) DeviceManagementException(org.eclipse.kapua.service.device.management.commons.exception.DeviceManagementException) DeviceCallExecutor(org.eclipse.kapua.service.device.management.commons.call.DeviceCallExecutor) ConfigurationRequestMessage(org.eclipse.kapua.service.device.management.configuration.message.internal.ConfigurationRequestMessage) ConfigurationRequestChannel(org.eclipse.kapua.service.device.management.configuration.message.internal.ConfigurationRequestChannel) StringWriter(java.io.StringWriter) AuthorizationService(org.eclipse.kapua.service.authorization.AuthorizationService) DeviceManagementSetting(org.eclipse.kapua.service.device.management.commons.setting.DeviceManagementSetting) DeviceEventService(org.eclipse.kapua.service.device.registry.event.DeviceEventService) DeviceEventCreator(org.eclipse.kapua.service.device.registry.event.DeviceEventCreator) ConfigurationRequestPayload(org.eclipse.kapua.service.device.management.configuration.message.internal.ConfigurationRequestPayload) ConfigurationResponseMessage(org.eclipse.kapua.service.device.management.configuration.message.internal.ConfigurationResponseMessage)

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