/
HawkbitSDKConfigurtion.java
102 lines (94 loc) · 4.34 KB
/
HawkbitSDKConfigurtion.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/**
* Copyright (c) 2023 Bosch.IO GmbH and others
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.hawkbit.sdk;
import feign.Contract;
import feign.MethodMetadata;
import feign.RequestInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnNotWebApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.openfeign.FeignClientsConfiguration;
import org.springframework.cloud.openfeign.hateoas.WebConvertersCustomizer;
import org.springframework.cloud.openfeign.support.HttpMessageConverterCustomizer;
import org.springframework.cloud.openfeign.support.SpringMvcContract;
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.hateoas.config.EnableHypermediaSupport;
import org.springframework.hateoas.config.WebConverters;
import org.springframework.http.MediaType;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.LinkedHashMap;
@Slf4j
@Configuration
@EnableConfigurationProperties({ HawkbitServer.class, Tenant.class})
@EnableHypermediaSupport(type = EnableHypermediaSupport.HypermediaType.HAL)
@Import(FeignClientsConfiguration.class)
public class HawkbitSDKConfigurtion {
/**
* An feign request interceptor to set the defined {@code Accept} and {@code Content-Type} headers for each request
* to {@code application/json}.
*
* TODO - is this needed?
*/
@Bean
@Primary
public RequestInterceptor jsonHeaderInterceptorOverride() {
return template -> template
.header("Accept", MediaType.APPLICATION_JSON_VALUE)
.header("Content-Type", MediaType.APPLICATION_JSON_VALUE);
}
// takes place only when spring app is started in non-web-app mode
// in that case org.springframework.cloud.openfeign.hateoas.FeignHalAutoConfiguration
// is explicitly disabled and HAL/HATEOAS support doesn't work
@Bean
@ConditionalOnMissingBean
@ConditionalOnNotWebApplication
@ConditionalOnClass({ WebConverters.class})
public HttpMessageConverterCustomizer webConvertersCustomizerOverrider(WebConverters webConverters) {
return new WebConvertersCustomizer(webConverters);
}
// another option would be something like (need to import io.github.openfeign:feign-jackson
// @Bean @Primary @ConditionalOnNotWebApplication
// public Decoder feignDecoderOverride() {
// return new ResponseEntityDecoder(new JacksonDecoder(new ObjectMapper().registerModule(new Jackson2HalModule())));
// }
/**
* Own implementation of the {@link SpringMvcContract} which catches the {@link IllegalStateException} which occurs
* due multiple produces and consumes values in the request-mapping
* annotation.https://github.com/spring-cloud/spring-cloud-netflix/issues/808
*
* TODO - is this needed?
*/
@Bean
@Primary
public Contract feignContractOverride() {
return new SpringMvcContract() {
@Override
protected void processAnnotationOnMethod(final MethodMetadata data, final Annotation methodAnnotation, final Method method) {
try {
super.processAnnotationOnMethod(data, methodAnnotation, method);
} catch (final IllegalStateException e) {
// ignore illegalstateexception here because it's thrown because of
// multiple consumers and produces, see
// https://github.com/spring-cloud/spring-cloud-netflix/issues/808
log.trace(e.getMessage(), e);
// This line from super is mandatory to avoid that access to the
// expander causes a nullpointer.
data.indexToExpander(new LinkedHashMap<>());
}
}
};
}
}