Menu

Netlib URL Adapter

Tobias Boese

Netlib URL Adapter

Abstract: The Netlib URL Adapter is used to use java.net.URL on top of every Netlib NetLayer implementation.

Introduction

The URL Adapter is a [Netlib adapter] to use java.net.URL on top of every NetLayer implementation.

Adapter to java.net.URL to redirect some outgoing URL connections

Usage

  • select the NetSocket implementation that should be used:

        // classic:   TcpipNetLayer with NetLayerIDs.TCPIP (--> HTTP over plain TCP/IP);
    // anonymous: TorNetLayer with NetLayerIDs.TOR (--> HTTP over TCP/IP over Tor network):
    NetLayer lowerNetLayer = NetFactory.getInstance().getNetLayerById(NetLayerIDs.TOR);

  • only if anonymous communication over Tor is selected: it makes sense to wait until the Tor startup is finished - optional:

        // wait until TOR is ready (optional):
    lowerNetLayer.waitUntilReady();

  • create a NetlibURLStreamHandlerFactory on to of the selected NetSocket:

        // prepare URL handling on top of the lowerNetLayer
    NetlibURLStreamHandlerFactory factory = new NetlibURLStreamHandlerFactory(false);
    // the following method could be called multiple times
    // to change layer used by the factory over the time:
    factory.setNetLayerForHttpHttpsFtp(lowerNetLayer);

  • create a java.net.URL object using the factory - works for "https" instead of "http", too:

        // create the suitable URL object
    String urlStr = "http://httptest.silvertunnel.org/httptest/bigtest.jsp?id=example2";
    URLStreamHandler handler = factory.createURLStreamHandler("http");
    URL context = null;
    URL url = new URL(context, urlStr, handler);

  • use the java.net.URL object to send the request and receive the response - this is not silvertunnel Netlib specific - read JDK docs to find out alternative ways:

        // send request with POST data
    URLConnection urlConnection = url.openConnection();
    urlConnection.setDoOutput(true);
    urlConnection.setDoInput(true);
    urlConnection.connect();
    OutputStream requestBodyOS = urlConnection.getOutputStream();
    requestBodyOS.write(...);
    requestBodyOS.close();

    // receive the response
    InputStream responseBodyIS = urlConnection.getInputStream();
    responseBodyIS.read(...);
    responseBodyIS.close();
    

Adapter to java.net.URL to redirect all outgoing URL connections of a JVM

Introduction

This section describes the mode of operation of org.silvertunnel.netlib.adapter.socket.URLGlobalUtil.
URLGlobalUtil can be used to redirect all outgoing URL connections of a JVM over a NetLayer? implementation of your choice.

Going this way has side effects to code in the same JVM. Therefore, do it only if really needed.

Usage

  • select the NetSocket implementation that should be used:

        // classic:   TcpipNetLayer with NetLayerIDs.TCPIP (--> HTTP over plain TCP/IP);
    // anonymous: TorNetLayer with NetLayerIDs.TOR (--> HTTP over TCP/IP over Tor network):
    NetLayer lowerNetLayer = NetFactory.getInstance().getNetLayerById(NetLayerIDs.TOR);

  • only if anonymous communication over Tor is selected: it makes sense to wait until the Tor startup is finished - optional:

        // wait until TOR is ready (optional):
    lowerNetLayer.waitUntilReady();

  • create a global redirection:

        // redirect URL handling (JVM global)
    URLGlobalUtil.initURLStreamHandlerFactory();
    // the following method could be called multiple times
    // to change layer used by the global factory over the time:
    URLGlobalUtil.setNetLayerUsedByURLStreamHandlerFactory(lowerNetLayer);

  • create a java.net.URL object - this is not silvertunnel Netlib specific - works for "https" instead of "http", too:

        // create a URL object
    String urlStr = "http://httptest.silvertunnel.org/httptest/bigtest.jsp?id=example3";
    URL url = new URL(urlStr);

  • use the java.net.URL object to send the request and receive the response - this is not silvertunnel Netlib specific - read JDK docs to find out alternative ways:

        // send request with POSTing data
    URLConnection urlConnection = url.openConnection();
    urlConnection.setDoOutput(true);
    urlConnection.setDoInput(true);
    urlConnection.connect();
    OutputStream requestBodyOS = urlConnection.getOutputStream();
    requestBodyOS.write(...);
    requestBodyOS.close();

    // receive the response
    InputStream responseBodyIS = urlConnection.getInputStream();
    responseBodyIS.read(...);
    responseBodyIS.close();
    

More


Top: [Netlib], Up: [Netlib Adapters], Next: [Netlib Name Service Adapter]


Related

Wiki: Netlib Adapters
Wiki: Netlib Best Practices
Wiki: Netlib HTTP
Wiki: Netlib Java Adapter
Wiki: Netlib Name Service Adapter
Wiki: Netlib NetLayer
Wiki: Netlib Socket Adapter Implementation Details
Wiki: Netlib

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.