org.apache.cxf.jaxrs.JAXRSInvoker 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;
import java.io.IOException;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.ResourceBundle;
import java.util.logging.Logger;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.container.AsyncResponse;
import javax.ws.rs.core.Application;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.Response;
import org.apache.cxf.common.classloader.ClassLoaderUtils;
import org.apache.cxf.common.classloader.ClassLoaderUtils.ClassLoaderHolder;
import org.apache.cxf.common.i18n.BundleUtils;
import org.apache.cxf.common.logging.LogUtils;
import org.apache.cxf.common.util.ClassHelper;
import org.apache.cxf.common.util.PropertyUtils;
import org.apache.cxf.helpers.CastUtils;
import org.apache.cxf.interceptor.Fault;
import org.apache.cxf.interceptor.InterceptorChain.State;
import org.apache.cxf.jaxrs.impl.AsyncResponseImpl;
import org.apache.cxf.jaxrs.impl.MetadataMap;
import org.apache.cxf.jaxrs.lifecycle.ResourceProvider;
import org.apache.cxf.jaxrs.model.ClassResourceInfo;
import org.apache.cxf.jaxrs.model.MethodInvocationInfo;
import org.apache.cxf.jaxrs.model.OperationResourceInfo;
import org.apache.cxf.jaxrs.model.OperationResourceInfoStack;
import org.apache.cxf.jaxrs.model.Parameter;
import org.apache.cxf.jaxrs.model.ParameterType;
import org.apache.cxf.jaxrs.model.ProviderInfo;
import org.apache.cxf.jaxrs.model.URITemplate;
import org.apache.cxf.jaxrs.provider.ProviderFactory;
import org.apache.cxf.jaxrs.utils.ExceptionUtils;
import org.apache.cxf.jaxrs.utils.InjectionUtils;
import org.apache.cxf.jaxrs.utils.JAXRSUtils;
import org.apache.cxf.message.Exchange;
import org.apache.cxf.message.Message;
import org.apache.cxf.message.MessageContentsList;
import org.apache.cxf.service.invoker.AbstractInvoker;
public class JAXRSInvoker extends AbstractInvoker {
private static final Logger LOG = LogUtils.getL7dLogger(JAXRSInvoker.class);
private static final ResourceBundle BUNDLE = BundleUtils.getBundle(JAXRSInvoker.class);
private static final String SERVICE_LOADER_AS_CONTEXT = "org.apache.cxf.serviceloader-context";
private static final String SERVICE_OBJECT_SCOPE = "org.apache.cxf.service.scope";
private static final String REQUEST_SCOPE = "request";
private static final String LAST_SERVICE_OBJECT = "org.apache.cxf.service.object.last";
private static final String REQUEST_WAS_SUSPENDED = "org.apache.cxf.service.request.suspended";
private static final String PROXY_INVOCATION_ERROR_FRAGMENT
= "object is not an instance of declaring class";
public JAXRSInvoker() {
}
public Object invoke(Exchange exchange, Object request) {
MessageContentsList responseList = checkExchangeForResponse(exchange);
if (responseList != null) {
return responseList;
}
AsyncResponse asyncResp = exchange.get(AsyncResponse.class);
if (asyncResp != null) {
AsyncResponseImpl asyncImpl = (AsyncResponseImpl)asyncResp;
asyncImpl.prepareContinuation();
asyncImpl.handleTimeout();
return handleAsyncResponse(exchange, asyncImpl.getResponseObject());
}
ResourceProvider provider = getResourceProvider(exchange);
Object rootInstance = null;
try {
rootInstance = getServiceObject(exchange);
Object serviceObject = getActualServiceObject(exchange, rootInstance);
return invoke(exchange, request, serviceObject);
} catch (WebApplicationException ex) {
responseList = checkExchangeForResponse(exchange);
if (responseList != null) {
return responseList;
}
return handleFault(ex, exchange.getInMessage());
} finally {
boolean suspended = PropertyUtils.isTrue(exchange.get(Message.SUSPENDED_INVOCATION))
|| exchange.getInMessage().getInterceptorChain().getState() == State.SUSPENDED;
if (exchange.isOneWay() || suspended) {
ProviderFactory.getInstance(exchange.getInMessage()).clearThreadLocalProxies();
}
if (!suspended && !isServiceObjectRequestScope(exchange.getInMessage())) {
provider.releaseInstance(exchange.getInMessage(), rootInstance);
} else {
exchange.put(REQUEST_WAS_SUSPENDED, suspended);
persistRoots(exchange, rootInstance, provider);
}
}
}
private Object handleAsyncResponse(Exchange exchange, Object asyncObj) {
if (asyncObj instanceof Throwable) {
return handleFault(new Fault((Throwable)asyncObj),
exchange.getInMessage(), null, null);
} else {
setResponseContentTypeIfNeeded(exchange.getInMessage(), asyncObj);
return new MessageContentsList(asyncObj);
}
}
private void persistRoots(Exchange exchange, Object rootInstance, Object provider) {
exchange.put(JAXRSUtils.ROOT_INSTANCE, rootInstance);
exchange.put(JAXRSUtils.ROOT_PROVIDER, provider);
}
@SuppressWarnings("unchecked")
public Object invoke(Exchange exchange, Object request, Object resourceObject) {
final OperationResourceInfo ori = exchange.get(OperationResourceInfo.class);
final ClassResourceInfo cri = ori.getClassResourceInfo();
final Message inMessage = exchange.getInMessage();
final ProviderFactory providerFactory = ProviderFactory.getInstance(inMessage);
boolean wasSuspended = exchange.remove(REQUEST_WAS_SUSPENDED) != null;
if (!wasSuspended) {
pushOntoStack(ori, ClassHelper.getRealClass(resourceObject), inMessage);
}
final boolean contextsAvailable = cri.contextsAvailable();
final boolean paramsAvailable = cri.paramsAvailable();
if (contextsAvailable || paramsAvailable) {
Object realResourceObject = ClassHelper.getRealObject(resourceObject);
if (paramsAvailable) {
JAXRSUtils.injectParameters(ori, realResourceObject, inMessage);
}
if (contextsAvailable) {
InjectionUtils.injectContexts(realResourceObject, cri, inMessage);
}
}
if (cri.isRoot()) {
ProviderInfo> appProvider =
(ProviderInfo>)exchange.getEndpoint().get(Application.class.getName());
if (appProvider != null) {
InjectionUtils.injectContexts(appProvider.getProvider(),
appProvider,
inMessage);
}
}
Method resourceMethod = cri.getMethodDispatcher().getMethod(ori);
Method methodToInvoke = null;
if (Proxy.class.isInstance(resourceObject)) {
methodToInvoke = cri.getMethodDispatcher().getProxyMethod(resourceMethod);
if (methodToInvoke == null) {
methodToInvoke = InjectionUtils.checkProxy(resourceMethod, resourceObject);
cri.getMethodDispatcher().addProxyMethod(resourceMethod, methodToInvoke);
}
} else {
methodToInvoke = resourceMethod;
}
List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy