001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.camel.component.cxf.jaxrs;
018    
019    import java.lang.reflect.Method;
020    
021    import javax.ws.rs.WebApplicationException;
022    
023    import org.apache.camel.ExchangePattern;
024    import org.apache.camel.Processor;
025    import org.apache.camel.RuntimeCamelException;
026    import org.apache.cxf.jaxrs.JAXRSInvoker;
027    import org.apache.cxf.jaxrs.model.OperationResourceInfo;
028    import org.apache.cxf.message.Exchange;
029    
030    public class CxfRsInvoker extends JAXRSInvoker {
031        
032        private Processor processor;
033        private CxfRsEndpoint endpoint;
034        
035        public CxfRsInvoker(CxfRsEndpoint endpoint, Processor processor) {
036            this.endpoint = endpoint;
037            this.processor = processor;
038        }
039        
040        protected Object performInvocation(Exchange cxfExchange, final Object serviceObject, Method method,
041                                           Object[] paramArray) throws Exception {
042            paramArray = insertExchange(method, paramArray, cxfExchange);
043            OperationResourceInfo ori = cxfExchange.get(OperationResourceInfo.class);        
044            if (ori.isSubResourceLocator()) {
045                // don't delegate the sub resource locator call to camel processor
046                return method.invoke(serviceObject, paramArray);
047            }
048           
049            ExchangePattern ep = ExchangePattern.InOut;
050            if (method.getReturnType() == Void.class) {
051                ep = ExchangePattern.InOnly;
052            } 
053            org.apache.camel.Exchange camelExchange = endpoint.createExchange(ep);
054            CxfRsBinding binding = endpoint.getBinding();
055            binding.populateExchangeFromCxfRsRequest(cxfExchange, camelExchange, method, paramArray);
056            try {
057                processor.process(camelExchange);
058            } catch (Exception exception) {
059                camelExchange.setException(exception);
060            }
061            if (camelExchange.getException() != null) {
062                Throwable exception = camelExchange.getException();
063                Object result = null;
064                if (exception instanceof RuntimeCamelException) {
065                    exception = ((RuntimeCamelException)exception).getCause();
066                }
067                if (exception instanceof WebApplicationException) {
068                    result = ((WebApplicationException)exception).getResponse();
069                }
070                return result;
071            }
072            return binding.populateCxfRsResponseFromExchange(camelExchange, cxfExchange);
073        }
074    
075    }