From fd2312f0ffa7a94e60c66c16b029c42d56ca6119 Mon Sep 17 00:00:00 2001 From: Jonathan Philip Knoblauch Date: Tue, 26 Apr 2016 16:57:40 +0200 Subject: [PATCH 1/5] Changed feign client calls to keep client up when download failed Signed-off-by: Jonathan Philip Knoblauch --- .../hawkbit/ddi/client/DdiExampleClient.java | 69 +++++++++++-------- 1 file changed, 41 insertions(+), 28 deletions(-) diff --git a/examples/hawkbit-example-ddi-client/src/main/java/org/eclipse/hawkbit/ddi/client/DdiExampleClient.java b/examples/hawkbit-example-ddi-client/src/main/java/org/eclipse/hawkbit/ddi/client/DdiExampleClient.java index ad9f086bb..0303fe627 100644 --- a/examples/hawkbit-example-ddi-client/src/main/java/org/eclipse/hawkbit/ddi/client/DdiExampleClient.java +++ b/examples/hawkbit-example-ddi-client/src/main/java/org/eclipse/hawkbit/ddi/client/DdiExampleClient.java @@ -16,6 +16,7 @@ import java.util.ArrayList; import java.util.List; import java.util.regex.Pattern; +import org.eclipse.hawkbit.ddi.client.resource.RootControllerResourceClient; import org.eclipse.hawkbit.ddi.client.strategy.PersistenceStrategy; import org.eclipse.hawkbit.ddi.json.model.DdiActionFeedback; import org.eclipse.hawkbit.ddi.json.model.DdiArtifact; @@ -41,9 +42,11 @@ public class DdiExampleClient implements Runnable { private final String controllerId; private Long actionIdOfLastInstalltion; - private final DdiDefaultFeignClient ddiDefaultFeignClient; + private final RootControllerResourceClient rootControllerResourceClient; private final PersistenceStrategy persistenceStrategy; - private STATUS clientStatus; + private DdiClientStatus clientStatus; + + private FinalResult finalReusltOfCurrentUpdate; /** * Constructor for the DDI example client. @@ -60,33 +63,34 @@ public class DdiExampleClient implements Runnable { public DdiExampleClient(final String baseUrl, final String controllerId, final String tenant, final PersistenceStrategy persistenceStrategy) { this.controllerId = controllerId; - this.ddiDefaultFeignClient = new DdiDefaultFeignClient(baseUrl, tenant); + this.rootControllerResourceClient = new DdiDefaultFeignClient(baseUrl, tenant) + .getRootControllerResourceClient(); this.actionIdOfLastInstalltion = null; this.persistenceStrategy = persistenceStrategy; - this.clientStatus = STATUS.DOWN; + this.clientStatus = DdiClientStatus.DOWN; } @Override public void run() { - clientStatus = STATUS.UP; + clientStatus = DdiClientStatus.UP; ResponseEntity response; - while (clientStatus == STATUS.UP) { + while (clientStatus == DdiClientStatus.UP) { LOGGER.info(" Controller {} polling from hawkBit server", controllerId); - response = ddiDefaultFeignClient.getRootControllerResourceClient().getControllerBase(controllerId); - final String pollingTime = response.getBody().getConfig().getPolling().getSleep(); - final LocalTime localtime = LocalTime.parse(pollingTime); + response = rootControllerResourceClient.getControllerBase(controllerId); + final String pollingTimeFormReponse = response.getBody().getConfig().getPolling().getSleep(); + final LocalTime localtime = LocalTime.parse(pollingTimeFormReponse); final long pollingIntervalInMillis = localtime.toNanoOfDay(); final Link controllerDeploymentBaseLink = response.getBody().getLink("deploymentBase"); - if (controllerDeploymentBaseLink != null) { final Long actionId = getActionIdOutOfLink(controllerDeploymentBaseLink); final Integer resource = getResourceOutOfLink(controllerDeploymentBaseLink); if (actionId != actionIdOfLastInstalltion) { + finalReusltOfCurrentUpdate = FinalResult.NONE; startDownload(actionId, resource); + finishUpdateProcess(actionId); actionIdOfLastInstalltion = actionId; } } - try { Thread.sleep(pollingIntervalInMillis); } catch (final InterruptedException e) { @@ -99,12 +103,12 @@ public class DdiExampleClient implements Runnable { * Stop the DDI example client */ public void stop() { - clientStatus = STATUS.DOWN; + clientStatus = DdiClientStatus.DOWN; } private void startDownload(final Long actionId, final Integer resource) { - final ResponseEntity respone = ddiDefaultFeignClient.getRootControllerResourceClient() + final ResponseEntity respone = rootControllerResourceClient .getControllerBasedeploymentAction(controllerId, Long.valueOf(actionId), Integer.valueOf(resource)); final DdiDeploymentBase ddiDeploymentBase = respone.getBody(); final List chunks = ddiDeploymentBase.getDeployment().getChunks(); @@ -115,7 +119,9 @@ public class DdiExampleClient implements Runnable { final String[] downloadLinkSep = downloadLink.getHref().split(Pattern.quote("/")); final Long softwareModuleId = Long.valueOf(downloadLinkSep[8]); for (final DdiArtifact ddiArtifact : artifactList) { - downloadArtifact(actionId, softwareModuleId, ddiArtifact.getFilename()); + if (finalReusltOfCurrentUpdate != FinalResult.FAILURE) { + downloadArtifact(actionId, softwareModuleId, ddiArtifact.getFilename()); + } } } } @@ -126,39 +132,46 @@ public class DdiExampleClient implements Runnable { "Starting download of artifact " + artifact); LOGGER.info("Starting download of artifact " + artifact); - final ResponseEntity responseDownloadArtifact = ddiDefaultFeignClient - .getRootControllerResourceClient().downloadArtifact(controllerId, softwareModuleId, artifact); + final ResponseEntity responseDownloadArtifact = rootControllerResourceClient + .downloadArtifact(controllerId, softwareModuleId, artifact); final HttpStatus statsuCode = responseDownloadArtifact.getStatusCode(); LOGGER.info("Finished download with stataus {}", statsuCode); try { persistenceStrategy.handleInputStream(responseDownloadArtifact.getBody(), artifact); + sendFeedBackMessage(actionId, ExecutionStatus.PROCEEDING, FinalResult.NONE, + "Downloaded artifact " + artifact); } catch (final IOException e) { - sendFeedBackMessage(actionId, ExecutionStatus.CLOSED, FinalResult.FAILURE, - "Downloaded of artifact " + artifact + " failed"); - return; + sendFeedBackMessage(actionId, ExecutionStatus.PROCEEDING, FinalResult.NONE, + "Downloaded of artifact " + artifact + "failed"); + finalReusltOfCurrentUpdate = FinalResult.FAILURE; } - sendFeedBackMessage(actionId, ExecutionStatus.PROCEEDING, FinalResult.NONE, "Downloaded artifact " + artifact); - simulateSuccessfulInstallation(actionId); } private void sendFeedBackMessage(final Long actionId, final ExecutionStatus executionStatus, final FinalResult finalResult, final String message) { - final DdiResult result = new DdiResult(finalResult, null); final List details = new ArrayList<>(); details.add(message); final DdiStatus ddiStatus = new DdiStatus(executionStatus, result, details); final String time = String.valueOf(LocalDateTime.now()); final DdiActionFeedback feedback = new DdiActionFeedback(actionId, time, ddiStatus); - ddiDefaultFeignClient.getRootControllerResourceClient().postBasedeploymentActionFeedback(feedback, controllerId, - actionId); + rootControllerResourceClient.postBasedeploymentActionFeedback(feedback, controllerId, actionId); LOGGER.info("Sent feedback message to HaktBit"); } - private void simulateSuccessfulInstallation(final Long actionId) { - sendFeedBackMessage(actionId, ExecutionStatus.CLOSED, FinalResult.SUCESS, "Simulated installation successful"); + private void finishUpdateProcess(final Long actionId) { + + if (finalReusltOfCurrentUpdate == FinalResult.FAILURE) { + sendFeedBackMessage(actionId, ExecutionStatus.CLOSED, FinalResult.FAILURE, "Error during update process"); + } + + if (finalReusltOfCurrentUpdate == FinalResult.NONE) { + sendFeedBackMessage(actionId, ExecutionStatus.CLOSED, FinalResult.SUCESS, + "Simulated installation successful"); + } + } private Long getActionIdOutOfLink(final Link controllerDeploymentBaseLink) { @@ -180,8 +193,8 @@ public class DdiExampleClient implements Runnable { /** * Enum for DDI running status. */ - public enum STATUS { - + public enum DdiClientStatus { UP, DOWN; } + } From e5ddf075be5384d6fe828cf8c3e9b1503d5bad07 Mon Sep 17 00:00:00 2001 From: Michael Hirsch Date: Wed, 27 Apr 2016 08:39:12 +0200 Subject: [PATCH 2/5] remove leading slash of path, because other resources also don't have the leading slash Signed-off-by: Michael Hirsch --- .../hawkbit/mgmt/client/resource/MgmtTargetClientResource.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/hawkbit-mgmt-api-client/src/main/java/org/eclipse/hawkbit/mgmt/client/resource/MgmtTargetClientResource.java b/examples/hawkbit-mgmt-api-client/src/main/java/org/eclipse/hawkbit/mgmt/client/resource/MgmtTargetClientResource.java index 5d043afe2..19f78a12f 100644 --- a/examples/hawkbit-mgmt-api-client/src/main/java/org/eclipse/hawkbit/mgmt/client/resource/MgmtTargetClientResource.java +++ b/examples/hawkbit-mgmt-api-client/src/main/java/org/eclipse/hawkbit/mgmt/client/resource/MgmtTargetClientResource.java @@ -17,5 +17,5 @@ import org.springframework.cloud.netflix.feign.FeignClient; @FeignClient(url = "${hawkbit.url:localhost:8080}/" + MgmtTargetClientResource.PATH) public interface MgmtTargetClientResource extends MgmtTargetRestApi { - static String PATH = "/rest/v1/targets"; + static String PATH = "rest/v1/targets"; } From 3574bdcc585abcf4bf986f567a0cfbbf3a049a49 Mon Sep 17 00:00:00 2001 From: Michael Hirsch Date: Wed, 27 Apr 2016 11:22:42 +0200 Subject: [PATCH 3/5] add authentication intercepter for ddi-client to authenticate e.g. via security token, correct parsing server poll time Signed-off-by: Michael Hirsch --- .../ddi/client/DdiDefaultFeignClient.java | 11 ++++++ .../hawkbit/ddi/client/DdiExampleClient.java | 26 ++++++++++++-- .../AuthenticationInterceptor.java | 19 ++++++++++ .../AuthenticationInterceptorFactory.java | 34 ++++++++++++++++++ ...ecurityTokenAuthenticationInterceptor.java | 35 +++++++++++++++++++ 5 files changed, 123 insertions(+), 2 deletions(-) create mode 100644 examples/hawkbit-example-ddi-client/src/main/java/org/eclipse/hawkbit/ddi/client/authenctication/AuthenticationInterceptor.java create mode 100644 examples/hawkbit-example-ddi-client/src/main/java/org/eclipse/hawkbit/ddi/client/authenctication/AuthenticationInterceptorFactory.java create mode 100644 examples/hawkbit-example-ddi-client/src/main/java/org/eclipse/hawkbit/ddi/client/authenctication/TargetSecurityTokenAuthenticationInterceptor.java diff --git a/examples/hawkbit-example-ddi-client/src/main/java/org/eclipse/hawkbit/ddi/client/DdiDefaultFeignClient.java b/examples/hawkbit-example-ddi-client/src/main/java/org/eclipse/hawkbit/ddi/client/DdiDefaultFeignClient.java index c9af1cb09..d156e8eb5 100644 --- a/examples/hawkbit-example-ddi-client/src/main/java/org/eclipse/hawkbit/ddi/client/DdiDefaultFeignClient.java +++ b/examples/hawkbit-example-ddi-client/src/main/java/org/eclipse/hawkbit/ddi/client/DdiDefaultFeignClient.java @@ -9,6 +9,7 @@ package org.eclipse.hawkbit.ddi.client; import org.apache.commons.lang.Validate; +import org.eclipse.hawkbit.ddi.client.authenctication.AuthenticationInterceptor; import org.eclipse.hawkbit.ddi.client.resource.RootControllerResourceClient; import org.eclipse.hawkbit.feign.core.client.ApplicationJsonRequestHeaderInterceptor; import org.eclipse.hawkbit.feign.core.client.IgnoreMultipleConsumersProducersSpringMvcContract; @@ -31,10 +32,20 @@ public class DdiDefaultFeignClient { private final String tenant; public DdiDefaultFeignClient(final String baseUrl, final String tenant) { + this(baseUrl, tenant, null); + } + + public DdiDefaultFeignClient(final String baseUrl, final String tenant, + final AuthenticationInterceptor authenticationInterceptor) { feignBuilder = Feign.builder().contract(new IgnoreMultipleConsumersProducersSpringMvcContract()) .requestInterceptor(new ApplicationJsonRequestHeaderInterceptor()) .requestInterceptor(new DdiAcceptedRequestInterceptor()).logLevel(Level.FULL) .logger(new Logger.ErrorLogger()).encoder(new JacksonEncoder()).decoder(new DdiDecoder()); + + if (authenticationInterceptor != null) { + feignBuilder.requestInterceptor(authenticationInterceptor); + } + Validate.notNull(baseUrl, "A baseUrl has to be set"); Validate.notNull(tenant, "A tenant has to be set"); this.baseUrl = baseUrl; diff --git a/examples/hawkbit-example-ddi-client/src/main/java/org/eclipse/hawkbit/ddi/client/DdiExampleClient.java b/examples/hawkbit-example-ddi-client/src/main/java/org/eclipse/hawkbit/ddi/client/DdiExampleClient.java index 0303fe627..6b407f74d 100644 --- a/examples/hawkbit-example-ddi-client/src/main/java/org/eclipse/hawkbit/ddi/client/DdiExampleClient.java +++ b/examples/hawkbit-example-ddi-client/src/main/java/org/eclipse/hawkbit/ddi/client/DdiExampleClient.java @@ -12,10 +12,12 @@ import java.io.IOException; import java.io.InputStream; import java.time.LocalDateTime; import java.time.LocalTime; +import java.time.temporal.ChronoField; import java.util.ArrayList; import java.util.List; import java.util.regex.Pattern; +import org.eclipse.hawkbit.ddi.client.authenctication.AuthenticationInterceptor; import org.eclipse.hawkbit.ddi.client.resource.RootControllerResourceClient; import org.eclipse.hawkbit.ddi.client.strategy.PersistenceStrategy; import org.eclipse.hawkbit.ddi.json.model.DdiActionFeedback; @@ -62,8 +64,28 @@ public class DdiExampleClient implements Runnable { */ public DdiExampleClient(final String baseUrl, final String controllerId, final String tenant, final PersistenceStrategy persistenceStrategy) { + this(baseUrl, controllerId, tenant, persistenceStrategy, null); + } + + /** + * Constructor for the DDI example client. + * + * @param baseUrl + * the base url of the hawkBit server + * @param controllerId + * the controller id that will be simulated + * @param tenant + * the tenant + * @param persistenceStrategy + * the persistence strategy for downloading artifacts + * @param authenticationInterceptor + * the authentication intercepter to authenticate the DDI client, + * might be {@code null} + */ + public DdiExampleClient(final String baseUrl, final String controllerId, final String tenant, + final PersistenceStrategy persistenceStrategy, final AuthenticationInterceptor authenticationInterceptor) { this.controllerId = controllerId; - this.rootControllerResourceClient = new DdiDefaultFeignClient(baseUrl, tenant) + this.rootControllerResourceClient = new DdiDefaultFeignClient(baseUrl, tenant, authenticationInterceptor) .getRootControllerResourceClient(); this.actionIdOfLastInstalltion = null; this.persistenceStrategy = persistenceStrategy; @@ -79,7 +101,7 @@ public class DdiExampleClient implements Runnable { response = rootControllerResourceClient.getControllerBase(controllerId); final String pollingTimeFormReponse = response.getBody().getConfig().getPolling().getSleep(); final LocalTime localtime = LocalTime.parse(pollingTimeFormReponse); - final long pollingIntervalInMillis = localtime.toNanoOfDay(); + final long pollingIntervalInMillis = localtime.getLong(ChronoField.MILLI_OF_DAY); final Link controllerDeploymentBaseLink = response.getBody().getLink("deploymentBase"); if (controllerDeploymentBaseLink != null) { final Long actionId = getActionIdOutOfLink(controllerDeploymentBaseLink); diff --git a/examples/hawkbit-example-ddi-client/src/main/java/org/eclipse/hawkbit/ddi/client/authenctication/AuthenticationInterceptor.java b/examples/hawkbit-example-ddi-client/src/main/java/org/eclipse/hawkbit/ddi/client/authenctication/AuthenticationInterceptor.java new file mode 100644 index 000000000..f1a746c1b --- /dev/null +++ b/examples/hawkbit-example-ddi-client/src/main/java/org/eclipse/hawkbit/ddi/client/authenctication/AuthenticationInterceptor.java @@ -0,0 +1,19 @@ +/** + * Copyright (c) 2015 Bosch Software Innovations GmbH and others. + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + */ +package org.eclipse.hawkbit.ddi.client.authenctication; + +import feign.RequestInterceptor; + +/** + * An DdiClient authentication intercepter to provide credential information + * e.g. in http-headers. + */ +public interface AuthenticationInterceptor extends RequestInterceptor { + +} diff --git a/examples/hawkbit-example-ddi-client/src/main/java/org/eclipse/hawkbit/ddi/client/authenctication/AuthenticationInterceptorFactory.java b/examples/hawkbit-example-ddi-client/src/main/java/org/eclipse/hawkbit/ddi/client/authenctication/AuthenticationInterceptorFactory.java new file mode 100644 index 000000000..5d2fb6706 --- /dev/null +++ b/examples/hawkbit-example-ddi-client/src/main/java/org/eclipse/hawkbit/ddi/client/authenctication/AuthenticationInterceptorFactory.java @@ -0,0 +1,34 @@ +/** + * Copyright (c) 2015 Bosch Software Innovations GmbH and others. + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + */ +package org.eclipse.hawkbit.ddi.client.authenctication; + +/** + * A factory to create {@link AuthenticationInterceptor}s. + */ +public class AuthenticationInterceptorFactory { + + private AuthenticationInterceptorFactory() { + // factory class no public constructor + } + + /** + * Creates a new {@link AuthenticationInterceptor} to authenticate a + * Ddi-Client via a target-security-token HTTP authorization header. + * + * @param targetSecurityToken + * the target-security-token to be added to the HTTP + * 'TargetToken' authorization header + * @return the authentication interceptor which can be used to authenticate + * an Ddi-Client + */ + public static AuthenticationInterceptor createTargetSecurityAuthenticator(final String targetSecurityToken) { + return new TargetSecurityTokenAuthenticationInterceptor(targetSecurityToken); + } + +} diff --git a/examples/hawkbit-example-ddi-client/src/main/java/org/eclipse/hawkbit/ddi/client/authenctication/TargetSecurityTokenAuthenticationInterceptor.java b/examples/hawkbit-example-ddi-client/src/main/java/org/eclipse/hawkbit/ddi/client/authenctication/TargetSecurityTokenAuthenticationInterceptor.java new file mode 100644 index 000000000..93559dd6d --- /dev/null +++ b/examples/hawkbit-example-ddi-client/src/main/java/org/eclipse/hawkbit/ddi/client/authenctication/TargetSecurityTokenAuthenticationInterceptor.java @@ -0,0 +1,35 @@ +/** + * Copyright (c) 2015 Bosch Software Innovations GmbH and others. + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + */ +package org.eclipse.hawkbit.ddi.client.authenctication; + +import com.google.common.net.HttpHeaders; + +import feign.RequestTemplate; + +/** + * Implementation of the {@link AuthenticationInterceptor} to add a given + * target-security-token to the HTTP authorization header. + */ +class TargetSecurityTokenAuthenticationInterceptor implements AuthenticationInterceptor { + + private final String targetSecurityToken; + + /** + * @param targetSecurityToken + * the security token to add to the authorization header + */ + TargetSecurityTokenAuthenticationInterceptor(final String targetSecurityToken) { + this.targetSecurityToken = targetSecurityToken; + } + + @Override + public void apply(final RequestTemplate template) { + template.header(HttpHeaders.AUTHORIZATION, "TargetToken " + targetSecurityToken); + } +} From 6cbbcffc9d036ca59c00392d265e973bc078d12c Mon Sep 17 00:00:00 2001 From: Jonathan Philip Knoblauch Date: Wed, 27 Apr 2016 11:38:50 +0200 Subject: [PATCH 4/5] Added inputstream download not on disk and final result validation. Signed-off-by: Jonathan Philip Knoblauch --- .../hawkbit/ddi/client/DdiExampleClient.java | 23 +++++++++++-------- .../strategy/DoNotSaveArtifactsStrategy.java | 5 +++- .../strategy/SaveArtifactsStrategy.java | 2 +- 3 files changed, 19 insertions(+), 11 deletions(-) diff --git a/examples/hawkbit-example-ddi-client/src/main/java/org/eclipse/hawkbit/ddi/client/DdiExampleClient.java b/examples/hawkbit-example-ddi-client/src/main/java/org/eclipse/hawkbit/ddi/client/DdiExampleClient.java index 0303fe627..129fd422d 100644 --- a/examples/hawkbit-example-ddi-client/src/main/java/org/eclipse/hawkbit/ddi/client/DdiExampleClient.java +++ b/examples/hawkbit-example-ddi-client/src/main/java/org/eclipse/hawkbit/ddi/client/DdiExampleClient.java @@ -45,8 +45,8 @@ public class DdiExampleClient implements Runnable { private final RootControllerResourceClient rootControllerResourceClient; private final PersistenceStrategy persistenceStrategy; private DdiClientStatus clientStatus; - - private FinalResult finalReusltOfCurrentUpdate; + private final DdiDefaultFeignClient sdiDefaultFeignClient; + private FinalResult finalResultOfCurrentUpdate; /** * Constructor for the DDI example client. @@ -63,13 +63,18 @@ public class DdiExampleClient implements Runnable { public DdiExampleClient(final String baseUrl, final String controllerId, final String tenant, final PersistenceStrategy persistenceStrategy) { this.controllerId = controllerId; - this.rootControllerResourceClient = new DdiDefaultFeignClient(baseUrl, tenant) - .getRootControllerResourceClient(); + sdiDefaultFeignClient = new DdiDefaultFeignClient(baseUrl, tenant); + + this.rootControllerResourceClient = sdiDefaultFeignClient.getRootControllerResourceClient(); this.actionIdOfLastInstalltion = null; this.persistenceStrategy = persistenceStrategy; this.clientStatus = DdiClientStatus.DOWN; } + public DdiDefaultFeignClient getSdiDefaultFeignClient() { + return sdiDefaultFeignClient; + } + @Override public void run() { clientStatus = DdiClientStatus.UP; @@ -85,7 +90,7 @@ public class DdiExampleClient implements Runnable { final Long actionId = getActionIdOutOfLink(controllerDeploymentBaseLink); final Integer resource = getResourceOutOfLink(controllerDeploymentBaseLink); if (actionId != actionIdOfLastInstalltion) { - finalReusltOfCurrentUpdate = FinalResult.NONE; + finalResultOfCurrentUpdate = FinalResult.NONE; startDownload(actionId, resource); finishUpdateProcess(actionId); actionIdOfLastInstalltion = actionId; @@ -119,7 +124,7 @@ public class DdiExampleClient implements Runnable { final String[] downloadLinkSep = downloadLink.getHref().split(Pattern.quote("/")); final Long softwareModuleId = Long.valueOf(downloadLinkSep[8]); for (final DdiArtifact ddiArtifact : artifactList) { - if (finalReusltOfCurrentUpdate != FinalResult.FAILURE) { + if (finalResultOfCurrentUpdate != FinalResult.FAILURE) { downloadArtifact(actionId, softwareModuleId, ddiArtifact.getFilename()); } } @@ -144,7 +149,7 @@ public class DdiExampleClient implements Runnable { } catch (final IOException e) { sendFeedBackMessage(actionId, ExecutionStatus.PROCEEDING, FinalResult.NONE, "Downloaded of artifact " + artifact + "failed"); - finalReusltOfCurrentUpdate = FinalResult.FAILURE; + finalResultOfCurrentUpdate = FinalResult.FAILURE; } } @@ -163,11 +168,11 @@ public class DdiExampleClient implements Runnable { private void finishUpdateProcess(final Long actionId) { - if (finalReusltOfCurrentUpdate == FinalResult.FAILURE) { + if (finalResultOfCurrentUpdate == FinalResult.FAILURE) { sendFeedBackMessage(actionId, ExecutionStatus.CLOSED, FinalResult.FAILURE, "Error during update process"); } - if (finalReusltOfCurrentUpdate == FinalResult.NONE) { + if (finalResultOfCurrentUpdate == FinalResult.NONE) { sendFeedBackMessage(actionId, ExecutionStatus.CLOSED, FinalResult.SUCESS, "Simulated installation successful"); } diff --git a/examples/hawkbit-example-ddi-client/src/main/java/org/eclipse/hawkbit/ddi/client/strategy/DoNotSaveArtifactsStrategy.java b/examples/hawkbit-example-ddi-client/src/main/java/org/eclipse/hawkbit/ddi/client/strategy/DoNotSaveArtifactsStrategy.java index 80743c99e..447885715 100644 --- a/examples/hawkbit-example-ddi-client/src/main/java/org/eclipse/hawkbit/ddi/client/strategy/DoNotSaveArtifactsStrategy.java +++ b/examples/hawkbit-example-ddi-client/src/main/java/org/eclipse/hawkbit/ddi/client/strategy/DoNotSaveArtifactsStrategy.java @@ -19,7 +19,10 @@ public class DoNotSaveArtifactsStrategy implements PersistenceStrategy { @Override public void handleInputStream(final InputStream in, final String artifactName) throws IOException { - // do nothing + + while (in.read() != -1) { + ; + } } } diff --git a/examples/hawkbit-example-ddi-client/src/main/java/org/eclipse/hawkbit/ddi/client/strategy/SaveArtifactsStrategy.java b/examples/hawkbit-example-ddi-client/src/main/java/org/eclipse/hawkbit/ddi/client/strategy/SaveArtifactsStrategy.java index 2ae7987cb..2908ee29f 100644 --- a/examples/hawkbit-example-ddi-client/src/main/java/org/eclipse/hawkbit/ddi/client/strategy/SaveArtifactsStrategy.java +++ b/examples/hawkbit-example-ddi-client/src/main/java/org/eclipse/hawkbit/ddi/client/strategy/SaveArtifactsStrategy.java @@ -26,7 +26,7 @@ public class SaveArtifactsStrategy implements PersistenceStrategy { @Override public void handleInputStream(final InputStream in, final String artifactName) throws IOException { final File tempDir = Files.createTempDir(); - final File file = new File(tempDir + "\\" + artifactName); + final File file = new File(tempDir, artifactName); final OutputStream out = new FileOutputStream(file); ByteStreams.copy(in, out); } From a288fb1476991179b4cf4f2e47214243d631438f Mon Sep 17 00:00:00 2001 From: Jonathan Philip Knoblauch Date: Wed, 27 Apr 2016 15:02:09 +0200 Subject: [PATCH 5/5] New names for persistenceArtifactStrategy interfaces and implementations Signed-off-by: Jonathan Philip Knoblauch --- .../hawkbit/ddi/client/DdiExampleClient.java | 36 +++++++++++-------- ...java => ArtifactsPersistenceStrategy.java} | 2 +- ...sStrategy.java => DoNotSaveArtifacts.java} | 4 +-- ... SaveArtifactsToLocalTempDirectories.java} | 4 +-- .../hawkbit/example/ddi/client/AppTest.java | 6 ++-- 5 files changed, 29 insertions(+), 23 deletions(-) rename examples/hawkbit-example-ddi-client/src/main/java/org/eclipse/hawkbit/ddi/client/strategy/{PersistenceStrategy.java => ArtifactsPersistenceStrategy.java} (94%) rename examples/hawkbit-example-ddi-client/src/main/java/org/eclipse/hawkbit/ddi/client/strategy/{DoNotSaveArtifactsStrategy.java => DoNotSaveArtifacts.java} (80%) rename examples/hawkbit-example-ddi-client/src/main/java/org/eclipse/hawkbit/ddi/client/strategy/{SaveArtifactsStrategy.java => SaveArtifactsToLocalTempDirectories.java} (84%) diff --git a/examples/hawkbit-example-ddi-client/src/main/java/org/eclipse/hawkbit/ddi/client/DdiExampleClient.java b/examples/hawkbit-example-ddi-client/src/main/java/org/eclipse/hawkbit/ddi/client/DdiExampleClient.java index 190ea622b..5945fef5d 100644 --- a/examples/hawkbit-example-ddi-client/src/main/java/org/eclipse/hawkbit/ddi/client/DdiExampleClient.java +++ b/examples/hawkbit-example-ddi-client/src/main/java/org/eclipse/hawkbit/ddi/client/DdiExampleClient.java @@ -19,7 +19,7 @@ import java.util.regex.Pattern; import org.eclipse.hawkbit.ddi.client.authenctication.AuthenticationInterceptor; import org.eclipse.hawkbit.ddi.client.resource.RootControllerResourceClient; -import org.eclipse.hawkbit.ddi.client.strategy.PersistenceStrategy; +import org.eclipse.hawkbit.ddi.client.strategy.ArtifactsPersistenceStrategy; import org.eclipse.hawkbit.ddi.json.model.DdiActionFeedback; import org.eclipse.hawkbit.ddi.json.model.DdiArtifact; import org.eclipse.hawkbit.ddi.json.model.DdiChunk; @@ -45,12 +45,12 @@ public class DdiExampleClient implements Runnable { private final String controllerId; private Long actionIdOfLastInstalltion; private final RootControllerResourceClient rootControllerResourceClient; - private final PersistenceStrategy persistenceStrategy; + private final ArtifactsPersistenceStrategy persistenceStrategy; private DdiClientStatus clientStatus; - + private FinalResult finalResultOfCurrentUpdate; - /** + /** * Constructor for the DDI example client. * * @param baseUrl @@ -63,7 +63,7 @@ public class DdiExampleClient implements Runnable { * the persistence strategy for downloading artifacts */ public DdiExampleClient(final String baseUrl, final String controllerId, final String tenant, - final PersistenceStrategy persistenceStrategy) { + final ArtifactsPersistenceStrategy persistenceStrategy) { this(baseUrl, controllerId, tenant, persistenceStrategy, null); } @@ -83,7 +83,7 @@ public class DdiExampleClient implements Runnable { * might be {@code null} */ public DdiExampleClient(final String baseUrl, final String controllerId, final String tenant, - final PersistenceStrategy persistenceStrategy, final AuthenticationInterceptor authenticationInterceptor) { + final ArtifactsPersistenceStrategy persistenceStrategy, final AuthenticationInterceptor authenticationInterceptor) { this.controllerId = controllerId; this.rootControllerResourceClient = new DdiDefaultFeignClient(baseUrl, tenant, authenticationInterceptor) .getRootControllerResourceClient(); @@ -129,26 +129,32 @@ public class DdiExampleClient implements Runnable { } private void startDownload(final Long actionId, final Integer resource) { - final ResponseEntity respone = rootControllerResourceClient .getControllerBasedeploymentAction(controllerId, Long.valueOf(actionId), Integer.valueOf(resource)); final DdiDeploymentBase ddiDeploymentBase = respone.getBody(); final List chunks = ddiDeploymentBase.getDeployment().getChunks(); for (final DdiChunk chunk : chunks) { final List artifactList = chunk.getArtifacts(); - final Link downloadLink = ddiDeploymentBase.getDeployment().getChunks().get(0).getArtifacts().get(0) - .getLink("download-http"); - final String[] downloadLinkSep = downloadLink.getHref().split(Pattern.quote("/")); - final Long softwareModuleId = Long.valueOf(downloadLinkSep[8]); - for (final DdiArtifact ddiArtifact : artifactList) { - if (finalResultOfCurrentUpdate != FinalResult.FAILURE) { - downloadArtifact(actionId, softwareModuleId, ddiArtifact.getFilename()); + if (artifactList.isEmpty()) { + sendFeedBackMessage(actionId, ExecutionStatus.PROCEEDING, FinalResult.NONE, + "No artifacts to download for softwaremodule " + chunk.getName()); + } else { + for (final DdiArtifact ddiArtifact : artifactList) { + if (finalResultOfCurrentUpdate != FinalResult.FAILURE) { + downloadArtifact(actionId, ddiArtifact); + } } } + } } - private void downloadArtifact(final Long actionId, final Long softwareModuleId, final String artifact) { + private void downloadArtifact(final Long actionId, final DdiArtifact ddiArtifact) { + + final String artifact = ddiArtifact.getFilename(); + final Link downloadLink = ddiArtifact.getLink("download-http"); + final String[] downloadLinkSep = downloadLink.getHref().split(Pattern.quote("/")); + final Long softwareModuleId = Long.valueOf(downloadLinkSep[8]); sendFeedBackMessage(actionId, ExecutionStatus.PROCEEDING, FinalResult.NONE, "Starting download of artifact " + artifact); diff --git a/examples/hawkbit-example-ddi-client/src/main/java/org/eclipse/hawkbit/ddi/client/strategy/PersistenceStrategy.java b/examples/hawkbit-example-ddi-client/src/main/java/org/eclipse/hawkbit/ddi/client/strategy/ArtifactsPersistenceStrategy.java similarity index 94% rename from examples/hawkbit-example-ddi-client/src/main/java/org/eclipse/hawkbit/ddi/client/strategy/PersistenceStrategy.java rename to examples/hawkbit-example-ddi-client/src/main/java/org/eclipse/hawkbit/ddi/client/strategy/ArtifactsPersistenceStrategy.java index e5a3a60d0..347b01bcc 100644 --- a/examples/hawkbit-example-ddi-client/src/main/java/org/eclipse/hawkbit/ddi/client/strategy/PersistenceStrategy.java +++ b/examples/hawkbit-example-ddi-client/src/main/java/org/eclipse/hawkbit/ddi/client/strategy/ArtifactsPersistenceStrategy.java @@ -14,7 +14,7 @@ import java.io.InputStream; /** * Interface of persistence strategy. */ -public interface PersistenceStrategy { +public interface ArtifactsPersistenceStrategy { /** * Method handling the artifact download. diff --git a/examples/hawkbit-example-ddi-client/src/main/java/org/eclipse/hawkbit/ddi/client/strategy/DoNotSaveArtifactsStrategy.java b/examples/hawkbit-example-ddi-client/src/main/java/org/eclipse/hawkbit/ddi/client/strategy/DoNotSaveArtifacts.java similarity index 80% rename from examples/hawkbit-example-ddi-client/src/main/java/org/eclipse/hawkbit/ddi/client/strategy/DoNotSaveArtifactsStrategy.java rename to examples/hawkbit-example-ddi-client/src/main/java/org/eclipse/hawkbit/ddi/client/strategy/DoNotSaveArtifacts.java index 447885715..7e5d72c4a 100644 --- a/examples/hawkbit-example-ddi-client/src/main/java/org/eclipse/hawkbit/ddi/client/strategy/DoNotSaveArtifactsStrategy.java +++ b/examples/hawkbit-example-ddi-client/src/main/java/org/eclipse/hawkbit/ddi/client/strategy/DoNotSaveArtifacts.java @@ -12,10 +12,10 @@ import java.io.IOException; import java.io.InputStream; /** - * Implementation of {@link PersistenceStrategy} for not downloading any + * Implementation of {@link ArtifactsPersistenceStrategy} for not downloading any * artifacts. */ -public class DoNotSaveArtifactsStrategy implements PersistenceStrategy { +public class DoNotSaveArtifacts implements ArtifactsPersistenceStrategy { @Override public void handleInputStream(final InputStream in, final String artifactName) throws IOException { diff --git a/examples/hawkbit-example-ddi-client/src/main/java/org/eclipse/hawkbit/ddi/client/strategy/SaveArtifactsStrategy.java b/examples/hawkbit-example-ddi-client/src/main/java/org/eclipse/hawkbit/ddi/client/strategy/SaveArtifactsToLocalTempDirectories.java similarity index 84% rename from examples/hawkbit-example-ddi-client/src/main/java/org/eclipse/hawkbit/ddi/client/strategy/SaveArtifactsStrategy.java rename to examples/hawkbit-example-ddi-client/src/main/java/org/eclipse/hawkbit/ddi/client/strategy/SaveArtifactsToLocalTempDirectories.java index 2908ee29f..11ac5698b 100644 --- a/examples/hawkbit-example-ddi-client/src/main/java/org/eclipse/hawkbit/ddi/client/strategy/SaveArtifactsStrategy.java +++ b/examples/hawkbit-example-ddi-client/src/main/java/org/eclipse/hawkbit/ddi/client/strategy/SaveArtifactsToLocalTempDirectories.java @@ -18,10 +18,10 @@ import com.google.common.io.ByteStreams; import com.google.common.io.Files; /** - * Implementation of {@link PersistenceStrategy} for downloading artifacts to + * Implementation of {@link ArtifactsPersistenceStrategy} for downloading artifacts to * the temp directory. */ -public class SaveArtifactsStrategy implements PersistenceStrategy { +public class SaveArtifactsToLocalTempDirectories implements ArtifactsPersistenceStrategy { @Override public void handleInputStream(final InputStream in, final String artifactName) throws IOException { diff --git a/examples/hawkbit-example-ddi-client/src/test/java/org/eclipse/hawkbit/example/ddi/client/AppTest.java b/examples/hawkbit-example-ddi-client/src/test/java/org/eclipse/hawkbit/example/ddi/client/AppTest.java index 2644f9d28..13eaa66d3 100644 --- a/examples/hawkbit-example-ddi-client/src/test/java/org/eclipse/hawkbit/example/ddi/client/AppTest.java +++ b/examples/hawkbit-example-ddi-client/src/test/java/org/eclipse/hawkbit/example/ddi/client/AppTest.java @@ -1,7 +1,7 @@ package org.eclipse.hawkbit.example.ddi.client; import org.eclipse.hawkbit.ddi.client.DdiExampleClient; -import org.eclipse.hawkbit.ddi.client.strategy.SaveArtifactsStrategy; +import org.eclipse.hawkbit.ddi.client.strategy.SaveArtifactsToLocalTempDirectories; import org.junit.Test; /** @@ -11,8 +11,8 @@ public class AppTest { @Test public void AppTest() { - final DdiExampleClient ddiClient = new DdiExampleClient("http://localhost:8080/", "Einstein15", "DEFAULT", - new SaveArtifactsStrategy()); + final DdiExampleClient ddiClient = new DdiExampleClient("http://localhost:8080/", "Einstein17", "DEFAULT", + new SaveArtifactsToLocalTempDirectories()); final Thread thread = new Thread(ddiClient); thread.run();