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.http4;
018    
019    import org.apache.http.HttpHost;
020    import org.apache.http.auth.AuthScope;
021    import org.apache.http.auth.Credentials;
022    import org.apache.http.auth.NTCredentials;
023    import org.apache.http.auth.UsernamePasswordCredentials;
024    import org.apache.http.client.HttpClient;
025    import org.apache.http.conn.params.ConnRoutePNames;
026    import org.apache.http.impl.client.DefaultHttpClient;
027    
028    /**
029     * Strategy for configuring the HttpClient with a proxy
030     */
031    public class ProxyHttpClientConfigurer implements HttpClientConfigurer {
032    
033        private final String host;
034        private final Integer port;
035    
036        private final String username;
037        private final String password;
038        private final String domain;
039        private final String ntHost;
040    
041        public ProxyHttpClientConfigurer(String host, Integer port) {
042            this(host, port, null, null, null, null);
043        }
044    
045        public ProxyHttpClientConfigurer(String host, Integer port, String username, String password, String domain, String ntHost) {
046            this.host = host;
047            this.port = port;
048            this.username = username;
049            this.password = password;
050            this.domain = domain;
051            this.ntHost = ntHost;
052        }
053    
054        public void configureHttpClient(HttpClient client) {
055            client.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, new HttpHost(host, port));
056    
057            if (username != null && password != null) {
058                Credentials defaultcreds;
059                if (domain != null) {
060                    defaultcreds = new NTCredentials(username, password, ntHost, domain);
061                } else {
062                    defaultcreds = new UsernamePasswordCredentials(username, password);
063                }
064                ((DefaultHttpClient) client).getCredentialsProvider().setCredentials(AuthScope.ANY, defaultcreds);
065            }
066        }
067    }