001/*
002 * Copyright 2012-2016 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.boot.web.client;
018
019import org.apache.http.HttpException;
020import org.apache.http.HttpHost;
021import org.apache.http.HttpRequest;
022import org.apache.http.client.HttpClient;
023import org.apache.http.impl.client.HttpClientBuilder;
024import org.apache.http.impl.conn.DefaultProxyRoutePlanner;
025import org.apache.http.protocol.HttpContext;
026
027import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
028import org.springframework.web.client.RestTemplate;
029
030/**
031 * Example configuration for using a {@link RestTemplateCustomizer} to configure a proxy.
032 *
033 * @author Andy Wilkinson
034 */
035public class RestTemplateProxyCustomizationExample {
036
037        /**
038         * A {@link RestTemplateCustomizer} that applies an HttpComponents-based request
039         * factory that is configured to use a proxy.
040         */
041        // tag::customizer[]
042        static class ProxyCustomizer implements RestTemplateCustomizer {
043
044                @Override
045                public void customize(RestTemplate restTemplate) {
046                        HttpHost proxy = new HttpHost("proxy.example.com");
047                        HttpClient httpClient = HttpClientBuilder.create()
048                                        .setRoutePlanner(new DefaultProxyRoutePlanner(proxy) {
049
050                                                @Override
051                                                public HttpHost determineProxy(HttpHost target,
052                                                                HttpRequest request, HttpContext context)
053                                                                                throws HttpException {
054                                                        if (target.getHostName().equals("192.168.0.5")) {
055                                                                return null;
056                                                        }
057                                                        return super.determineProxy(target, request, context);
058                                                }
059
060                                        }).build();
061                        restTemplate.setRequestFactory(
062                                        new HttpComponentsClientHttpRequestFactory(httpClient));
063                }
064
065        }
066        // end::customizer[]
067
068}