What is Ferris Net

This is the Ferris Net project. It contains network utilities plus classes which enhance or extend the capabilities of the objects already in java.net.

org.ferris.net.runner.HttpRequestRunner

HttpRequestRunner is the primary utility. This class models the QueryRunner in Jakarta commons DBUtils. The purpose of this class is to take the effort out of making GET or POST requests to a web server and processing the results. The example below shows how to make a request to a web server and return the results as a list of String objects. Of course, the response can be parsed in any way necessary. The response can be processed as a list of User objects or even a list of PDF objects - it all depends on what the web server returns. The API is designed to always return a list so if your request will produce only 1 result (i.e. a single User object, etc.) you still need to put that 1 object in a list then get the first element of the list. When creating the HttpRequestRunner object, credentials for username/password can be specified for BASIC authentication. If security is needed, just use the HTTPS version HttpsRequestRunner.

Example

// Create a runner which will make requests to the "www.company.com" server.
// using the standard HTTP port of 80.  The request will be parsed into a 
// list of Strings.

HttpRequestRunner<String> 
    runner = new HttpRequestRunner<String>(new AuthScope("www.company.com",80), null);
   
 
// Add values for first name, last name, and upload a file when the request is made.
// Do not worry about escaping special characters.  HttpRequestRunner will
// use URLEncoder to make sure special characters are escaped properly.

runner.addParameter("Dideric");
runner.addParameter("van der Waal");
runner.addParameter(new File("/home/dideric/pictures/MyPicture.jpg"));


// Make a POST to the "/a/resource.asp" resource of the "www.company.com" 
// server.  
//
// The "?" after "resource.asp" is the standard "?" needed as if you 
// were constructing an HTTP GET and wanted to put in parameters. This "?" is 
// required to have the string formatted properly and is used for making either
// a GET or a POST.  Think of this "?" as the word "values" in an SQL
// insert statement (insert into TABLE values ('str',1); ).  The same way
// the word "values" is required for a properly formatted SQL statement, this
// "?" is required for a properly formatted HttpRequestRunner statement.  
//
// The "?" after "first_name", "last_name" and "my_picture_file" are value
// placeholders.  Think of them exactly the same way as when you prepare an
// SQL string for the PreparedStatement object.  The "?" will be replaced with 
// the values specified by the addParameter(..) method above.  There is no
// index parameter to the addParameter(..) method so make sure you counting is 
// correct.
// 
// StringHttpResponseHandler is responsible for processing the request 
// when it comes back. See below for the implementation of  StringHttpResponseHandler.

List<String> letters = runner.post(
      "/a/resource.asp?first_name=?&last_name=?&my_picture_file=?"
     , new StringHttpResponseHandler()
);
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.io.IOUtils;
import org.ferris.net.runner.HttpResponseHandler;

public class StringHttpResponseHandler implements HttpResponseHandler<String>
{
    public List<String> response(InputStream response) {
        try {
            List l = IOUtils.readLines(response, "UTF-8");
            List<String> ls = new ArrayList<String>(l.size());
            for (Iterator itr = l.iterator(); itr.hasNext();) {
                ls.add((String)itr.next());
            }           
            return ls;
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            IOUtils.closeQuietly(response);
        }
    }
}

Proxy without Authentication

If you need to connect through a proxy, specify the proxy using the standard Java proxy system properties.

I've yet to get it working with an NTLM proxy yet so if you know how, contact me.

Command Line

-Dhttp.proxyHost=www.proxy.com -Dhttp.proxyPort=8282

Code

System.setProperty("http.proxyHost", "www.proxy.com");        
System.setProperty("http.proxyPort","8282");

Proxy with Authentication

Proxy authentication support is still experimental. There are 2 public static class members you can define which will set credentials for the proxy. Again, this is experimental so contact me if you can code in a better solution.

public class HttpRequestRunner 
{
    public static AuthScope proxyAuthScope;
    public static Credentials proxyCredentials;
...
}