View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.net;
19  
20  import java.io.IOException;
21  import java.net.InetAddress;
22  import java.net.InetSocketAddress;
23  import java.net.Proxy;
24  import java.net.ServerSocket;
25  import java.net.Socket;
26  import java.net.UnknownHostException;
27  
28  import javax.net.SocketFactory;
29  
30  /***
31   * DefaultSocketFactory implements the SocketFactory interface by
32   * simply wrapping the java.net.Socket and java.net.ServerSocket
33   * constructors.  It is the default SocketFactory used by
34   * {@link org.apache.commons.net.SocketClient}
35   * implementations.
36   * <p>
37   * <p>
38   * @see SocketFactory
39   * @see SocketClient
40   * @see SocketClient#setSocketFactory
41   ***/
42  
43  public class DefaultSocketFactory extends SocketFactory
44  {
45      /** The proxy to use when creating new sockets. */
46      private final Proxy connProxy;
47  
48      /**
49       * The default constructor.
50       */
51      public DefaultSocketFactory()
52      {
53          this(null);
54      }
55  
56      /**
57       * A constructor for sockets with proxy support.
58       *
59       * @param proxy The Proxy to use when creating new Sockets.
60       * @since 3.2
61       */
62      public DefaultSocketFactory(Proxy proxy)
63      {
64          connProxy = proxy;
65      }
66  
67      /**
68       * Creates an unconnected Socket.
69       *
70       * @return A new unconnected Socket.
71       * @exception IOException If an I/O error occurs while creating the Socket.
72       * @since 3.2
73       */
74      @Override
75      public Socket createSocket() throws IOException
76      {
77          if (connProxy != null)
78          {
79              return new Socket(connProxy);
80          }
81          return new Socket();
82      }
83  
84      /***
85       * Creates a Socket connected to the given host and port.
86       * <p>
87       * @param host The hostname to connect to.
88       * @param port The port to connect to.
89       * @return A Socket connected to the given host and port.
90       * @exception UnknownHostException  If the hostname cannot be resolved.
91       * @exception IOException If an I/O error occurs while creating the Socket.
92       ***/
93      @Override
94      public Socket createSocket(String host, int port)
95      throws UnknownHostException, IOException
96      {
97          if (connProxy != null)
98          {
99              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 }