org.apache.cxf.jaxrs.JAXRSInvoker Maven / Gradle / Ivy
/**
* 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.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.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.OperationResourceInfo;
import org.apache.cxf.jaxrs.model.ProviderInfo;
import org.apache.cxf.jaxrs.model.URITemplate;
import org.apache.cxf.jaxrs.provider.ServerProviderFactory;
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 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();
try {
asyncImpl.handleTimeout();
return handleAsyncResponse(exchange, asyncImpl);
} catch (Throwable t) {
return handleAsyncFault(exchange, asyncImpl, t);
}
}
ResourceProvider provider = getResourceProvider(exchange);
Object rootInstance = null;
Message inMessage = exchange.getInMessage();
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, inMessage);
} finally {
boolean suspended = isSuspended(exchange);
if (suspended || exchange.isOneWay() || inMessage.get(Message.THREAD_CONTEXT_SWITCHED) != null) {
ServerProviderFactory.clearThreadLocalProxies(inMessage);
}
if (suspended || isServiceObjectRequestScope(inMessage)) {
persistRoots(exchange, rootInstance, provider);
} else {
provider.releaseInstance(inMessage, rootInstance);
}
}
}
private boolean isSuspended(Exchange exchange) {
return exchange.getInMessage().getInterceptorChain().getState() == State.SUSPENDED;
}
private Object handleAsyncResponse(Exchange exchange, AsyncResponseImpl ar) {
Object asyncObj = ar.getResponseObject();
if (asyncObj instanceof Throwable) {
return handleAsyncFault(exchange, ar, (Throwable)asyncObj);
} else {
setResponseContentTypeIfNeeded(exchange.getInMessage(), asyncObj);
return new MessageContentsList(asyncObj);
}
}
private Object handleAsyncFault(Exchange exchange, AsyncResponseImpl ar, Throwable t) {
try {
return handleFault(new Fault(t), exchange.getInMessage(), null, null);
} catch (Fault ex) {
ar.setUnmappedThrowable(ex.getCause());
if (isSuspended(exchange)) {
ar.reset();
exchange.getInMessage().getInterceptorChain().unpause();
}
return new MessageContentsList(Response.serverError().build());
}
}
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 ServerProviderFactory providerFactory = ServerProviderFactory.getInstance(inMessage);
if (cri.isRoot()) {
cri.injectContexts(resourceObject, ori, inMessage);
ProviderInfo appProvider = providerFactory.getApplicationProvider();
if (appProvider != null) {
InjectionUtils.injectContexts(appProvider.getProvider(),
appProvider,
inMessage);
}
}
Method methodToInvoke = getMethodToInvoke(cri, ori, resourceObject);
List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy