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