Repository: cxf Updated Branches: refs/heads/3.0.x-fixes a94b303ed -> 3832db055
CXF-6132: Provide JAX-RS ServletContextInitializer. Initial implementation Project: http://git-wip-us.apache.org/repos/asf/cxf/repo Commit: http://git-wip-us.apache.org/repos/asf/cxf/commit/3832db05 Tree: http://git-wip-us.apache.org/repos/asf/cxf/tree/3832db05 Diff: http://git-wip-us.apache.org/repos/asf/cxf/diff/3832db05 Branch: refs/heads/3.0.x-fixes Commit: 3832db055cbc8b7341bdb6fe4ba8ef5efb6be105 Parents: a94b303 Author: reta <drr...@gmail.com> Authored: Mon Jan 26 20:58:01 2015 -0500 Committer: Sergey Beryozkin <sberyoz...@talend.com> Committed: Wed Feb 11 10:24:26 2015 +0000 ---------------------------------------------------------------------- .../jaxrs/servlet/CXFNonSpringJaxrsServlet.java | 2 +- rt/pom.xml | 1 + .../javax.servlet.ServletContainerInitializer | 1 + rt/rs/http-sci/pom.xml | 59 +++++++++++++ .../JaxrsServletContainerInitializer.java | 89 ++++++++++++++++++++ 5 files changed, 151 insertions(+), 1 deletion(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cxf/blob/3832db05/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/servlet/CXFNonSpringJaxrsServlet.java ---------------------------------------------------------------------- diff --git a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/servlet/CXFNonSpringJaxrsServlet.java b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/servlet/CXFNonSpringJaxrsServlet.java index db9ff27..8329de5 100644 --- a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/servlet/CXFNonSpringJaxrsServlet.java +++ b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/servlet/CXFNonSpringJaxrsServlet.java @@ -514,7 +514,7 @@ public class CXFNonSpringJaxrsServlet extends CXFNonSpringServlet { protected boolean isIgnoreApplicationPath(ServletConfig servletConfig) { String ignoreParam = servletConfig.getInitParameter(IGNORE_APP_PATH_PARAM); - return ignoreParam == null || MessageUtils.isTrue(ignoreParam); + return ignoreParam != null && MessageUtils.isTrue(ignoreParam); } protected void createServerFromApplication(ServletConfig servletConfig) http://git-wip-us.apache.org/repos/asf/cxf/blob/3832db05/rt/pom.xml ---------------------------------------------------------------------- diff --git a/rt/pom.xml b/rt/pom.xml index 11c71c5..d25cb57 100644 --- a/rt/pom.xml +++ b/rt/pom.xml @@ -58,6 +58,7 @@ <module>ws/mex</module> <module>ws/eventing</module> <module>rs/client</module> + <module>rs/http-sci</module> <module>rs/description</module> <module>rs/extensions/providers</module> <module>rs/extensions/search</module> http://git-wip-us.apache.org/repos/asf/cxf/blob/3832db05/rt/rs/http-sci/main/resources/META-INF/services/javax.servlet.ServletContainerInitializer ---------------------------------------------------------------------- diff --git a/rt/rs/http-sci/main/resources/META-INF/services/javax.servlet.ServletContainerInitializer b/rt/rs/http-sci/main/resources/META-INF/services/javax.servlet.ServletContainerInitializer new file mode 100644 index 0000000..3d22cbc --- /dev/null +++ b/rt/rs/http-sci/main/resources/META-INF/services/javax.servlet.ServletContainerInitializer @@ -0,0 +1 @@ +org.apache.cxf.jaxrs.servlet.JaxrsServletContainerInitializer \ No newline at end of file http://git-wip-us.apache.org/repos/asf/cxf/blob/3832db05/rt/rs/http-sci/pom.xml ---------------------------------------------------------------------- diff --git a/rt/rs/http-sci/pom.xml b/rt/rs/http-sci/pom.xml new file mode 100644 index 0000000..9886ed4 --- /dev/null +++ b/rt/rs/http-sci/pom.xml @@ -0,0 +1,59 @@ +<?xml version="1.0"?> +<!-- + 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. +--> +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> + <modelVersion>4.0.0</modelVersion> + <artifactId>cxf-rt-rs-http-sci</artifactId> + <packaging>bundle</packaging> + <name>Apache CXF JAX-RS Service Description</name> + <description>Apache CXF JAX-RS Service Description</description> + <url>http://cxf.apache.org</url> + <parent> + <groupId>org.apache.cxf</groupId> + <artifactId>cxf-parent</artifactId> + <version>3.1.0-SNAPSHOT</version> + <relativePath>../../../parent/pom.xml</relativePath> + </parent> + <properties> + <cxf.osgi.import> + javax.servlet*;version="${cxf.osgi.javax.servlet.version}", + </cxf.osgi.import> + </properties> + <dependencies> + <dependency> + <groupId>org.apache.cxf</groupId> + <artifactId>cxf-rt-frontend-jaxrs</artifactId> + <version>${project.version}</version> + </dependency> + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>${cxf.servlet-api.group}</groupId> + <artifactId>${cxf.servlet-api.artifact}</artifactId> + <scope>provided</scope> + </dependency> + </dependencies> + <build> + <plugins> + </plugins> + </build> +</project> http://git-wip-us.apache.org/repos/asf/cxf/blob/3832db05/rt/rs/http-sci/src/main/java/org/apache/cxf/jaxrs/servlet/JaxrsServletContainerInitializer.java ---------------------------------------------------------------------- diff --git a/rt/rs/http-sci/src/main/java/org/apache/cxf/jaxrs/servlet/JaxrsServletContainerInitializer.java b/rt/rs/http-sci/src/main/java/org/apache/cxf/jaxrs/servlet/JaxrsServletContainerInitializer.java new file mode 100644 index 0000000..7be0198 --- /dev/null +++ b/rt/rs/http-sci/src/main/java/org/apache/cxf/jaxrs/servlet/JaxrsServletContainerInitializer.java @@ -0,0 +1,89 @@ +/** + * 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.servlet; + +import java.io.IOException; +import java.lang.annotation.Annotation; +import java.util.Arrays; +import java.util.Collection; +import java.util.Map; +import java.util.Set; +import java.util.logging.Logger; + +import javax.servlet.ServletContainerInitializer; +import javax.servlet.ServletContext; +import javax.servlet.ServletException; +import javax.servlet.ServletRegistration.Dynamic; +import javax.servlet.annotation.HandlesTypes; +import javax.ws.rs.Path; +import javax.ws.rs.core.Application; +import javax.ws.rs.ext.Provider; + +import org.apache.cxf.common.logging.LogUtils; +import org.apache.cxf.common.util.ClasspathScanner; + +@HandlesTypes(Application.class) +public class JaxrsServletContainerInitializer implements ServletContainerInitializer { + private static final Logger LOG = LogUtils.getL7dLogger(JaxrsServletContainerInitializer.class); + + @Override + public void onStartup(final Set< Class< ? > > classes, final ServletContext ctx) throws ServletException { + final Dynamic servlet = ctx.addServlet("CXFServlet", CXFNonSpringJaxrsServlet.class); + servlet.addMapping("/*"); + + final Class< ? > application = findCandidate(classes); + if (application != null) { + servlet.setInitParameter("javax.ws.rs.Application", application.getName()); + } else { + try { + final Map< Class< ? extends Annotation >, Collection< Class< ? > > > providersAndResources = + ClasspathScanner.findClasses(Arrays.asList(ClasspathScanner.WILDCARD), + Arrays.asList(Provider.class, Path.class)); + + servlet.setInitParameter("jaxrs.providers", getClassNames(providersAndResources.get(Provider.class))); + servlet.setInitParameter("jaxrs.serviceClasses", getClassNames(providersAndResources.get(Path.class))); + } catch (final ClassNotFoundException | IOException ex) { + throw new ServletException("Unabled to perform classpath scan", ex); + } + } + } + + private static String getClassNames(final Collection< Class< ? > > classes) { + final StringBuilder classNames = new StringBuilder(); + + for (final Class< ? > clazz: classes) { + classNames + .append((classNames.length() > 0) ? "," : "") + .append(clazz.getName()); + } + + return classNames.toString(); + } + + private static Class< ? > findCandidate(final Set< Class< ? > > classes) { + for (final Class< ? > clazz: classes) { + if (!clazz.getPackage().equals(JaxrsServletContainerInitializer.class.getPackage())) { + LOG.fine("Found JAX-RS application to initialize: " + clazz.getName()); + return clazz; + } + } + + return null; + } +}