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.task;
018    
019    import java.lang.reflect.Proxy;
020    import java.net.URI;
021    import java.net.URISyntaxException;
022    import javax.servlet.http.HttpServletRequest;
023    import javax.servlet.http.HttpServletResponse;
024    
025    import com.google.appengine.api.taskqueue.Queue;
026    import com.google.appengine.api.taskqueue.TaskOptions;
027    import org.apache.camel.Exchange;
028    import org.apache.camel.Producer;
029    import org.apache.camel.component.gae.bind.HttpBindingInvocationHandler;
030    import org.apache.camel.component.gae.bind.InboundBinding;
031    import org.apache.camel.component.gae.bind.OutboundBinding;
032    import org.apache.camel.component.gae.bind.OutboundBindingSupport;
033    import org.apache.camel.component.http.HttpBinding;
034    import org.apache.camel.component.http.HttpClientConfigurer;
035    import org.apache.camel.component.servlet.ServletComponent;
036    import org.apache.camel.component.servlet.ServletEndpoint;
037    import org.apache.commons.httpclient.HttpConnectionManager;
038    import org.apache.commons.httpclient.params.HttpClientParams;
039    
040    /**
041     * Represents a <a href="http://camel.apache.org/gtask.html">Google App Engine Task Queueing endpoint</a>.
042     */
043    public class GTaskEndpoint extends ServletEndpoint implements OutboundBindingSupport<GTaskEndpoint, TaskOptions, Void> {
044    
045        private OutboundBinding<GTaskEndpoint, TaskOptions, Void> outboundBinding;
046        private InboundBinding<GTaskEndpoint, HttpServletRequest, HttpServletResponse> inboundBinding;
047    
048        private String workerRoot;
049        
050        private Queue queue;
051        
052        public GTaskEndpoint(String endpointUri, ServletComponent component,
053                URI httpUri, HttpClientParams params,
054                HttpConnectionManager httpConnectionManager,
055                HttpClientConfigurer clientConfigurer) throws URISyntaxException {
056            super(endpointUri, component, httpUri, params, httpConnectionManager, clientConfigurer);
057        }
058    
059        public OutboundBinding<GTaskEndpoint, TaskOptions, Void> getOutboundBinding() {
060            return outboundBinding;
061        }
062    
063        public void setOutboundBinding(OutboundBinding<GTaskEndpoint, TaskOptions, Void> outboundBinding) {
064            this.outboundBinding = outboundBinding;
065        }
066        
067        public InboundBinding<GTaskEndpoint, HttpServletRequest, HttpServletResponse> getInboundBinding() {
068            return inboundBinding;
069        }
070    
071        public void setInboundBinding(
072                InboundBinding<GTaskEndpoint, HttpServletRequest, HttpServletResponse> inboundBinding) {
073            this.inboundBinding = inboundBinding;
074        }
075    
076        /**
077         * Proxies the {@link HttpBinding} returned by {@link super#getBinding()}
078         * with a dynamic proxy. The proxy's invocation handler further delegates to
079         * {@link InboundBinding#readRequest(org.apache.camel.Endpoint, Exchange, Object)}
080         * .
081         * 
082         * @return proxied {@link HttpBinding}.
083         */
084        @Override
085        public HttpBinding getBinding() {
086            return (HttpBinding)Proxy.newProxyInstance(getClass().getClassLoader(), new Class[] {HttpBinding.class}, 
087                    new HttpBindingInvocationHandler<GTaskEndpoint, HttpServletRequest, HttpServletResponse>(
088                            this, super.getBinding(), getInboundBinding()));
089        }
090    
091        /**
092         * @see #setWorkerRoot(String)
093         */
094        public String getWorkerRoot() {
095            return workerRoot;
096        }
097    
098        /**
099         * Sets the web hook path root. 
100         *
101         * @param workerRoot
102         *            the assumed web hook path root. The default is
103         *            <code>worker</code>. The servlet handling the callback from
104         *            the task queueing service should have a <code>/worker/*</code>
105         *            servlet mapping in this case. If another servlet mapping is
106         *            used it must be set here accordingly.
107         */
108        public void setWorkerRoot(String workerRoot) {
109            this.workerRoot = workerRoot;
110        }
111    
112        public Queue getQueue() {
113            return queue;
114        }
115        
116        public void setQueue(Queue queue) {
117            this.queue = queue;
118        }
119        
120        public Producer createProducer() throws Exception {
121            return new GTaskProducer(this);
122        }
123    
124    }