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.http;
018    
019    import com.google.appengine.api.urlfetch.HTTPRequest;
020    import com.google.appengine.api.urlfetch.HTTPResponse;
021    import com.google.appengine.api.urlfetch.URLFetchService;
022    
023    import org.apache.camel.Exchange;
024    import org.apache.camel.component.gae.bind.OutboundBinding;
025    import org.apache.camel.impl.DefaultProducer;
026    
027    public class GHttpProducer extends DefaultProducer {
028    
029        public GHttpProducer(GHttpEndpoint endpoint) {
030            super(endpoint);
031        }
032        
033        @Override
034        public GHttpEndpoint getEndpoint() {
035            return (GHttpEndpoint)super.getEndpoint();
036        }
037        
038        public OutboundBinding<GHttpEndpoint, HTTPRequest, HTTPResponse> getOutboundBinding() {
039            return getEndpoint().getOutboundBinding();
040        }
041        
042        public URLFetchService getUrlFetchService() {
043            return getEndpoint().getUrlFetchService();
044        }
045    
046        /**
047         * Invokes the URL fetch service.
048         * 
049         * @param exchange
050         *            contains the request data in the in-message. The result is
051         *            written to the out-message.
052         * @throws GHttpException
053         *             if the response code is >= 400 and
054         *             {@link GHttpEndpoint#isThrowExceptionOnFailure()} returns
055         *             <code>true</code>.
056         * @see GHttpBinding
057         */
058        public void process(Exchange exchange) throws Exception {
059            HTTPRequest request = getOutboundBinding().writeRequest(getEndpoint(), exchange, null);
060            HTTPResponse response = getUrlFetchService().fetch(request);
061            getOutboundBinding().readResponse(getEndpoint(), exchange, response);
062        }
063    
064    }