Fixed nullpointer exception and optimized the code here and there.

Signed-off-by: kaizimmerm <kai.zimmermann@bosch-si.com>
This commit is contained in:
kaizimmerm
2016-06-23 09:02:05 +02:00
parent 717a23671e
commit fa2477f5cb
5 changed files with 42 additions and 37 deletions

View File

@@ -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<MgmtSoftwareModule> 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<MgmtDistributionSet> 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<MgmtTarget> 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());

View File

@@ -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

View File

@@ -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;

View File

@@ -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);

View File

@@ -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.