org.apache.cxf.jaxrs.servlet.CXFNonSpringJaxrsServlet Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of cxf-bundle-minimal Show documentation
Show all versions of cxf-bundle-minimal Show documentation
Apache CXF Minimal Bundle Jar
/**
* 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.Method;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.ws.rs.core.Application;
import org.apache.cxf.common.classloader.ClassLoaderUtils;
import org.apache.cxf.common.logging.LogUtils;
import org.apache.cxf.common.util.PrimitiveUtils;
import org.apache.cxf.common.util.StringUtils;
import org.apache.cxf.helpers.CastUtils;
import org.apache.cxf.interceptor.Interceptor;
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.InjectionUtils;
import org.apache.cxf.jaxrs.utils.ResourceUtils;
import org.apache.cxf.message.Message;
import org.apache.cxf.message.MessageUtils;
import org.apache.cxf.service.invoker.Invoker;
import org.apache.cxf.transport.servlet.CXFNonSpringServlet;
public class CXFNonSpringJaxrsServlet extends CXFNonSpringServlet {
private static final long serialVersionUID = -8916352798780577499L;
private static final Logger LOG = LogUtils.getL7dLogger(CXFNonSpringJaxrsServlet.class);
private static final String USER_MODEL_PARAM = "user.model";
private static final String SERVICE_ADDRESS_PARAM = "jaxrs.address";
private static final String IGNORE_APP_PATH_PARAM = "jaxrs.application.address.ignore";
private static final String SERVICE_CLASSES_PARAM = "jaxrs.serviceClasses";
private static final String PROVIDERS_PARAM = "jaxrs.providers";
private static final String OUT_INTERCEPTORS_PARAM = "jaxrs.outInterceptors";
private static final String OUT_FAULT_INTERCEPTORS_PARAM = "jaxrs.outFaultInterceptors";
private static final String IN_INTERCEPTORS_PARAM = "jaxrs.inInterceptors";
private static final String INVOKER_PARAM = "jaxrs.invoker";
private static final String SERVICE_SCOPE_PARAM = "jaxrs.scope";
private static final String EXTENSIONS_PARAM = "jaxrs.extensions";
private static final String LANGUAGES_PARAM = "jaxrs.languages";
private static final String PROPERTIES_PARAM = "jaxrs.properties";
private static final String SCHEMAS_PARAM = "jaxrs.schemaLocations";
private static final String DOC_LOCATION_PARAM = "jaxrs.documentLocation";
private static final String STATIC_SUB_RESOLUTION_PARAM = "jaxrs.static.subresources";
private static final String SERVICE_SCOPE_SINGLETON = "singleton";
private static final String SERVICE_SCOPE_REQUEST = "prototype";
private static final String PARAMETER_SPLIT_CHAR = "class.parameter.split.char";
private static final String DEFAULT_PARAMETER_SPLIT_CHAR = ",";
private static final String SPACE_PARAMETER_SPLIT_CHAR = "space";
private static final String JAXRS_APPLICATION_PARAM = "javax.ws.rs.Application";
private ClassLoader classLoader;
@Override
public void init(ServletConfig servletConfig) throws ServletException {
super.init(servletConfig);
String splitChar = getParameterSplitChar(servletConfig);
String applicationClass = servletConfig.getInitParameter(JAXRS_APPLICATION_PARAM);
if (applicationClass != null) {
createServerFromApplication(applicationClass, servletConfig, splitChar);
return;
}
JAXRSServerFactoryBean bean = new JAXRSServerFactoryBean();
bean.setBus(getBus());
String address = servletConfig.getInitParameter(SERVICE_ADDRESS_PARAM);
if (address == null) {
address = "/";
}
bean.setAddress(address);
bean.setStaticSubresourceResolution(getStaticSubResolutionValue(servletConfig));
String modelRef = servletConfig.getInitParameter(USER_MODEL_PARAM);
if (modelRef != null) {
bean.setModelRef(modelRef.trim());
}
setDocLocation(bean, servletConfig);
setSchemasLocations(bean, servletConfig);
setAllInterceptors(bean, servletConfig, splitChar);
setInvoker(bean, servletConfig);
Map, Map>> resourceClasses =
getServiceClasses(servletConfig, modelRef != null, splitChar);
Map, ResourceProvider> resourceProviders =
getResourceProviders(servletConfig, resourceClasses);
List> providers = getProviders(servletConfig, splitChar);
bean.setResourceClasses(new ArrayList>(resourceClasses.keySet()));
bean.setProviders(providers);
for (Map.Entry, ResourceProvider> entry : resourceProviders.entrySet()) {
bean.setResourceProvider(entry.getKey(), entry.getValue());
}
setExtensions(bean, servletConfig);
bean.create();
}
protected String getParameterSplitChar(ServletConfig servletConfig) {
String param = servletConfig.getInitParameter(PARAMETER_SPLIT_CHAR);
if (!StringUtils.isEmpty(param) && SPACE_PARAMETER_SPLIT_CHAR.equals(param.trim())) {
return " ";
} else {
return DEFAULT_PARAMETER_SPLIT_CHAR;
}
}
protected boolean getStaticSubResolutionValue(ServletConfig servletConfig) {
String param = servletConfig.getInitParameter(STATIC_SUB_RESOLUTION_PARAM);
if (param != null) {
return Boolean.valueOf(param.trim());
} else {
return false;
}
}
protected void setExtensions(JAXRSServerFactoryBean bean, ServletConfig servletConfig) {
bean.setExtensionMappings(
CastUtils.cast((Map, ?>)parseMapSequence(servletConfig.getInitParameter(EXTENSIONS_PARAM))));
bean.setLanguageMappings(
CastUtils.cast((Map, ?>)parseMapSequence(servletConfig.getInitParameter(LANGUAGES_PARAM))));
bean.setProperties(CastUtils.cast(
parseMapSequence(servletConfig.getInitParameter(PROPERTIES_PARAM)),
String.class, Object.class));
}
protected void setAllInterceptors(JAXRSServerFactoryBean bean, ServletConfig servletConfig,
String splitChar)
throws ServletException {
setInterceptors(bean, servletConfig, OUT_INTERCEPTORS_PARAM, splitChar);
setInterceptors(bean, servletConfig, OUT_FAULT_INTERCEPTORS_PARAM, splitChar);
setInterceptors(bean, servletConfig, IN_INTERCEPTORS_PARAM, splitChar);
}
protected void setSchemasLocations(JAXRSServerFactoryBean bean, ServletConfig servletConfig) {
String schemas = servletConfig.getInitParameter(SCHEMAS_PARAM);
if (schemas == null) {
return;
}
String[] locations = StringUtils.split(schemas, " ");
List list = new ArrayList();
for (String loc : locations) {
String theLoc = loc.trim();
if (theLoc.length() != 0) {
list.add(theLoc);
}
}
if (list.size() > 0) {
bean.setSchemaLocations(list);
}
}
protected void setDocLocation(JAXRSServerFactoryBean bean, ServletConfig servletConfig) {
String wadlLoc = servletConfig.getInitParameter(DOC_LOCATION_PARAM);
if (wadlLoc != null) {
bean.setDocLocation(wadlLoc);
}
}
@SuppressWarnings("unchecked")
protected void setInterceptors(JAXRSServerFactoryBean bean, ServletConfig servletConfig,
String paramName,
String splitChar) throws ServletException {
String value = servletConfig.getInitParameter(paramName);
if (value == null) {
return;
}
String[] values = StringUtils.split(value, splitChar);
List> list = new ArrayList>();
for (String interceptorVal : values) {
Map> props = new HashMap>();
String theValue = getClassNameAndProperties(interceptorVal, props);
if (theValue.length() != 0) {
try {
Class> intClass = loadClass(theValue, "Interceptor");
Object object = intClass.newInstance();
injectProperties(object, props);
list.add((Interceptor extends Message>)object);
} catch (ServletException ex) {
throw ex;
} catch (Exception ex) {
LOG.warning("Interceptor class " + theValue + " can not be created");
throw new ServletException(ex);
}
}
}
if (list.size() > 0) {
if (OUT_INTERCEPTORS_PARAM.equals(paramName)) {
bean.setOutInterceptors(list);
} else if (OUT_FAULT_INTERCEPTORS_PARAM.equals(paramName)) {
bean.setOutFaultInterceptors(list);
} else {
bean.setInInterceptors(list);
}
}
}
protected void setInvoker(JAXRSServerFactoryBean bean, ServletConfig servletConfig)
throws ServletException {
String value = servletConfig.getInitParameter(INVOKER_PARAM);
if (value == null) {
return;
}
Map> props = new HashMap>();
String theValue = getClassNameAndProperties(value, props);
if (theValue.length() != 0) {
try {
Class> intClass = loadClass(theValue, "Invoker");
Object object = intClass.newInstance();
injectProperties(object, props);
bean.setInvoker((Invoker)object);
} catch (ServletException ex) {
throw ex;
} catch (Exception ex) {
LOG.warning("Invoker class " + theValue + " can not be created");
throw new ServletException(ex);
}
}
}
protected Map, Map>> getServiceClasses(ServletConfig servletConfig,
boolean modelAvailable,
String splitChar) throws ServletException {
String serviceBeans = servletConfig.getInitParameter(SERVICE_CLASSES_PARAM);
if (serviceBeans == null) {
if (modelAvailable) {
return Collections.emptyMap();
}
throw new ServletException("At least one resource class should be specified");
}
String[] classNames = StringUtils.split(serviceBeans, splitChar);
Map, Map>> map =
new HashMap, Map>>();
for (String cName : classNames) {
Map> props = new HashMap>();
String theName = getClassNameAndProperties(cName, props);
if (theName.length() != 0) {
Class> cls = loadClass(theName);
map.put(cls, props);
}
}
if (map.isEmpty()) {
throw new ServletException("At least one resource class should be specified");
}
return map;
}
protected List> getProviders(ServletConfig servletConfig, String splitChar) throws ServletException {
String providersList = servletConfig.getInitParameter(PROVIDERS_PARAM);
if (providersList == null) {
return Collections.EMPTY_LIST;
}
String[] classNames = StringUtils.split(providersList, splitChar);
List