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