From d99722dcdfe29f0966699f4ea31289f8135b9d7a Mon Sep 17 00:00:00 2001 From: Alberto Codutti Date: Fri, 22 Jan 2021 17:06:55 +0100 Subject: [PATCH] Refactored 'character.encoding' in 'device.management.character.encoding' Signed-off-by: Alberto Codutti --- assembly/api/docker/Dockerfile | 1 + assembly/console/entrypoint/run-console | 4 ++ deployment/minishift/README.md | 51 ++++--------------- deployment/openshift/README.md | 41 ++++----------- .../setting/DeviceManagementSettingKey.java | 2 +- .../device-management-setting.properties | 2 +- .../message/internal/AssetRequestPayload.java | 4 +- .../internal/AssetResponsePayload.java | 4 +- .../DeviceBundleManagementServiceImpl.java | 11 ++-- ...iceConfigurationManagementServiceImpl.java | 17 ++----- .../DeviceSnapshotManagementServiceImpl.java | 11 ++-- .../DevicePackageManagementServiceImpl.java | 23 +++------ .../kapua/TranslatorAppBundleKuraKapua.java | 12 ++--- .../TranslatorAppConfigurationKuraKapua.java | 13 +++-- .../kapua/TranslatorAppPackageKuraKapua.java | 14 ++--- .../kapua/TranslatorAppSnapshotKuraKapua.java | 16 +++--- 16 files changed, 80 insertions(+), 146 deletions(-) diff --git a/assembly/api/docker/Dockerfile b/assembly/api/docker/Dockerfile index 2310111a9b5..0c3f7eb8763 100644 --- a/assembly/api/docker/Dockerfile +++ b/assembly/api/docker/Dockerfile @@ -37,6 +37,7 @@ ENV JAVA_OPTS "-Dcommons.db.schema.update=true \ -Dcertificate.jwt.private.key=file:///var/opt/jetty/key.pk8 \ -Dcertificate.jwt.certificate=file:///var/opt/jetty/cert.pem \ -Ddatastore.disable=\${KAPUA_DISABLE_DATASTORE:-false} \ + -Ddevice.management.response.stacktrace.show=\${DEVICE_MANAGEMENT_RESPONSE_STACKTRACE_SHOW:-false} \ -Djavax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory" USER 0 diff --git a/assembly/console/entrypoint/run-console b/assembly/console/entrypoint/run-console index c63ddbca86a..0234bae871b 100755 --- a/assembly/console/entrypoint/run-console +++ b/assembly/console/entrypoint/run-console @@ -59,6 +59,10 @@ test -n "${MFA_SCRATCH_CODES_NUMBER}" && JAVA_OPTS="${JAVA_OPTS} -Dauthenticatio test -n "${MFA_CODE_DIGITS_NUMBER}" && JAVA_OPTS="${JAVA_OPTS} -Dauthentication.mfa.code.digits.number=${MFA_CODE_DIGITS_NUMBER}" test -n "${MFA_TRUST_KEY_DURATION}" && JAVA_OPTS="${JAVA_OPTS} -Dauthentication.mfa.trust.key.duration=${MFA_TRUST_KEY_DURATION}" +# Device Mannagement Configurations +: DEVICE_MANAGEMENT_RESPONSE_STACKTRACE_SHOW=${DEVICE_MANAGEMENT_RESPONSE_STACKTRACE_SHOW:=false } +test -n "${DEVICE_MANAGEMENT_RESPONSE_STACKTRACE_SHOW}" && JAVA_OPTS="${JAVA_OPTS} -Ddevice.management.response.stacktrace.show${DEVICE_MANAGEMENT_RESPONSE_STACKTRACE_SHOW}" + export JAVA_OPTS # Continue with startup diff --git a/deployment/minishift/README.md b/deployment/minishift/README.md index f875259e4f9..5daf4636680 100644 --- a/deployment/minishift/README.md +++ b/deployment/minishift/README.md @@ -1,7 +1,6 @@ # Kapua on Minishift -To learn more on how to run Kapua in Minishift, please -consult [developer manual](https://github.com/eclipse/kapua/blob/develop/docs/developer-guide/en/running.md#minishift). +To learn more on how to run Kapua in Minishift, please consult [developer manual](https://github.com/eclipse/kapua/blob/develop/docs/developer-guide/en/running.md#minishift). The most recent version of the documentation can be viewed online at: @@ -14,49 +13,37 @@ You are required to have _Docker_, _Minishift_ and _VirtualBox_ installed.
VirtualBox will be used as VM driver for Minishift. ### Fetching Docker images - You'll need to have the images on your local Docker registry.
You can pull them from a Docker registry or build them from the code. -If you choose to build them from the code please go -to [Building containers from scratch](#Building-containers-from-scratch) section. +If you choose to build them from the code please go to [Building containers from scratch](#Building-containers-from-scratch) section. If you want to pull them from a Docker registry, run: - ```bash ./minishift-pull-images.sh ``` ### Running - First, you need the create and start the Minishift VM.
Run: - ```bash ./minishift-initialize.sh ``` - -This script will set _VirtualBox_ as VM driver of Minishift and start a Minishift VM with 6GB of RAM, 3 CPUs and 20GB of -storage. +This script will set _VirtualBox_ as VM driver of Minishift and start a Minishift VM with 6GB of RAM, 3 CPUs and 20GB of storage. After the Minishift VM has started you can deploy the Kapua components by running: - ```bash ./minishift-deploy.sh ``` - This script will create new apps for each Kapua component. ### Accessing components - After deployment and startup of containers, they can be accessed at the following endpoints | Application/Service | Endpoint | User | Password | Others | |---------------------|--------------------------------------------------|--------------|----------------|-----------------------------------| -| H2 SQL | _None_ | _None_ | _ -None_ | This service is not exposed | -| Elasticsearch | _None_ | _None_ | _ -None_ | This service is not exposed | +| H2 SQL | _None_ | _None_ | _None_ | This service is not exposed | +| Elasticsearch | _None_ | _None_ | _None_ | This service is not exposed | | Broker | broker-eclipse-kapua.192.168.99.100.nip.io:31883 | kapua-broker | kapua-password | | | Admin WEB Console | console-eclipse-kapua.192.168.99.100.nip.io:80 | kapua-sys | kapua-password | | | REST API endpoint | api-eclipse-kapua.192.168.99.100.nip.io:80 | kapua-sys | kapua-password | API KEY: `12345678kapua-password` | @@ -64,29 +51,21 @@ None_ | This service is not exposed | Please note that endpoints are available only from the host machine. ### Checking - -You can check the status of the pods using the OpenShift Client from the command line or accessing the OpenShift Web -Console. +You can check the status of the pods using the OpenShift Client from the command line or accessing the OpenShift Web Console. To access the OpenShift Web Console you can run: - ```bash minishift dashboard ``` ### Tear down - To stop and remove the Eclipse Kapua project from OpenShift, run: - ```bash ./minishift-undeploy.sh ``` - -This command can take a while to be executed. Even if the `Eclipse Kapua` project has disappeared, processing still -going on underneath. +This command can take a while to be executed. Even if the `Eclipse Kapua` project has disappeared, processing still going on underneath. To destroy the Minishift VM, runL - ```bash ./minishift-destroy.sh ``` @@ -94,42 +73,34 @@ To destroy the Minishift VM, runL ### Advanced options #### Setting the Kapua version - Other than the default deployment it is possible to run other versions of Kapua. -By default the `latest` version of Kapua will be brought up. You can change the version of Kapua by exporting the -environment variable `IMAGE_VERSION`. +By default the `latest` version of Kapua will be brought up. +You can change the version of Kapua by exporting the environment variable `IMAGE_VERSION`. Example: - ```bash export IMAGE_VERSION=1.0.0 ``` #### Passing additional JAVA_OPTS - -If you want to pass to the JVM additional optional parameters you can set the `JAVA_OPTS_EXTRA` environment -variable.
+If you want to pass to the JVM additional optional parameters you can set the `JAVA_OPTS_EXTRA` environment variable.
Example: - ```bash export JAVA_OPTS_EXTRA="-Ddevice.management.request.timeout=60000" ``` #### Building containers from scratch - If you want to build containers from the code, you'll need to build the whole Kapua Project. From the project root directory, run: - ```bash mvn clean install -Pdocker ``` To build also the Admin Web Console container, which is excluded by default, add the `console` profile: - ```bash mvn clean install -Pconsole,docker ``` -After the build has completed follow the steps from the [Running](#Running) section. +After the build has completed follow the steps from the [Running](#Running) section. \ No newline at end of file diff --git a/deployment/openshift/README.md b/deployment/openshift/README.md index 974e51634ce..2a2af423b3f 100644 --- a/deployment/openshift/README.md +++ b/deployment/openshift/README.md @@ -1,7 +1,6 @@ # Kapua on OpenShift -To learn more on how to run Kapua in Openshift, please -consult [developer manual](https://github.com/eclipse/kapua/blob/develop/docs/developer-guide/en/running.md#openshift). +To learn more on how to run Kapua in Openshift, please consult [developer manual](https://github.com/eclipse/kapua/blob/develop/docs/developer-guide/en/running.md#openshift). The most recent version of the documentation can be viewed online at: @@ -11,104 +10,82 @@ The most recent version of the documentation can be viewed online at: ## How to run Kapua on OpenShift. You are required to have OpenShift Origin installed.
-If you are running on MacOS or Windows machine, please go to the _Minishift_ deployment. OpenShift Origin is supported -only on Linux OSs. +If you are running on MacOS or Windows machine, please go to the _Minishift_ deployment. +OpenShift Origin is supported only on Linux OSs. ### Running - First, you need the OpenShift client and a OpenShift Origin cluster running.
Just run: - ```bash ./openshift-start.sh ``` - This script will download the OpenShift Client and start the OpenShift cluster. Then you need to initialize the OpenShift Cluster by running: - ```bash ./openshift-initialize.sh ``` - This script will create a new project into OpenShift. Final step of the deployment, is the deployment of the Docker images themself.
Run: - ```bash ./openshift-deploy.sh ``` - This script will create new apps for each Kapua component. ### Accessing components - After deployment and startup of containers, they can be accessed at the following endpoints | Application/Service | Endpoint | User | Password | Others | |---------------------|-------------------------------------------------------|--------------|----------------|-----------------------------------| -| H2 SQL | _None_ | _None_ | _ -None_ | This service is not exposed | -| Elasticsearch | _None_ | _None_ | _ -None_ | This service is not exposed | +| H2 SQL | _None_ | _None_ | _None_ | This service is not exposed | +| Elasticsearch | _None_ | _None_ | _None_ | This service is not exposed | | Broker | broker-eclipse-kapua.:31883 | kapua-broker | kapua-password | | | Admin WEB Console | console-eclipse-kapua.:80 | kapua-sys | kapua-password | | | REST API endpoint | api-eclipse-kapua.:80 | kapua-sys | kapua-password | API KEY: `12345678kapua-password` | ### Checking +You can check the status of the pods using the OpenShift Client from the command line or accessing the OpenShift Web Console. -You can check the status of the pods using the OpenShift Client from the command line or accessing the OpenShift Web -Console. ### Tear down - To stop and remove the Eclipse Kapua project from OpenShift, run: - ```bash ./openshift-destroy.sh ``` - After this command, you'll need to run again `./openshift-initialize.sh` if you want to deploy again Kapua. ### Advanced options #### Setting the Kapua version - Other than the default deployment it is possible to run other versions of Kapua. -By default the `latest` version of Kapua will be brought up. You can change the version of Kapua by exporting the -environment variable `IMAGE_VERSION`. +By default the `latest` version of Kapua will be brought up. +You can change the version of Kapua by exporting the environment variable `IMAGE_VERSION`. Example: - ```bash export IMAGE_VERSION=1.0.0 ``` #### Passing additional JAVA_OPTS - -If you want to pass to the JVM additional optional parameters you can set the `JAVA_OPTS_EXTRA` environment -variable.
+If you want to pass to the JVM additional optional parameters you can set the `JAVA_OPTS_EXTRA` environment variable.
Example: - ```bash export JAVA_OPTS_EXTRA="-Ddevice.management.request.timeout=60000" ``` #### Building containers from scratch - If you want to build containers from the code, you'll need to build the whole Kapua Project. From the project root directory, run: - ```bash mvn clean install -f external/pom.xml mvn clean install -Pdocker ``` To build also the Admin Web Console container, which is excluded by default, add the `console` profile: - ```bash mvn clean install -f external/pom.xml mvn clean install -Pconsole,docker diff --git a/service/device/commons/src/main/java/org/eclipse/kapua/service/device/management/commons/setting/DeviceManagementSettingKey.java b/service/device/commons/src/main/java/org/eclipse/kapua/service/device/management/commons/setting/DeviceManagementSettingKey.java index f081edd0f00..2fb66b12592 100644 --- a/service/device/commons/src/main/java/org/eclipse/kapua/service/device/management/commons/setting/DeviceManagementSettingKey.java +++ b/service/device/commons/src/main/java/org/eclipse/kapua/service/device/management/commons/setting/DeviceManagementSettingKey.java @@ -26,7 +26,7 @@ public enum DeviceManagementSettingKey implements SettingKey { * * @since 1.0.0 */ - CHAR_ENCODING("character.encoding"), + CHAR_ENCODING("device.management.character.encoding"), /** * Request timeout. diff --git a/service/device/commons/src/main/resources/device-management-setting.properties b/service/device/commons/src/main/resources/device-management-setting.properties index 6193af20601..6b178e0b6b4 100644 --- a/service/device/commons/src/main/resources/device-management-setting.properties +++ b/service/device/commons/src/main/resources/device-management-setting.properties @@ -11,7 +11,7 @@ # Eurotech - initial API and implementation # ############################################################################### -character.encoding=UTF-8 +device.management.character.encoding=UTF-8 device.management.request.timeout=30000 diff --git a/service/device/management/asset/internal/src/main/java/org/eclipse/kapua/service/device/management/asset/message/internal/AssetRequestPayload.java b/service/device/management/asset/internal/src/main/java/org/eclipse/kapua/service/device/management/asset/message/internal/AssetRequestPayload.java index a2635aae5ee..77aecb67378 100644 --- a/service/device/management/asset/internal/src/main/java/org/eclipse/kapua/service/device/management/asset/message/internal/AssetRequestPayload.java +++ b/service/device/management/asset/internal/src/main/java/org/eclipse/kapua/service/device/management/asset/message/internal/AssetRequestPayload.java @@ -35,8 +35,8 @@ public class AssetRequestPayload extends KapuaPayloadImpl implements KapuaRequestPayload { private static final DeviceAssetFactory DEVICE_ASSET_FACTORY = KapuaLocator.getInstance().getFactory(DeviceAssetFactory.class); - private static final DeviceManagementSetting CONFIG = DeviceManagementSetting.getInstance(); - private static final String CHAR_ENCODING = CONFIG.getString(DeviceManagementSettingKey.CHAR_ENCODING); + + private static final String CHAR_ENCODING = DeviceManagementSetting.getInstance().getString(DeviceManagementSettingKey.CHAR_ENCODING); public DeviceAssets getDeviceAssets() throws JAXBException, XMLStreamException, FactoryConfigurationError, SAXException, UnsupportedEncodingException { DeviceAssets deviceAssets = DEVICE_ASSET_FACTORY.newAssetListResult(); diff --git a/service/device/management/asset/internal/src/main/java/org/eclipse/kapua/service/device/management/asset/message/internal/AssetResponsePayload.java b/service/device/management/asset/internal/src/main/java/org/eclipse/kapua/service/device/management/asset/message/internal/AssetResponsePayload.java index b153c99b6dd..9f02c56499e 100644 --- a/service/device/management/asset/internal/src/main/java/org/eclipse/kapua/service/device/management/asset/message/internal/AssetResponsePayload.java +++ b/service/device/management/asset/internal/src/main/java/org/eclipse/kapua/service/device/management/asset/message/internal/AssetResponsePayload.java @@ -35,8 +35,8 @@ public class AssetResponsePayload extends KapuaResponsePayloadImpl implements KapuaResponsePayload { private static final DeviceAssetFactory DEVICE_ASSET_FACTORY = KapuaLocator.getInstance().getFactory(DeviceAssetFactory.class); - private static final DeviceManagementSetting CONFIG = DeviceManagementSetting.getInstance(); - private static final String CHAR_ENCODING = CONFIG.getString(DeviceManagementSettingKey.CHAR_ENCODING); + + private static final String CHAR_ENCODING = DeviceManagementSetting.getInstance().getString(DeviceManagementSettingKey.CHAR_ENCODING); public DeviceAssets getDeviceAssets() throws JAXBException, XMLStreamException, FactoryConfigurationError, SAXException, UnsupportedEncodingException { DeviceAssets deviceAssets = DEVICE_ASSET_FACTORY.newAssetListResult(); diff --git a/service/device/management/bundle/internal/src/main/java/org/eclipse/kapua/service/device/management/bundle/internal/DeviceBundleManagementServiceImpl.java b/service/device/management/bundle/internal/src/main/java/org/eclipse/kapua/service/device/management/bundle/internal/DeviceBundleManagementServiceImpl.java index 2250c708bfd..4a7e9696b51 100644 --- a/service/device/management/bundle/internal/src/main/java/org/eclipse/kapua/service/device/management/bundle/internal/DeviceBundleManagementServiceImpl.java +++ b/service/device/management/bundle/internal/src/main/java/org/eclipse/kapua/service/device/management/bundle/internal/DeviceBundleManagementServiceImpl.java @@ -50,6 +50,8 @@ public class DeviceBundleManagementServiceImpl extends AbstractDeviceManagementS private static final String SCOPE_ID = "scopeId"; private static final String DEVICE_ID = "deviceId"; + private static final String CHAR_ENCODING = DeviceManagementSetting.getInstance().getString(DeviceManagementSettingKey.CHAR_ENCODING); + @Override public DeviceBundles get(KapuaId scopeId, KapuaId deviceId, Long timeout) throws KapuaException { @@ -92,17 +94,14 @@ public DeviceBundles get(KapuaId scopeId, KapuaId deviceId, Long timeout) if (responseMessage.getResponseCode().isAccepted()) { BundleResponsePayload responsePayload = responseMessage.getPayload(); - DeviceManagementSetting config = DeviceManagementSetting.getInstance(); - String charEncoding = config.getString(DeviceManagementSettingKey.CHAR_ENCODING); - - String body = null; + String body; try { - body = new String(responsePayload.getBody(), charEncoding); + body = new String(responsePayload.getBody(), CHAR_ENCODING); } catch (Exception e) { throw new DeviceManagementResponseException(e, responsePayload.getBody()); } - DeviceBundles deviceBundleList = null; + DeviceBundles deviceBundleList; try { deviceBundleList = XmlUtil.unmarshal(body, DeviceBundlesImpl.class); } catch (Exception e) { diff --git a/service/device/management/configuration/internal/src/main/java/org/eclipse/kapua/service/device/management/configuration/internal/DeviceConfigurationManagementServiceImpl.java b/service/device/management/configuration/internal/src/main/java/org/eclipse/kapua/service/device/management/configuration/internal/DeviceConfigurationManagementServiceImpl.java index 2e3e9c7b720..c9412255e82 100644 --- a/service/device/management/configuration/internal/src/main/java/org/eclipse/kapua/service/device/management/configuration/internal/DeviceConfigurationManagementServiceImpl.java +++ b/service/device/management/configuration/internal/src/main/java/org/eclipse/kapua/service/device/management/configuration/internal/DeviceConfigurationManagementServiceImpl.java @@ -55,6 +55,8 @@ @KapuaProvider public class DeviceConfigurationManagementServiceImpl extends AbstractDeviceManagementServiceImpl implements DeviceConfigurationManagementService { + private static final String CHAR_ENCODING = DeviceManagementSetting.getInstance().getString(DeviceManagementSettingKey.CHAR_ENCODING); + private static final DeviceConfigurationFactory DEVICE_CONFIGURATION_FACTORY = LOCATOR.getFactory(DeviceConfigurationFactory.class); private static final String SCOPE_ID = "scopeId"; @@ -104,14 +106,11 @@ public DeviceConfiguration get(KapuaId scopeId, KapuaId deviceId, String configu if (responseMessage.getResponseCode().isAccepted()) { ConfigurationResponsePayload responsePayload = responseMessage.getPayload(); - DeviceManagementSetting config = DeviceManagementSetting.getInstance(); - String charEncoding = config.getString(DeviceManagementSettingKey.CHAR_ENCODING); - DeviceConfiguration deviceConfiguration = null; if (responsePayload.hasBody()) { String body = null; try { - body = new String(responsePayload.getBody(), charEncoding); + body = new String(responsePayload.getBody(), CHAR_ENCODING); } catch (Exception e) { throw new DeviceManagementResponseException(e, responsePayload.getBody()); } @@ -160,12 +159,9 @@ public void put(KapuaId scopeId, KapuaId deviceId, DeviceComponentConfiguration DeviceConfiguration deviceConfiguration = DEVICE_CONFIGURATION_FACTORY.newConfigurationInstance(); deviceConfiguration.getComponentConfigurations().add(deviceComponentConfiguration); - 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); + byte[] requestBody = sw.toString().getBytes(CHAR_ENCODING); configurationRequestPayload.setBody(requestBody); } catch (Exception e) { @@ -234,12 +230,9 @@ public void put(KapuaId scopeId, KapuaId deviceId, DeviceConfiguration deviceCon 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); + byte[] requestBody = sw.toString().getBytes(CHAR_ENCODING); configurationRequestPayload.setBody(requestBody); } catch (Exception e) { diff --git a/service/device/management/configuration/internal/src/main/java/org/eclipse/kapua/service/device/management/snapshot/internal/DeviceSnapshotManagementServiceImpl.java b/service/device/management/configuration/internal/src/main/java/org/eclipse/kapua/service/device/management/snapshot/internal/DeviceSnapshotManagementServiceImpl.java index ed11f1be3c1..1c0d7a96aa7 100644 --- a/service/device/management/configuration/internal/src/main/java/org/eclipse/kapua/service/device/management/snapshot/internal/DeviceSnapshotManagementServiceImpl.java +++ b/service/device/management/configuration/internal/src/main/java/org/eclipse/kapua/service/device/management/snapshot/internal/DeviceSnapshotManagementServiceImpl.java @@ -46,6 +46,8 @@ @KapuaProvider public class DeviceSnapshotManagementServiceImpl extends AbstractDeviceManagementServiceImpl implements DeviceSnapshotManagementService { + private static final String CHAR_ENCODING = DeviceManagementSetting.getInstance().getString(DeviceManagementSettingKey.CHAR_ENCODING); + @Override public DeviceSnapshots get(KapuaId scopeId, KapuaId deviceId, Long timeout) throws KapuaException { @@ -88,17 +90,14 @@ public DeviceSnapshots get(KapuaId scopeId, KapuaId deviceId, Long timeout) if (responseMessage.getResponseCode().isAccepted()) { SnapshotResponsePayload responsePayload = responseMessage.getPayload(); - DeviceManagementSetting config = DeviceManagementSetting.getInstance(); - String charEncoding = config.getString(DeviceManagementSettingKey.CHAR_ENCODING); - - String body = null; + String body; try { - body = new String(responsePayload.getBody(), charEncoding); + body = new String(responsePayload.getBody(), CHAR_ENCODING); } catch (Exception e) { throw new DeviceManagementResponseException(e, (Object) responsePayload.getBody()); } - DeviceSnapshots deviceSnapshots = null; + DeviceSnapshots deviceSnapshots; try { deviceSnapshots = XmlUtil.unmarshal(body, DeviceSnapshotsImpl.class); } catch (Exception e) { diff --git a/service/device/management/packages/internal/src/main/java/org/eclipse/kapua/service/device/management/packages/internal/DevicePackageManagementServiceImpl.java b/service/device/management/packages/internal/src/main/java/org/eclipse/kapua/service/device/management/packages/internal/DevicePackageManagementServiceImpl.java index 46e90a89979..615e5d5ae69 100644 --- a/service/device/management/packages/internal/src/main/java/org/eclipse/kapua/service/device/management/packages/internal/DevicePackageManagementServiceImpl.java +++ b/service/device/management/packages/internal/src/main/java/org/eclipse/kapua/service/device/management/packages/internal/DevicePackageManagementServiceImpl.java @@ -74,10 +74,12 @@ @KapuaProvider public class DevicePackageManagementServiceImpl extends AbstractDeviceManagementServiceImpl implements DevicePackageManagementService { - private static final DevicePackageFactory DEVICE_PACKAGE_FACTORY = LOCATOR.getFactory(DevicePackageFactory.class); + private static final String CHAR_ENCODING = DeviceManagementSetting.getInstance().getString(DeviceManagementSettingKey.CHAR_ENCODING); private static final PackageManagementServiceSetting PACKAGE_MANAGEMENT_SERVICE_SETTING = PackageManagementServiceSetting.getInstance(); + private static final DevicePackageFactory DEVICE_PACKAGE_FACTORY = LOCATOR.getFactory(DevicePackageFactory.class); + private static final String SCOPE_ID = "scopeId"; private static final String DEVICE_ID = "deviceId"; @@ -129,12 +131,10 @@ public DevicePackages getInstalled(KapuaId scopeId, KapuaId deviceId, Long timeo DevicePackages devicePackages; if (responsePayload.hasBody()) { - DeviceManagementSetting config = DeviceManagementSetting.getInstance(); - String charEncoding = config.getString(DeviceManagementSettingKey.CHAR_ENCODING); String body; try { - body = new String(responsePayload.getBody(), charEncoding); + body = new String(responsePayload.getBody(), CHAR_ENCODING); } catch (Exception e) { throw new DeviceManagementResponseException(e, responsePayload.getBody()); } @@ -143,8 +143,8 @@ public DevicePackages getInstalled(KapuaId scopeId, KapuaId deviceId, Long timeo devicePackages = XmlUtil.unmarshal(body, DevicePackagesImpl.class); } catch (Exception e) { throw new DeviceManagementResponseException(e, body); - } + } else { devicePackages = new DevicePackagesImpl(); } @@ -487,12 +487,9 @@ public DevicePackageInstallOperation installStatus(KapuaId scopeId, KapuaId devi if (responseMessage.getResponseCode().isAccepted()) { PackageResponsePayload responsePayload = responseMessage.getPayload(); - DeviceManagementSetting config = DeviceManagementSetting.getInstance(); - String charEncoding = config.getString(DeviceManagementSettingKey.CHAR_ENCODING); - String body; try { - body = new String(responsePayload.getBody(), charEncoding); + body = new String(responsePayload.getBody(), CHAR_ENCODING); } catch (Exception e) { throw new DeviceManagementResponseException(e, responsePayload.getBody()); @@ -503,7 +500,6 @@ public DevicePackageInstallOperation installStatus(KapuaId scopeId, KapuaId devi installOperation = XmlUtil.unmarshal(body, DevicePackageInstallOperationImpl.class); } catch (Exception e) { throw new DeviceManagementResponseException(e, body); - } return installOperation; @@ -639,15 +635,11 @@ public DevicePackageUninstallOperation uninstallStatus(KapuaId scopeId, KapuaId if (responseMessage.getResponseCode().isAccepted()) { PackageResponsePayload responsePayload = responseMessage.getPayload(); - DeviceManagementSetting config = DeviceManagementSetting.getInstance(); - String charEncoding = config.getString(DeviceManagementSettingKey.CHAR_ENCODING); - String body; try { - body = new String(responsePayload.getBody(), charEncoding); + body = new String(responsePayload.getBody(), CHAR_ENCODING); } catch (Exception e) { throw new DeviceManagementResponseException(e, responsePayload.getBody()); - } DevicePackageUninstallOperation uninstallOperation; @@ -655,7 +647,6 @@ public DevicePackageUninstallOperation uninstallStatus(KapuaId scopeId, KapuaId uninstallOperation = XmlUtil.unmarshal(body, DevicePackageUninstallOperationImpl.class); } catch (Exception e) { throw new DeviceManagementResponseException(e, body); - } return uninstallOperation; diff --git a/translator/kapua/kura/src/main/java/org/eclipse/kapua/translator/kura/kapua/TranslatorAppBundleKuraKapua.java b/translator/kapua/kura/src/main/java/org/eclipse/kapua/translator/kura/kapua/TranslatorAppBundleKuraKapua.java index b203de567e1..60dbb9aedbc 100644 --- a/translator/kapua/kura/src/main/java/org/eclipse/kapua/translator/kura/kapua/TranslatorAppBundleKuraKapua.java +++ b/translator/kapua/kura/src/main/java/org/eclipse/kapua/translator/kura/kapua/TranslatorAppBundleKuraKapua.java @@ -46,6 +46,8 @@ */ public class TranslatorAppBundleKuraKapua extends AbstractSimpleTranslatorResponseKuraKapua { + private static final String CHAR_ENCODING = DeviceManagementSetting.getInstance().getString(DeviceManagementSettingKey.CHAR_ENCODING); + private static final KapuaLocator LOCATOR = KapuaLocator.getInstance(); public TranslatorAppBundleKuraKapua() { @@ -74,12 +76,10 @@ protected BundleResponsePayload translatePayload(KuraResponsePayload kuraRespons BundleResponsePayload bundleResponsePayload = TranslatorKuraKapuaUtils.buildBaseResponsePayload(kuraResponsePayload, new BundleResponsePayload()); if (kuraResponsePayload.hasBody()) { - DeviceManagementSetting config = DeviceManagementSetting.getInstance(); - String charEncoding = config.getString(DeviceManagementSettingKey.CHAR_ENCODING); String body; try { - body = new String(kuraResponsePayload.getBody(), charEncoding); + body = new String(kuraResponsePayload.getBody(), CHAR_ENCODING); } catch (Exception e) { throw new TranslatorException(TranslatorErrorCodes.INVALID_PAYLOAD, e, kuraResponsePayload.getBody()); } @@ -91,7 +91,7 @@ protected BundleResponsePayload translatePayload(KuraResponsePayload kuraRespons throw new TranslatorException(TranslatorErrorCodes.INVALID_PAYLOAD, e, body); } - translate(bundleResponsePayload, charEncoding, kuraBundles); + translate(bundleResponsePayload, kuraBundles); } // Return Kapua Payload @@ -103,7 +103,7 @@ protected BundleResponsePayload translatePayload(KuraResponsePayload kuraRespons } } - private void translate(BundleResponsePayload bundleResponsePayload, String charEncoding, KuraBundles kuraBundles) throws KapuaException { + private void translate(BundleResponsePayload bundleResponsePayload, KuraBundles kuraBundles) throws KapuaException { try { DeviceBundleFactory deviceBundleFactory = LOCATOR.getFactory(DeviceBundleFactory.class); @@ -121,7 +121,7 @@ private void translate(BundleResponsePayload bundleResponsePayload, String charE StringWriter sw = new StringWriter(); XmlUtil.marshal(deviceBundles, sw); - byte[] requestBody = sw.toString().getBytes(charEncoding); + byte[] requestBody = sw.toString().getBytes(CHAR_ENCODING); bundleResponsePayload.setBody(requestBody); } catch (Exception e) { diff --git a/translator/kapua/kura/src/main/java/org/eclipse/kapua/translator/kura/kapua/TranslatorAppConfigurationKuraKapua.java b/translator/kapua/kura/src/main/java/org/eclipse/kapua/translator/kura/kapua/TranslatorAppConfigurationKuraKapua.java index aa67bbd54aa..70c6c6b559f 100644 --- a/translator/kapua/kura/src/main/java/org/eclipse/kapua/translator/kura/kapua/TranslatorAppConfigurationKuraKapua.java +++ b/translator/kapua/kura/src/main/java/org/eclipse/kapua/translator/kura/kapua/TranslatorAppConfigurationKuraKapua.java @@ -52,6 +52,8 @@ */ public class TranslatorAppConfigurationKuraKapua extends AbstractSimpleTranslatorResponseKuraKapua { + private static final String CHAR_ENCODING = DeviceManagementSetting.getInstance().getString(DeviceManagementSettingKey.CHAR_ENCODING); + public TranslatorAppConfigurationKuraKapua() { super(ConfigurationResponseMessage.class); } @@ -77,13 +79,10 @@ protected ConfigurationResponsePayload translatePayload(KuraResponsePayload kura try { ConfigurationResponsePayload configurationResponsePayload = TranslatorKuraKapuaUtils.buildBaseResponsePayload(kuraResponsePayload, new ConfigurationResponsePayload()); - DeviceManagementSetting config = DeviceManagementSetting.getInstance(); - String charEncoding = config.getString(DeviceManagementSettingKey.CHAR_ENCODING); - if (kuraResponsePayload.hasBody()) { String body; try { - body = new String(kuraResponsePayload.getBody(), charEncoding); + body = new String(kuraResponsePayload.getBody(), CHAR_ENCODING); } catch (Exception e) { throw new TranslatorException(TranslatorErrorCodes.INVALID_PAYLOAD, e, (Object) configurationResponsePayload.getBody()); } @@ -95,7 +94,7 @@ protected ConfigurationResponsePayload translatePayload(KuraResponsePayload kura throw new TranslatorException(TranslatorErrorCodes.INVALID_PAYLOAD, e, body); } - translateBody(configurationResponsePayload, charEncoding, kuraDeviceConfiguration); + translateBody(configurationResponsePayload, kuraDeviceConfiguration); } // Return Kapua Payload @@ -107,7 +106,7 @@ protected ConfigurationResponsePayload translatePayload(KuraResponsePayload kura } } - private void translateBody(ConfigurationResponsePayload configurationResponsePayload, String charEncoding, KuraDeviceConfiguration kuraDeviceConfiguration) + private void translateBody(ConfigurationResponsePayload configurationResponsePayload, KuraDeviceConfiguration kuraDeviceConfiguration) throws TranslatorException { try { DeviceConfigurationImpl deviceConfiguration = new DeviceConfigurationImpl(); @@ -129,7 +128,7 @@ private void translateBody(ConfigurationResponsePayload configurationResponsePay StringWriter sw = new StringWriter(); XmlUtil.marshal(deviceConfiguration, sw); - byte[] requestBody = sw.toString().getBytes(charEncoding); + byte[] requestBody = sw.toString().getBytes(CHAR_ENCODING); configurationResponsePayload.setBody(requestBody); } catch (Exception e) { diff --git a/translator/kapua/kura/src/main/java/org/eclipse/kapua/translator/kura/kapua/TranslatorAppPackageKuraKapua.java b/translator/kapua/kura/src/main/java/org/eclipse/kapua/translator/kura/kapua/TranslatorAppPackageKuraKapua.java index 9243b4e2959..404f529e142 100644 --- a/translator/kapua/kura/src/main/java/org/eclipse/kapua/translator/kura/kapua/TranslatorAppPackageKuraKapua.java +++ b/translator/kapua/kura/src/main/java/org/eclipse/kapua/translator/kura/kapua/TranslatorAppPackageKuraKapua.java @@ -53,6 +53,8 @@ */ public class TranslatorAppPackageKuraKapua extends AbstractSimpleTranslatorResponseKuraKapua { + private static final String CHAR_ENCODING = DeviceManagementSetting.getInstance().getString(DeviceManagementSettingKey.CHAR_ENCODING); + private static final KapuaLocator LOCATOR = KapuaLocator.getInstance(); public TranslatorAppPackageKuraKapua() { @@ -116,22 +118,20 @@ protected PackageResponsePayload translatePayload(KuraResponsePayload kuraRespon String body; if (kuraResponsePayload.hasBody()) { - DeviceManagementSetting config = DeviceManagementSetting.getInstance(); - String charEncoding = config.getString(DeviceManagementSettingKey.CHAR_ENCODING); try { - body = new String(kuraResponsePayload.getBody(), charEncoding); + body = new String(kuraResponsePayload.getBody(), CHAR_ENCODING); } catch (Exception e) { throw new TranslatorException(TranslatorErrorCodes.INVALID_PAYLOAD, e, (Object) kuraResponsePayload.getBody()); } - KuraDeploymentPackages kuraDeploymentPackages = null; + KuraDeploymentPackages kuraDeploymentPackages; try { kuraDeploymentPackages = XmlUtil.unmarshal(body, KuraDeploymentPackages.class); } catch (Exception e) { throw new TranslatorException(TranslatorErrorCodes.INVALID_PAYLOAD, e, body); } - translate(responsePayload, charEncoding, kuraDeploymentPackages); + translate(responsePayload, kuraDeploymentPackages); } } else { if (kuraResponsePayload.hasBody()) { @@ -150,7 +150,7 @@ protected PackageResponsePayload translatePayload(KuraResponsePayload kuraRespon } } - private void translate(PackageResponsePayload packageResponsePayload, String charEncoding, KuraDeploymentPackages kuraDeploymentPackages) throws KapuaException { + private void translate(PackageResponsePayload packageResponsePayload, KuraDeploymentPackages kuraDeploymentPackages) throws KapuaException { try { DevicePackageFactory devicePackageFactory = LOCATOR.getFactory(DevicePackageFactory.class); @@ -180,7 +180,7 @@ private void translate(PackageResponsePayload packageResponsePayload, String cha StringWriter sw = new StringWriter(); XmlUtil.marshal(deviceDeploymentPackages, sw); - byte[] requestBody = sw.toString().getBytes(charEncoding); + byte[] requestBody = sw.toString().getBytes(CHAR_ENCODING); packageResponsePayload.setBody(requestBody); } diff --git a/translator/kapua/kura/src/main/java/org/eclipse/kapua/translator/kura/kapua/TranslatorAppSnapshotKuraKapua.java b/translator/kapua/kura/src/main/java/org/eclipse/kapua/translator/kura/kapua/TranslatorAppSnapshotKuraKapua.java index 7c8f638bd6a..2d9d06eb409 100644 --- a/translator/kapua/kura/src/main/java/org/eclipse/kapua/translator/kura/kapua/TranslatorAppSnapshotKuraKapua.java +++ b/translator/kapua/kura/src/main/java/org/eclipse/kapua/translator/kura/kapua/TranslatorAppSnapshotKuraKapua.java @@ -44,6 +44,8 @@ */ public class TranslatorAppSnapshotKuraKapua extends AbstractSimpleTranslatorResponseKuraKapua { + private static final String CHAR_ENCODING = DeviceManagementSetting.getInstance().getString(DeviceManagementSettingKey.CHAR_ENCODING); + private static final KapuaLocator LOCATOR = KapuaLocator.getInstance(); public TranslatorAppSnapshotKuraKapua() { @@ -71,14 +73,12 @@ protected SnapshotResponsePayload translatePayload(KuraResponsePayload kuraRespo try { SnapshotResponsePayload snapshotResponsePayload = TranslatorKuraKapuaUtils.buildBaseResponsePayload(kuraResponsePayload, new SnapshotResponsePayload()); - DeviceManagementSetting config = DeviceManagementSetting.getInstance(); - String charEncoding = config.getString(DeviceManagementSettingKey.CHAR_ENCODING); - KuraSnapshotIds snapshotIdResult = null; if (kuraResponsePayload.hasBody()) { - String body = null; + + String body; try { - body = new String(kuraResponsePayload.getBody(), charEncoding); + body = new String(kuraResponsePayload.getBody(), CHAR_ENCODING); } catch (Exception e) { throw new TranslatorException(TranslatorErrorCodes.INVALID_PAYLOAD, e, (Object) snapshotResponsePayload.getBody()); } @@ -90,7 +90,7 @@ protected SnapshotResponsePayload translatePayload(KuraResponsePayload kuraRespo } } - translateBody(snapshotResponsePayload, charEncoding, snapshotIdResult); + translateBody(snapshotResponsePayload, snapshotIdResult); // Return Kapua Payload return snapshotResponsePayload; @@ -101,7 +101,7 @@ protected SnapshotResponsePayload translatePayload(KuraResponsePayload kuraRespo } } - private void translateBody(SnapshotResponsePayload snapshotResponsePayload, String charEncoding, KuraSnapshotIds kuraSnapshotIdResult) throws TranslatorException { + private void translateBody(SnapshotResponsePayload snapshotResponsePayload, KuraSnapshotIds kuraSnapshotIdResult) throws TranslatorException { try { DeviceSnapshotFactory deviceSnapshotFactory = LOCATOR.getFactory(DeviceSnapshotFactory.class); @@ -118,7 +118,7 @@ private void translateBody(SnapshotResponsePayload snapshotResponsePayload, Stri StringWriter sw = new StringWriter(); XmlUtil.marshal(deviceSnapshots, sw); - byte[] requestBody = sw.toString().getBytes(charEncoding); + byte[] requestBody = sw.toString().getBytes(CHAR_ENCODING); snapshotResponsePayload.setBody(requestBody); }