Repository: cxf Updated Branches: refs/heads/3.1.x-fixes d3e47e330 -> 72547f021
[CXF-5439,CXF-6779] Adding a CXF Provider annotation, marking Swagger2Feature, updating Spring Boot demo Project: http://git-wip-us.apache.org/repos/asf/cxf/repo Commit: http://git-wip-us.apache.org/repos/asf/cxf/commit/72547f02 Tree: http://git-wip-us.apache.org/repos/asf/cxf/tree/72547f02 Diff: http://git-wip-us.apache.org/repos/asf/cxf/diff/72547f02 Branch: refs/heads/3.1.x-fixes Commit: 72547f021ffb15dbb0642515e830272ff48bacd5 Parents: d3e47e3 Author: Sergey Beryozkin <[email protected]> Authored: Tue Feb 16 17:01:48 2016 +0000 Committer: Sergey Beryozkin <[email protected]> Committed: Tue Feb 16 17:18:24 2016 +0000 ---------------------------------------------------------------------- .../org/apache/cxf/annotations/Provider.java | 34 +++++++++++ .../samples/jax_rs/jaxrs_spring_boot/pom.xml | 19 +++++-- .../java/sample/rs/service/HelloService.java | 4 +- .../rs/service/SampleScanRestApplication.java | 13 +++-- .../AbstractSpringComponentScanServer.java | 60 ++++++++++++++++++-- .../AbstractSpringConfigurationFactory.java | 2 +- .../cxf/jaxrs/swagger/Swagger2Feature.java | 3 + 7 files changed, 119 insertions(+), 16 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cxf/blob/72547f02/core/src/main/java/org/apache/cxf/annotations/Provider.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/cxf/annotations/Provider.java b/core/src/main/java/org/apache/cxf/annotations/Provider.java new file mode 100644 index 0000000..4bf751e --- /dev/null +++ b/core/src/main/java/org/apache/cxf/annotations/Provider.java @@ -0,0 +1,34 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.cxf.annotations; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Target(ElementType.TYPE) +@Retention(RetentionPolicy.RUNTIME) +public @interface Provider { + enum Type { InInterceptor, OutInterceptor, InFaultInterceptor, OutFaultInterceptor, Feature } + enum Scope { Server, Client, All } + + Type value(); + Scope scope() default Scope.All; +} http://git-wip-us.apache.org/repos/asf/cxf/blob/72547f02/distribution/src/main/release/samples/jax_rs/jaxrs_spring_boot/pom.xml ---------------------------------------------------------------------- diff --git a/distribution/src/main/release/samples/jax_rs/jaxrs_spring_boot/pom.xml b/distribution/src/main/release/samples/jax_rs/jaxrs_spring_boot/pom.xml index 00a4aa9..4bb4896 100644 --- a/distribution/src/main/release/samples/jax_rs/jaxrs_spring_boot/pom.xml +++ b/distribution/src/main/release/samples/jax_rs/jaxrs_spring_boot/pom.xml @@ -46,18 +46,27 @@ <artifactId>cxf-rt-transports-http</artifactId> <version>${cxf.version}</version> </dependency> - <dependency> + <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-rs-service-description</artifactId> <version>${cxf.version}</version> </dependency> - + <dependency> + <groupId>io.swagger</groupId> + <artifactId>swagger-jaxrs</artifactId> + <version>1.5.4</version> + <exclusions> + <exclusion> + <groupId>javax.ws.rs</groupId> + <artifactId>jsr311-api</artifactId> + </exclusion> + </exclusions> + </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>1.2.3.RELEASE</version> </dependency> - </dependencies> <build> <plugins> @@ -65,9 +74,11 @@ <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> - <mainClass>sample.rs.service.SampleRestApplication</mainClass> <!-- + <mainClass>sample.rs.service.SampleRestApplication</mainClass> + --> <mainClass>sample.rs.service.SampleScanRestApplication</mainClass> + <!-- <mainClass>sample.rs.service.SampleScanRestApplication2</mainClass> --> </configuration> http://git-wip-us.apache.org/repos/asf/cxf/blob/72547f02/distribution/src/main/release/samples/jax_rs/jaxrs_spring_boot/src/main/java/sample/rs/service/HelloService.java ---------------------------------------------------------------------- diff --git a/distribution/src/main/release/samples/jax_rs/jaxrs_spring_boot/src/main/java/sample/rs/service/HelloService.java b/distribution/src/main/release/samples/jax_rs/jaxrs_spring_boot/src/main/java/sample/rs/service/HelloService.java index 6bf34ed..beef6f5 100644 --- a/distribution/src/main/release/samples/jax_rs/jaxrs_spring_boot/src/main/java/sample/rs/service/HelloService.java +++ b/distribution/src/main/release/samples/jax_rs/jaxrs_spring_boot/src/main/java/sample/rs/service/HelloService.java @@ -22,8 +22,10 @@ import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; +import org.springframework.stereotype.Service; @Path("/sayHello") +@Service public class HelloService { @GET @@ -33,4 +35,4 @@ public class HelloService { return "Hello " + a + ", Welcome to CXF RS Spring Boot World!!!"; } -} \ No newline at end of file +} http://git-wip-us.apache.org/repos/asf/cxf/blob/72547f02/distribution/src/main/release/samples/jax_rs/jaxrs_spring_boot/src/main/java/sample/rs/service/SampleScanRestApplication.java ---------------------------------------------------------------------- diff --git a/distribution/src/main/release/samples/jax_rs/jaxrs_spring_boot/src/main/java/sample/rs/service/SampleScanRestApplication.java b/distribution/src/main/release/samples/jax_rs/jaxrs_spring_boot/src/main/java/sample/rs/service/SampleScanRestApplication.java index c7643b2..5be53a8 100644 --- a/distribution/src/main/release/samples/jax_rs/jaxrs_spring_boot/src/main/java/sample/rs/service/SampleScanRestApplication.java +++ b/distribution/src/main/release/samples/jax_rs/jaxrs_spring_boot/src/main/java/sample/rs/service/SampleScanRestApplication.java @@ -18,6 +18,7 @@ */ package sample.rs.service; import org.apache.cxf.jaxrs.spring.SpringComponentScanServer; +import org.apache.cxf.jaxrs.swagger.Swagger2Feature; import org.apache.cxf.transport.servlet.CXFServlet; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @@ -37,11 +38,13 @@ public class SampleScanRestApplication { public ServletRegistrationBean servletRegistrationBean(ApplicationContext context) { return new ServletRegistrationBean(new CXFServlet(), "/services/helloservice/*"); } - - @Bean - public HelloService helloService() { - return new HelloService(); + public Swagger2Feature swaggerFeature(ApplicationContext context) { + // Or create a simple Swagger2Feature @Component-annotated extension + // and drop this method if a default feature setup is OK + Swagger2Feature feature = new Swagger2Feature(); + feature.setRunAsFilter(true); + return feature; } - + } http://git-wip-us.apache.org/repos/asf/cxf/blob/72547f02/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/spring/AbstractSpringComponentScanServer.java ---------------------------------------------------------------------- diff --git a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/spring/AbstractSpringComponentScanServer.java b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/spring/AbstractSpringComponentScanServer.java index 31284aa..927c5e2 100644 --- a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/spring/AbstractSpringComponentScanServer.java +++ b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/spring/AbstractSpringComponentScanServer.java @@ -25,18 +25,28 @@ import java.util.List; import javax.ws.rs.Path; import javax.ws.rs.ext.Provider; +import org.apache.cxf.annotations.Provider.Scope; +import org.apache.cxf.feature.Feature; +import org.apache.cxf.interceptor.Interceptor; import org.apache.cxf.jaxrs.JAXRSServerFactoryBean; import org.apache.cxf.jaxrs.lifecycle.ResourceProvider; +import org.apache.cxf.message.Message; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.FilterType; @ComponentScan( - includeFilters = @ComponentScan.Filter(type = FilterType.ANNOTATION, value = {Path.class, Provider.class }) + includeFilters = @ComponentScan.Filter(type = FilterType.ANNOTATION, + value = {Path.class, + Provider.class, + org.apache.cxf.annotations.Provider.class}) ) public abstract class AbstractSpringComponentScanServer extends AbstractSpringConfigurationFactory { private List<ResourceProvider> resourceProviders = new LinkedList<ResourceProvider>(); private List<Object> jaxrsProviders = new LinkedList<Object>(); + private List<Feature> cxfFeatures = new LinkedList<Feature>(); + private List<Interceptor<? extends Message>> cxfInInterceptors = new LinkedList<Interceptor<?>>(); + private List<Interceptor<? extends Message>> cxfOutInterceptors = new LinkedList<Interceptor<?>>(); private Class<? extends Annotation> serviceAnnotation; protected AbstractSpringComponentScanServer() { @@ -46,28 +56,58 @@ public abstract class AbstractSpringComponentScanServer extends AbstractSpringCo } protected void setJaxrsResources(JAXRSServerFactoryBean factory) { boolean checkJaxrsRoots = checkJaxrsRoots(); - boolean checkJaxrsProviders = checkJaxrsProviders(); + boolean checkJaxrsProviders = checkJaxrsProviders(); + boolean checkCxfProviders = checkCxfProviders(); for (String beanName : applicationContext.getBeanDefinitionNames()) { if (checkJaxrsRoots && isAnnotationAvailable(beanName, Path.class) - && (serviceAnnotation == null || isAnnotationAvailable(beanName, serviceAnnotation))) { + && matchesServiceAnnotation(beanName)) { SpringResourceFactory resourceFactory = new SpringResourceFactory(beanName); resourceFactory.setApplicationContext(applicationContext); resourceProviders.add(resourceFactory); } else if (checkJaxrsProviders && isAnnotationAvailable(beanName, Provider.class) - && (serviceAnnotation == null || isAnnotationAvailable(beanName, serviceAnnotation))) { + && matchesServiceAnnotation(beanName)) { jaxrsProviders.add(applicationContext.getBean(beanName)); + } else if (checkCxfProviders && isAnnotationAvailable(beanName, + org.apache.cxf.annotations.Provider.class) && matchesServiceAnnotation(beanName)) { + addCxfProvider(applicationContext.getBean(beanName)); } } factory.setResourceProviders(getResourceProviders()); factory.setProviders(getJaxrsProviders()); + factory.setFeatures(getFeatures()); + factory.setInInterceptors(getInInterceptors()); + factory.setOutInterceptors(getOutInterceptors()); + } + protected void addCxfProvider(Object bean) { + org.apache.cxf.annotations.Provider ann = + bean.getClass().getAnnotation(org.apache.cxf.annotations.Provider.class); + if (ann.scope() == Scope.Client) { + return; + } + if (ann.value() == org.apache.cxf.annotations.Provider.Type.Feature) { + cxfFeatures.add((Feature)bean); + } else if (ann.value() == org.apache.cxf.annotations.Provider.Type.InInterceptor) { + cxfInInterceptors.add((Interceptor<?>)bean); + } else if (ann.value() == org.apache.cxf.annotations.Provider.Type.OutInterceptor) { + cxfOutInterceptors.add((Interceptor<?>)bean); + } + + } + protected boolean matchesServiceAnnotation(String beanName) { + return serviceAnnotation == null || isAnnotationAvailable(beanName, serviceAnnotation); + } protected <A extends Annotation> boolean isAnnotationAvailable(String beanName, Class<A> annClass) { return applicationContext.findAnnotationOnBean(beanName, annClass) != null; } + protected boolean checkCxfProviders() { + return true; + } + protected boolean checkJaxrsProviders() { return true; } @@ -83,6 +123,16 @@ public abstract class AbstractSpringComponentScanServer extends AbstractSpringCo protected List<Object> getJaxrsProviders() { return jaxrsProviders; } - + @Override + public List<Feature> getFeatures() { + return cxfFeatures; + } + @Override + public List<Interceptor<? extends Message>> getInInterceptors() { + return cxfInInterceptors; + } + public List<Interceptor<? extends Message>> getOutInterceptors() { + return cxfOutInterceptors; + } } http://git-wip-us.apache.org/repos/asf/cxf/blob/72547f02/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/spring/AbstractSpringConfigurationFactory.java ---------------------------------------------------------------------- diff --git a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/spring/AbstractSpringConfigurationFactory.java b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/spring/AbstractSpringConfigurationFactory.java index 1ee09fa..bf37436 100644 --- a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/spring/AbstractSpringConfigurationFactory.java +++ b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/spring/AbstractSpringConfigurationFactory.java @@ -65,7 +65,7 @@ public abstract class AbstractSpringConfigurationFactory return Collections.emptyList(); } - protected List<Feature> getFeatures() { + public List<Feature> getFeatures() { return Collections.emptyList(); } http://git-wip-us.apache.org/repos/asf/cxf/blob/72547f02/rt/rs/description/src/main/java/org/apache/cxf/jaxrs/swagger/Swagger2Feature.java ---------------------------------------------------------------------- diff --git a/rt/rs/description/src/main/java/org/apache/cxf/jaxrs/swagger/Swagger2Feature.java b/rt/rs/description/src/main/java/org/apache/cxf/jaxrs/swagger/Swagger2Feature.java index da84457..5072104 100644 --- a/rt/rs/description/src/main/java/org/apache/cxf/jaxrs/swagger/Swagger2Feature.java +++ b/rt/rs/description/src/main/java/org/apache/cxf/jaxrs/swagger/Swagger2Feature.java @@ -35,6 +35,8 @@ import javax.ws.rs.core.Context; import javax.ws.rs.core.Response; import javax.ws.rs.core.UriInfo; +import org.apache.cxf.annotations.Provider; +import org.apache.cxf.annotations.Provider.Type; import org.apache.cxf.common.util.StringUtils; import org.apache.cxf.endpoint.Server; import org.apache.cxf.jaxrs.JAXRSServiceFactoryBean; @@ -50,6 +52,7 @@ import io.swagger.jaxrs.config.DefaultReaderConfig; import io.swagger.jaxrs.config.ReaderConfig; import io.swagger.jaxrs.listing.ApiListingResource; +@Provider(Type.Feature) public class Swagger2Feature extends AbstractSwaggerFeature { private String host;
