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