Add example feign core project

add system client api


Signed-off-by: SirWayne <dennis.melzer@bosch-si.com>
This commit is contained in:
SirWayne
2016-04-26 16:19:37 +02:00
parent 8addb05f09
commit c2649b4b54
24 changed files with 294 additions and 343 deletions

View File

@@ -55,11 +55,21 @@
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.eclipse.hawkbit</groupId>
<artifactId>hawkbit-example-feign-core-client</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.hawkbit</groupId>
<artifactId>hawkbit-mgmt-api</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.hawkbit</groupId>
<artifactId>hawkbit-system-api</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.netflix.feign</groupId>
<artifactId>feign-core</artifactId>

View File

@@ -17,13 +17,14 @@ import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import feign.Contract;
import feign.auth.BasicAuthRequestInterceptor;
@SpringBootApplication
@EnableFeignClients
@EnableConfigurationProperties(ClientConfigurationProperties.class)
@Configuration
public class Application implements CommandLineRunner {
@Autowired
@@ -56,16 +57,6 @@ public class Application implements CommandLineRunner {
return new BasicAuthRequestInterceptor(configuration.getUsername(), configuration.getPassword());
}
@Bean
public ApplicationJsonRequestHeaderInterceptor jsonHeaderInterceptor() {
return new ApplicationJsonRequestHeaderInterceptor();
}
@Bean
public Contract feignContract() {
return new IgnoreMultipleConsumersProducersSpringMvcContract();
}
private boolean containsArg(final String containsArg, final String... args) {
for (final String arg : args) {
if (arg.equalsIgnoreCase(containsArg)) {

View File

@@ -1,28 +0,0 @@
/**
* 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.mgmt.client;
import org.springframework.http.MediaType;
import feign.RequestInterceptor;
import feign.RequestTemplate;
/**
* An feign request interceptor to set the defined {@code Accept} and
* {@code Content-Type} headers for each request to {@code application/json}.
*/
public class ApplicationJsonRequestHeaderInterceptor implements RequestInterceptor {
@Override
public void apply(final RequestTemplate template) {
template.header("Accept", MediaType.APPLICATION_JSON_VALUE);
template.header("Content-Type", MediaType.APPLICATION_JSON_VALUE);
}
}

View File

@@ -1,43 +0,0 @@
/**
* 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.mgmt.client;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.netflix.feign.support.SpringMvcContract;
import feign.MethodMetadata;
/**
* Own implementation of the {@link SpringMvcContract} which catches the
* {@link IllegalStateException} which occurs due multiple produces and consumes
* values in the request-mapping
* annoation.https://github.com/spring-cloud/spring-cloud-netflix/issues/808
*/
public class IgnoreMultipleConsumersProducersSpringMvcContract extends SpringMvcContract {
private static final Logger LOGGER = LoggerFactory
.getLogger(IgnoreMultipleConsumersProducersSpringMvcContract.class);
@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
LOGGER.trace(e.getMessage(), e);
}
}
}

View File

@@ -1,149 +0,0 @@
/**
* 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.mgmt.client;
import org.eclipse.hawkbit.mgmt.client.resource.MgmtDistributionSetClientResource;
import org.eclipse.hawkbit.mgmt.client.resource.MgmtDistributionSetTagClientResource;
import org.eclipse.hawkbit.mgmt.client.resource.MgmtDistributionSetTypeClientResource;
import org.eclipse.hawkbit.mgmt.client.resource.MgmtDownloadArtifactClientResource;
import org.eclipse.hawkbit.mgmt.client.resource.MgmtDownloadClientResource;
import org.eclipse.hawkbit.mgmt.client.resource.MgmtRolloutClientResource;
import org.eclipse.hawkbit.mgmt.client.resource.MgmtSoftwareModuleClientResource;
import org.eclipse.hawkbit.mgmt.client.resource.MgmtSoftwareModuleTypeClientResource;
import org.eclipse.hawkbit.mgmt.client.resource.MgmtTargetClientResource;
import org.eclipse.hawkbit.mgmt.client.resource.MgmtTargetTagClientResource;
import org.springframework.cloud.netflix.feign.support.ResponseEntityDecoder;
import org.springframework.hateoas.hal.Jackson2HalModule;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import feign.Feign;
import feign.Feign.Builder;
import feign.Logger;
import feign.Logger.Level;
import feign.jackson.JacksonDecoder;
import feign.jackson.JacksonEncoder;
/**
*
*/
public class MgmtDefaultFeignClient {
private MgmtDistributionSetClientResource mgmtDistributionSetClientResource;
private MgmtDistributionSetTagClientResource mgmtDistributionSetTagClientResource;
private MgmtDistributionSetTypeClientResource mgmtDistributionSetTypeClientResource;
private MgmtRolloutClientResource mgmtRolloutClientResource;
private MgmtSoftwareModuleClientResource mgmtSoftwareModuleClientResource;
private MgmtSoftwareModuleTypeClientResource mgmtSoftwareModuleTypeClientResource;
private MgmtTargetClientResource mgmtTargetClientResource;
private MgmtTargetTagClientResource mgmtTargetTagClientResource;
private MgmtDownloadClientResource mgmtDownloadClientResource;
private MgmtDownloadArtifactClientResource mgmtDownloadArtifactClientResource;
private final Builder feignBuilder;
private final String baseUrl;
public MgmtDefaultFeignClient(final String baseUrl) {
final ObjectMapper mapper = new ObjectMapper()
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
.registerModule(new Jackson2HalModule());
feignBuilder = Feign.builder().contract(new IgnoreMultipleConsumersProducersSpringMvcContract())
.requestInterceptor(new ApplicationJsonRequestHeaderInterceptor()).logLevel(Level.FULL)
.logger(new Logger.ErrorLogger()).encoder(new JacksonEncoder())
.decoder(new ResponseEntityDecoder(new JacksonDecoder(mapper)));
this.baseUrl = baseUrl;
}
public Builder getFeignBuilder() {
return feignBuilder;
}
public MgmtDistributionSetClientResource getMgmtDistributionSetClientResource() {
if (mgmtDistributionSetClientResource == null) {
mgmtDistributionSetClientResource = feignBuilder.target(MgmtDistributionSetClientResource.class,
this.baseUrl + MgmtDistributionSetClientResource.PATH);
}
return mgmtDistributionSetClientResource;
}
public MgmtDistributionSetTagClientResource getMgmtDistributionSetTagClientResource() {
if (mgmtDistributionSetTagClientResource == null) {
mgmtDistributionSetTagClientResource = feignBuilder.target(MgmtDistributionSetTagClientResource.class,
this.baseUrl + MgmtDistributionSetTagClientResource.PATH);
}
return mgmtDistributionSetTagClientResource;
}
public MgmtDistributionSetTypeClientResource getMgmtDistributionSetTypeClientResource() {
if (mgmtDistributionSetTypeClientResource == null) {
mgmtDistributionSetTypeClientResource = feignBuilder.target(MgmtDistributionSetTypeClientResource.class,
this.baseUrl + MgmtDistributionSetTypeClientResource.PATH);
}
return mgmtDistributionSetTypeClientResource;
}
public MgmtRolloutClientResource getMgmtRolloutClientResource() {
if (mgmtRolloutClientResource == null) {
mgmtRolloutClientResource = feignBuilder.target(MgmtRolloutClientResource.class,
this.baseUrl + MgmtRolloutClientResource.PATH);
}
return mgmtRolloutClientResource;
}
public MgmtSoftwareModuleClientResource getMgmtSoftwareModuleClientResource() {
if (mgmtSoftwareModuleClientResource == null) {
mgmtSoftwareModuleClientResource = feignBuilder.target(MgmtSoftwareModuleClientResource.class,
this.baseUrl + MgmtSoftwareModuleClientResource.PATH);
}
return mgmtSoftwareModuleClientResource;
}
public MgmtSoftwareModuleTypeClientResource getMgmtSoftwareModuleTypeClientResource() {
if (mgmtSoftwareModuleTypeClientResource == null) {
mgmtSoftwareModuleTypeClientResource = feignBuilder.target(MgmtSoftwareModuleTypeClientResource.class,
this.baseUrl + MgmtSoftwareModuleTypeClientResource.PATH);
}
return mgmtSoftwareModuleTypeClientResource;
}
public MgmtTargetClientResource getMgmtTargetClientResource() {
if (mgmtTargetClientResource == null) {
mgmtTargetClientResource = feignBuilder.target(MgmtTargetClientResource.class,
this.baseUrl + MgmtTargetClientResource.PATH);
}
return mgmtTargetClientResource;
}
public MgmtTargetTagClientResource getMgmtTargetTagClientResource() {
if (mgmtTargetTagClientResource == null) {
mgmtTargetTagClientResource = feignBuilder.target(MgmtTargetTagClientResource.class,
this.baseUrl + MgmtTargetTagClientResource.PATH);
}
return mgmtTargetTagClientResource;
}
public MgmtDownloadClientResource getMgmtDownloadClientResource() {
if (mgmtDownloadClientResource == null) {
mgmtDownloadClientResource = feignBuilder.target(MgmtDownloadClientResource.class,
this.baseUrl + MgmtDownloadClientResource.PATH);
}
return mgmtDownloadClientResource;
}
public MgmtDownloadArtifactClientResource getMgmtDownloadArtifactClientResource() {
if (mgmtDownloadArtifactClientResource == null) {
mgmtDownloadArtifactClientResource = feignBuilder.target(MgmtDownloadArtifactClientResource.class,
this.baseUrl + MgmtDownloadArtifactClientResource.PATH);
}
return mgmtDownloadArtifactClientResource;
}
}

View File

@@ -8,8 +8,17 @@
*/
package org.eclipse.hawkbit.mgmt.client.resource;
import org.eclipse.hawkbit.mgmt.json.model.artifact.MgmtArtifact;
import org.eclipse.hawkbit.mgmt.rest.api.MgmtSoftwareModuleRestApi;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import feign.Param;
/**
* Client binding for the SoftwareModule resource of the management API.
@@ -18,4 +27,11 @@ import org.springframework.cloud.netflix.feign.FeignClient;
public interface MgmtSoftwareModuleClientResource extends MgmtSoftwareModuleRestApi {
static String PATH = "rest/v1/softwaremodules";
@RequestMapping(method = RequestMethod.POST, value = "/{softwareModuleId}/artifacts")
ResponseEntity<MgmtArtifact> uploadArtifact(@PathVariable("softwareModuleId") final Long softwareModuleId,
@Param("file") final MultipartFile file,
@RequestParam(value = "filename", required = false) final String optionalFileName,
@RequestParam(value = "md5sum", required = false) final String md5Sum,
@RequestParam(value = "sha1sum", required = false) final String sha1Sum);
}

View File

@@ -0,0 +1,21 @@
/**
* 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.mgmt.client.resource;
import org.eclipse.hawkbit.system.rest.api.SystemRestApi;
import org.springframework.cloud.netflix.feign.FeignClient;
/**
* Client binding for the {@link SystemRestApi}.
*
*/
@FeignClient(url = "${hawkbit.url:localhost:8080}/" + SystemClientResource.PATH)
public interface SystemClientResource extends SystemRestApi {
static String PATH = "rest/v1/system";
}

View File

@@ -0,0 +1,22 @@
/**
* 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.mgmt.client.resource;
import org.eclipse.hawkbit.system.rest.api.SystemManagementRestApi;
import org.springframework.cloud.netflix.feign.FeignClient;
/**
* Client binding for the {@link SystemManagementRestApi}.
*
*/
@FeignClient(url = "${hawkbit.url:localhost:8080}/" + SystemManagementClientResource.PATH)
public interface SystemManagementClientResource extends SystemManagementRestApi {
static String PATH = "system/admin";
}