From fa2477f5cb1fe83f2840caa358b9396226c94c1b Mon Sep 17 00:00:00 2001 From: kaizimmerm Date: Thu, 23 Jun 2016 09:02:05 +0200 Subject: [PATCH] Fixed nullpointer exception and optimized the code here and there. Signed-off-by: kaizimmerm --- .../scenarios/ConfigurableScenario.java | 23 +++++++++------- .../client/scenarios/upload/ArtifactFile.java | 9 +++++-- .../upload/FeignMultipartEncoder.java | 16 +++-------- .../amqp/AmqpMessageHandlerService.java | 27 ++++++++++++------- .../jpa/JpaControllerManagement.java | 4 +-- 5 files changed, 42 insertions(+), 37 deletions(-) diff --git a/examples/hawkbit-example-mgmt-simulator/src/main/java/org/eclipse/hawkbit/mgmt/client/scenarios/ConfigurableScenario.java b/examples/hawkbit-example-mgmt-simulator/src/main/java/org/eclipse/hawkbit/mgmt/client/scenarios/ConfigurableScenario.java index 2cb339a1f..a09459e07 100644 --- a/examples/hawkbit-example-mgmt-simulator/src/main/java/org/eclipse/hawkbit/mgmt/client/scenarios/ConfigurableScenario.java +++ b/examples/hawkbit-example-mgmt-simulator/src/main/java/org/eclipse/hawkbit/mgmt/client/scenarios/ConfigurableScenario.java @@ -42,6 +42,8 @@ import org.springframework.beans.factory.annotation.Qualifier; */ public class ConfigurableScenario { + private static final int PAGE_SIZE = 100; + private static final Logger LOGGER = LoggerFactory.getLogger(ConfigurableScenario.class); private final MgmtDistributionSetClientResource distributionSetResource; @@ -109,25 +111,25 @@ public class ConfigurableScenario { private void deleteSoftwareModules() { PagedList modules; do { - modules = softwareModuleResource.getSoftwareModules(0, 100, null, null).getBody(); + modules = softwareModuleResource.getSoftwareModules(0, PAGE_SIZE, null, null).getBody(); modules.getContent().forEach(module -> softwareModuleResource.deleteSoftwareModule(module.getModuleId())); - } while (modules.getTotal() > 100); + } while (modules.getTotal() > PAGE_SIZE); } private void deleteDistributionSets() { PagedList distributionSets; do { - distributionSets = distributionSetResource.getDistributionSets(0, 100, null, null).getBody(); + distributionSets = distributionSetResource.getDistributionSets(0, PAGE_SIZE, null, null).getBody(); distributionSets.getContent().forEach(set -> distributionSetResource.deleteDistributionSet(set.getDsId())); - } while (distributionSets.getTotal() > 100); + } while (distributionSets.getTotal() > PAGE_SIZE); } private void deleteTargets() { PagedList targets; do { - targets = targetResource.getTargets(0, 100, null, null).getBody(); + targets = targetResource.getTargets(0, PAGE_SIZE, null, null).getBody(); targets.getContent().forEach(target -> targetResource.deleteTarget(target.getControllerId())); - } while (targets.getTotal() > 100); + } while (targets.getTotal() > PAGE_SIZE); } private void runRollouts(final Scenario scenario) { @@ -217,10 +219,11 @@ public class ConfigurableScenario { private void createTargets(final Scenario scenario) { LOGGER.info("Creating {} targets", scenario.getTargets()); - for (int i = 0; i < scenario.getTargets() / 100; i++) { - targetResource.createTargets(new TargetBuilder().controllerId(scenario.getTargetName()) - .address(scenario.getTargetAddress()).buildAsList(i * 100, - (i + 1) * 100 > scenario.getTargets() ? (scenario.getTargets() - (i * 100)) : 100)); + for (int i = 0; i < (scenario.getTargets() / PAGE_SIZE); i++) { + targetResource.createTargets( + new TargetBuilder().controllerId(scenario.getTargetName()).address(scenario.getTargetAddress()) + .buildAsList(i * PAGE_SIZE, (i + 1) * PAGE_SIZE > scenario.getTargets() + ? (scenario.getTargets() - (i * PAGE_SIZE)) : PAGE_SIZE)); } LOGGER.info("Creating {} targets -> Done", scenario.getTargets()); diff --git a/examples/hawkbit-example-mgmt-simulator/src/main/java/org/eclipse/hawkbit/mgmt/client/scenarios/upload/ArtifactFile.java b/examples/hawkbit-example-mgmt-simulator/src/main/java/org/eclipse/hawkbit/mgmt/client/scenarios/upload/ArtifactFile.java index adad1fe77..379455580 100644 --- a/examples/hawkbit-example-mgmt-simulator/src/main/java/org/eclipse/hawkbit/mgmt/client/scenarios/upload/ArtifactFile.java +++ b/examples/hawkbit-example-mgmt-simulator/src/main/java/org/eclipse/hawkbit/mgmt/client/scenarios/upload/ArtifactFile.java @@ -12,11 +12,16 @@ import java.io.ByteArrayInputStream; import java.io.File; import java.io.IOException; import java.io.InputStream; +import java.util.Optional; import org.springframework.util.Assert; import org.springframework.util.FileCopyUtils; import org.springframework.web.multipart.MultipartFile; +/** + * Implementation for {@link MultipartFile} for hawkBit artifact upload. + * + */ public class ArtifactFile implements MultipartFile { private final String name; @@ -55,9 +60,9 @@ public class ArtifactFile implements MultipartFile { final byte[] content) { Assert.hasLength(name, "Name must not be null"); this.name = name; - this.originalFilename = originalFilename != null ? originalFilename : ""; + this.originalFilename = Optional.ofNullable(originalFilename).orElse(""); this.contentType = contentType; - this.content = content != null ? content : new byte[0]; + this.content = Optional.ofNullable(content).orElse(new byte[0]); } @Override diff --git a/examples/hawkbit-example-mgmt-simulator/src/main/java/org/eclipse/hawkbit/mgmt/client/scenarios/upload/FeignMultipartEncoder.java b/examples/hawkbit-example-mgmt-simulator/src/main/java/org/eclipse/hawkbit/mgmt/client/scenarios/upload/FeignMultipartEncoder.java index 00424c5e5..5d1e43e96 100644 --- a/examples/hawkbit-example-mgmt-simulator/src/main/java/org/eclipse/hawkbit/mgmt/client/scenarios/upload/FeignMultipartEncoder.java +++ b/examples/hawkbit-example-mgmt-simulator/src/main/java/org/eclipse/hawkbit/mgmt/client/scenarios/upload/FeignMultipartEncoder.java @@ -40,7 +40,7 @@ public class FeignMultipartEncoder implements Encoder { private final HttpHeaders multipartHeaders = new HttpHeaders(); private final HttpHeaders jsonHeaders = new HttpHeaders(); - public static final Charset UTF_8 = Charset.forName("UTF-8"); + private static final Charset UTF_8 = Charset.forName("UTF-8"); public FeignMultipartEncoder() { multipartHeaders.setContentType(MediaType.MULTIPART_FORM_DATA); @@ -48,11 +48,8 @@ public class FeignMultipartEncoder implements Encoder { } @Override - public void encode(final Object object, final Type bodyType, final RequestTemplate template) - throws EncodeException { - + public void encode(final Object object, final Type bodyType, final RequestTemplate template) { encodeMultipartFormRequest(object, template); - } private void encodeMultipartFormRequest(final Object value, final RequestTemplate template) { @@ -66,8 +63,7 @@ public class FeignMultipartEncoder implements Encoder { } @SuppressWarnings("unchecked") - private void encodeRequest(final Object value, final HttpHeaders requestHeaders, final RequestTemplate template) - throws EncodeException { + private void encodeRequest(final Object value, final HttpHeaders requestHeaders, final RequestTemplate template) { final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); final HttpOutputMessage dummyRequest = new HttpOutputMessageImpl(outputStream, requestHeaders); try { @@ -150,12 +146,6 @@ public class FeignMultipartEncoder implements Encoder { return this.filename; } - @Override - public InputStream getInputStream() throws IOException, IllegalStateException { - return super.getInputStream(); // To change body of generated - // methods, choose Tools | Templates. - } - @Override public long contentLength() throws IOException { return size; diff --git a/hawkbit-dmf-amqp/src/main/java/org/eclipse/hawkbit/amqp/AmqpMessageHandlerService.java b/hawkbit-dmf-amqp/src/main/java/org/eclipse/hawkbit/amqp/AmqpMessageHandlerService.java index 3f42c2937..27cbc0f69 100644 --- a/hawkbit-dmf-amqp/src/main/java/org/eclipse/hawkbit/amqp/AmqpMessageHandlerService.java +++ b/hawkbit-dmf-amqp/src/main/java/org/eclipse/hawkbit/amqp/AmqpMessageHandlerService.java @@ -14,6 +14,7 @@ import java.util.Collections; import java.util.List; import java.util.UUID; +import org.apache.commons.lang3.ArrayUtils; import org.apache.commons.lang3.StringUtils; import org.eclipse.hawkbit.api.HostnameResolver; import org.eclipse.hawkbit.artifact.repository.model.DbArtifact; @@ -344,16 +345,7 @@ public class AmqpMessageHandlerService extends BaseAmqpService { final ActionUpdateStatus actionUpdateStatus = convertMessage(message, ActionUpdateStatus.class); final Action action = checkActionExist(message, actionUpdateStatus); - final ActionStatus actionStatus = entityFactory.generateActionStatus(); - actionUpdateStatus.getMessage().forEach(actionStatus::addMessage); - - if (message.getMessageProperties().getCorrelationId().length > 0) { - actionStatus.addMessage(RepositoryConstants.SERVER_MESSAGE_PREFIX + "DMF message correlation-id " - + message.getMessageProperties().getCorrelationId()); - } - - actionStatus.setAction(action); - actionStatus.setOccurredAt(System.currentTimeMillis()); + final ActionStatus actionStatus = createActionStatus(message, actionUpdateStatus, action); switch (actionUpdateStatus.getActionStatus()) { case DOWNLOAD: @@ -391,6 +383,21 @@ public class AmqpMessageHandlerService extends BaseAmqpService { } } + private ActionStatus createActionStatus(final Message message, final ActionUpdateStatus actionUpdateStatus, + final Action action) { + final ActionStatus actionStatus = entityFactory.generateActionStatus(); + actionUpdateStatus.getMessage().forEach(actionStatus::addMessage); + + if (ArrayUtils.isNotEmpty(message.getMessageProperties().getCorrelationId())) { + actionStatus.addMessage(RepositoryConstants.SERVER_MESSAGE_PREFIX + "DMF message correlation-id " + + message.getMessageProperties().getCorrelationId()); + } + + actionStatus.setAction(action); + actionStatus.setOccurredAt(System.currentTimeMillis()); + return actionStatus; + } + private Action getUpdateActionStatus(final ActionStatus actionStatus) { if (actionStatus.getStatus().equals(Status.CANCELED)) { return controllerManagement.addCancelActionStatus(actionStatus); diff --git a/hawkbit-repository/hawkbit-repository-jpa/src/main/java/org/eclipse/hawkbit/repository/jpa/JpaControllerManagement.java b/hawkbit-repository/hawkbit-repository-jpa/src/main/java/org/eclipse/hawkbit/repository/jpa/JpaControllerManagement.java index abe4dba03..b9dfce105 100644 --- a/hawkbit-repository/hawkbit-repository-jpa/src/main/java/org/eclipse/hawkbit/repository/jpa/JpaControllerManagement.java +++ b/hawkbit-repository/hawkbit-repository-jpa/src/main/java/org/eclipse/hawkbit/repository/jpa/JpaControllerManagement.java @@ -253,8 +253,8 @@ public class JpaControllerManagement implements ControllerManagement { public Action addUpdateActionStatus(@NotNull final ActionStatus actionStatus) { final JpaAction action = (JpaAction) actionStatus.getAction(); - // if action is already closed we accept further status updates on if - // permitted so by configuration. This is especially use full if the + // if action is already closed we accept further status updates if + // permitted so by configuration. This is especially useful if the // action status feedback channel order from the device cannot be // guaranteed. However, if an action is closed we do not accept further // close messages.