org.apache.cxf.jaxrs.provider.ProviderFactory Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of cxf-bundle-jaxrs Show documentation
Show all versions of cxf-bundle-jaxrs Show documentation
Apache CXF JAX-RS 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.provider;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;
import java.util.logging.Logger;
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.common.logging.LogUtils;
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.ext.SystemQueryHandler;
import org.apache.cxf.jaxrs.impl.RequestPreprocessor;
import org.apache.cxf.jaxrs.impl.WebApplicationExceptionMapper;
import org.apache.cxf.jaxrs.model.ProviderInfo;
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();
static {
// TODO : do dynamic instantiation of JSON and few other default providers
JSONProvider jsonProvider = null;
try {
jsonProvider = new JSONProvider();
} catch (Throwable ex) {
String message = "Problem with instantiating the default JSON provider, ";
if (ex.getMessage() != null) {
message += ex.getMessage();
} else {
message += "exception class : " + ex.getClass().getName();
}
LOG.info(message);
}
SHARED_FACTORY.setProviders(new JAXBElementProvider(),
jsonProvider,
new BinaryDataProvider(),
new SourceProvider(),
new FormEncodingProvider(),
new PrimitiveTextProvider(),
new MultipartProvider(),
new WebApplicationExceptionMapper(),
new SystemQueryHandler());
}
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 ProviderFactory() {
}
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 mt) {
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);
}
if (candidates.size() == 0) {
return null;
}
Collections.sort((List)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);
}
if (candidates.size() == 0) {
return null;
}
Collections.sort(candidates, new ParameterHandlerComparator());
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);
}
if (candidates.size() == 0) {
return null;
}
Collections.sort(candidates, new ResponseExceptionMapperComparator());
return candidates.get(0);
}
private static void handleMapper(List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy