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); + } +}