use of org.eclipse.kapua.service.device.management.packages.model.DevicePackages 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.service.device.management.packages.model.DevicePackages in project kapua by eclipse.
the class Devices method getPackages.
/**
* Returns the list of packages deployed on a device.
*
* @param deviceId
* The client ID of the Device requested.
* @return The list of the installed packages.
*/
@GET
@Path("{deviceId}/packages")
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
public DevicePackages getPackages(@PathParam("deviceId") String deviceId) {
DevicePackages result = packageFactory.newDeviceDeploymentPackages();
try {
KapuaId scopeId = KapuaSecurityUtils.getSession().getScopeId();
KapuaId id = KapuaEid.parseShortId(deviceId);
result = packageService.getInstalled(scopeId, id, null);
} catch (Throwable t) {
handleException(t);
}
return returnNotNullEntity(result);
}
use of org.eclipse.kapua.service.device.management.packages.model.DevicePackages in project kapua by eclipse.
the class GwtDeviceManagementServiceImpl method findDevicePackages.
//
// Packages
//
@Override
public List<GwtDeploymentPackage> findDevicePackages(String scopeShortId, String deviceShortId) throws GwtKapuaException {
List<GwtDeploymentPackage> gwtPkgs = new ArrayList<GwtDeploymentPackage>();
try {
KapuaLocator locator = KapuaLocator.getInstance();
DevicePackageManagementService deviceManagementService = locator.getService(DevicePackageManagementService.class);
KapuaId scopeId = KapuaEid.parseShortId(scopeShortId);
KapuaId deviceId = KapuaEid.parseShortId(deviceShortId);
DevicePackages deploymentPackages = deviceManagementService.getInstalled(scopeId, deviceId, null);
for (DevicePackage deploymentPackage : deploymentPackages.getPackages()) {
GwtDeploymentPackage gwtPkg = new GwtDeploymentPackage();
gwtPkg.setName(deploymentPackage.getName());
gwtPkg.setVersion(deploymentPackage.getVersion());
DevicePackageBundleInfos devicePackageBundleInfos = deploymentPackage.getBundleInfos();
if (devicePackageBundleInfos != null) {
List<GwtBundleInfo> gwtBundleInfos = new ArrayList<GwtBundleInfo>();
for (DevicePackageBundleInfo bundleInfo : devicePackageBundleInfos.getBundlesInfos()) {
GwtBundleInfo gwtBundleInfo = new GwtBundleInfo();
gwtBundleInfo.setName(bundleInfo.getName());
gwtBundleInfo.setVersion(bundleInfo.getVersion());
gwtBundleInfos.add(gwtBundleInfo);
}
gwtPkg.setBundleInfos(gwtBundleInfos);
}
gwtPkgs.add(gwtPkg);
}
} catch (Throwable t) {
KapuaExceptionHandler.handle(t);
}
return gwtPkgs;
}
use of org.eclipse.kapua.service.device.management.packages.model.DevicePackages in project kapua by eclipse.
the class DevicePackageManagementServiceImpl method getInstalled.
@Override
public DevicePackages getInstalled(KapuaId scopeId, KapuaId deviceId, Long timeout) throws KapuaException {
//
// Argument Validation
ArgumentValidator.notNull(scopeId, "scopeId");
ArgumentValidator.notNull(deviceId, "deviceId");
//
// 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.read, scopeId));
//
// Prepare the request
PackageRequestChannel packageRequestChannel = new PackageRequestChannel();
packageRequestChannel.setAppName(PackageAppProperties.APP_NAME);
packageRequestChannel.setVersion(PackageAppProperties.APP_VERSION);
packageRequestChannel.setMethod(KapuaMethod.READ);
packageRequestChannel.setPackageResource(null);
PackageRequestPayload packageRequestPayload = new PackageRequestPayload();
PackageRequestMessage packageRequestMessage = new PackageRequestMessage();
packageRequestMessage.setScopeId(scopeId);
packageRequestMessage.setDeviceId(deviceId);
packageRequestMessage.setCapturedOn(new Date());
packageRequestMessage.setPayload(packageRequestPayload);
packageRequestMessage.setChannel(packageRequestChannel);
//
// Do get
@SuppressWarnings({ "rawtypes", "unchecked" }) DeviceCallExecutor deviceApplicationCall = new DeviceCallExecutor(packageRequestMessage, timeout);
PackageResponseMessage responseMessage = (PackageResponseMessage) deviceApplicationCall.send();
//
// Parse the response
PackageResponsePayload responsePayload = responseMessage.getPayload();
DevicePackages devicePackages;
if (responsePayload.getBody() != null) {
DeviceManagementSetting config = DeviceManagementSetting.getInstance();
String charEncoding = config.getString(DeviceManagementSettingKey.CHAR_ENCODING);
String body = null;
try {
body = new String(responsePayload.getBody(), charEncoding);
} catch (Exception e) {
throw new DeviceManagementException(DeviceManagementErrorCodes.RESPONSE_PARSE_EXCEPTION, e, responsePayload.getBody());
}
try {
devicePackages = XmlUtil.unmarshal(body, DevicePackagesImpl.class);
} catch (Exception e) {
throw new DeviceManagementException(DeviceManagementErrorCodes.RESPONSE_PARSE_EXCEPTION, e, body);
}
} else {
devicePackages = new DevicePackagesImpl();
}
//
// Create event
DeviceEventService deviceEventService = locator.getService(DeviceEventService.class);
DeviceEventFactory deviceEventFactory = locator.getFactory(DeviceEventFactory.class);
DeviceEventCreator deviceEventCreator = deviceEventFactory.newCreator(scopeId, deviceId, responseMessage.getReceivedOn(), PackageAppProperties.APP_NAME.getValue());
deviceEventCreator.setPosition(responseMessage.getPosition());
deviceEventCreator.setSentOn(responseMessage.getSentOn());
deviceEventCreator.setAction(KapuaMethod.READ);
deviceEventCreator.setResponseCode(responseMessage.getResponseCode());
deviceEventCreator.setEventMessage(responseMessage.getPayload().toDisplayString());
deviceEventService.create(deviceEventCreator);
return devicePackages;
}
Aggregations