SDK AutoConfig & improvements & fixes (#1663)
* SDK autoconfiguration added * Option for custom error decoder and request interceptor added * Fixed authentication for targets with security token Signed-off-by: Marinov Avgustin <Avgustin.Marinov@bosch.com>
This commit is contained in:
@@ -12,6 +12,7 @@ package org.eclipse.hawkbit.sdk;
|
||||
import feign.Client;
|
||||
import feign.Contract;
|
||||
import feign.Feign;
|
||||
import feign.RequestInterceptor;
|
||||
import feign.codec.Decoder;
|
||||
import feign.codec.Encoder;
|
||||
import feign.codec.ErrorDecoder;
|
||||
@@ -22,13 +23,45 @@ import org.springframework.util.ObjectUtils;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Base64;
|
||||
import java.util.Objects;
|
||||
import java.util.function.BiFunction;
|
||||
|
||||
@Slf4j
|
||||
@Builder
|
||||
public class HawkbitClient {
|
||||
|
||||
private static final String AUTHORIZATION = "Authorization";
|
||||
private static final ErrorDecoder DEFAULT_ERROR_DECODER = new ErrorDecoder.Default();
|
||||
private static final ErrorDecoder DEFAULT_ERROR_DECODER_0 = new ErrorDecoder.Default();
|
||||
|
||||
public static final ErrorDecoder DEFAULT_ERROR_DECODER = (methodKey, response) -> {
|
||||
final Exception e = DEFAULT_ERROR_DECODER_0.decode(methodKey, response);
|
||||
log.trace("REST API call failed!", e);
|
||||
return e;
|
||||
};
|
||||
|
||||
public static final BiFunction<Tenant, Controller, RequestInterceptor> DEFAULT_REQUEST_INTERCEPTOR_FN =
|
||||
(tenant, controller) ->
|
||||
controller == null ?
|
||||
template -> {
|
||||
template.header(
|
||||
AUTHORIZATION,
|
||||
|
||||
"Basic " +
|
||||
Base64.getEncoder()
|
||||
.encodeToString(
|
||||
(Objects.requireNonNull(tenant.getUsername(), "User is null!") +
|
||||
":" +
|
||||
Objects.requireNonNull(tenant.getPassword(),"Password is not available!"))
|
||||
.getBytes(StandardCharsets.ISO_8859_1)));
|
||||
} :
|
||||
template -> {
|
||||
if (ObjectUtils.isEmpty(tenant.getGatewayToken())) {
|
||||
if (!ObjectUtils.isEmpty(controller.getSecurityToken())) {
|
||||
template.header(AUTHORIZATION, "TargetToken " + controller.getSecurityToken());
|
||||
} // else do not sent authentication
|
||||
} else {
|
||||
template.header(AUTHORIZATION, "GatewayToken " + tenant.getGatewayToken());
|
||||
}
|
||||
};
|
||||
|
||||
private final HawkbitServer hawkBitServerProperties;
|
||||
|
||||
@@ -37,14 +70,31 @@ public class HawkbitClient {
|
||||
private final Decoder decoder;
|
||||
private final Contract contract;
|
||||
|
||||
private final ErrorDecoder errorDecoder;
|
||||
private final BiFunction<Tenant, Controller, RequestInterceptor> requestInterceptorFn;
|
||||
|
||||
public HawkbitClient(
|
||||
final HawkbitServer hawkBitServerProperties,
|
||||
final Client client, final Encoder encoder, final Decoder decoder, final Contract contract) {
|
||||
this(hawkBitServerProperties, client, encoder, decoder, contract, null, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Customizers gets default ones and could
|
||||
*/
|
||||
public HawkbitClient(
|
||||
final HawkbitServer hawkBitServerProperties,
|
||||
final Client client, final Encoder encoder, final Decoder decoder, final Contract contract,
|
||||
final ErrorDecoder errorDecoder,
|
||||
final BiFunction<Tenant, Controller, RequestInterceptor> requestInterceptorFn) {
|
||||
this.hawkBitServerProperties = hawkBitServerProperties;
|
||||
this.client = client;
|
||||
this.encoder = encoder;
|
||||
this.decoder = decoder;
|
||||
this.contract = contract;
|
||||
|
||||
this.errorDecoder = errorDecoder == null ? DEFAULT_ERROR_DECODER : errorDecoder;
|
||||
this.requestInterceptorFn = requestInterceptorFn == null ? DEFAULT_REQUEST_INTERCEPTOR_FN : requestInterceptorFn;
|
||||
}
|
||||
|
||||
public <T> T mgmtService(final Class<T> serviceType, final Tenant tenantProperties) {
|
||||
@@ -54,41 +104,16 @@ public class HawkbitClient {
|
||||
return service(serviceType, tenantProperties, controller);
|
||||
}
|
||||
|
||||
private <T> T service(final Class<T> serviceType, final Tenant tenantProperties, final Controller controller) {
|
||||
private <T> T service(final Class<T> serviceType, final Tenant tenant, final Controller controller) {
|
||||
return Feign.builder().client(client)
|
||||
.encoder(encoder)
|
||||
.decoder(decoder)
|
||||
.errorDecoder((methodKey, response) -> {
|
||||
final Exception e = DEFAULT_ERROR_DECODER.decode(methodKey, response);
|
||||
log.trace("REST API call failed!", e);
|
||||
return e;
|
||||
})
|
||||
.errorDecoder(errorDecoder)
|
||||
.contract(contract)
|
||||
.requestInterceptor(controller == null ?
|
||||
template -> {
|
||||
template.header(AUTHORIZATION,
|
||||
"Basic " +
|
||||
Base64.getEncoder()
|
||||
.encodeToString(
|
||||
(Objects.requireNonNull(tenantProperties.getUsername(),
|
||||
"User is null!") +
|
||||
":" +
|
||||
Objects.requireNonNull(tenantProperties.getPassword(),
|
||||
"Password is not available!"))
|
||||
.getBytes(StandardCharsets.ISO_8859_1)));
|
||||
} :
|
||||
template -> {
|
||||
if (ObjectUtils.isEmpty(tenantProperties.getGatewayToken())) {
|
||||
if (!ObjectUtils.isEmpty(controller.getSecurityToken())) {
|
||||
template.header(AUTHORIZATION, "TargetToken " + controller.getSecurityToken());
|
||||
} // else do not sent authentication
|
||||
} else {
|
||||
template.header(AUTHORIZATION, "GatewayToken " + tenantProperties.getGatewayToken());
|
||||
}
|
||||
})
|
||||
.requestInterceptor(requestInterceptorFn.apply(tenant, controller))
|
||||
.target(serviceType,
|
||||
controller == null ?
|
||||
hawkBitServerProperties.getMgmtUrl() :
|
||||
hawkBitServerProperties.getDdiUrl());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -25,6 +25,7 @@ import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.springframework.context.annotation.Primary;
|
||||
import org.springframework.context.annotation.PropertySource;
|
||||
import org.springframework.hateoas.config.EnableHypermediaSupport;
|
||||
import org.springframework.hateoas.config.WebConverters;
|
||||
import org.springframework.http.MediaType;
|
||||
@@ -38,6 +39,7 @@ import java.util.LinkedHashMap;
|
||||
@EnableConfigurationProperties({ HawkbitServer.class, Tenant.class})
|
||||
@EnableHypermediaSupport(type = EnableHypermediaSupport.HypermediaType.HAL)
|
||||
@Import(FeignClientsConfiguration.class)
|
||||
@PropertySource("classpath:/hawkbit-sdk-defaults.properties")
|
||||
public class HawkbitSDKConfigurtion {
|
||||
|
||||
/**
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
org.eclipse.hawkbit.sdk.HawkbitSDKConfigurtion
|
||||
@@ -8,5 +8,4 @@
|
||||
# SPDX-License-Identifier: EPL-2.0
|
||||
#
|
||||
|
||||
spring.cloud.openfeign.httpclient.hc5.enabled=true
|
||||
|
||||
spring.cloud.openfeign.httpclient.hc5.enabled=true
|
||||
Reference in New Issue
Block a user