Skip to content

Commit

Permalink
Improved Translator KuraResponsePayload.body reader
Browse files Browse the repository at this point in the history
Signed-off-by: Alberto Codutti <alberto.codutti@eurotech.com>
  • Loading branch information
Coduz committed Jan 26, 2021
1 parent ad999db commit 4824d08
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 53 deletions.
Expand Up @@ -76,20 +76,7 @@ protected BundleResponsePayload translatePayload(KuraResponsePayload kuraRespons
BundleResponsePayload bundleResponsePayload = TranslatorKuraKapuaUtils.buildBaseResponsePayload(kuraResponsePayload, new BundleResponsePayload());

if (kuraResponsePayload.hasBody()) {

String body;
try {
body = new String(kuraResponsePayload.getBody(), CHAR_ENCODING);
} catch (Exception e) {
throw new TranslatorException(TranslatorErrorCodes.INVALID_PAYLOAD, e, kuraResponsePayload.getBody());
}

KuraBundles kuraBundles;
try {
kuraBundles = XmlUtil.unmarshal(body, KuraBundles.class);
} catch (Exception e) {
throw new TranslatorException(TranslatorErrorCodes.INVALID_PAYLOAD, e, body);
}
KuraBundles kuraBundles = TranslatorKuraKapuaUtils.readBodyAs(kuraResponsePayload.getBody(), KuraBundles.class);

translate(bundleResponsePayload, kuraBundles);
}
Expand Down
Expand Up @@ -80,19 +80,7 @@ protected ConfigurationResponsePayload translatePayload(KuraResponsePayload kura
ConfigurationResponsePayload configurationResponsePayload = TranslatorKuraKapuaUtils.buildBaseResponsePayload(kuraResponsePayload, new ConfigurationResponsePayload());

if (kuraResponsePayload.hasBody()) {
String body;
try {
body = new String(kuraResponsePayload.getBody(), CHAR_ENCODING);
} catch (Exception e) {
throw new TranslatorException(TranslatorErrorCodes.INVALID_PAYLOAD, e, (Object) configurationResponsePayload.getBody());
}

KuraDeviceConfiguration kuraDeviceConfiguration = null;
try {
kuraDeviceConfiguration = XmlUtil.unmarshal(body, KuraDeviceConfiguration.class);
} catch (Exception e) {
throw new TranslatorException(TranslatorErrorCodes.INVALID_PAYLOAD, e, body);
}
KuraDeviceConfiguration kuraDeviceConfiguration = TranslatorKuraKapuaUtils.readBodyAs(kuraResponsePayload.getBody(), KuraDeviceConfiguration.class);

translateBody(configurationResponsePayload, kuraDeviceConfiguration);
}
Expand Down
Expand Up @@ -116,21 +116,9 @@ protected PackageResponsePayload translatePayload(KuraResponsePayload kuraRespon
responsePayload.setPackageDownloadOperationStatus(DevicePackageDownloadStatus.NONE);
}

String body;
if (kuraResponsePayload.hasBody()) {
KuraDeploymentPackages kuraDeploymentPackages = TranslatorKuraKapuaUtils.readBodyAs(kuraResponsePayload.getBody(), KuraDeploymentPackages.class);

try {
body = new String(kuraResponsePayload.getBody(), CHAR_ENCODING);
} catch (Exception e) {
throw new TranslatorException(TranslatorErrorCodes.INVALID_PAYLOAD, e, (Object) kuraResponsePayload.getBody());
}

KuraDeploymentPackages kuraDeploymentPackages;
try {
kuraDeploymentPackages = XmlUtil.unmarshal(body, KuraDeploymentPackages.class);
} catch (Exception e) {
throw new TranslatorException(TranslatorErrorCodes.INVALID_PAYLOAD, e, body);
}
translate(responsePayload, kuraDeploymentPackages);
}
} else {
Expand Down
Expand Up @@ -75,19 +75,7 @@ protected SnapshotResponsePayload translatePayload(KuraResponsePayload kuraRespo

KuraSnapshotIds snapshotIdResult = null;
if (kuraResponsePayload.hasBody()) {

String body;
try {
body = new String(kuraResponsePayload.getBody(), CHAR_ENCODING);
} catch (Exception e) {
throw new TranslatorException(TranslatorErrorCodes.INVALID_PAYLOAD, e, (Object) snapshotResponsePayload.getBody());
}

try {
snapshotIdResult = XmlUtil.unmarshal(body, KuraSnapshotIds.class);
} catch (Exception e) {
throw new TranslatorException(TranslatorErrorCodes.INVALID_PAYLOAD, e, body);
}
snapshotIdResult = TranslatorKuraKapuaUtils.readBodyAs(kuraResponsePayload.getBody(), KuraSnapshotIds.class);
}

translateBody(snapshotResponsePayload, snapshotIdResult);
Expand Down
Expand Up @@ -15,6 +15,7 @@

import org.eclipse.kapua.KapuaException;
import org.eclipse.kapua.commons.setting.system.SystemSetting;
import org.eclipse.kapua.commons.util.xml.XmlUtil;
import org.eclipse.kapua.locator.KapuaLocator;
import org.eclipse.kapua.message.KapuaMessageFactory;
import org.eclipse.kapua.message.KapuaPosition;
Expand Down Expand Up @@ -42,7 +43,9 @@ public final class TranslatorKuraKapuaUtils {
private static final KapuaLocator LOCATOR = KapuaLocator.getInstance();
private static final KapuaMessageFactory KAPUA_MESSAGE_FACTORY = LOCATOR.getFactory(KapuaMessageFactory.class);

private static final String CHAR_ENCODING = DeviceManagementSetting.getInstance().getString(DeviceManagementSettingKey.CHAR_ENCODING);
private static final String CONTROL_MESSAGE_CLASSIFIER = SystemSetting.getInstance().getMessageClassifier();

private static final boolean SHOW_STACKTRACE = DeviceManagementSetting.getInstance().getBoolean(DeviceManagementSettingKey.SHOW_STACKTRACE, false);

private TranslatorKuraKapuaUtils() {
Expand Down Expand Up @@ -85,6 +88,31 @@ public static void validateKuraResponseChannel(KuraResponseChannel kuraResponseC
}
}

/**
* Reads the given {@code byte[]} as the requested {@code returnAs} parameter.
*
* @param bytesToRead The {@link KapuaResponsePayload#getBody()}
* @param returnAs The {@link Class} to read as.
* @param <T> The type of the retrun.
* @return Returns the given {@code byte[]} as the given {@link Class}
* @throws TranslatorException If the {@code byte[]} is not uspported or the {@code byte[]} cannot be read as the given {@link Class}
* @since 1.5.0
*/
public static <T> T readBodyAs(byte[] bytesToRead, Class<T> returnAs) throws TranslatorException {
String body;
try {
body = new String(bytesToRead, CHAR_ENCODING);
} catch (Exception e) {
throw new TranslatorException(TranslatorErrorCodes.INVALID_PAYLOAD, e, (Object) bytesToRead);
}

try {
return XmlUtil.unmarshal(body, returnAs);
} catch (Exception e) {
throw new TranslatorException(TranslatorErrorCodes.INVALID_PAYLOAD, e, body);
}
}

/**
* Builds the {@link KapuaResponsePayload} with commons property in {@link KuraResponsePayload}
*
Expand Down

0 comments on commit 4824d08

Please sign in to comment.