Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/**
* 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.cxf.common.classloader.ClassLoaderUtils;
import org.apache.cxf.common.logging.LogUtils;
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.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 USER_MODEL_PARAM = "user.model";
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 OUT_INTERCEPTORS_PARAM = "jaxrs.outInterceptors";
private static final String IN_INTERCEPTORS_PARAM = "jaxrs.inInterceptors";
private static final String SERVICE_SCOPE_PARAM = "jaxrs.scope";
private static final String SCHEMAS_PARAM = "jaxrs.schemaLocations";
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;
}
JAXRSServerFactoryBean bean = new JAXRSServerFactoryBean();
String address = servletConfig.getInitParameter(SERVICE_ADDRESS_PARAM);
if (address == null) {
address = "/";
}
bean.setAddress(address);
String modelRef = servletConfig.getInitParameter(USER_MODEL_PARAM);
if (modelRef != null) {
bean.setModelRef(modelRef.trim());
}
setSchemasLocations(bean, servletConfig);
setInterceptors(bean, servletConfig, OUT_INTERCEPTORS_PARAM);
setInterceptors(bean, servletConfig, IN_INTERCEPTORS_PARAM);
List resourceClasses = getServiceClasses(servletConfig, modelRef != null);
Map resourceProviders =
getResourceProviders(servletConfig, resourceClasses);
List> providers = getProviders(servletConfig);
bean.setResourceClasses(resourceClasses);
bean.setProviders(providers);
for (Map.Entry entry : resourceProviders.entrySet()) {
bean.setResourceProvider(entry.getKey(), entry.getValue());
}
bean.create();
}
protected void setSchemasLocations(JAXRSServerFactoryBean bean, ServletConfig servletConfig) {
String schemas = servletConfig.getInitParameter(SCHEMAS_PARAM);
if (schemas == null) {
return;
}
String[] locations = schemas.split(" ");
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 setInterceptors(JAXRSServerFactoryBean bean, ServletConfig servletConfig,
String paramName) {
String value = servletConfig.getInitParameter(paramName);
if (value == null) {
return;
}
String[] values = value.split(" ");
List list = new ArrayList();
for (String interceptorVal : values) {
String theValue = interceptorVal.trim();
if (theValue.length() != 0) {
try {
Class> intClass = ClassLoaderUtils.loadClass(theValue,
CXFNonSpringJaxrsServlet.class);
list.add((Interceptor)intClass.newInstance());
} catch (ClassNotFoundException ex) {
LOG.warning("Interceptor class " + theValue + " can not be found");
} catch (InstantiationException ex) {
LOG.warning(theValue + " class can not be instantiated");
ex.printStackTrace();
} catch (IllegalAccessException ex) {
LOG.warning("CXF Interceptor can not be instantiated due to IllegalAccessException");
} catch (ClassCastException ex) {
LOG.warning(theValue + " class does not implement " + Interceptor.class.getName());
}
}
}
if (list.size() > 0) {
if (OUT_INTERCEPTORS_PARAM.equals(paramName)) {
bean.setOutInterceptors(list);
} else {
bean.setInInterceptors(list);
}
}
}
protected List getServiceClasses(ServletConfig servletConfig,
boolean modelAvailable) throws ServletException {
String serviceBeans = servletConfig.getInitParameter(SERVICE_CLASSES_PARAM);
if (serviceBeans == null) {
if (modelAvailable) {
return Collections.emptyList();
}
throw new ServletException("At least one resource class should be specified");
}
String[] classNames = serviceBeans.split(" ");
List resourceClasses = new ArrayList();
for (String cName : classNames) {
String theName = cName.trim();
if (theName.length() != 0) {
Class> cls = loadClass(theName);
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