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
018 package org.apache.commons.net;
019
020 import java.io.IOException;
021 import java.net.InetAddress;
022 import java.net.InetSocketAddress;
023 import java.net.Proxy;
024 import java.net.ServerSocket;
025 import java.net.Socket;
026 import java.net.UnknownHostException;
027
028 import javax.net.SocketFactory;
029
030 /***
031 * DefaultSocketFactory implements the SocketFactory interface by
032 * simply wrapping the java.net.Socket and java.net.ServerSocket
033 * constructors. It is the default SocketFactory used by
034 * {@link org.apache.commons.net.SocketClient}
035 * implementations.
036 * <p>
037 * <p>
038 * @see SocketFactory
039 * @see SocketClient
040 * @see SocketClient#setSocketFactory
041 ***/
042
043 public class DefaultSocketFactory extends SocketFactory
044 {
045 /** The proxy to use when creating new sockets. */
046 private final Proxy connProxy;
047
048 /**
049 * The default constructor.
050 */
051 public DefaultSocketFactory()
052 {
053 this(null);
054 }
055
056 /**
057 * A constructor for sockets with proxy support.
058 *
059 * @param proxy The Proxy to use when creating new Sockets.
060 * @since 3.2
061 */
062 public DefaultSocketFactory(Proxy proxy)
063 {
064 connProxy = proxy;
065 }
066
067 /**
068 * Creates an unconnected Socket.
069 *
070 * @return A new unconnected Socket.
071 * @exception IOException If an I/O error occurs while creating the Socket.
072 * @since 3.2
073 */
074 @Override
075 public Socket createSocket() throws IOException
076 {
077 if (connProxy != null)
078 {
079 return new Socket(connProxy);
080 }
081 return new Socket();
082 }
083
084 /***
085 * Creates a Socket connected to the given host and port.
086 * <p>
087 * @param host The hostname to connect to.
088 * @param port The port to connect to.
089 * @return A Socket connected to the given host and port.
090 * @exception UnknownHostException If the hostname cannot be resolved.
091 * @exception IOException If an I/O error occurs while creating the Socket.
092 ***/
093 @Override
094 public Socket createSocket(String host, int port)
095 throws UnknownHostException, IOException
096 {
097 if (connProxy != null)
098 {
099 Socket s = new Socket(connProxy);
100 s.connect(new InetSocketAddress(host, port));
101 return s;
102 }
103 return new Socket(host, port);
104 }
105
106 /***
107 * Creates a Socket connected to the given host and port.
108 * <p>
109 * @param address The address of the host to connect to.
110 * @param port The port to connect to.
111 * @return A Socket connected to the given host and port.
112 * @exception IOException If an I/O error occurs while creating the Socket.
113 ***/
114 @Override
115 public Socket createSocket(InetAddress address, int port)
116 throws IOException
117 {
118 if (connProxy != null)
119 {
120 Socket s = new Socket(connProxy);
121 s.connect(new InetSocketAddress(address, port));
122 return s;
123 }
124 return new Socket(address, port);
125 }
126
127 /***
128 * Creates a Socket connected to the given host and port and
129 * originating from the specified local address and port.
130 * <p>
131 * @param host The hostname to connect to.
132 * @param port The port to connect to.
133 * @param localAddr The local address to use.
134 * @param localPort The local port to use.
135 * @return A Socket connected to the given host and port.
136 * @exception UnknownHostException If the hostname cannot be resolved.
137 * @exception IOException If an I/O error occurs while creating the Socket.
138 ***/
139 @Override
140 public Socket createSocket(String host, int port,
141 InetAddress localAddr, int localPort)
142 throws UnknownHostException, IOException
143 {
144 if (connProxy != null)
145 {
146 Socket s = new Socket(connProxy);
147 s.bind(new InetSocketAddress(localAddr, localPort));
148 s.connect(new InetSocketAddress(host, port));
149 return s;
150 }
151 return new Socket(host, port, localAddr, localPort);
152 }
153
154 /***
155 * Creates a Socket connected to the given host and port and
156 * originating from the specified local address and port.
157 * <p>
158 * @param address The address of the host to connect to.
159 * @param port The port to connect to.
160 * @param localAddr The local address to use.
161 * @param localPort The local port to use.
162 * @return A Socket connected to the given host and port.
163 * @exception IOException If an I/O error occurs while creating the Socket.
164 ***/
165 @Override
166 public Socket createSocket(InetAddress address, int port,
167 InetAddress localAddr, int localPort)
168 throws IOException
169 {
170 if (connProxy != null)
171 {
172 Socket s = new Socket(connProxy);
173 s.bind(new InetSocketAddress(localAddr, localPort));
174 s.connect(new InetSocketAddress(address, port));
175 return s;
176 }
177 return new Socket(address, port, localAddr, localPort);
178 }
179
180 /***
181 * Creates a ServerSocket bound to a specified port. A port
182 * of 0 will create the ServerSocket on a system-determined free port.
183 * <p>
184 * @param port The port on which to listen, or 0 to use any free port.
185 * @return A ServerSocket that will listen on a specified port.
186 * @exception IOException If an I/O error occurs while creating
187 * the ServerSocket.
188 ***/
189 public ServerSocket createServerSocket(int port) throws IOException
190 {
191 return new ServerSocket(port);
192 }
193
194 /***
195 * Creates a ServerSocket bound to a specified port with a given
196 * maximum queue length for incoming connections. A port of 0 will
197 * create the ServerSocket on a system-determined free port.
198 * <p>
199 * @param port The port on which to listen, or 0 to use any free port.
200 * @param backlog The maximum length of the queue for incoming connections.
201 * @return A ServerSocket that will listen on a specified port.
202 * @exception IOException If an I/O error occurs while creating
203 * the ServerSocket.
204 ***/
205 public ServerSocket createServerSocket(int port, int backlog)
206 throws IOException
207 {
208 return new ServerSocket(port, backlog);
209 }
210
211 /***
212 * Creates a ServerSocket bound to a specified port on a given local
213 * address with a given maximum queue length for incoming connections.
214 * A port of 0 will
215 * create the ServerSocket on a system-determined free port.
216 * <p>
217 * @param port The port on which to listen, or 0 to use any free port.
218 * @param backlog The maximum length of the queue for incoming connections.
219 * @param bindAddr The local address to which the ServerSocket should bind.
220 * @return A ServerSocket that will listen on a specified port.
221 * @exception IOException If an I/O error occurs while creating
222 * the ServerSocket.
223 ***/
224 public ServerSocket createServerSocket(int port, int backlog,
225 InetAddress bindAddr)
226 throws IOException
227 {
228 return new ServerSocket(port, backlog, bindAddr);
229 }
230 }