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.restlet;
018    
019    import org.apache.camel.Exchange;
020    import org.apache.camel.impl.DefaultProducer;
021    import org.apache.commons.logging.Log;
022    import org.apache.commons.logging.LogFactory;
023    import org.restlet.Client;
024    import org.restlet.Context;
025    import org.restlet.data.Request;
026    import org.restlet.data.Response;
027    
028    /**
029     * A Camel producer that acts as a client to Restlet server.
030     *
031     * @version $Revision: 778399 $
032     */
033    public class RestletProducer extends DefaultProducer {
034        private static final Log LOG = LogFactory.getLog(RestletProducer.class);
035        private Client client;
036    
037        public RestletProducer(RestletEndpoint endpoint) throws Exception {
038            super(endpoint);
039            client = new Client(endpoint.getProtocol());
040            client.setContext(new Context());
041        }
042    
043        @Override
044        public void doStart() throws Exception {
045            super.doStart();
046            client.start();
047        }
048        
049        @Override
050        public void doStop() throws Exception {
051            client.stop();
052            super.doStop();
053        }
054        
055        public void process(Exchange exchange) throws Exception {
056            RestletEndpoint endpoint = (RestletEndpoint)getEndpoint();
057            
058            String resourceUri = buildUri(endpoint);
059            Request request = new Request(endpoint.getRestletMethod(), resourceUri);
060    
061            RestletBinding binding = endpoint.getRestletBinding();
062            binding.populateRestletRequestFromExchange(request, exchange);
063            
064            if (LOG.isDebugEnabled()) {
065                LOG.debug("Client sends a request (method: " + request.getMethod() 
066                        + ", uri: " + resourceUri + ")");
067            }
068            
069            Response response = client.handle(request);
070            binding.populateExchangeFromRestletResponse(exchange, response);
071        }
072    
073        private static String buildUri(RestletEndpoint endpoint) {
074            return endpoint.getProtocol() + "://" + endpoint.getHost() + ":" 
075                + endpoint.getPort() + endpoint.getUriPattern();
076        }
077    
078    }