@ThreadSafe public class HTTPSession extends Object implements Closeable
A Session encapsulate a number of other objects:
As a rule, if the client gives an HTTPSession object to the "create method"
procedures of HTTPFactory (e.g. HTTPFactory.Get or HTTPFactory.Post)
then that creation call must specify a url that is "compatible" with the
scope of the session. The method url is
If the HTTPFactory method creation call does not specify a session
object, then one is created (and destroyed) behind the scenes
along with the method.
Note that the term legalurl in the following code means that the url has
reserved characters within identifiers in escaped form. This is
particularly an issue for queries. Especially square brackets
(e.g. ?x[0:5]) are an issue. Recently (2018) Apache Tomcat stopped
accepting square brackets (and maybe other characters) as ok
when left unencoded. So, now we need to be aware of this
and cause queries encoding to include square brackets.
As of the move to Apache Httpclient 4.4 and later, the underlying
HttpClient objects are generally immutable. This means that at least
this class (HTTPSession) and the HTTPMethod class must store the
relevant info and create the HttpClient and HttpMethod objects
dynamically. This also means that when a parameter is changed (Agent,
for example), any existing cached HttpClient must be thrown away and
reconstructed using the change. As a rule, the HttpClient object will be
created at the last minute so that multiple parameter changes can be
effected without have to re-create the HttpClient for each parameter
change. Also note that the immutable objects will be cached and reused
if no parameters are changed.
Authorization
We assume that the session supports two CredentialsProvider instances:
one global to all HTTPSession objects and one specific to each
HTTPSession object. The global one is used unless a local one was specified.
As an aside, authentication is a bit tricky because some
authorization schemes use redirection. That is, the initial request
is made to server X, but X says: goto to server Y" to get, say, and
authorization token. Then Y says: return to X with this token and
proceed.
SSL
TBD.
Notes:
1. Setting of credentials via HTTPSession is no longer supported.
Instead, the user must use CredentialsProvider.setCredentials
2. CredentialsProviders are all assumed to be caching (see
the code for BasicCredentialsProvider for the canonical case.
This has consequences for proxy because an attempt
will be made to insert them into the chosen CredentialsProvider.
3. The new class HTTPAuthCache is used instead of BasicAuthCache
so that when authorization fails, the AuthCache.remove method
is forwarded to the CredentialsProvider.clear or the
HTTPCredentialsProvider.remove method.
| Modifier and Type | Class and Description |
|---|---|
static class |
HTTPSession.Methods |
| Modifier and Type | Field and Description |
|---|---|
static String |
ACCEPT_ENCODING |
protected static ucar.httpservices.HTTPSession.AuthControls |
authcontrols |
protected org.apache.http.client.config.RequestConfig |
cachedconfig |
protected boolean |
cachevalid |
protected static org.apache.http.HttpResponseInterceptor |
CEKILL |
protected boolean |
closed |
protected static ucar.httpservices.HTTPConnections |
connmgr |
protected static Map<String,org.apache.http.client.entity.InputStreamFactory> |
contentDecoderMap |
protected static List<org.apache.http.HttpRequestInterceptor> |
dbgreq |
protected static List<org.apache.http.HttpResponseInterceptor> |
dbgrsp |
static String |
HEADER_USERAGENT |
protected String |
identifier |
protected Map<ucar.httpservices.HTTPSession.Prop,Object> |
localsettings |
static org.slf4j.Logger |
log |
protected ConcurrentSkipListSet<HTTPMethod> |
methods |
protected URI |
requestURI |
protected org.apache.http.auth.AuthScope |
scope |
protected URI |
scopeURI |
protected HTTPAuthCache |
sessioncache |
protected org.apache.http.client.protocol.HttpClientContext |
sessioncontext |
protected org.apache.http.client.CredentialsProvider |
sessionprovider |
protected String |
sessionURI |
static boolean |
TESTING |
protected static boolean |
USEPOOL
Determine wether to use a Pooling connection manager
or to manage a bunch of individual connections.
|
| Modifier | Constructor and Description |
|---|---|
protected |
HTTPSession() |
| Modifier and Type | Method and Description |
|---|---|
protected static KeyStore |
buildkeystore(String keypath,
String keypassword) |
protected static String |
checkCompressors(String compressors) |
HTTPSession |
clearCookies() |
static void |
clearkeystore() |
protected void |
clearProvider()
This is used by HTTPMethod to sclear the credentials provider.
|
void |
clearState()
Deprecated.
|
void |
close()
Close the session.
|
static HTTPUtil.InterceptRequest |
debugRequestInterceptor() |
static HTTPUtil.InterceptResponse |
debugResponseInterceptor() |
org.apache.http.auth.AuthScope |
getAuthScope() |
List<org.apache.http.cookie.Cookie> |
getCookies() |
protected static int |
getDPropInt(String key) |
static int |
getGlobalMaxConnection() |
static Object |
getGlobalSetting(String key) |
static String |
getGlobalUserAgent() |
int |
getMethodcount() |
static int |
getRetryCount()
Deprecated.
|
String |
getSessionID()
Extract the sessionid cookie value
|
String |
getSessionURI() |
String |
getSessionURL()
Deprecated.
|
Object |
getSetting(String key) |
protected void |
init(org.apache.http.auth.AuthScope scope,
String actualurl) |
boolean |
isClosed() |
Map<ucar.httpservices.HTTPSession.Prop,Object> |
mergedSettings() |
static void |
rebuildkeystore(String path,
String pwd) |
static void |
removeGlobalCompression() |
static void |
resetInterceptors() |
protected void |
setAuthenticationAndProxy(org.apache.http.impl.client.HttpClientBuilder cb)
Handle authentication and Proxy'ing
|
HTTPSession |
setConnectionTimeout(int timeout) |
protected void |
setCredentials(URI uri)
This is used by HTTPMethod to set the in-url name+pwd into
the credentials provider, if defined.
|
HTTPSession |
setCredentialsProvider(org.apache.http.auth.AuthScope scope,
org.apache.http.client.CredentialsProvider provider)
Deprecated.
|
HTTPSession |
setCredentialsProvider(org.apache.http.client.CredentialsProvider provider) |
HTTPSession |
setCredentialsProvider(String url,
org.apache.http.client.CredentialsProvider provider)
Deprecated.
|
protected static void |
setDefaults(Map<ucar.httpservices.HTTPSession.Prop,Object> props) |
HTTPSession |
setFollowRedirects(boolean tf)
Enable/disable redirection following
Default is yes.
|
static void |
setGlobalCompression()
Deprecated.
|
static void |
setGlobalCompression(String compressors) |
static void |
setGlobalConnectionTimeout(int timeout) |
static void |
setGlobalCredentialsProvider(org.apache.http.auth.AuthScope scope,
org.apache.http.client.CredentialsProvider provider)
Deprecated.
|
static void |
setGlobalCredentialsProvider(org.apache.http.client.CredentialsProvider provider) |
static void |
setGlobalCredentialsProvider(org.apache.http.client.CredentialsProvider provider,
String scheme)
Deprecated.
|
static void |
setGlobalCredentialsProvider(String url,
org.apache.http.client.CredentialsProvider provider)
Deprecated.
|
static void |
setGlobalFollowRedirects(boolean tf)
Enable/disable redirection following
Default is yes.
|
static void |
setGlobalMaxConnections(int n) |
static void |
setGlobalMaxRedirects(int n)
Set the max number of redirects to follow
|
static void |
setGlobalProxy(String host,
int port)
Deprecated.
|
static void |
setGlobalRetryCount(int n) |
static void |
setGlobalSoTimeout(int timeout) |
static void |
setGlobalUserAgent(String userAgent) |
static void |
setInterceptors(boolean print) |
protected static void |
setInterceptors(org.apache.http.impl.client.HttpClientBuilder cb) |
HTTPSession |
setMaxRedirects(int n)
Set the max number of redirects to follow
|
void |
setProxy(String host,
int port)
Deprecated.
|
HTTPSession |
setSoTimeout(int timeout) |
HTTPSession |
setUserAgent(String agent) |
HTTPSession |
setUseSessions(boolean tf)
Should we use sessionid's?
|
protected static void |
track(HTTPSession session) |
static void |
validatestate() |
public static boolean TESTING
protected static final boolean USEPOOL
public static final String HEADER_USERAGENT
public static final String ACCEPT_ENCODING
public static final org.slf4j.Logger log
protected static org.apache.http.HttpResponseInterceptor CEKILL
protected static List<org.apache.http.HttpRequestInterceptor> dbgreq
protected static List<org.apache.http.HttpResponseInterceptor> dbgrsp
protected static ucar.httpservices.HTTPConnections connmgr
protected static Map<String,org.apache.http.client.entity.InputStreamFactory> contentDecoderMap
protected static ucar.httpservices.HTTPSession.AuthControls authcontrols
protected String sessionURI
protected URI scopeURI
protected org.apache.http.auth.AuthScope scope
protected boolean closed
protected org.apache.http.client.CredentialsProvider sessionprovider
protected ConcurrentSkipListSet<HTTPMethod> methods
protected String identifier
protected org.apache.http.client.protocol.HttpClientContext sessioncontext
protected HTTPAuthCache sessioncache
protected URI requestURI
protected boolean cachevalid
protected org.apache.http.client.config.RequestConfig cachedconfig
protected HTTPSession()
throws HTTPException
HTTPExceptionprotected static int getDPropInt(String key)
public static void setGlobalUserAgent(String userAgent)
public static String getGlobalUserAgent()
public static void setGlobalMaxConnections(int n)
public static int getGlobalMaxConnection()
public static void setGlobalConnectionTimeout(int timeout)
public static void setGlobalSoTimeout(int timeout)
public static void setGlobalFollowRedirects(boolean tf)
public static void setGlobalMaxRedirects(int n)
n - public static void setGlobalCompression(String compressors)
public static void removeGlobalCompression()
public static void setGlobalCredentialsProvider(org.apache.http.client.CredentialsProvider provider)
throws HTTPException
provider - the credentials providerHTTPExceptionpublic static void setGlobalRetryCount(int n)
protected void init(org.apache.http.auth.AuthScope scope,
String actualurl)
throws HTTPException
HTTPExceptionprotected static void setInterceptors(org.apache.http.impl.client.HttpClientBuilder cb)
public org.apache.http.auth.AuthScope getAuthScope()
public String getSessionURI()
public String getSessionID()
public HTTPSession setUserAgent(String agent)
public HTTPSession setSoTimeout(int timeout)
public HTTPSession setConnectionTimeout(int timeout)
public HTTPSession setMaxRedirects(int n)
n - public HTTPSession setFollowRedirects(boolean tf)
public HTTPSession setUseSessions(boolean tf)
tf - public List<org.apache.http.cookie.Cookie> getCookies()
public HTTPSession clearCookies()
public void close()
close in interface Closeableclose in interface AutoCloseablepublic HTTPSession setCredentialsProvider(org.apache.http.client.CredentialsProvider provider) throws HTTPException
provider - the credentials providerHTTPExceptionprotected void clearProvider()
protected void setCredentials(URI uri) throws HTTPException
HTTPExceptionprotected void setAuthenticationAndProxy(org.apache.http.impl.client.HttpClientBuilder cb)
throws HTTPException
cb - HTTPExceptionpublic boolean isClosed()
public int getMethodcount()
protected static void track(HTTPSession session)
public static void setInterceptors(boolean print)
public static void resetInterceptors()
public static HTTPUtil.InterceptRequest debugRequestInterceptor()
public static HTTPUtil.InterceptResponse debugResponseInterceptor()
public static void clearkeystore()
@Deprecated public static void setGlobalCredentialsProvider(org.apache.http.auth.AuthScope scope, org.apache.http.client.CredentialsProvider provider) throws HTTPException
HTTPException@Deprecated public static void setGlobalCredentialsProvider(String url, org.apache.http.client.CredentialsProvider provider) throws HTTPException
HTTPException@Deprecated public HTTPSession setCredentialsProvider(String url, org.apache.http.client.CredentialsProvider provider) throws HTTPException
HTTPException@Deprecated public HTTPSession setCredentialsProvider(org.apache.http.auth.AuthScope scope, org.apache.http.client.CredentialsProvider provider) throws HTTPException
HTTPException@Deprecated public static int getRetryCount()
@Deprecated public static void setGlobalCompression()
@Deprecated public static void setGlobalProxy(String host, int port)
@Deprecated public void setProxy(String host, int port)
@Deprecated public static void setGlobalCredentialsProvider(org.apache.http.client.CredentialsProvider provider, String scheme) throws HTTPException
HTTPException@Deprecated public void clearState()
@Deprecated public String getSessionURL()
public static void validatestate()