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