From 99f0a4e882d01716bc5fd96691fc61def98c82fa Mon Sep 17 00:00:00 2001 From: Jonathan Philip Knoblauch Date: Tue, 26 Apr 2016 14:08:42 +0200 Subject: [PATCH] Extenden functions of DDI client - polling time is taken form server - client can be stopped my call from out side Signed-off-by: Jonathan Philip Knoblauch --- ...plicationJsonRequestHeaderInterceptor.java | 2 - .../hawkbit/ddi/client/DdiDecoder.java | 4 +- .../hawkbit/ddi/client/DdiExampleClient.java | 82 ++++++++----------- .../strategy/DoNotSaveArtifactsStrategy.java | 7 +- .../client/strategy/PersistenceStrategy.java | 5 +- .../strategy/SaveArtifactsStrategy.java | 23 ++---- 6 files changed, 43 insertions(+), 80 deletions(-) diff --git a/examples/hawkbit-example-ddi-client/src/main/java/org/eclipse/hawkbit/ddi/client/ApplicationJsonRequestHeaderInterceptor.java b/examples/hawkbit-example-ddi-client/src/main/java/org/eclipse/hawkbit/ddi/client/ApplicationJsonRequestHeaderInterceptor.java index 97a3e4395..5f7f46013 100644 --- a/examples/hawkbit-example-ddi-client/src/main/java/org/eclipse/hawkbit/ddi/client/ApplicationJsonRequestHeaderInterceptor.java +++ b/examples/hawkbit-example-ddi-client/src/main/java/org/eclipse/hawkbit/ddi/client/ApplicationJsonRequestHeaderInterceptor.java @@ -23,9 +23,7 @@ public class ApplicationJsonRequestHeaderInterceptor implements RequestIntercept public void apply(final RequestTemplate template) { template.header("Accept", MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_OCTET_STREAM_VALUE, MediaType.TEXT_PLAIN_VALUE); - // template.header("Accept", MediaType.APPLICATION_OCTET_STREAM_VALUE); template.header("Content-Type", MediaType.APPLICATION_JSON_VALUE); - } } diff --git a/examples/hawkbit-example-ddi-client/src/main/java/org/eclipse/hawkbit/ddi/client/DdiDecoder.java b/examples/hawkbit-example-ddi-client/src/main/java/org/eclipse/hawkbit/ddi/client/DdiDecoder.java index 90fc0e7bd..47a59a667 100644 --- a/examples/hawkbit-example-ddi-client/src/main/java/org/eclipse/hawkbit/ddi/client/DdiDecoder.java +++ b/examples/hawkbit-example-ddi-client/src/main/java/org/eclipse/hawkbit/ddi/client/DdiDecoder.java @@ -33,6 +33,7 @@ import feign.jackson.JacksonDecoder; public class DdiDecoder implements Decoder { ObjectMapper mapper; + private final String octentTypeOctetStream = "[application/octet-stream]"; public DdiDecoder() { mapper = new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false) @@ -44,8 +45,7 @@ public class DdiDecoder implements Decoder { final Map> header = response.headers(); final String contentType = String.valueOf(header.get("Content-Type")); - // TODO parameter verwenden - if (contentType.equals("[application/octet-stream]")) { + if (contentType.equals(octentTypeOctetStream)) { return ResponseEntity.ok(response.body().asInputStream()); } final ResponseEntityDecoder responseEntityDecoder = new ResponseEntityDecoder(new JacksonDecoder(mapper)); 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 e0a01e814..ae33f1c39 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 @@ -8,7 +8,9 @@ */ package org.eclipse.hawkbit.ddi.client; +import java.io.IOException; import java.io.InputStream; +import java.time.LocalTime; import java.util.ArrayList; import java.util.List; import java.util.regex.Pattern; @@ -36,9 +38,11 @@ public class DdiExampleClient implements Runnable { private final String controllerId; private Long actionIdOfLastInstalltion; private final DdiDefaultFeignClient ddiDefaultFeignClient; - private final long pollingIntervalInMillis; + private long pollingIntervalInMillis; private final PersistenceStrategy persistenceStrategy; + private STATUS clientStatus; + public DdiExampleClient(final String baseUrl, final String controllerId, final String tenant, final long pollingIntervalInMillis, final PersistenceStrategy persistenceStrategy) { this.controllerId = controllerId; @@ -46,27 +50,28 @@ public class DdiExampleClient implements Runnable { this.actionIdOfLastInstalltion = null; this.pollingIntervalInMillis = pollingIntervalInMillis; this.persistenceStrategy = persistenceStrategy; + this.clientStatus = STATUS.DOWN; } @Override public void run() { + clientStatus = STATUS.UP; + ResponseEntity response = ddiDefaultFeignClient.getRootControllerResourceClient() + .getControllerBase(controllerId); + final String pollingTime = response.getBody().getConfig().getPolling().getSleep(); + final LocalTime localtime = LocalTime.parse(pollingTime); + pollingIntervalInMillis = localtime.toNanoOfDay(); - ResponseEntity response; - - while (!Thread.currentThread().isInterrupted()) { + while (clientStatus == STATUS.UP) { response = ddiDefaultFeignClient.getRootControllerResourceClient().getControllerBase(controllerId); - final DdiControllerBase controllerBase = response.getBody(); - final Link controllerDeploymentBaseLink = controllerBase.getLink("deploymentBase"); + // final DdiControllerBase controllerBase = response.getBody(); + final Link controllerDeploymentBaseLink = response.getBody().getLink("deploymentBase"); if (controllerDeploymentBaseLink != null) { final Long actionId = getActionIdOutOfLink(controllerDeploymentBaseLink); final Integer resource = getResourceOutOfLink(controllerDeploymentBaseLink); if (actionId != actionIdOfLastInstalltion) { - startDownload(actionId, resource); - - simulateSuccessfulInstallation(actionId); - actionIdOfLastInstalltion = actionId; } } @@ -81,9 +86,12 @@ public class DdiExampleClient implements Runnable { } + public void stop() { + clientStatus = STATUS.DOWN; + } + private void startDownload(final Long actionId, final Integer resource) { - // resource has not been downloaded and installed final ResponseEntity respone = ddiDefaultFeignClient.getRootControllerResourceClient() .getControllerBasedeploymentAction(controllerId, Long.valueOf(actionId), Integer.valueOf(resource)); final DdiDeploymentBase ddiDeploymentBase = respone.getBody(); @@ -94,7 +102,6 @@ public class DdiExampleClient implements Runnable { .getLink("download-http"); final String[] downloadLinkSep = downloadLink.getHref().split(Pattern.quote("/")); final Long softwareModuleId = Long.valueOf(downloadLinkSep[8]); - // download all artifacts for (final DdiArtifact ddiArtifact : artifactList) { downloadArtifact(actionId, softwareModuleId, ddiArtifact.getFilename()); } @@ -111,9 +118,17 @@ public class DdiExampleClient implements Runnable { .getRootControllerResourceClient().downloadArtifact(controllerId, softwareModuleId, artifact); final HttpStatus statsuCode = responseDownloadArtifact.getStatusCode(); System.out.println("Finished download with stataus " + statsuCode); - persistenceStrategy.handleInputStream(responseDownloadArtifact.getBody(), artifact); + try { + persistenceStrategy.handleInputStream(responseDownloadArtifact.getBody(), artifact); + } catch (final IOException e) { + sendFeedBackMessage(actionId, ExecutionStatus.CLOSED, FinalResult.FAILURE, + "Downloaded of artifact " + artifact + " failed"); + return; + } sendFeedBackMessage(actionId, ExecutionStatus.PROCEEDING, FinalResult.NONE, "Downloaded artifact " + artifact); + + simulateSuccessfulInstallation(actionId); } private void sendFeedBackMessage(final Long actionId, final ExecutionStatus executionStatus, @@ -127,14 +142,12 @@ public class DdiExampleClient implements Runnable { final DdiActionFeedback feedback = new DdiActionFeedback(actionId, time, ddiStatus); final ResponseEntity response = ddiDefaultFeignClient.getRootControllerResourceClient() .postBasedeploymentActionFeedback(feedback, controllerId, actionId); - final HttpStatus statsuCode = response.getStatusCode(); System.out.println("Message send with stataus " + statsuCode); } private void simulateSuccessfulInstallation(final Long actionId) { - sendFeedBackMessage(actionId, ExecutionStatus.PROCEEDING, FinalResult.SUCESS, - "Simulated installation successful"); + sendFeedBackMessage(actionId, ExecutionStatus.CLOSED, FinalResult.SUCESS, "Simulated installation successful"); } private Long getActionIdOutOfLink(final Link controllerDeploymentBaseLink) { @@ -153,39 +166,8 @@ public class DdiExampleClient implements Runnable { return segments[8].split(Pattern.quote("?")); } - // private RootControllerResourceClient getDownloadFeignClient() { - // - // final Builder feignBuilder = Feign.builder().contract(new - // IgnoreMultipleConsumersProducersSpringMvcContract()) - // .requestInterceptor(new - // ApplicationJsonRequestHeaderInterceptor()).logLevel(Level.FULL) - // .logger(new Logger.ErrorLogger()).encoder(new - // JacksonEncoder()).decoder(new Decoder() { - // @Override - // public Object decode(final Response response, final Type type) - // throws IOException, DecodeException, FeignException { - // - // // TODO download - // final InputStream stream = response.body().asInputStream(); - // - // final FileSystem local = FileSystems.getDefault(); - // - // System.out.println("Status is " + response.status()); - // - // final ResponseEntity test = new ResponseEntity( - // HttpStatus.valueOf(response.status())); - // - // return test; - // } - // }); - // - // final RootControllerResourceClient rootControllerResourceClient = - // feignBuilder - // .target(RootControllerResourceClient.class, - // "http://localhost:8080/DEFAULT/controller/v1"); - // - // return rootControllerResourceClient; - // - // } + public enum STATUS { + UP, DOWN; + } } 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 15ea10829..0e9ea82e1 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 @@ -16,14 +16,9 @@ import java.io.InputStream; */ public class DoNotSaveArtifactsStrategy implements PersistenceStrategy { - @Override - public String getPersistenceStrategy() { - return "nosave"; - } - @Override public void handleInputStream(final InputStream in, final String artifactName) { - // down but do not save + // do nothing } } 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/PersistenceStrategy.java index 1c5c309e1..c8080a1d6 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/PersistenceStrategy.java @@ -8,6 +8,7 @@ */ package org.eclipse.hawkbit.ddi.client.strategy; +import java.io.IOException; import java.io.InputStream; /** @@ -16,8 +17,6 @@ import java.io.InputStream; */ public interface PersistenceStrategy { - public String getPersistenceStrategy(); - - public void handleInputStream(InputStream in, String artifactName); + public void handleInputStream(InputStream in, 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/SaveArtifactsStrategy.java index ad1c573b1..dbf13ef9c 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 @@ -15,6 +15,7 @@ import java.io.InputStream; import java.io.OutputStream; import com.google.common.io.ByteStreams; +import com.google.common.io.Files; /** * @author Jonathan Knoblauch @@ -23,23 +24,11 @@ import com.google.common.io.ByteStreams; public class SaveArtifactsStrategy implements PersistenceStrategy { @Override - public String getPersistenceStrategy() { - - return "save"; - } - - @Override - public void handleInputStream(final InputStream in, final String artifactName) { - - final File file = new File("C:\\testdownload\\" + artifactName); - - try { - final OutputStream out = new FileOutputStream(file); - ByteStreams.copy(in, out); - } catch (final IOException e) { - e.printStackTrace(); - // TODO throw - } + public void handleInputStream(final InputStream in, final String artifactName) throws IOException { + final File tempDir = Files.createTempDir(); + final File file = new File(tempDir + "\\" + artifactName); + final OutputStream out = new FileOutputStream(file); + ByteStreams.copy(in, out); } }