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.provider;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import java.util.logging.Logger;
import javax.ws.rs.core.Application;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.ext.ContextResolver;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.MessageBodyReader;
import javax.ws.rs.ext.MessageBodyWriter;
import org.apache.cxf.Bus;
import org.apache.cxf.common.classloader.ClassLoaderUtils;
import org.apache.cxf.common.logging.LogUtils;
import org.apache.cxf.common.util.ClassHelper;
import org.apache.cxf.endpoint.Endpoint;
import org.apache.cxf.jaxrs.client.ResponseExceptionMapper;
import org.apache.cxf.jaxrs.ext.ParameterHandler;
import org.apache.cxf.jaxrs.ext.RequestHandler;
import org.apache.cxf.jaxrs.ext.ResponseHandler;
import org.apache.cxf.jaxrs.impl.RequestPreprocessor;
import org.apache.cxf.jaxrs.impl.WebApplicationExceptionMapper;
import org.apache.cxf.jaxrs.model.ClassResourceInfo;
import org.apache.cxf.jaxrs.model.ProviderInfo;
import org.apache.cxf.jaxrs.model.wadl.WadlGenerator;
import org.apache.cxf.jaxrs.utils.InjectionUtils;
import org.apache.cxf.jaxrs.utils.JAXRSUtils;
import org.apache.cxf.message.Message;
public final class ProviderFactory {
private static final Logger LOG = LogUtils.getL7dLogger(ProviderFactory.class);
private static final ProviderFactory SHARED_FACTORY = new ProviderFactory();
private static final String JAXB_PROVIDER_NAME = "org.apache.cxf.jaxrs.provider.JAXBElementProvider";
private static final String JSON_PROVIDER_NAME = "org.apache.cxf.jaxrs.provider.JSONProvider";
static {
SHARED_FACTORY.setProviders(new BinaryDataProvider(),
new SourceProvider(),
new FormEncodingProvider(),
new PrimitiveTextProvider(),
new MultipartProvider(),
new WebApplicationExceptionMapper(),
new WadlGenerator());
}
private List> messageReaders =
new ArrayList>();
private List> messageWriters =
new ArrayList>();
private List> contextResolvers =
new ArrayList>(1);
private List> exceptionMappers =
new ArrayList>(1);
private List> requestHandlers =
new ArrayList>(1);
private List> responseHandlers =
new ArrayList>(1);
private List> paramHandlers =
new ArrayList>(1);
private List> responseExceptionMappers =
new ArrayList>(1);
private RequestPreprocessor requestPreprocessor;
private ProviderInfo application;
private List> jaxbReaders =
new ArrayList>();
private List> jaxbWriters =
new ArrayList>();
private ProviderFactory() {
initJaxbProviders();
}
// Not ideal but in the end seems like the simplest option compared
// to adding default readers/writers to existing messageReaders/Writers
// (due to all sort of conflicts with custom providers) and cloning
// at the request time
private void initJaxbProviders() {
Object jaxbProvider = createProvider(JAXB_PROVIDER_NAME);
if (jaxbProvider != null) {
jaxbReaders.add(new ProviderInfo((MessageBodyReader)jaxbProvider));
jaxbWriters.add(new ProviderInfo((MessageBodyWriter)jaxbProvider));
}
Object jsonProvider = createProvider(JSON_PROVIDER_NAME);
if (jsonProvider != null) {
jaxbReaders.add(new ProviderInfo((MessageBodyReader)jsonProvider));
jaxbWriters.add(new ProviderInfo((MessageBodyWriter)jsonProvider));
}
injectContexts(jaxbReaders, jaxbWriters);
}
private static Object createProvider(String className) {
try {
return ClassLoaderUtils.loadClass(className, ProviderFactory.class).newInstance();
} catch (Throwable ex) {
String message = "Problem with setting the default provider " + className;
if (ex.getMessage() != null) {
message += ex.getMessage();
} else {
message += ", exception class : " + ex.getClass().getName();
}
LOG.info(message);
}
return null;
}
public static ProviderFactory getInstance() {
return new ProviderFactory();
}
public static ProviderFactory getInstance(Message m) {
Endpoint e = m.getExchange().get(Endpoint.class);
return (ProviderFactory)e.get(ProviderFactory.class.getName());
}
public static ProviderFactory getSharedInstance() {
return SHARED_FACTORY;
}
public ContextResolver createContextResolver(Type contextType,
Message m) {
Object mt = m.get(Message.CONTENT_TYPE);
return createContextResolver(contextType, m,
mt == null ? MediaType.valueOf("*/*") : MediaType.valueOf(mt.toString()));
}
@SuppressWarnings("unchecked")
public ContextResolver createContextResolver(Type contextType,
Message m,
MediaType type) {
for (ProviderInfo cr : contextResolvers) {
Type[] types = cr.getProvider().getClass().getGenericInterfaces();
for (Type t : types) {
if (t instanceof ParameterizedType) {
ParameterizedType pt = (ParameterizedType)t;
Type[] args = pt.getActualTypeArguments();
for (int i = 0; i < args.length; i++) {
if (contextType == args[i]) {
InjectionUtils.injectContextFields(cr.getProvider(), cr, m);
InjectionUtils.injectContextMethods(cr.getProvider(), cr, m);
return cr.getProvider();
}
}
}
}
}
return null;
}
public ExceptionMapper createExceptionMapper(Class> exceptionType, Message m) {
ExceptionMapper mapper = doCreateExceptionMapper(exceptionType, m);
if (mapper != null || this == SHARED_FACTORY) {
return mapper;
}
return SHARED_FACTORY.createExceptionMapper(exceptionType, m);
}
@SuppressWarnings("unchecked")
private ExceptionMapper doCreateExceptionMapper(
Class> exceptionType, Message m) {
List> candidates = new LinkedList>();
for (ProviderInfo em : exceptionMappers) {
handleMapper((List)candidates, em, exceptionType, m, ExceptionMapper.class);
}
if (candidates.size() == 0) {
return null;
}
Collections.sort(candidates, new ExceptionMapperComparator());
return candidates.get(0);
}
@SuppressWarnings("unchecked")
public ParameterHandler createParameterHandler(Class> paramType) {
List> candidates = new LinkedList>();
for (ProviderInfo em : paramHandlers) {
handleMapper((List)candidates, em, paramType, null, ParameterHandler.class);
}
if (candidates.size() == 0) {
return null;
}
Collections.sort(candidates, new ClassComparator());
return candidates.get(0);
}
@SuppressWarnings("unchecked")
public ResponseExceptionMapper createResponseExceptionMapper(
Class> paramType) {
List> candidates = new LinkedList>();
for (ProviderInfo em : responseExceptionMappers) {
handleMapper((List)candidates, em, paramType, null, ResponseExceptionMapper.class);
}
if (candidates.size() == 0) {
return null;
}
Collections.sort(candidates, new ClassComparator());
return candidates.get(0);
}
private static void handleMapper(List