}
@SuppressWarnings("unchecked")
public Object invoke(Exchange exchange, Object request, Object resourceObject) {
OperationResourceInfo ori = exchange.get(OperationResourceInfo.class);
ClassResourceInfo cri = ori.getClassResourceInfo();
boolean wasSuspended = exchange.remove(REQUEST_WAS_SUSPENDED) != null;
if (!wasSuspended) {
pushOntoStack(ori, ClassHelper.getRealClass(resourceObject), exchange.getInMessage());
if (cri.isRoot()) {
Object realResourceObject = ClassHelper.getRealObject(resourceObject);
JAXRSUtils.injectParameters(ori, realResourceObject,
exchange.getInMessage());
InjectionUtils.injectContexts(realResourceObject,
ori.getClassResourceInfo(),
exchange.getInMessage());
ProviderInfo<?> appProvider =
(ProviderInfo)exchange.getEndpoint().get(Application.class.getName());
if (appProvider != null) {
InjectionUtils.injectContexts(appProvider.getProvider(),
appProvider,
exchange.getInMessage());
}
}
}
Method methodToInvoke = InjectionUtils.checkProxy(
cri.getMethodDispatcher().getMethod(ori), resourceObject);
List<Object> params = null;
if (request instanceof List) {
params = CastUtils.cast((List<?>)request);
} else if (request != null) {
params = new MessageContentsList(request);
}
Object result = null;
ClassLoader contextLoader = null;
try {
if (setServiceLoaderAsContextLoader(exchange.getInMessage())) {
contextLoader = Thread.currentThread().getContextClassLoader();
Thread.currentThread().setContextClassLoader(resourceObject.getClass().getClassLoader());
}
result = invoke(exchange, resourceObject, methodToInvoke, params);
} catch (Fault ex) {
Response excResponse = JAXRSUtils.convertFaultToResponse(ex.getCause(),
exchange.getInMessage());
if (excResponse == null) {
ProviderFactory.getInstance(exchange.getInMessage()).clearThreadLocalProxies();
ClassResourceInfo criRoot =
(ClassResourceInfo)exchange.get(JAXRSUtils.ROOT_RESOURCE_CLASS);
if (criRoot != null) {
criRoot.clearThreadLocalProxies();
}
exchange.put(Message.PROPOGATE_EXCEPTION,
JAXRSUtils.propogateException(exchange.getInMessage()));
throw ex;
}
return new MessageContentsList(excResponse);
} finally {
exchange.put(LAST_SERVICE_OBJECT, resourceObject);
if (contextLoader != null) {
Thread.currentThread().setContextClassLoader(contextLoader);
}
}
if (ori.isSubResourceLocator()) {
try {
Message msg = exchange.getInMessage();
MultivaluedMap<String, String> values = getTemplateValues(msg);
String subResourcePath = values.getFirst(URITemplate.FINAL_MATCH_GROUP);
String httpMethod = (String)msg.get(Message.HTTP_REQUEST_METHOD);
String contentType = (String)msg.get(Message.CONTENT_TYPE);
if (contentType == null) {
contentType = "*/*";
}
List<MediaType> acceptContentType =
(List<MediaType>)msg.getExchange().get(Message.ACCEPT_CONTENT_TYPE);
result = checkResultObject(result, subResourcePath);
ClassResourceInfo subCri = cri.getSubResource(
methodToInvoke.getReturnType(),
ClassHelper.getRealClass(result));
if (subCri == null) {
org.apache.cxf.common.i18n.Message errorM =
new org.apache.cxf.common.i18n.Message("NO_SUBRESOURCE_FOUND",
BUNDLE,
subResourcePath);
LOG.severe(errorM.toString());
throw new WebApplicationException(404);
}
OperationResourceInfo subOri = JAXRSUtils.findTargetMethod(subCri,
exchange.getInMessage(),
httpMethod,
values,
contentType,
acceptContentType,