Java Code Examples for javax.net.ServerSocketFactory#createServerSocket()

The following examples show how to use javax.net.ServerSocketFactory#createServerSocket() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DefaultSSLServSocketFac.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
    // reserve the security properties
    String reservedSSFacProvider =
        Security.getProperty("ssl.ServerSocketFactory.provider");

    try {
        Security.setProperty("ssl.ServerSocketFactory.provider", "oops");
        ServerSocketFactory ssocketFactory =
                    SSLServerSocketFactory.getDefault();
        SSLServerSocket sslServerSocket =
                    (SSLServerSocket)ssocketFactory.createServerSocket();
    } catch (Exception e) {
        if (!(e.getCause() instanceof ClassNotFoundException)) {
            throw e;
        }
        // get the expected exception
    } finally {
        // restore the security properties
        if (reservedSSFacProvider == null) {
            reservedSSFacProvider = "";
        }
        Security.setProperty("ssl.ServerSocketFactory.provider",
                                                reservedSSFacProvider);
    }
}
 
Example 2
Source File: DefaultSSLServSocketFac.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
    // reserve the security properties
    String reservedSSFacProvider =
        Security.getProperty("ssl.ServerSocketFactory.provider");

    try {
        Security.setProperty("ssl.ServerSocketFactory.provider", "oops");
        ServerSocketFactory ssocketFactory =
                    SSLServerSocketFactory.getDefault();
        SSLServerSocket sslServerSocket =
                    (SSLServerSocket)ssocketFactory.createServerSocket();
    } catch (Exception e) {
        if (!(e.getCause() instanceof ClassNotFoundException)) {
            throw e;
        }
        // get the expected exception
    } finally {
        // restore the security properties
        if (reservedSSFacProvider == null) {
            reservedSSFacProvider = "";
        }
        Security.setProperty("ssl.ServerSocketFactory.provider",
                                                reservedSSFacProvider);
    }
}
 
Example 3
Source File: DefaultSSLServSocketFac.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
    // reserve the security properties
    String reservedSSFacProvider =
        Security.getProperty("ssl.ServerSocketFactory.provider");

    try {
        Security.setProperty("ssl.ServerSocketFactory.provider", "oops");
        ServerSocketFactory ssocketFactory =
                    SSLServerSocketFactory.getDefault();
        SSLServerSocket sslServerSocket =
                    (SSLServerSocket)ssocketFactory.createServerSocket();
    } catch (Exception e) {
        if (!(e.getCause() instanceof ClassNotFoundException)) {
            throw e;
        }
        // get the expected exception
    } finally {
        // restore the security properties
        if (reservedSSFacProvider == null) {
            reservedSSFacProvider = "";
        }
        Security.setProperty("ssl.ServerSocketFactory.provider",
                                                reservedSSFacProvider);
    }
}
 
Example 4
Source File: DefaultSSLServSocketFac.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
    // reserve the security properties
    String reservedSSFacProvider =
        Security.getProperty("ssl.ServerSocketFactory.provider");

    try {
        Security.setProperty("ssl.ServerSocketFactory.provider", "oops");
        ServerSocketFactory ssocketFactory =
                    SSLServerSocketFactory.getDefault();
        SSLServerSocket sslServerSocket =
                    (SSLServerSocket)ssocketFactory.createServerSocket();
    } catch (Exception e) {
        if (!(e.getCause() instanceof ClassNotFoundException)) {
            throw e;
        }
        // get the expected exception
    } finally {
        // restore the security properties
        if (reservedSSFacProvider == null) {
            reservedSSFacProvider = "";
        }
        Security.setProperty("ssl.ServerSocketFactory.provider",
                                                reservedSSFacProvider);
    }
}
 
Example 5
Source File: DefaultSSLServSocketFac.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
    // reserve the security properties
    String reservedSSFacProvider =
        Security.getProperty("ssl.ServerSocketFactory.provider");

    try {
        Security.setProperty("ssl.ServerSocketFactory.provider", "oops");
        ServerSocketFactory ssocketFactory =
                    SSLServerSocketFactory.getDefault();
        SSLServerSocket sslServerSocket =
                    (SSLServerSocket)ssocketFactory.createServerSocket();
    } catch (Exception e) {
        if (!(e.getCause() instanceof ClassNotFoundException)) {
            throw e;
        }
        // get the expected exception
    } finally {
        // restore the security properties
        if (reservedSSFacProvider == null) {
            reservedSSFacProvider = "";
        }
        Security.setProperty("ssl.ServerSocketFactory.provider",
                                                reservedSSFacProvider);
    }
}
 
Example 6
Source File: DefaultSSLServSocketFac.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
    // reserve the security properties
    String reservedSSFacProvider =
        Security.getProperty("ssl.ServerSocketFactory.provider");

    try {
        Security.setProperty("ssl.ServerSocketFactory.provider", "oops");
        ServerSocketFactory ssocketFactory =
                    SSLServerSocketFactory.getDefault();
        SSLServerSocket sslServerSocket =
                    (SSLServerSocket)ssocketFactory.createServerSocket();
    } catch (Exception e) {
        if (!(e.getCause() instanceof ClassNotFoundException)) {
            throw e;
        }
        // get the expected exception
    } finally {
        // restore the security properties
        if (reservedSSFacProvider == null) {
            reservedSSFacProvider = "";
        }
        Security.setProperty("ssl.ServerSocketFactory.provider",
                                                reservedSSFacProvider);
    }
}
 
Example 7
Source File: DefaultSSLServSocketFac.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
    // reserve the security properties
    String reservedSSFacProvider =
        Security.getProperty("ssl.ServerSocketFactory.provider");

    try {
        Security.setProperty("ssl.ServerSocketFactory.provider", "oops");
        ServerSocketFactory ssocketFactory =
                    SSLServerSocketFactory.getDefault();
        SSLServerSocket sslServerSocket =
                    (SSLServerSocket)ssocketFactory.createServerSocket();
    } catch (Exception e) {
        if (!(e.getCause() instanceof ClassNotFoundException)) {
            throw e;
        }
        // get the expected exception
    } finally {
        // restore the security properties
        if (reservedSSFacProvider == null) {
            reservedSSFacProvider = "";
        }
        Security.setProperty("ssl.ServerSocketFactory.provider",
                                                reservedSSFacProvider);
    }
}
 
Example 8
Source File: DefaultSSLServSocketFac.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
    // reserve the security properties
    String reservedSSFacProvider =
        Security.getProperty("ssl.ServerSocketFactory.provider");

    try {
        Security.setProperty("ssl.ServerSocketFactory.provider", "oops");
        ServerSocketFactory ssocketFactory =
                    SSLServerSocketFactory.getDefault();
        SSLServerSocket sslServerSocket =
                    (SSLServerSocket)ssocketFactory.createServerSocket();
    } catch (Exception e) {
        if (!(e.getCause() instanceof ClassNotFoundException)) {
            throw e;
        }
        // get the expected exception
    } finally {
        // restore the security properties
        if (reservedSSFacProvider == null) {
            reservedSSFacProvider = "";
        }
        Security.setProperty("ssl.ServerSocketFactory.provider",
                                                reservedSSFacProvider);
    }
}
 
Example 9
Source File: NetworkServerControlImpl.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
 * Create the right kind of server socket
 */

private ServerSocket createServerSocket()
	throws IOException
{
	if (hostAddress == null)
		hostAddress = InetAddress.getByName(hostArg);
	// Make a list of valid
	// InetAddresses for NetworkServerControl
	// admin commands.
	buildLocalAddressList(hostAddress);
										
	// Create the right kind of socket
	switch (getSSLMode()) {
	case SSL_OFF:
	default:
		ServerSocketFactory sf =
			ServerSocketFactory.getDefault();
		return sf.createServerSocket(portNumber
									 ,0,
									 hostAddress);
	case SSL_BASIC:
		SSLServerSocketFactory ssf =
			(SSLServerSocketFactory)SSLServerSocketFactory.getDefault();
		return (SSLServerSocket)ssf.createServerSocket(portNumber,
													   0,
													   hostAddress);
	case SSL_PEER_AUTHENTICATION:
		SSLServerSocketFactory ssf2 =
			(SSLServerSocketFactory)SSLServerSocketFactory.getDefault();
		SSLServerSocket sss2= 
			(SSLServerSocket)ssf2.createServerSocket(portNumber,
													 0,
													 hostAddress);
		sss2.setNeedClientAuth(true);
		return sss2;
	}
}
 
Example 10
Source File: NetworkServerControlImpl.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
 * Create the right kind of server socket
 */

private ServerSocket createServerSocket()
	throws IOException
{
	if (hostAddress == null)
		hostAddress = InetAddress.getByName(hostArg);
	// Make a list of valid
	// InetAddresses for NetworkServerControl
	// admin commands.
	buildLocalAddressList(hostAddress);
										
	// Create the right kind of socket
	switch (getSSLMode()) {
	case SSL_OFF:
	default:
		ServerSocketFactory sf =
			ServerSocketFactory.getDefault();
		return sf.createServerSocket(portNumber
									 ,0,
									 hostAddress);
	case SSL_BASIC:
		SSLServerSocketFactory ssf =
			(SSLServerSocketFactory)SSLServerSocketFactory.getDefault();
		return (SSLServerSocket)ssf.createServerSocket(portNumber,
													   0,
													   hostAddress);
	case SSL_PEER_AUTHENTICATION:
		SSLServerSocketFactory ssf2 =
			(SSLServerSocketFactory)SSLServerSocketFactory.getDefault();
		SSLServerSocket sss2= 
			(SSLServerSocket)ssf2.createServerSocket(portNumber,
													 0,
													 hostAddress);
		sss2.setNeedClientAuth(true);
		return sss2;
	}
}
 
Example 11
Source File: InputSocket.java    From ambari-logsearch with Apache License 2.0 5 votes vote down vote up
@Override
public void start() throws Exception {
  logger.info("Starting socket server (port: {}, protocol: {}, secure: {})", port, protocol, secure);
  ServerSocketFactory socketFactory = secure ? SSLServerSocketFactory.getDefault() : ServerSocketFactory.getDefault();
  InputSocketMarker inputSocketMarker = new InputSocketMarker(this, port, protocol, secure, log4j);
  LogsearchConversion loggerConverter = new LogsearchConversion();

  try {
    serverSocket = socketFactory.createServerSocket(port);
    while (!isDrain()) {
      Socket socket = serverSocket.accept();
      if (log4j) {
        try (ObjectInputStream ois = new ObjectInputStream(new BufferedInputStream(socket.getInputStream()))) {
          LoggingEvent loggingEvent = (LoggingEvent) ois.readObject();
          String jsonStr = loggerConverter.createOutput(loggingEvent);
          logger.trace("Incoming socket logging event: " + jsonStr);
          outputLine(jsonStr, inputSocketMarker);
        }
      } else {
        try (BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));) {
          String line = in.readLine();
          logger.trace("Incoming socket message: " + line);
          outputLine(line, inputSocketMarker);
        }
      }
    }
  } catch (SocketException socketEx) {
    logger.warn("{}", socketEx.getMessage());
  } finally {
    serverSocket.close();
  }
}
 
Example 12
Source File: MonitoringServer.java    From gsn with GNU General Public License v3.0 5 votes vote down vote up
/**
    * Return statistics about the GSN server, faking an HTTP server for retro-compatibility
    * 
    * the protocol is similar to the carbon protocol used by Graphite, except the timestamp
    * http://matt.aimonetti.net/posts/2013/06/26/practical-guide-to-graphite-monitoring/
    */
public MonitoringServer(int port) {
	super("Monitoring Server");

	try {
		ServerSocketFactory serverSocketFactory = ServerSocketFactory.getDefault();
           socket = serverSocketFactory.createServerSocket(port);
           socket.setSoTimeout(1000);
	}catch(Exception e){
		logger.error("unable to open socket for monitoring",e);
	}
}
 
Example 13
Source File: SSLServerSocketConnectionFactory.java    From jsmpp with Apache License 2.0 4 votes vote down vote up
public ServerConnection listen(int port, int timeout) throws IOException {
  ServerSocketFactory serverSocketFactory = SSLServerSocketFactory.getDefault();
  ServerSocket serverSocket = serverSocketFactory.createServerSocket(port);
  serverSocket.setSoTimeout(timeout);
  return new ServerSocketConnection(serverSocket);
}
 
Example 14
Source File: AbstractServer.java    From davmail with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Bind server socket on defined port.
 *
 * @throws DavMailException unable to create server socket
 */
public void bind() throws DavMailException {
    String bindAddress = Settings.getProperty("davmail.bindAddress");
    String keystoreFile = Settings.getProperty("davmail.ssl.keystoreFile");

    ServerSocketFactory serverSocketFactory;
    if (keystoreFile == null || keystoreFile.length() == 0 || nosslFlag) {
        serverSocketFactory = ServerSocketFactory.getDefault();
    } else {
        try {

            // SSLContext is environment for implementing JSSE...
            // create ServerSocketFactory
            SSLContext sslContext = SSLContext.getInstance("TLS");

            // initialize sslContext to work with key managers
            sslContext.init(getKeyManagers(), getTrustManagers(), null);

            // create ServerSocketFactory from sslContext
            serverSocketFactory = sslContext.getServerSocketFactory();
        } catch (IOException | GeneralSecurityException ex) {
            throw new DavMailException("LOG_EXCEPTION_CREATING_SSL_SERVER_SOCKET", getProtocolName(), port, ex.getMessage() == null ? ex.toString() : ex.getMessage());
        }
    }
    try {
        // create the server socket
        if (bindAddress == null || bindAddress.length() == 0) {
            serverSocket = serverSocketFactory.createServerSocket(port);
        } else {
            serverSocket = serverSocketFactory.createServerSocket(port, 0, Inet4Address.getByName(bindAddress));
        }
        if (serverSocket instanceof SSLServerSocket) {
            // CVE-2014-3566 disable SSLv3
            HashSet<String> protocols = new HashSet<>();
            for (String protocol : ((SSLServerSocket) serverSocket).getEnabledProtocols()) {
                if (!protocol.startsWith("SSL")) {
                    protocols.add(protocol);
                }
            }
            ((SSLServerSocket) serverSocket).setEnabledProtocols(protocols.toArray(new String[0]));
            ((SSLServerSocket) serverSocket).setNeedClientAuth(Settings.getBooleanProperty("davmail.ssl.needClientAuth", false));
        }

    } catch (IOException e) {
        throw new DavMailException("LOG_SOCKET_BIND_FAILED", getProtocolName(), port);
    }
}
 
Example 15
Source File: ServerSocketFactoryTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
public final void test_createServerSocket() throws Exception {
    ServerSocketFactory sf = ServerSocketFactory.getDefault();
    ServerSocket ss = sf.createServerSocket();
    assertNotNull(ss);
    ss.close();
}
 
Example 16
Source File: SocketCreator.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
public ServerSocket createServerSocket(int nport, int backlog,
      InetAddress bindAddr, GFLogWriter log, int socketBufferSize)
      throws IOException {
    //       rw.readLock().lockInterruptibly();
//       try {
        printConfig( log );
        if ( this.useSSL ) {
          if (this.sslContext == null) {
            throw new GemFireConfigException("SSL not configured correctly, Please look at previous error");
          }
          ServerSocketFactory ssf = this.sslContext.getServerSocketFactory();
          SSLServerSocket serverSocket = (SSLServerSocket)ssf.createServerSocket();
          serverSocket.setReuseAddress(true);
          // If necessary, set the receive buffer size before binding the socket so
          // that large buffers will be allocated on accepted sockets (see
          // java.net.ServerSocket.setReceiverBufferSize javadocs)
          if (socketBufferSize != -1) {
            serverSocket.setReceiveBufferSize(socketBufferSize);
          }
          serverSocket.bind(new InetSocketAddress(bindAddr, nport), backlog);
          finishServerSocket(serverSocket);
          return serverSocket;
        } 
        else {
          //log.info("Opening server socket on " + nport, new Exception("SocketCreation"));
          ServerSocket result = new ServerSocket();
          result.setReuseAddress(true);
          // If necessary, set the receive buffer size before binding the socket so
          // that large buffers will be allocated on accepted sockets (see
          // java.net.ServerSocket.setReceiverBufferSize javadocs)
          if (socketBufferSize != -1) {
            result.setReceiveBufferSize(socketBufferSize);
          }
          try {
            result.bind(new InetSocketAddress(bindAddr, nport), backlog);
          }
          catch (BindException e) {
            BindException throwMe = new BindException(LocalizedStrings.SocketCreator_FAILED_TO_CREATE_SERVER_SOCKET_ON_0_1.toLocalizedString(new Object[] {bindAddr, Integer.valueOf(nport)}));
            throwMe.initCause(e);
            throw throwMe;
          }
          return result;
        }
//       }
//       finally {
//         rw.readLock().unlock();
//       }
  }
 
Example 17
Source File: SSLServerSocketConnectionFactory.java    From jsmpp with Apache License 2.0 4 votes vote down vote up
public ServerConnection listen(int port) throws IOException {
  ServerSocketFactory serverSocketFactory = SSLServerSocketFactory.getDefault();
  ServerSocket serverSocket = serverSocketFactory.createServerSocket(port);
  return new ServerSocketConnection(serverSocket);
}
 
Example 18
Source File: SocketCreator.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
public ServerSocket createServerSocket(int nport, int backlog,
      InetAddress bindAddr, GFLogWriter log, int socketBufferSize)
      throws IOException {
    //       rw.readLock().lockInterruptibly();
//       try {
        printConfig( log );
        if ( this.useSSL ) {
          if (this.sslContext == null) {
            throw new GemFireConfigException("SSL not configured correctly, Please look at previous error");
          }
          ServerSocketFactory ssf = this.sslContext.getServerSocketFactory();
          SSLServerSocket serverSocket = (SSLServerSocket)ssf.createServerSocket();
          serverSocket.setReuseAddress(true);
          // If necessary, set the receive buffer size before binding the socket so
          // that large buffers will be allocated on accepted sockets (see
          // java.net.ServerSocket.setReceiverBufferSize javadocs)
          if (socketBufferSize != -1) {
            serverSocket.setReceiveBufferSize(socketBufferSize);
          }
          serverSocket.bind(new InetSocketAddress(bindAddr, nport), backlog);
          finishServerSocket(serverSocket);
          return serverSocket;
        } 
        else {
          //log.info("Opening server socket on " + nport, new Exception("SocketCreation"));
          ServerSocket result = new ServerSocket();
          result.setReuseAddress(true);
          // If necessary, set the receive buffer size before binding the socket so
          // that large buffers will be allocated on accepted sockets (see
          // java.net.ServerSocket.setReceiverBufferSize javadocs)
          if (socketBufferSize != -1) {
            result.setReceiveBufferSize(socketBufferSize);
          }
          try {
            result.bind(new InetSocketAddress(bindAddr, nport), backlog);
          }
          catch (BindException e) {
            BindException throwMe = new BindException(LocalizedStrings.SocketCreator_FAILED_TO_CREATE_SERVER_SOCKET_ON_0_1.toLocalizedString(new Object[] {bindAddr, Integer.valueOf(nport)}));
            throwMe.initCause(e);
            throw throwMe;
          }
          return result;
        }
//       }
//       finally {
//         rw.readLock().unlock();
//       }
  }
 
Example 19
Source File: Https.java    From PacketProxy with Apache License 2.0 4 votes vote down vote up
public static ServerSocket createServerSSLSocket(int listen_port, String commonName, CA ca) throws Exception {
	SSLContext sslContext = createSSLContext(commonName, ca);
	ServerSocketFactory ssf = sslContext.getServerSocketFactory();
	return (SSLServerSocket) ssf.createServerSocket(listen_port);
}
 
Example 20
Source File: TelnetTerminalServer.java    From lanterna with GNU Lesser General Public License v3.0 2 votes vote down vote up
/**
 * Creates a new TelnetTerminalServer on a specific port through a ServerSocketFactory with a certain Charset
 * @param serverSocketFactory ServerSocketFactory to use when creating the ServerSocket
 * @param port Port to listen for incoming telnet connections
 * @param charset Character set to use
 * @throws IOException If there was an underlying I/O exception
 */
public TelnetTerminalServer(ServerSocketFactory serverSocketFactory, int port, Charset charset) throws IOException {
    this.serverSocket = serverSocketFactory.createServerSocket(port);
    this.charset = charset;
}