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  
28  /**
29   * Server socket factory for FTPS connections.
30   * @since 2.2
31   */
32  public class FTPSServerSocketFactory extends ServerSocketFactory {
33  
34      /** Factory for secure socket factories */
35      private final SSLContext context;
36  
37      public FTPSServerSocketFactory(SSLContext context) {
38          this.context = context;
39      }
40  
41      // Override the default superclass method
42      @Override
43      public ServerSocket createServerSocket() throws IOException {
44          return init(this.context.getServerSocketFactory().createServerSocket());
45      }
46  
47      @Override
48      public ServerSocket createServerSocket(int port) throws IOException {
49          return init(this.context.getServerSocketFactory().createServerSocket(port));
50      }
51  
52      @Override
53      public ServerSocket createServerSocket(int port, int backlog) throws IOException {
54          return init(this.context.getServerSocketFactory().createServerSocket(port, backlog));
55      }
56  
57      @Override
58      public ServerSocket createServerSocket(int port, int backlog, InetAddress ifAddress) throws IOException {
59          return init(this.context.getServerSocketFactory().createServerSocket(port, backlog, ifAddress));
60      }
61  
62      /**
63       * Sets the socket so newly accepted connections will use SSL client mode.
64       *
65       * @param socket the SSLServerSocket to initialise
66       * @return the socket
67       * @throws ClassCastException if socket is not an instance of SSLServerSocket
68       */
69      public ServerSocket init(ServerSocket socket) {
70          ((SSLServerSocket) socket).setUseClientMode(true);
71          return socket;
72      }
73  }
74