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.ahc;
018    
019    import java.net.URI;
020    import java.util.Map;
021    
022    import com.ning.http.client.AsyncHttpClient;
023    import com.ning.http.client.AsyncHttpClientConfig;
024    import org.apache.camel.Endpoint;
025    import org.apache.camel.impl.HeaderFilterStrategyComponent;
026    import org.apache.camel.util.CastUtils;
027    import org.apache.camel.util.URISupport;
028    import org.apache.camel.util.UnsafeUriCharactersEncoder;
029    
030    /**
031     *  Defines the <a href="http://camel.apache.org/ahc.html">Async HTTP Client Component</a>
032     */
033    public class AhcComponent extends HeaderFilterStrategyComponent {
034    
035        private AsyncHttpClient client;
036        private AsyncHttpClientConfig clientConfig;
037        private AhcBinding binding;
038    
039        @Override
040        protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object> parameters) throws Exception {
041            String addressUri = remaining;
042    
043            AhcEndpoint endpoint = new AhcEndpoint(uri, this, null);
044            setEndpointHeaderFilterStrategy(endpoint);
045            endpoint.setClient(getClient());
046            endpoint.setClientConfig(getClientConfig());
047            endpoint.setBinding(getBinding());
048            setProperties(endpoint, parameters);
049    
050            // restructure uri to be based on the parameters left as we dont want to include the Camel internal options
051            addressUri = UnsafeUriCharactersEncoder.encode(addressUri);
052            URI httpUri = URISupport.createRemainingURI(new URI(addressUri), CastUtils.cast(parameters));
053            endpoint.setHttpUri(httpUri);
054    
055            return endpoint;
056        }
057    
058        public AsyncHttpClient getClient() {
059            return client;
060        }
061    
062        public void setClient(AsyncHttpClient client) {
063            this.client = client;
064        }
065    
066        public AhcBinding getBinding() {
067            if (binding == null) {
068                binding = new DefaultAhcBinding();
069            }
070            return binding;
071        }
072    
073        public void setBinding(AhcBinding binding) {
074            this.binding = binding;
075        }
076    
077        public AsyncHttpClientConfig getClientConfig() {
078            return clientConfig;
079        }
080    
081        public void setClientConfig(AsyncHttpClientConfig clientConfig) {
082            this.clientConfig = clientConfig;
083        }
084    }