Example usage for javax.net SocketFactory getDefault

List of usage examples for javax.net SocketFactory getDefault

Introduction

In this page you can find the example usage for javax.net SocketFactory getDefault.

Prototype

public static SocketFactory getDefault() 

Source Link

Document

Returns a copy of the environment's default socket factory.

Usage

From source file:net.javacrumbs.mocksocket.SampleTest.java

@Test
public void testConditionalAddress() throws Exception {
    byte[] mockData = new byte[] { 1, 2, 3, 4 };
    expectCall().andWhenRequest(address(is("example.org:1234"))).thenReturn(data(mockData));

    Socket socket = SocketFactory.getDefault().createSocket("example.org", 1234);
    byte[] data = IOUtils.toByteArray(socket.getInputStream());
    socket.close();/*from   ww w .j  a v a2 s . c  om*/
    assertThat(data, is(mockData));
}

From source file:net.NetUtils.java

/**
 * Get the default socket factory as specified by the configuration
 * parameter <tt>hadoop.rpc.socket.factory.default</tt>
 * //from ww  w .j av  a  2  s.  c om
 * @param conf the configuration
 * @return the default socket factory as specified in the configuration or
 *         the JVM default socket factory if the configuration does not
 *         contain a default socket factory property.
 */
public static SocketFactory getDefaultSocketFactory(Configuration conf) {

    String propValue = conf.get("hadoop.rpc.socket.factory.class.default");
    if ((propValue == null) || (propValue.length() == 0))
        return SocketFactory.getDefault();

    return getSocketFactoryFromProperty(conf, propValue);
}

From source file:cz.jirutka.spring.unboundid.LdapConnectionFactoryBean.java

public LDAPConnection getObject() throws GeneralSecurityException, LDAPException {
    SocketFactory socketFactory;/*from   w w  w  .j  ava2 s.co  m*/

    if (ssl && sslTrustAll) {
        sslTrustManager = new TrustAllTrustManager();
    }

    if (sslTrustManager != null) {
        socketFactory = new SSLUtil(sslTrustManager).createSSLSocketFactory();
    } else if (ssl) {
        socketFactory = SSLSocketFactory.getDefault();
    } else {
        socketFactory = SocketFactory.getDefault();
    }

    connection = new LDAPConnection(socketFactory, host, port);

    if (hasText(bindDN) && hasText(password)) {
        connection.bind(bindDN, password);
    }

    return connection;
}

From source file:net.javacrumbs.mocksocket.SampleTest.java

@Test
public void testConditionalData() throws Exception {
    byte[] dataToWrite = new byte[] { 5, 4, 3, 2 };
    byte[] mockData = new byte[] { 1, 2, 3, 4 };
    expectCall().andWhenRequest(data(is(dataToWrite))).thenReturn(data(mockData));

    Socket socket = SocketFactory.getDefault().createSocket("example.org", 1234);
    IOUtils.write(dataToWrite, socket.getOutputStream());
    byte[] data = IOUtils.toByteArray(socket.getInputStream());
    socket.close();/*from   www  .j  a v  a  2 s  .  c om*/
    assertThat(data, is(mockData));
}

From source file:net.javacrumbs.mocksocket.SampleTest.java

@Test
public void testRequest() throws Exception {
    byte[] dataToWrite = new byte[] { 5, 4, 3, 2 };
    expectCall().andReturn(emptyResponse());

    Socket socket = SocketFactory.getDefault().createSocket("example.org", 1234);
    IOUtils.write(dataToWrite, socket.getOutputStream());
    socket.close();/*from   w  ww . j  a  v a2s  .  co  m*/
    assertThat(recordedConnections().get(0), data(is(dataToWrite)));
    assertThat(recordedConnections().get(0), address(is("example.org:1234")));
}

From source file:com.buaa.cfs.utils.NetUtils.java

/**
 * Get the default socket factory as specified by the configuration parameter <tt>hadoop.rpc.socket.factory.default</tt>
 *
 * @param conf the configuration/*from www.j a v  a  2 s  .co  m*/
 *
 * @return the default socket factory as specified in the configuration or the JVM default socket factory if the
 * configuration does not contain a default socket factory property.
 */
public static SocketFactory getDefaultSocketFactory(Configuration conf) {

    String propValue = conf.get(CommonConfigurationKeysPublic.HADOOP_RPC_SOCKET_FACTORY_CLASS_DEFAULT_KEY,
            CommonConfigurationKeysPublic.HADOOP_RPC_SOCKET_FACTORY_CLASS_DEFAULT_DEFAULT);
    if ((propValue == null) || (propValue.length() == 0))
        return SocketFactory.getDefault();

    return getSocketFactoryFromProperty(conf, propValue);
}

From source file:greensopinion.restexample.test.web.WebApplicationContainer.java

private boolean testAvailablePort(int port) {
    try {/*  w  w  w . ja va 2s  .  c o  m*/
        Socket socket = SocketFactory.getDefault().createSocket();
        try {
            socket.bind(new InetSocketAddress("localhost", port));
            return true;
        } catch (IOException e) {
            return false;
        } finally {
            socket.close();
        }
    } catch (IOException e) {
        throw new IllegalStateException(e);
    }
}

From source file:greensopinion.restexample.test.web.WebApplicationContainer.java

private boolean testForSuccessfulStartup() {
    // test to see if the web container is listening on the address/port combo
    try {//from w w  w . jav a 2s .  com
        Socket socket = SocketFactory.getDefault().createSocket("localhost", port);
        socket.close();
        return true;
    } catch (UnknownHostException e) {
        throw new IllegalStateException(e);
    } catch (IOException e) {
        return false;
    }
}

From source file:com.twosigma.beakerx.kernel.MagicKernelManager.java

private void initClientServer() {
    this.clientServer = new ClientServer(port, GatewayServer.defaultAddress(), pythonPort,
            GatewayServer.defaultAddress(), GatewayServer.DEFAULT_CONNECT_TIMEOUT,
            GatewayServer.DEFAULT_READ_TIMEOUT, ServerSocketFactory.getDefault(), SocketFactory.getDefault(),
            null);//from  ww  w.  j  av  a 2 s  . c om
}

From source file:com.leanplum.internal.WebSocketClient.java

public void connect() {
    if (mThread != null && mThread.isAlive()) {
        return;//from   ww w  . j a  v  a  2s.c  o m
    }

    mThread = new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                int port = (mURI.getPort() != -1) ? mURI.getPort()
                        : (mURI.getScheme().equals("wss") ? 443 : 80);

                String path = TextUtils.isEmpty(mURI.getPath()) ? "/" : mURI.getPath();
                if (!TextUtils.isEmpty(mURI.getQuery())) {
                    path += "?" + mURI.getQuery();
                }

                String originScheme = mURI.getScheme().equals("wss") ? "https" : "http";
                URI origin = null;
                try {
                    origin = new URI(originScheme, "//" + mURI.getHost(), null);
                } catch (URISyntaxException e) {
                    Util.handleException(e);
                }

                SocketFactory factory;
                try {
                    factory = mURI.getScheme().equals("wss") ? getSSLSocketFactory()
                            : SocketFactory.getDefault();
                } catch (GeneralSecurityException e) {
                    Util.handleException(e);
                    return;
                }
                try {
                    mSocket = factory.createSocket(mURI.getHost(), port);
                } catch (IOException e) {
                    Util.handleException(e);
                }

                PrintWriter out = new PrintWriter(mSocket.getOutputStream());
                out.print("GET " + path + " HTTP/1.1\r\n");
                out.print("Upgrade: websocket\r\n");
                out.print("Connection: Upgrade\r\n");
                out.print("Host: " + mURI.getHost() + "\r\n");
                out.print("Origin: " + (origin != null ? origin.toString() : "unknown") + "\r\n");
                out.print("Sec-WebSocket-Key: " + createSecret() + "\r\n");
                out.print("Sec-WebSocket-Version: 13\r\n");
                if (mExtraHeaders != null) {
                    for (NameValuePair pair : mExtraHeaders) {
                        out.print(String.format("%s: %s\r\n", pair.getName(), pair.getValue()));
                    }
                }
                out.print("\r\n");
                out.flush();

                HybiParser.HappyDataInputStream stream = new HybiParser.HappyDataInputStream(
                        mSocket.getInputStream());

                // Read HTTP response status line.

                StatusLine statusLine = parseStatusLine(readLine(stream));

                if (statusLine == null) {
                    throw new HttpException("Received no reply from server.");
                } else if (statusLine.getStatusCode() != HttpStatus.SC_SWITCHING_PROTOCOLS) {
                    throw new HttpResponseException(statusLine.getStatusCode(), statusLine.getReasonPhrase());
                }

                // Read HTTP response headers.
                String line;
                while (!TextUtils.isEmpty(line = readLine(stream))) {
                    Header header = parseHeader(line);
                    if (header.getName().equals("Sec-WebSocket-Accept")) {
                        // FIXME: Verify the response...
                    }
                }

                mListener.onConnect();

                // Now decode websocket frames.
                mParser.start(stream);

            } catch (EOFException ex) {
                Log.d("WebSocket EOF!", ex);
                mListener.onDisconnect(0, "EOF");

            } catch (SSLException ex) {
                // Connection reset by peer
                Log.d("Websocket SSL error!", ex);
                mListener.onDisconnect(0, "SSL");

            } catch (Exception e) {
                mListener.onError(e);
            }
        }
    });
    mThread.start();
}