Return-Path: X-Original-To: apmail-cxf-commits-archive@www.apache.org Delivered-To: apmail-cxf-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id CDC3E181DF for ; Tue, 16 Feb 2016 17:02:04 +0000 (UTC) Received: (qmail 50998 invoked by uid 500); 16 Feb 2016 17:02:04 -0000 Delivered-To: apmail-cxf-commits-archive@cxf.apache.org Received: (qmail 50934 invoked by uid 500); 16 Feb 2016 17:02:04 -0000 Mailing-List: contact commits-help@cxf.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@cxf.apache.org Delivered-To: mailing list commits@cxf.apache.org Received: (qmail 50925 invoked by uid 99); 16 Feb 2016 17:02:04 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 16 Feb 2016 17:02:04 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 95961E0577; Tue, 16 Feb 2016 17:02:04 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: sergeyb@apache.org To: commits@cxf.apache.org Message-Id: <551d3aed1a1b4276add4ec2f257cb380@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: cxf git commit: [CXF-5439, CXF-6779] Adding a CXF Provider annotation, marking Swagger2Feature, updating Spring Boot demo Date: Tue, 16 Feb 2016 17:02:04 +0000 (UTC) Repository: cxf Updated Branches: refs/heads/master d68286f71 -> 4946ee58a [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/4946ee58 Tree: http://git-wip-us.apache.org/repos/asf/cxf/tree/4946ee58 Diff: http://git-wip-us.apache.org/repos/asf/cxf/diff/4946ee58 Branch: refs/heads/master Commit: 4946ee58ae2e2a3ff81904ff08ed2032048cdbc7 Parents: d68286f Author: Sergey Beryozkin Authored: Tue Feb 16 17:01:48 2016 +0000 Committer: Sergey Beryozkin Committed: Tue Feb 16 17:01:48 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/4946ee58/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/4946ee58/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 e9cbd7c..acfbcbc 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 @@ cxf-rt-transports-http ${cxf.version} - + org.apache.cxf cxf-rt-rs-service-description ${cxf.version} - + + io.swagger + swagger-jaxrs + 1.5.4 + + + javax.ws.rs + jsr311-api + + + org.springframework.boot spring-boot-starter-web 1.2.3.RELEASE - @@ -65,9 +74,11 @@ org.springframework.boot spring-boot-maven-plugin - sample.rs.service.SampleRestApplication sample.rs.service.SampleScanRestApplication + http://git-wip-us.apache.org/repos/asf/cxf/blob/4946ee58/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/4946ee58/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/4946ee58/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 resourceProviders = new LinkedList(); private List jaxrsProviders = new LinkedList(); + private List cxfFeatures = new LinkedList(); + private List> cxfInInterceptors = new LinkedList>(); + private List> cxfOutInterceptors = new LinkedList>(); private Class 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 boolean isAnnotationAvailable(String beanName, Class 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 getJaxrsProviders() { return jaxrsProviders; } - + @Override + public List getFeatures() { + return cxfFeatures; + } + @Override + public List> getInInterceptors() { + return cxfInInterceptors; + } + public List> getOutInterceptors() { + return cxfOutInterceptors; + } } http://git-wip-us.apache.org/repos/asf/cxf/blob/4946ee58/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 getFeatures() { + public List getFeatures() { return Collections.emptyList(); } http://git-wip-us.apache.org/repos/asf/cxf/blob/4946ee58/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;