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 2E0BD181BA for ; Fri, 3 Jul 2015 23:05:33 +0000 (UTC) Received: (qmail 33074 invoked by uid 500); 3 Jul 2015 23:05:33 -0000 Delivered-To: apmail-cxf-commits-archive@cxf.apache.org Received: (qmail 33005 invoked by uid 500); 3 Jul 2015 23:05:33 -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 32996 invoked by uid 99); 3 Jul 2015 23:05:33 -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; Fri, 03 Jul 2015 23:05:33 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 9EDE3E35C8; Fri, 3 Jul 2015 23:05:32 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: ay@apache.org To: commits@cxf.apache.org Message-Id: X-Mailer: ASF-Git Admin Mailer Subject: cxf git commit: [CXF-6476] Introduce Swagger 2.0 API to the swagger feature Date: Fri, 3 Jul 2015 23:05:32 +0000 (UTC) Repository: cxf Updated Branches: refs/heads/master 0f7b744eb -> 7988c4a97 [CXF-6476] Introduce Swagger 2.0 API to the swagger feature Project: http://git-wip-us.apache.org/repos/asf/cxf/repo Commit: http://git-wip-us.apache.org/repos/asf/cxf/commit/7988c4a9 Tree: http://git-wip-us.apache.org/repos/asf/cxf/tree/7988c4a9 Diff: http://git-wip-us.apache.org/repos/asf/cxf/diff/7988c4a9 Branch: refs/heads/master Commit: 7988c4a974118cc5c2a8bd956fe396a7d5e8bb5c Parents: 0f7b744 Author: Akitoshi Yoshida Authored: Fri Jul 3 12:15:10 2015 +0200 Committer: Akitoshi Yoshida Committed: Sat Jul 4 01:03:53 2015 +0200 ---------------------------------------------------------------------- .../jax_rs/description_swagger2/README.txt | 23 +++ .../samples/jax_rs/description_swagger2/pom.xml | 163 +++++++++++++++++++ .../java/demo/jaxrs/swagger/server/Item.java | 49 ++++++ .../java/demo/jaxrs/swagger/server/Sample.java | 130 +++++++++++++++ .../java/demo/jaxrs/swagger/server/Server.java | 74 +++++++++ .../META-INF/cxf/org.apache.cxf.Logger | 1 + .../webjars/swagger-ui/2.1.0/index.html | 84 ++++++++++ .../src/main/resources/logback.xml | 16 ++ parent/pom.xml | 24 ++- rt/rs/description/pom.xml | 5 + .../jaxrs/swagger/AbstractSwaggerFeature.java | 128 +++++++++++++++ .../cxf/jaxrs/swagger/Swagger2Feature.java | 107 ++++++++++++ .../cxf/jaxrs/swagger/SwaggerFeature.java | 103 +----------- 13 files changed, 799 insertions(+), 108 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cxf/blob/7988c4a9/distribution/src/main/release/samples/jax_rs/description_swagger2/README.txt ---------------------------------------------------------------------- diff --git a/distribution/src/main/release/samples/jax_rs/description_swagger2/README.txt b/distribution/src/main/release/samples/jax_rs/description_swagger2/README.txt new file mode 100644 index 0000000..0ceee5b --- /dev/null +++ b/distribution/src/main/release/samples/jax_rs/description_swagger2/README.txt @@ -0,0 +1,23 @@ +JAX-RS Search Demo +================= + +The demo shows a basic usage of Swagger API documentation with REST based Web Services using +JAX-RS 2.0 (JSR-339). Swagger UI is available at: http://localhost:9000/ + +Building and running the demo using Maven +--------------------------------------- + +From the base directory of this sample (i.e., where this README file is +located), the Maven pom.xml file can be used to build and run the demo. + + +Using either UNIX or Windows: + + mvn install + mvn -Pserver (from one command line window) + + +To remove the target dir, run mvn clean". + + + http://git-wip-us.apache.org/repos/asf/cxf/blob/7988c4a9/distribution/src/main/release/samples/jax_rs/description_swagger2/pom.xml ---------------------------------------------------------------------- diff --git a/distribution/src/main/release/samples/jax_rs/description_swagger2/pom.xml b/distribution/src/main/release/samples/jax_rs/description_swagger2/pom.xml new file mode 100644 index 0000000..313383e --- /dev/null +++ b/distribution/src/main/release/samples/jax_rs/description_swagger2/pom.xml @@ -0,0 +1,163 @@ + + + + 4.0.0 + jax_rs_description_swagger2 + JAX-RS Swagger Demo + JAX-RS Basic Demo + + org.apache.cxf.samples + cxf-samples + 3.1.2-SNAPSHOT + ../.. + + + + ${project.version} + 3.1 + + + + + server + + test + + + org.codehaus.mojo + exec-maven-plugin + + + test + + java + + + demo.jaxrs.swagger.server.Server + + + + + + + + + + + + + org.apache.maven.plugins + maven-dependency-plugin + 2.9 + + + generate-resources + + unpack + + + + + org.webjars + swagger-ui + 2.1.0 + true + ${project.build.directory}/classes + **/index.html + + + + + + + + + + + + io.swagger + swagger-jaxrs + + + javax.ws.rs + jsr311-api + + + + + org.webjars + swagger-ui + 2.1.0 + provided + + + javassist + javassist + 3.12.1.GA + + + com.fasterxml.jackson.jaxrs + jackson-jaxrs-json-provider + 2.4.1 + + + com.fasterxml.jackson.core + jackson-core + 2.4.1 + + + org.apache.cxf + cxf-rt-transports-http + 3.1.2-SNAPSHOT + + + + org.apache.cxf + cxf-rt-transports-http-jetty + 3.1.2-SNAPSHOT + + + org.apache.cxf + cxf-rt-frontend-jaxrs + 3.1.2-SNAPSHOT + + + org.apache.cxf + cxf-rt-rs-service-description + 3.1.2-SNAPSHOT + + + commons-lang + commons-lang + + + commons-httpclient + commons-httpclient + + + javax.ws.rs + javax.ws.rs-api + + + org.eclipse.jetty + jetty-webapp + + + http://git-wip-us.apache.org/repos/asf/cxf/blob/7988c4a9/distribution/src/main/release/samples/jax_rs/description_swagger2/src/main/java/demo/jaxrs/swagger/server/Item.java ---------------------------------------------------------------------- diff --git a/distribution/src/main/release/samples/jax_rs/description_swagger2/src/main/java/demo/jaxrs/swagger/server/Item.java b/distribution/src/main/release/samples/jax_rs/description_swagger2/src/main/java/demo/jaxrs/swagger/server/Item.java new file mode 100644 index 0000000..1b5be9b --- /dev/null +++ b/distribution/src/main/release/samples/jax_rs/description_swagger2/src/main/java/demo/jaxrs/swagger/server/Item.java @@ -0,0 +1,49 @@ +/** + * 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 demo.jaxrs.swagger.server; + +public class Item { + private String name; + private String value; + + public Item() { + } + + public Item(final String name, final String value) { + this.name = name; + this.value = value; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getValue() { + return value; + } + + public void setValue(String value) { + this.value = value; + } +} http://git-wip-us.apache.org/repos/asf/cxf/blob/7988c4a9/distribution/src/main/release/samples/jax_rs/description_swagger2/src/main/java/demo/jaxrs/swagger/server/Sample.java ---------------------------------------------------------------------- diff --git a/distribution/src/main/release/samples/jax_rs/description_swagger2/src/main/java/demo/jaxrs/swagger/server/Sample.java b/distribution/src/main/release/samples/jax_rs/description_swagger2/src/main/java/demo/jaxrs/swagger/server/Sample.java new file mode 100644 index 0000000..56d3213 --- /dev/null +++ b/distribution/src/main/release/samples/jax_rs/description_swagger2/src/main/java/demo/jaxrs/swagger/server/Sample.java @@ -0,0 +1,130 @@ +/** + * 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 demo.jaxrs.swagger.server; + +import java.util.Arrays; + +import javax.ws.rs.Consumes; +import javax.ws.rs.DELETE; +import javax.ws.rs.DefaultValue; +import javax.ws.rs.FormParam; +import javax.ws.rs.GET; +import javax.ws.rs.HeaderParam; +import javax.ws.rs.POST; +import javax.ws.rs.PUT; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +import javax.ws.rs.Produces; +import javax.ws.rs.QueryParam; +import javax.ws.rs.core.Context; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; +import javax.ws.rs.core.UriInfo; + +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiImplicitParam; +import io.swagger.annotations.ApiImplicitParams; +import io.swagger.annotations.ApiOperation; +import io.swagger.annotations.ApiParam; + +@Path("/sample") +@Api(value = "/sample", description = "Sample JAX-RS service with Swagger documentation") +public class Sample { + @Produces({ MediaType.APPLICATION_JSON }) + @GET + @ApiOperation( + value = "Get operation with Response and @Default value", + notes = "Get operation with Response and @Default value", + response = Item.class, + responseContainer = "List" + ) + public Response getItems( + @ApiParam(value = "Page to fetch", required = true) @QueryParam("page") @DefaultValue("1") int page) { + return Response.ok( + Arrays.asList( + new Item("Item 1", "Value 1"), + new Item("Item 2", "Value 2") + ) + ).build(); + } + + @Produces({ MediaType.APPLICATION_JSON }) + @Path("/{name}") + @GET + @ApiOperation( + value = "Get operation with type and headers", + notes = "Get operation with type and headers", + response = Item.class + ) + public Item getItem( + @ApiParam(value = "language", required = true) @HeaderParam("Accept-Language") final String language, + @ApiParam(value = "name", required = true) @PathParam("name") String name) { + return new Item("name", "Value in " + language); + } + + @Consumes({ MediaType.APPLICATION_JSON }) + @POST + @ApiOperation( + value = "Post operation with entity in a body", + notes = "Post operation with entity in a body", + response = Item.class + ) + public Response createItem( + @Context final UriInfo uriInfo, + @ApiParam(value = "item", required = true) final Item item) { + + return Response + .created(uriInfo.getBaseUriBuilder().path(item.getName()).build()) + .entity(item).build(); + } + + @Produces({ MediaType.APPLICATION_JSON }) + @Path("/{name}") + @PUT + @ApiOperation( + value = "Put operation with form parameter", + notes = "Put operation with form parameter", + response = Item.class + ) + public Item updateItem( + @ApiParam(value = "name", required = true) @PathParam("name") String name, + @ApiParam(value = "value", required = true) @FormParam("value") String value) { + return new Item(name, value); + } + + @Path("/{name}") + @DELETE + @ApiOperation( + value = "Delete operation with implicit header", + notes = "Delete operation with implicit header" + ) + @ApiImplicitParams( + @ApiImplicitParam( + name = "Accept-Language", + value = "language", + required = true, + dataType = "String", + paramType = "header" + ) + ) + public Response delete(@ApiParam(value = "name", required = true) @PathParam("name") String name) { + return Response.ok().build(); + } +} http://git-wip-us.apache.org/repos/asf/cxf/blob/7988c4a9/distribution/src/main/release/samples/jax_rs/description_swagger2/src/main/java/demo/jaxrs/swagger/server/Server.java ---------------------------------------------------------------------- diff --git a/distribution/src/main/release/samples/jax_rs/description_swagger2/src/main/java/demo/jaxrs/swagger/server/Server.java b/distribution/src/main/release/samples/jax_rs/description_swagger2/src/main/java/demo/jaxrs/swagger/server/Server.java new file mode 100644 index 0000000..e06d496 --- /dev/null +++ b/distribution/src/main/release/samples/jax_rs/description_swagger2/src/main/java/demo/jaxrs/swagger/server/Server.java @@ -0,0 +1,74 @@ +/** + * 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 demo.jaxrs.swagger.server; + +import com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider; + +import org.apache.commons.lang.StringUtils; +import org.apache.cxf.jaxrs.provider.MultipartProvider; +import org.apache.cxf.jaxrs.servlet.CXFNonSpringJaxrsServlet; +import org.apache.cxf.jaxrs.swagger.Swagger2Feature; +import org.eclipse.jetty.servlet.DefaultServlet; +import org.eclipse.jetty.servlet.ServletContextHandler; +import org.eclipse.jetty.servlet.ServletHolder; + +public class Server { + + protected Server() throws Exception { + org.eclipse.jetty.server.Server server = new org.eclipse.jetty.server.Server(9000); + + // Configuring all static web resource + final ServletHolder staticHolder = new ServletHolder(new DefaultServlet()); + // Register and map the dispatcher servlet + final ServletHolder servletHolder = new ServletHolder(new CXFNonSpringJaxrsServlet()); + final ServletContextHandler context = new ServletContextHandler(); + context.setContextPath("/"); + context.addServlet(staticHolder, "/static/*"); + context.addServlet(servletHolder, "/*"); + context.setResourceBase( + getClass().getResource("/META-INF/resources/webjars/swagger-ui/2.1.0").toURI().toString()); + + servletHolder.setInitParameter("redirects-list", + "/ /index.html /.*[.]js /css/.* /images/.* lib/.* .*ico"); + servletHolder.setInitParameter("redirect-servlet-name", staticHolder.getName()); + servletHolder.setInitParameter("redirect-attributes", "javax.servlet.include.request_uri"); + servletHolder.setInitParameter("jaxrs.serviceClasses", Sample.class.getName()); + servletHolder.setInitParameter("jaxrs.features", Swagger2Feature.class.getName()); + servletHolder.setInitParameter("jaxrs.providers", StringUtils.join( + new String[] { + MultipartProvider.class.getName(), + JacksonJsonProvider.class.getName() + }, ",") + ); + + server.setHandler(context); + server.start(); + server.join(); + } + + public static void main(String args[]) throws Exception { + new Server(); + System.out.println("Server ready..."); + + Thread.sleep(5 * 6000 * 1000); + System.out.println("Server exiting"); + System.exit(0); + } +} http://git-wip-us.apache.org/repos/asf/cxf/blob/7988c4a9/distribution/src/main/release/samples/jax_rs/description_swagger2/src/main/resources/META-INF/cxf/org.apache.cxf.Logger ---------------------------------------------------------------------- diff --git a/distribution/src/main/release/samples/jax_rs/description_swagger2/src/main/resources/META-INF/cxf/org.apache.cxf.Logger b/distribution/src/main/release/samples/jax_rs/description_swagger2/src/main/resources/META-INF/cxf/org.apache.cxf.Logger new file mode 100644 index 0000000..27dd788 --- /dev/null +++ b/distribution/src/main/release/samples/jax_rs/description_swagger2/src/main/resources/META-INF/cxf/org.apache.cxf.Logger @@ -0,0 +1 @@ +org.apache.cxf.common.logging.Slf4jLogger \ No newline at end of file http://git-wip-us.apache.org/repos/asf/cxf/blob/7988c4a9/distribution/src/main/release/samples/jax_rs/description_swagger2/src/main/resources/META-INF/resources/webjars/swagger-ui/2.1.0/index.html ---------------------------------------------------------------------- diff --git a/distribution/src/main/release/samples/jax_rs/description_swagger2/src/main/resources/META-INF/resources/webjars/swagger-ui/2.1.0/index.html b/distribution/src/main/release/samples/jax_rs/description_swagger2/src/main/resources/META-INF/resources/webjars/swagger-ui/2.1.0/index.html new file mode 100644 index 0000000..6095ccc --- /dev/null +++ b/distribution/src/main/release/samples/jax_rs/description_swagger2/src/main/resources/META-INF/resources/webjars/swagger-ui/2.1.0/index.html @@ -0,0 +1,84 @@ + + + + + Swagger UI + JAX-RS demo + + + + + + + + + + + + + + + + + + + + +
 
+
+ + http://git-wip-us.apache.org/repos/asf/cxf/blob/7988c4a9/distribution/src/main/release/samples/jax_rs/description_swagger2/src/main/resources/logback.xml ---------------------------------------------------------------------- diff --git a/distribution/src/main/release/samples/jax_rs/description_swagger2/src/main/resources/logback.xml b/distribution/src/main/release/samples/jax_rs/description_swagger2/src/main/resources/logback.xml new file mode 100644 index 0000000..0a3e031 --- /dev/null +++ b/distribution/src/main/release/samples/jax_rs/description_swagger2/src/main/resources/logback.xml @@ -0,0 +1,16 @@ + + + + + [%level] %d{yyyy-MM-dd HH:mm:ss.SSS} %logger{36} - [%X] %msg%n + + + + + + + + + + + \ No newline at end of file http://git-wip-us.apache.org/repos/asf/cxf/blob/7988c4a9/parent/pom.xml ---------------------------------------------------------------------- diff --git a/parent/pom.xml b/parent/pom.xml index 0af2cf6..9052b21 100644 --- a/parent/pom.xml +++ b/parent/pom.xml @@ -142,6 +142,7 @@ 1.3.1.RELEASE spring-test 1.3.12 + 1.5.0 1.7 4.4.1 3.1.4 @@ -579,7 +580,7 @@ ${cxf.osgi.dynamic.import} - ${cxf.export.service} + ${cxf.export.service} ${cxf.bundle.activator} @@ -1237,10 +1238,10 @@ ${cxf.spring.security.version} - org.apache.olingo - olingo-odata2-core - ${cxf.olingo.version} - + org.apache.olingo + olingo-odata2-core + ${cxf.olingo.version} + org.springframework.security spring-security-core @@ -1771,16 +1772,21 @@ swagger-jaxrs_2.10 ${cxf.swagger.version} - + + io.swagger + swagger-jaxrs + ${cxf.swagger2.version} + + org.apache.tika tika-core ${cxf.tika.version} - - + + org.apache.tika tika-parsers ${cxf.tika.version} - + io.dropwizard.metrics metrics-core http://git-wip-us.apache.org/repos/asf/cxf/blob/7988c4a9/rt/rs/description/pom.xml ---------------------------------------------------------------------- diff --git a/rt/rs/description/pom.xml b/rt/rs/description/pom.xml index c5575ad..2d768d6 100644 --- a/rt/rs/description/pom.xml +++ b/rt/rs/description/pom.xml @@ -87,6 +87,11 @@ swagger-jaxrs_2.10 true + + io.swagger + swagger-jaxrs + true + http://git-wip-us.apache.org/repos/asf/cxf/blob/7988c4a9/rt/rs/description/src/main/java/org/apache/cxf/jaxrs/swagger/AbstractSwaggerFeature.java ---------------------------------------------------------------------- diff --git a/rt/rs/description/src/main/java/org/apache/cxf/jaxrs/swagger/AbstractSwaggerFeature.java b/rt/rs/description/src/main/java/org/apache/cxf/jaxrs/swagger/AbstractSwaggerFeature.java new file mode 100644 index 0000000..5b45829 --- /dev/null +++ b/rt/rs/description/src/main/java/org/apache/cxf/jaxrs/swagger/AbstractSwaggerFeature.java @@ -0,0 +1,128 @@ +/** + * 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.jaxrs.swagger; + +import org.apache.cxf.Bus; +import org.apache.cxf.endpoint.Server; +import org.apache.cxf.feature.AbstractFeature; +import org.apache.cxf.jaxrs.JAXRSServiceFactoryBean; +import org.apache.cxf.jaxrs.model.AbstractResourceInfo; + +abstract class AbstractSwaggerFeature extends AbstractFeature { + protected boolean scan = true; + protected boolean runAsFilter; + private String resourcePackage; + private String version = "1.0.0"; + private String basePath; + private String title = "Sample REST Application"; + private String description = "The Application"; + private String contact = "committer@apache.org"; + private String license = "Apache 2.0 License"; + private String licenseUrl = "http://www.apache.org/licenses/LICENSE-2.0.html"; + + @Override + public void initialize(Server server, Bus bus) { + calculateDefaultResourcePackage(server); + calculateDefaultBasePath(server); + addSwaggerResource(server); + + initializeProvider(server.getEndpoint(), bus); + } + + protected abstract void addSwaggerResource(Server server); + + private void calculateDefaultResourcePackage(Server server) { + JAXRSServiceFactoryBean serviceFactoryBean = + (JAXRSServiceFactoryBean)server.getEndpoint().get(JAXRSServiceFactoryBean.class.getName()); + AbstractResourceInfo resourceInfo = serviceFactoryBean.getClassResourceInfo().get(0); + + if ((resourceInfo != null) + && (getResourcePackage() == null || getResourcePackage().length() == 0)) { + setResourcePackage(resourceInfo.getServiceClass().getPackage().getName()); + } + } + + private void calculateDefaultBasePath(Server server) { + if (getBasePath() == null || getBasePath().length() == 0) { + String address = server.getEndpoint().getEndpointInfo().getAddress(); + setBasePath(address); + } + } + public String getResourcePackage() { + return resourcePackage; + } + public void setResourcePackage(String resourcePackage) { + this.resourcePackage = resourcePackage; + } + public String getVersion() { + return version; + } + public void setVersion(String version) { + this.version = version; + } + public String getBasePath() { + return basePath; + } + public void setBasePath(String basePath) { + this.basePath = basePath; + } + public String getTitle() { + return title; + } + public void setTitle(String title) { + this.title = title; + } + public String getDescription() { + return description; + } + public void setDescription(String description) { + this.description = description; + } + public String getContact() { + return contact; + } + public void setContact(String contact) { + this.contact = contact; + } + public String getLicense() { + return license; + } + public void setLicense(String license) { + this.license = license; + } + public String getLicenseUrl() { + return licenseUrl; + } + public void setLicenseUrl(String licenseUrl) { + this.licenseUrl = licenseUrl; + } + public boolean isScan() { + return scan; + } + public void setScan(boolean scan) { + this.scan = scan; + } + + public boolean isRunAsFilter() { + return runAsFilter; + } + public void setRunAsFilter(boolean runAsFilter) { + this.runAsFilter = runAsFilter; + } +} http://git-wip-us.apache.org/repos/asf/cxf/blob/7988c4a9/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 new file mode 100644 index 0000000..d21c559 --- /dev/null +++ b/rt/rs/description/src/main/java/org/apache/cxf/jaxrs/swagger/Swagger2Feature.java @@ -0,0 +1,107 @@ +/** + * 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.jaxrs.swagger; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +import javax.ws.rs.container.ContainerRequestContext; +import javax.ws.rs.container.ContainerRequestFilter; +import javax.ws.rs.container.PreMatching; +import javax.ws.rs.core.Context; +import javax.ws.rs.core.Response; +import javax.ws.rs.core.UriInfo; + +import org.apache.cxf.endpoint.Server; +import org.apache.cxf.jaxrs.JAXRSServiceFactoryBean; +import org.apache.cxf.jaxrs.ext.MessageContext; +import org.apache.cxf.jaxrs.model.ClassResourceInfo; +import org.apache.cxf.jaxrs.provider.ServerProviderFactory; +import org.apache.cxf.jaxrs.utils.InjectionUtils; + +import io.swagger.jaxrs.config.BeanConfig; +import io.swagger.jaxrs.listing.ApiListingResource; +import io.swagger.jaxrs.listing.SwaggerSerializers; + +public class Swagger2Feature extends AbstractSwaggerFeature { + + @Override + protected void addSwaggerResource(Server server) { + ApiListingResource apiListingResource = new ApiListingResource(); + if (!runAsFilter) { + List serviceBeans = new ArrayList(); + serviceBeans.add(apiListingResource); + JAXRSServiceFactoryBean sfb = + (JAXRSServiceFactoryBean)server.getEndpoint().get(JAXRSServiceFactoryBean.class.getName()); + sfb.setResourceClassesFromBeans(serviceBeans); + for (ClassResourceInfo cri : sfb.getClassResourceInfo()) { + if (ApiListingResource.class == cri.getResourceClass()) { + InjectionUtils.injectContextProxiesAndApplication(cri, apiListingResource, null); + } + } + } + List providers = new ArrayList(); + if (runAsFilter) { + providers.add(new SwaggerContainerRequestFilter(apiListingResource)); + } + providers.add(new SwaggerSerializers()); + ((ServerProviderFactory)server.getEndpoint().get( + ServerProviderFactory.class.getName())).setUserProviders(providers); + + BeanConfig beanConfig = new BeanConfig(); + beanConfig.setResourcePackage(getResourcePackage()); + beanConfig.setVersion(getVersion()); + beanConfig.setBasePath(getBasePath()); + beanConfig.setTitle(getTitle()); + beanConfig.setDescription(getDescription()); + beanConfig.setContact(getContact()); + beanConfig.setLicense(getLicense()); + beanConfig.setLicenseUrl(getLicenseUrl()); + beanConfig.setScan(isScan()); + } + + @PreMatching + private static class SwaggerContainerRequestFilter implements ContainerRequestFilter { + private static final String APIDOCS_LISTING_PATH_JSON = "swagger.json"; + private static final String APIDOCS_LISTING_PATH_YAML = "swagger.yaml"; + + private ApiListingResource apiListingResource; + @Context + private MessageContext mc; + public SwaggerContainerRequestFilter(ApiListingResource apiListingResource) { + this.apiListingResource = apiListingResource; + } + + @Override + public void filter(ContainerRequestContext requestContext) throws IOException { + UriInfo ui = mc.getUriInfo(); + if (ui.getPath().endsWith(APIDOCS_LISTING_PATH_JSON)) { + Response r = + apiListingResource.getListingJson(null, mc.getServletConfig(), mc.getHttpHeaders(), ui); + requestContext.abortWith(r); + } else if (ui.getPath().endsWith(APIDOCS_LISTING_PATH_YAML)) { + Response r = + apiListingResource.getListingYaml(null, mc.getServletConfig(), mc.getHttpHeaders(), ui); + requestContext.abortWith(r); + } + } + + } +} http://git-wip-us.apache.org/repos/asf/cxf/blob/7988c4a9/rt/rs/description/src/main/java/org/apache/cxf/jaxrs/swagger/SwaggerFeature.java ---------------------------------------------------------------------- diff --git a/rt/rs/description/src/main/java/org/apache/cxf/jaxrs/swagger/SwaggerFeature.java b/rt/rs/description/src/main/java/org/apache/cxf/jaxrs/swagger/SwaggerFeature.java index 85ef7a8..b4191e3 100644 --- a/rt/rs/description/src/main/java/org/apache/cxf/jaxrs/swagger/SwaggerFeature.java +++ b/rt/rs/description/src/main/java/org/apache/cxf/jaxrs/swagger/SwaggerFeature.java @@ -36,31 +36,15 @@ import com.wordnik.swagger.jaxrs.listing.ApiDeclarationProvider; import com.wordnik.swagger.jaxrs.listing.ApiListingResourceJSON; import com.wordnik.swagger.jaxrs.listing.ResourceListingProvider; -import org.apache.cxf.Bus; import org.apache.cxf.endpoint.Server; -import org.apache.cxf.feature.AbstractFeature; import org.apache.cxf.jaxrs.JAXRSServiceFactoryBean; import org.apache.cxf.jaxrs.ext.MessageContext; -import org.apache.cxf.jaxrs.model.AbstractResourceInfo; import org.apache.cxf.jaxrs.provider.ServerProviderFactory; -public class SwaggerFeature extends AbstractFeature { - - private String resourcePackage; - private String version = "1.0.0"; - private String basePath; - private String title = "Sample REST Application"; - private String description = "The Application"; - private String contact = "committer@apache.org"; - private String license = "Apache 2.0 License"; - private String licenseUrl = "http://www.apache.org/licenses/LICENSE-2.0.html"; - private boolean scan = true; - private boolean runAsFilter; - +public class SwaggerFeature extends AbstractSwaggerFeature { + @Override - public void initialize(Server server, Bus bus) { - calculateDefaultResourcePackage(server); - calculateDefaultBasePath(server); + protected void addSwaggerResource(Server server) { ApiListingResourceJSON apiListingResource = new ApiListingResourceJSON(); if (!runAsFilter) { List serviceBeans = new ArrayList(); @@ -87,86 +71,7 @@ public class SwaggerFeature extends AbstractFeature { beanConfig.setLicense(getLicense()); beanConfig.setLicenseUrl(getLicenseUrl()); beanConfig.setScan(isScan()); - initializeProvider(server.getEndpoint(), bus); - } - private void calculateDefaultResourcePackage(Server server) { - JAXRSServiceFactoryBean serviceFactoryBean = - (JAXRSServiceFactoryBean)server.getEndpoint().get(JAXRSServiceFactoryBean.class.getName()); - AbstractResourceInfo resourceInfo = serviceFactoryBean.getClassResourceInfo().get(0); - - if ((resourceInfo != null) - && (getResourcePackage() == null || getResourcePackage().length() == 0)) { - setResourcePackage(resourceInfo.getServiceClass().getPackage().getName()); - } - } - - private void calculateDefaultBasePath(Server server) { - if (getBasePath() == null || getBasePath().length() == 0) { - String address = server.getEndpoint().getEndpointInfo().getAddress(); - setBasePath(address); - } - } - public String getResourcePackage() { - return resourcePackage; - } - public void setResourcePackage(String resourcePackage) { - this.resourcePackage = resourcePackage; - } - public String getVersion() { - return version; - } - public void setVersion(String version) { - this.version = version; - } - public String getBasePath() { - return basePath; - } - public void setBasePath(String basePath) { - this.basePath = basePath; - } - public String getTitle() { - return title; - } - public void setTitle(String title) { - this.title = title; - } - public String getDescription() { - return description; - } - public void setDescription(String description) { - this.description = description; - } - public String getContact() { - return contact; - } - public void setContact(String contact) { - this.contact = contact; - } - public String getLicense() { - return license; - } - public void setLicense(String license) { - this.license = license; - } - public String getLicenseUrl() { - return licenseUrl; - } - public void setLicenseUrl(String licenseUrl) { - this.licenseUrl = licenseUrl; - } - public boolean isScan() { - return scan; - } - public void setScan(boolean scan) { - this.scan = scan; - } - - public boolean isRunAsFilter() { - return runAsFilter; - } - public void setRunAsFilter(boolean runAsFilter) { - this.runAsFilter = runAsFilter; - } + } @PreMatching private static class SwaggerContainerRequestFilter implements ContainerRequestFilter {