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.ftp;
19  
20  import java.io.IOException;
21  import java.net.InetAddress;
22  import java.net.ServerSocket;
23  
24  import javax.net.ServerSocketFactory;
25  import javax.net.ssl.SSLContext;
26  import javax.net.ssl.SSLServerSocket;
27  import javax.net.ssl.SSLServerSocketFactory;
28  
29  /**
30   * Server socket factory for FTPS connections.
31   *
32   * @since 2.2
33   */
34  public class FTPSServerSocketFactory extends ServerSocketFactory {
35  
36      /** Factory for secure socket factories */
37      private final SSLContext sslContext;
38  
39      /**
40       * Constructs a new instance for the given SSL context.
41       *
42       * @param sslContext The SSL context.
43       */
44      public FTPSServerSocketFactory(final SSLContext sslContext) {
45          this.sslContext = sslContext;
46      }
47  
48      @SuppressWarnings("resource") // Factory method.
49      @Override
50      public ServerSocket createServerSocket() throws IOException {
51          return init(getServerSocketFactory().createServerSocket());
52      }
53  
54      @SuppressWarnings("resource") // Factory method.
55      @Override
56      public ServerSocket createServerSocket(final int port) throws IOException {
57          return init(getServerSocketFactory().createServerSocket(port));
58      }
59  
60      @SuppressWarnings("resource") // Factory method.
61      @Override
62      public ServerSocket createServerSocket(final int port, final int backlog) throws IOException {
63          return init(getServerSocketFactory().createServerSocket(port, backlog));
64      }
65  
66      @SuppressWarnings("resource") // Factory method.
67      @Override
68      public ServerSocket createServerSocket(final int port, final int backlog, final InetAddress ifAddress) throws IOException {
69          return init(getServerSocketFactory().createServerSocket(port, backlog, ifAddress));
70      }
71  
72      private SSLServerSocketFactory getServerSocketFactory() {
73          return this.sslContext.getServerSocketFactory();
74      }
75  
76      /**
77       * Sets the socket so newly accepted connections will use SSL client mode.
78       *
79       * @param socket the SSLServerSocket to initialize
80       * @return the socket
81       * @throws ClassCastException if socket is not an instance of SSLServerSocket
82       */
83      public ServerSocket init(final ServerSocket socket) {
84          ((SSLServerSocket) socket).setUseClientMode(true);
85          return socket;
86      }
87  }