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.gae.bind;
018    
019    import java.lang.reflect.InvocationHandler;
020    import java.lang.reflect.Method;
021    
022    import org.apache.camel.Endpoint;
023    import org.apache.camel.Exchange;
024    import org.apache.camel.component.http.HttpBinding;
025    import org.apache.camel.component.http.HttpMessage;
026    
027    /**
028     * Post-processes {@link HttpBinding} invocations by delegating to an
029     * endpoint's {@link InboundBinding}.
030     */
031    public class HttpBindingInvocationHandler<E extends Endpoint, S, T> implements InvocationHandler {
032    
033        private E endpoint;
034        private HttpBinding httpBinding;
035        private InboundBinding<E, S, T> inboundBinding; 
036        
037        public HttpBindingInvocationHandler(E endpoint, HttpBinding httpBinding, InboundBinding<E, S, T> inboundBinding) {
038            this.endpoint = endpoint;
039            this.httpBinding = httpBinding;
040            this.inboundBinding = inboundBinding;
041        }
042        
043        @SuppressWarnings("unchecked")
044        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
045            Object result = method.invoke(httpBinding, args); // updates args
046            if (method.getName().equals("readRequest") && (args.length == 2)) {
047                HttpMessage message = (HttpMessage)args[1];
048                // prepare exchange for further inbound binding operations
049                message.getExchange().setIn(message);
050                // delegate further request binding operations to inbound binding
051                inboundBinding.readRequest(endpoint, message.getExchange(), (S)args[0]);
052            } else if (method.getName().equals("writeResponse") && (args.length == 2)) {
053                // delegate further response binding operations to inbound binding
054                inboundBinding.writeResponse(endpoint, (Exchange)args[0], (T)args[1]);
055            }
056            return result;
057        }
058        
059    }