org.apache.cxf.jaxrs.servlet.CXFNonSpringJaxrsServlet Maven / Gradle / Ivy
/**
* 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.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.logging.Logger;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.ws.rs.core.Application;
import javax.ws.rs.ext.Provider;
import org.apache.commons.lang.ClassUtils;
import org.apache.cxf.common.logging.LogUtils;
import org.apache.cxf.jaxrs.JAXRSServerFactoryBean;
import org.apache.cxf.jaxrs.lifecycle.PerRequestResourceProvider;
import org.apache.cxf.jaxrs.lifecycle.ResourceProvider;
import org.apache.cxf.jaxrs.lifecycle.SingletonResourceProvider;
import org.apache.cxf.jaxrs.utils.ResourceUtils;
import org.apache.cxf.transport.servlet.CXFNonSpringServlet;
public class CXFNonSpringJaxrsServlet extends CXFNonSpringServlet {
private static final Logger LOG = LogUtils.getL7dLogger(CXFNonSpringJaxrsServlet.class);
private static final String SERVICE_ADDRESS_PARAM = "jaxrs.address";
private static final String SERVICE_CLASSES_PARAM = "jaxrs.serviceClasses";
private static final String PROVIDERS_PARAM = "jaxrs.providers";
private static final String SERVICE_SCOPE_PARAM = "jaxrs.scope";
private static final String SERVICE_SCOPE_SINGLETON = "singleton";
private static final String SERVICE_SCOPE_REQUEST = "prototype";
private static final String JAXRS_APPLICATION_PARAM = "javax.ws.rs.Application";
@Override
public void loadBus(ServletConfig servletConfig) throws ServletException {
super.loadBus(servletConfig);
String applicationClass = servletConfig.getInitParameter(JAXRS_APPLICATION_PARAM);
if (applicationClass != null) {
createServerFromApplication(applicationClass);
return;
}
String address = servletConfig.getInitParameter(SERVICE_ADDRESS_PARAM);
if (address == null) {
address = "/";
}
List resourceClasses = getServiceClasses(servletConfig);
Map resourceProviders =
getResourceProviders(servletConfig, resourceClasses);
List providers = getProviders(servletConfig);
JAXRSServerFactoryBean bean = new JAXRSServerFactoryBean();
bean.setAddress(address);
bean.setResourceClasses(resourceClasses);
bean.setProviders(providers);
for (Map.Entry entry : resourceProviders.entrySet()) {
bean.setResourceProvider(entry.getKey(), entry.getValue());
}
bean.create();
}
protected List getServiceClasses(ServletConfig servletConfig) throws ServletException {
String serviceBeans = servletConfig.getInitParameter(SERVICE_CLASSES_PARAM);
if (serviceBeans == null) {
throw new ServletException("At least one resource class should be specified");
}
String[] classNames = serviceBeans.split(" ");
List resourceClasses = new ArrayList();
for (String cName : classNames) {
if (cName.length() != 0) {
Class cls = loadClass(cName);
resourceClasses.add(cls);
}
}
if (resourceClasses.isEmpty()) {
throw new ServletException("At least one resource class should be specified");
}
return resourceClasses;
}
protected List getProviders(ServletConfig servletConfig) throws ServletException {
String providersList = servletConfig.getInitParameter(PROVIDERS_PARAM);
if (providersList == null) {
return Collections.EMPTY_LIST;
}
String[] classNames = providersList.split(" ");
List