@ThreadSafe public class HTTPSession extends Object implements Closeable
A Session encapsulate a number of other objects:
Currently, it is assumed that only one set of credentials is needed, whether directly for server X or for server Y. This may change in the future.
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.
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.
| 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 |
protected static Map<org.apache.http.auth.AuthScope,HTTPProviderFactory> |
globalcredfactories |
static String |
HEADER_USERAGENT |
protected String |
identifier |
protected Map<org.apache.http.auth.AuthScope,org.apache.http.client.CredentialsProvider> |
localcreds |
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 org.apache.http.impl.client.BasicAuthCache |
sessioncache |
protected org.apache.http.client.protocol.HttpClientContext |
sessioncontext |
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() |
HTTPSession |
clearCredentialsCache() |
HTTPSession |
clearCredentialsCache(org.apache.http.auth.AuthScope template) |
static void |
clearGlobalCredentials()
Remove any global credentials that have been previosly set with the
setGlobalCredentials(Credentials)
or setCredentialsProviderFactory(ucar.httpservices.HTTPProviderFactory, org.apache.http.auth.AuthScope) methods. |
static void |
clearkeystore() |
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() |
org.apache.http.impl.client.BasicAuthCache |
getCredentialsCache() |
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() |
protected static void |
kill() |
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) |
HTTPSession |
setCredentials(org.apache.http.auth.Credentials creds)
It is convenient to be able to directly set the Credentials
(not the provider) when those credentials are fixed.
|
HTTPSession |
setCredentials(org.apache.http.auth.Credentials creds,
org.apache.http.auth.AuthScope scope)
It is convenient to be able to directly set the Credentials
(not the provider) when those credentials are fixed.
|
HTTPSession |
setCredentials(String url,
org.apache.http.auth.Credentials creds)
Deprecated.
|
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(org.apache.http.client.CredentialsProvider provider,
org.apache.http.auth.AuthScope scope)
This is the most general case
|
HTTPSession |
setCredentialsProvider(String url,
org.apache.http.client.CredentialsProvider provider)
Deprecated.
|
static void |
setCredentialsProviderFactory(HTTPProviderFactory factory,
org.apache.http.auth.AuthScope scope) |
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 |
setGlobalCredentials(org.apache.http.auth.Credentials creds)
It is convenient to be able to directly set the Credentials
(not the provider) when those credentials are fixed.
|
static void |
setGlobalCredentials(org.apache.http.auth.Credentials creds,
org.apache.http.auth.AuthScope scope)
It is convenient to be able to directly set the Credentials
(not the provider) when those credentials are fixed.
|
static void |
setGlobalCredentials(String url,
org.apache.http.auth.Credentials creds)
Deprecated.
|
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)
Deprecated.
|
static void |
setGlobalCredentialsProvider(org.apache.http.client.CredentialsProvider provider,
org.apache.http.auth.AuthScope scope)
Deprecated.
|
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 Map<org.apache.http.auth.AuthScope,HTTPProviderFactory> globalcredfactories
protected static ucar.httpservices.HTTPSession.AuthControls authcontrols
protected String sessionURI
protected URI scopeURI
protected org.apache.http.auth.AuthScope scope
protected boolean closed
protected Map<org.apache.http.auth.AuthScope,org.apache.http.client.CredentialsProvider> localcreds
protected ConcurrentSkipListSet<HTTPMethod> methods
protected String identifier
protected org.apache.http.client.protocol.HttpClientContext sessioncontext
protected org.apache.http.impl.client.BasicAuthCache 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 setCredentialsProviderFactory(HTTPProviderFactory factory, org.apache.http.auth.AuthScope scope) throws HTTPException
factory - the credentials provider factoryscope - where to use it (i.e. on what host)HTTPExceptionpublic static void setGlobalCredentials(org.apache.http.auth.Credentials creds)
throws HTTPException
creds - HTTPExceptionpublic static void setGlobalCredentials(org.apache.http.auth.Credentials creds,
org.apache.http.auth.AuthScope scope)
throws HTTPException
creds - scope - where to use it (i.e. on what host)HTTPExceptionpublic static void clearGlobalCredentials()
setGlobalCredentials(Credentials)
or setCredentialsProviderFactory(ucar.httpservices.HTTPProviderFactory, org.apache.http.auth.AuthScope) methods.public 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 HTTPSession clearCredentialsCache()
public HTTPSession clearCredentialsCache(org.apache.http.auth.AuthScope template)
public org.apache.http.impl.client.BasicAuthCache getCredentialsCache()
public void close()
close in interface Closeableclose in interface AutoCloseablepublic HTTPSession setCredentialsProvider(org.apache.http.client.CredentialsProvider provider) throws HTTPException
provider - HTTPExceptionpublic HTTPSession setCredentialsProvider(org.apache.http.client.CredentialsProvider provider, org.apache.http.auth.AuthScope scope) throws HTTPException
provider - the credentials providerscope - where to use it (i.e. on what host+port)HTTPExceptionpublic HTTPSession setCredentials(org.apache.http.auth.Credentials creds) throws HTTPException
creds - HTTPExceptionpublic HTTPSession setCredentials(org.apache.http.auth.Credentials creds, org.apache.http.auth.AuthScope scope) throws HTTPException
creds - scope - where to use it (i.e. on what host)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 static void setGlobalCredentials(String url, org.apache.http.auth.Credentials creds) throws HTTPException
HTTPException@Deprecated public HTTPSession setCredentials(String url, org.apache.http.auth.Credentials creds) 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()
protected static void kill()
public static void validatestate()
@Deprecated public static void setGlobalCredentialsProvider(org.apache.http.client.CredentialsProvider provider) throws HTTPException
provider - HTTPException@Deprecated public static void setGlobalCredentialsProvider(org.apache.http.client.CredentialsProvider provider, org.apache.http.auth.AuthScope scope) throws HTTPException
provider - the credentials providerscope - where to use it (i.e. on what host)HTTPException