In order to provide some API stability and to simplify common use, the Apache libraries have been wrapped. The wrapping code is in the ucar.httpservices package. The new public classes are: HTTPFactory.java, HTTPFormBuilder.java, HTTPSession.java, HTTPMethod.java, HTTPMethodStream.java, and HTTPException.java. Additionally, the class ucar.nc2.util.net.HttpClientManager is closely tied to the ucar.httpservices package.
This document describes some of the methods for each class. For a complete description of the API, the javadoc files should be referenced.
Note that when the code was switched to using httpclient version 4.5, the API was changed somewhat to reflect experience with previous versions. Some methods exist to support Backward compatibility, but are deprecated.
The primary key for an HTTPsession object is an instance of AuthScope, which consists of a host (an ip address or name such as unidata.ucar.edu), a port number, and a scheme (e.g. "http" or "https").
Effectively, an AuthScope is intended to map to a specific server somewhere in the internet. Authentication information (credentials) are tied to that scope and all http methods (put, get, etc.) operate in the context of an AuthScope.
Notes:
The non authentication related global methods of general interest are as follows.
static public void setGlobalUserAgent(String userAgent)
static public void setGlobalCompression(String compressors)As a rule, the only allowable compressors are "gzip" and "deflate".
static public void setMaxRedirects(int n)
static public void setFollowRedirects(boolean tf)
public void close()HTTPSession is AutoCloseable so try-with-resources can be used.
public void setCompression(String compressors)
public void removeCompression()
public void setUserAgent(String agent)
public void setMaxRedirects(int n)
public void setFollowRedirects(boolean tf)
public void clearCookies()
public ListgetCookies()
static public void setGlobalCredentialsProvider(String url, CredentialsProvider provider) throws HTTPExceptionThis is no longer supported because only the host+port part of the URL would be used and that was provided at session creation.
static public void setGlobalCredentials(String url, Credentials creds) throws HTTPExceptionSame reason as immediate prior case.
public void setCredentials(String url, Credentials creds) throws HTTPExceptionSame reason as immediate prior case.
public void setCredentialsProvider(String url, CredentialsProvider provider) throws HTTPExceptionSame reason as immediate prior case.
To see this, consider the case where we want to access a number of datasets on a server, where each dataset might be protected by some combination of username+password. This is often called BASIC authentication. When a request is made for one of these datasets, the CredentialsProvider is invoked and it may do something like pop up a window to ask the user for the needed username+password for this dataset. When another dataset is accessed, it may pop up another window. Thus the same CredentialsProvider may be used repeatedly. In each case, it would return a specific instance of Credentials that contains the requisite username+password for that specific dataset.
HTTPSession supports credentials caching so that if the client repeatedly accesses the same dataset, it will be asked for the username+password only once.
As a rule, it is best to use the methods here that take CredentialsProvider argument. The case of a Credentials argument is only for convenience when only a specific set of credentials is known a-priori.
static public void setGlobalCredentialsProvider(CredentialsProvider provider) throws HTTPException
static public void setGlobalCredentials(Credentials creds) throws HTTPException
public void setCredentialsProvider(CredentialsProvider provider) throws HTTPException
public void setCredentials(Credentials creds) throws HTTPException
static public void setGlobalSSLAuth(String keypath, String keypassword, String trustpath, String trustpassword)
For this case, the client must provide access to a password protected "keystore" in which the client certificate is stored. The first two parameters of the above method specify these.
In addition, the client may wish to use a so-called trust store in which the set of certificates for trusted servers is stored. the last two arguments specify these.
The above method is not strictly necessary since the relevant values can be specified on the java VM command line using the following JVM parameters.
static public void setGlobalProxy(String proxyurl)
Notes:
-D Flag | Description |
---|---|
tds.http.conntimeout | Connection timeout in seconds |
tds.http.sotimeout | Socket timeout in seconds |
An instance of HTTPMethod always operates in the context of a parent HTTPSession object. It is possible to create an HTTPMethod object without specifying a parent session object. In that case, a temporary HTTPSession object is created by the method object. The lifetime of the session object is the same as the method object: that is, when the method object is closed, the temporary session object will also be closed.
HTTPMethod takes a URL string as its primary argument. Optionally (but normally) , it may take an HTTPSession object as an additional argument.
An important point about the URL argument is that it must be
compatible with the AuthScope of the parent HTTPSession
object. The term "compatible" means the following.
The method url is
For session creation, the following methods are provided:
For method creattion, the supported methods are: GET, HEAD, PUT, POST,
and OPTIONS. For each method Xxx, the following factory methods are provided.
This class contains only static methods and these methods
perform the following utility operations.
HTTPMethodStream
This class is a subclass of java.io.InputStream. Its purpose
is to allow other classes to access the data stream associated with a
method response. It is often convenient to get data using an
HTTPMethod instance in a Java scope, but then pass the
associated input stream out of scope. When the stream is closed (or hits
EOF) somewhere else in the code, it is desirable that the associated
method and its resources be closed automatically.
HTTPException
This class is a subclass of java.io.IOException.
It is the exception for reporting errors out of the
ucar.httpservices package.
HTTPFactory
The HTTPFactory class provides the API for obtaining
instances of an HTTPSession or HTTPMethod object.
The idea is that the argument(s) are sufficient to extract a usable
host+port+scheme. The
AuthScope
argument uses a class specific to the Apache httpclient (core) library.
static public HTTPSession newSession(String host, int port) throws HTTPException
static public HTTPSession newSession(String url) throws HTTPException
static public HTTPSession newSession(AuthScope target) throws HTTPException
As described above, if the session is not explicit, then a temporary session
object will be created with the AuthScope taken from the url.
Also, if the session is explicit, then the url argument must be compatible
with the session's AuthScope.
static public HTTPMethod Xxx(HTTPSession session, String url) throws HTTPException
static public HTTPMethod Xxx(String url) throws HTTPException
ucar.nc2.util.net.HttpClientManager
This class is a utility class to perform some specialized
actions. Its name begins with "Http" rather than
"HTTP" for historical reasons. It has been extensively modified
internally to utilize the ucar.httpservices package.
String getUrlContentsAsString(String urlencoded, int maxKbytes)
Make a request on the specified url and return its response
as a String of max length maxKbytes.
void copyUrlContentsToFile(String urlencoded, File file) throws HTTPException
Make a request on the specified url, obtain its contents
and write to the specified file.
long appendUrlContentsToFile(String url, File file, long start, long end)
Make a request on the specified url, obtain its contents
and append to the specified file.
void init(CredentialsProvider provider, String userAgent)
Initialize to use a specified credentials provider and user agent
when making requests.
Examples
Example 1: Create/Use/Release Cycle
This example uses try-with-resources instead
of explicit calls to the close methods.
public class Main
{
public static void main(String[] argv)
{
String url = argv[0];
try (HTTPSession session = new HTTPSession(url);
HTTPMethod method = HTTPMethod.Get(session,url);
int status = method.execute();
System.out.printf("Execute: status code = %d\n", status);
} // implicitly calls method.close() then session.close()
}
}
Example 2: Using HttpClientManager
public class Main
{
public static void main(String[] argv)
{
String url = argv[0[];
string content = HttpClientManager.getUrlContentsAsString(url,1024);
}
}
Example 3: Setting Some Global Parameters
This example set some global parameters and also shows
the use of an implicit session.
public class Main
{
public static void main(String[] argv)
{
String url = argv[0];
String userpwd = argv[1];
// Set some parameters at the global level
HTTPSession.setGlobalCredentials(new UsernamePasswordCredentials(userpwd));
HTTPSession.setGlobalUserAgent("netcdf/java");
HTTPSession.setMaxConnections(4);
HTTPSession.setGlobalAuthenticationPreemptive(true);
try (HTTPMethod method = HTTPMethod.Get(url)) {//implicit session
int status = method.execute();
System.out.printf("Execute: status code = %d\n", status);
}
}
}
Example 4: Setting Some Local Parameters
public class Main
{
public static void main(String[] argv)
{
String url = argv[0];
String userpwd = argv[1];
try (HTTPSession session = new HTTPSession(url)) {
session.setCredentials(new UsernamePasswordCredentials(userpwd));
session.setUserAgent("agent");
session.setConnectionManagerTimeout(475);
session.setSoTimeout(475);
try (HTTPMethod method = HTTPMethod.Get(session)) {
int status = method.execute();
System.out.printf("Execute: status code = %d\n", status);
} // method.close()
} // session.close()
}
}
Appendices
Appendix A: AuthScope
The
AuthScope
class is used in a number of places in the ucar.httpservices
package. It has a number of constructors, the primary ones
are as follows.
If the scheme is not explicit then the default scheme (usually "http").
The realm argument should always be AuthScope.ANY_REALM.
AuthScope(String hostname, int port)
AuthScope(String hostname, int port, String realm, String scheme)
Author
Author: Dennis Heimbigner
Affiliation: UCAR/Unidata
email: dmh@ucar.edu