Search in sources :

Example 46 with ServerSocket

use of java.net.ServerSocket in project hadoop by apache.

the class TestJettyHelper method createJettyServer.

private Server createJettyServer() {
    try {
        InetAddress localhost = InetAddress.getByName("localhost");
        String host = "localhost";
        ServerSocket ss = new ServerSocket(0, 50, localhost);
        int port = ss.getLocalPort();
        ss.close();
        Server server = new Server();
        ServerConnector conn = new ServerConnector(server);
        HttpConfiguration http_config = new HttpConfiguration();
        http_config.setRequestHeaderSize(JettyUtils.HEADER_SIZE);
        http_config.setResponseHeaderSize(JettyUtils.HEADER_SIZE);
        http_config.setSecureScheme("https");
        http_config.addCustomizer(new SecureRequestCustomizer());
        ConnectionFactory connFactory = new HttpConnectionFactory(http_config);
        conn.addConnectionFactory(connFactory);
        conn.setHost(host);
        conn.setPort(port);
        if (ssl) {
            SslContextFactory sslContextFactory = new SslContextFactory();
            sslContextFactory.setNeedClientAuth(false);
            sslContextFactory.setKeyStorePath(keyStore);
            sslContextFactory.setKeyStoreType(keyStoreType);
            sslContextFactory.setKeyStorePassword(keyStorePassword);
            conn.addFirstConnectionFactory(new SslConnectionFactory(sslContextFactory, HttpVersion.HTTP_1_1.asString()));
        }
        server.addConnector(conn);
        return server;
    } catch (Exception ex) {
        throw new RuntimeException("Could not start embedded servlet container, " + ex.getMessage(), ex);
    }
}
Also used : SecureRequestCustomizer(org.eclipse.jetty.server.SecureRequestCustomizer) Server(org.eclipse.jetty.server.Server) HttpConnectionFactory(org.eclipse.jetty.server.HttpConnectionFactory) ServerSocket(java.net.ServerSocket) HttpConfiguration(org.eclipse.jetty.server.HttpConfiguration) SslConnectionFactory(org.eclipse.jetty.server.SslConnectionFactory) MalformedURLException(java.net.MalformedURLException) UnknownHostException(java.net.UnknownHostException) ServerConnector(org.eclipse.jetty.server.ServerConnector) HttpConnectionFactory(org.eclipse.jetty.server.HttpConnectionFactory) SslConnectionFactory(org.eclipse.jetty.server.SslConnectionFactory) ConnectionFactory(org.eclipse.jetty.server.ConnectionFactory) SslContextFactory(org.eclipse.jetty.util.ssl.SslContextFactory) InetAddress(java.net.InetAddress)

Example 47 with ServerSocket

use of java.net.ServerSocket in project hadoop by apache.

the class TestWebHdfsTimeouts method setUp.

@Before
public void setUp() throws Exception {
    Configuration conf = WebHdfsTestUtil.createConf();
    serverSocket = new ServerSocket(0, CONNECTION_BACKLOG);
    nnHttpAddress = new InetSocketAddress("localhost", serverSocket.getLocalPort());
    conf.set(DFSConfigKeys.DFS_NAMENODE_HTTP_ADDRESS_KEY, "localhost:" + serverSocket.getLocalPort());
    if (timeoutSource == TimeoutSource.Configuration) {
        String v = Integer.toString(SHORT_SOCKET_TIMEOUT) + "ms";
        conf.set(HdfsClientConfigKeys.DFS_WEBHDFS_SOCKET_CONNECT_TIMEOUT_KEY, v);
        conf.set(HdfsClientConfigKeys.DFS_WEBHDFS_SOCKET_READ_TIMEOUT_KEY, v);
    }
    fs = WebHdfsTestUtil.getWebHdfsFileSystem(conf, WebHdfsConstants.WEBHDFS_SCHEME);
    if (timeoutSource == TimeoutSource.ConnectionFactory) {
        fs.connectionFactory = connectionFactory;
    }
    clients = new ArrayList<SocketChannel>();
    serverThread = null;
}
Also used : SocketChannel(java.nio.channels.SocketChannel) Configuration(org.apache.hadoop.conf.Configuration) InetSocketAddress(java.net.InetSocketAddress) ServerSocket(java.net.ServerSocket) Before(org.junit.Before)

Example 48 with ServerSocket

use of java.net.ServerSocket in project hadoop by apache.

the class TestServer method testBind.

@Test
public void testBind() throws Exception {
    Configuration conf = new Configuration();
    ServerSocket socket = new ServerSocket();
    InetSocketAddress address = new InetSocketAddress("0.0.0.0", 0);
    socket.bind(address);
    try {
        int min = socket.getLocalPort();
        int max = min + 100;
        conf.set("TestRange", min + "-" + max);
        ServerSocket socket2 = new ServerSocket();
        InetSocketAddress address2 = new InetSocketAddress("0.0.0.0", 0);
        Server.bind(socket2, address2, 10, conf, "TestRange");
        try {
            assertTrue(socket2.isBound());
            assertTrue(socket2.getLocalPort() > min);
            assertTrue(socket2.getLocalPort() <= max);
        } finally {
            socket2.close();
        }
    } finally {
        socket.close();
    }
}
Also used : Configuration(org.apache.hadoop.conf.Configuration) InetSocketAddress(java.net.InetSocketAddress) ServerSocket(java.net.ServerSocket) Test(org.junit.Test)

Example 49 with ServerSocket

use of java.net.ServerSocket in project hadoop by apache.

the class TestServer method testBindSimple.

@Test
public void testBindSimple() throws Exception {
    ServerSocket socket = new ServerSocket();
    InetSocketAddress address = new InetSocketAddress("0.0.0.0", 0);
    Server.bind(socket, address, 10);
    try {
        assertTrue(socket.isBound());
    } finally {
        socket.close();
    }
}
Also used : InetSocketAddress(java.net.InetSocketAddress) ServerSocket(java.net.ServerSocket) Test(org.junit.Test)

Example 50 with ServerSocket

use of java.net.ServerSocket in project hadoop by apache.

the class TestServer method testEmptyConfig.

@Test
public void testEmptyConfig() throws Exception {
    Configuration conf = new Configuration();
    conf.set("TestRange", "");
    ServerSocket socket = new ServerSocket();
    InetSocketAddress address = new InetSocketAddress("0.0.0.0", 0);
    try {
        Server.bind(socket, address, 10, conf, "TestRange");
        assertTrue(socket.isBound());
    } finally {
        socket.close();
    }
}
Also used : Configuration(org.apache.hadoop.conf.Configuration) InetSocketAddress(java.net.InetSocketAddress) ServerSocket(java.net.ServerSocket) Test(org.junit.Test)

Aggregations

ServerSocket (java.net.ServerSocket)736 IOException (java.io.IOException)336 Socket (java.net.Socket)265 InetSocketAddress (java.net.InetSocketAddress)131 Test (org.junit.Test)118 SocketException (java.net.SocketException)56 InputStream (java.io.InputStream)51 SocketTimeoutException (java.net.SocketTimeoutException)43 OutputStream (java.io.OutputStream)41 InetAddress (java.net.InetAddress)41 BindException (java.net.BindException)28 URL (java.net.URL)28 SSLServerSocket (javax.net.ssl.SSLServerSocket)26 InputStreamReader (java.io.InputStreamReader)24 UnknownHostException (java.net.UnknownHostException)24 File (java.io.File)23 BufferedReader (java.io.BufferedReader)21 SSLSocket (javax.net.ssl.SSLSocket)21 DatagramSocket (java.net.DatagramSocket)20 ServerSocketChannel (java.nio.channels.ServerSocketChannel)16