net package

The net package contains some network classes like HttpUrlConnection which extends the java.net.HttpUrlConnection.

The HttpProxyConnection design

The class HttpProxyConnection is a simple facade to the HTTP network classes. It wrapps the cookie handling. The idea of this class is to have a simpler API to a HTTP connection. The user of this class has not to care about the whole communication. The method send sends a HTTP request to the initialized url. The method returns the raw request without the HTTP header. The class holds internal a CookieManager.

The following features are supported:

  • Cookie handling
  • automatic compression and decompression (method setCompressed(boolean))
  • Support of a timeout by sending a HTTP-connection
  • encoding support

To have HTTPS support a subclasses of the class HttpProxyConnection have to overwrite the method getNewConnection which returns a new connection.

There is a simple pesudo code example:

    protected java.net.HttpURLConnection getNewConnection( URL theUrl, 
                                                           LogInformation logInfo,
                                                           int socketTimeout ) 
    {        
        if( theUrl.getProtocol().equals( "https" ) )
        {
            HttpsURLConnection theConnection = new HttpsURLConnection( getLogInformation(), theUrl, trustDecider );

            if( socketTimeout>0 )
                theConnection.setSocketTimeout( socketTimeout );
            return theConnection;           
        }

        return super.getNewConnection( theUrl, logInfo, socketTimeout );
    }    

The class diagramm shows the classes from the net package for HTTP connection:




URL-List

The URLListConfiguration implements the functionality to configure more than one url source. The class checks which of the connection is available. The number of failures can be defined.

The class HighAvailableURLListConfiguration contains additional a list with currently not available URL's. Each time an available URL is requested the class HighAvailableURLListConfiguration checks if the URL is still available. A thread checks in a defined interval the currently not available URL list.


Example of the URLListConfiguration

    Properties prop = new Properties();
    prop.put( "server.url1", "http://www.sun.com" );
    prop.put( "server.url2", "http://www.ibm.com" );
    prop.put( "server.url3", "http://aaa" );        
    prop.put( "server.url4", "http://www.amazon.com" );
    prop.put( "server.url5", "http://bbb" );
    prop.put( "server.url6", "http://www.amazon.de" );
    prop.put( "server.url7", "cc.fsd.dfs.fsd" );
    int numberOfFailures = 2;
    URLListConfiguration urlList = new URLListConfiguration( null, prop, numberOfFailures );
    
    assertEquals( urlList.getAvailableServerURL().toString(), "http://www.sun.com" );
    assertEquals( urlList.getAvailableServerURL().toString(), "http://www.ibm.com" );
    
    if( numberOfFailures==-1 || numberOfFailures>=1 )        
        assertEquals( urlList.getAvailableServerURL().toString(), "http://www.amazon.com" );

    if( numberOfFailures==-1 || numberOfFailures>=2 )
        assertEquals( urlList.getAvailableServerURL().toString(), "http://www.amazon.de" );

    assertEquals( urlList.getAvailableServerURL().toString(), "http://www.sun.com" );
    assertEquals( urlList.getAvailableServerURL().toString(), "http://www.ibm.com" );


Client/Server

This library also contains a simple framework to implement a simple client/server communication. As default a server can extends the class AbstractServer. A client extends the AbstractClient class: