001/*
002 * Copyright 2002-2017 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 *      https://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.http.client.support;
018
019import java.net.InetSocketAddress;
020import java.net.Proxy;
021import java.net.SocketAddress;
022
023import org.springframework.beans.factory.FactoryBean;
024import org.springframework.beans.factory.InitializingBean;
025import org.springframework.lang.Nullable;
026import org.springframework.util.Assert;
027
028/**
029 * {@link FactoryBean} that creates a {@link Proxy java.net.Proxy}.
030 *
031 * @author Arjen Poutsma
032 * @since 3.0.4
033 * @see FactoryBean
034 * @see Proxy
035 */
036public class ProxyFactoryBean implements FactoryBean<Proxy>, InitializingBean {
037
038        private Proxy.Type type = Proxy.Type.HTTP;
039
040        @Nullable
041        private String hostname;
042
043        private int port = -1;
044
045        @Nullable
046        private Proxy proxy;
047
048
049        /**
050         * Set the proxy type.
051         * <p>Defaults to {@link java.net.Proxy.Type#HTTP}.
052         */
053        public void setType(Proxy.Type type) {
054                this.type = type;
055        }
056
057        /**
058         * Set the proxy host name.
059         */
060        public void setHostname(String hostname) {
061                this.hostname = hostname;
062        }
063
064        /**
065         * Set the proxy port.
066         */
067        public void setPort(int port) {
068                this.port = port;
069        }
070
071
072        @Override
073        public void afterPropertiesSet() throws IllegalArgumentException {
074                Assert.notNull(this.type, "Property 'type' is required");
075                Assert.notNull(this.hostname, "Property 'hostname' is required");
076                if (this.port < 0 || this.port > 65535) {
077                        throw new IllegalArgumentException("Property 'port' value out of range: " + this.port);
078                }
079
080                SocketAddress socketAddress = new InetSocketAddress(this.hostname, this.port);
081                this.proxy = new Proxy(this.type, socketAddress);
082        }
083
084
085        @Override
086        @Nullable
087        public Proxy getObject() {
088                return this.proxy;
089        }
090
091        @Override
092        public Class<?> getObjectType() {
093                return Proxy.class;
094        }
095
096        @Override
097        public boolean isSingleton() {
098                return true;
099        }
100
101}