public class TLcdOWSHttpTransport extends Object implements ILcdOWSTransport
This implementation supports both HTTP GET and POST requests.
For POST requests, the MIME types text/xml
and application/x-www-form-urlencoded
are supported.
By default, a maximum HTTP GET query length of 2048 characters is enforced; if the query length is larger,
the communication automatically switches to HTTP POST if supported by the server. The maximum HTTP GET query length
can be customized through the System property com.luciad.ogc.ows.model.TLcdOWSHttpTransport.maxGetQueryLength
.
This implementation also supports connecting with servers that require authentication according to the HTTP Basic Authentication mechanism. Credentials can be supplied through the constructors of this class.
Based on the supported request methods at the service, performRequest(TLcdOWSOperation, ILcdOWSRequest)
tries different requests in case of failure. By default, POST requests are tried before GET requests,
if both are available. This behavior can be configured at construction time.
Constructor and Description |
---|
TLcdOWSHttpTransport()
Creates a new HTTP transport.
|
TLcdOWSHttpTransport(boolean aPreferPOST)
Creates a new HTTP transport with the given request type preference.
|
TLcdOWSHttpTransport(String aUsername,
String aPassword)
Creates a new HTTP transport with the given username and password.
|
TLcdOWSHttpTransport(String aUsername,
String aPassword,
boolean aPreferPOST)
Creates a new HTTP transport with the given credentials and request type preference.
|
Modifier and Type | Method and Description |
---|---|
boolean |
canPerformRequest(TLcdOWSOperation aOperation,
ILcdOWSRequest aRequest)
Fast check to see whether or not this transport is capable of performing the request.
|
protected void |
configureConnection(URI aURI,
ILcdOWSRequest aRequest,
HttpURLConnection aConnection)
Configures the specified connection.
|
TLcdOWSDCP |
createDCP(URI aURI)
Creates an OpenGIS Web Service DCP that can be used to access the specified URI using this transport.
|
protected TLcdOWSInputStream |
handleResponse(URI aURI,
ILcdOWSRequest aRequest,
int aResponse,
String aResponseMessage,
Map aHeaderFields)
This method is called if an error response code is received from the HTTP server
and if the content of response is not a service exception.
|
boolean |
isCompatibleURI(URI aUri)
Determines whether the specified URI is compatible with this transport implementation.
|
protected HttpURLConnection |
openConnection(URL aUrl)
Opens a connection to the specified URL.
|
TLcdOWSInputStream |
performRequest(TLcdOWSOperation aOperation,
ILcdOWSRequest aRequest)
Performs the specified operation passing in the parameters defined in the request object.
|
protected void |
sendData(HttpURLConnection aConnection,
ILcdOWSRequest aRequest,
String aMethod)
Writes request data to the given connection, if required by the request method.
|
public TLcdOWSHttpTransport()
performRequest(TLcdOWSOperation, ILcdOWSRequest)
first tries GET requests instead of POST requests.public TLcdOWSHttpTransport(String aUsername, String aPassword)
performRequest(TLcdOWSOperation, ILcdOWSRequest)
first tries GET requests instead of POST requests.aUsername
- username to authenticate at the server; this is only used if the password is also setaPassword
- password to authenticate at the server; this is only used if the username is also setpublic TLcdOWSHttpTransport(boolean aPreferPOST)
aPreferPOST
- indicates whether POST requests are preferred above GETpublic TLcdOWSHttpTransport(String aUsername, String aPassword, boolean aPreferPOST)
aPreferPOST
- indicates whether POST requests are preferred above GETaUsername
- username to authenticate at the server; this is only used if the password is also setaPassword
- password to authenticate at the server; this is only used if the username is also setpublic boolean canPerformRequest(TLcdOWSOperation aOperation, ILcdOWSRequest aRequest)
ILcdOWSTransport
Fast check to see whether or not this transport is capable of performing the request.
When this method returns false
, ILcdOWSTransport.performRequest(TLcdOWSOperation, ILcdOWSRequest)
will not be
capable of performing the request and throw an error.
When this method returns true
, it is still possible that calling ILcdOWSTransport.performRequest(TLcdOWSOperation, ILcdOWSRequest)
throws an error (for example when the server is offline).
The default implementation returns true
.
canPerformRequest
in interface ILcdOWSTransport
aOperation
- The operationaRequest
- The request parametersfalse
when this transport cannot perform the request, true
when this transport probably can perform the request.public TLcdOWSInputStream performRequest(TLcdOWSOperation aOperation, ILcdOWSRequest aRequest) throws TLcdOWSTransportException
ILcdOWSRequest.getTransportData()
,
represented by a Map
object (see URLConnection.getRequestProperties()
).
The returned TLcdOWSInputStream
contains, next to the MIME type and
input stream, the response headers of the connection, represented as a Map
object
(see URLConnection.getHeaderFields()
).performRequest
in interface ILcdOWSTransport
aOperation
- the operation to performaRequest
- the request parametersTLcdOWSTransportException
- if an error occurs in the transport layerpublic boolean isCompatibleURI(URI aUri)
URI.getScheme()
)
that equals "http" or "https".isCompatibleURI
in interface ILcdOWSTransport
aUri
- the URI to testpublic TLcdOWSDCP createDCP(URI aURI)
TLcdOWSOperation
object that is needed to perform initial requests
(in other words, requests that need to be performed before web service metadata has been retrieved).
By default, a TLcdOWSDCP
is returned that is configured
with a TLcdOWSHTTP
object consisting of a GET request method.createDCP
in interface ILcdOWSTransport
aURI
- the URINullPointerException
- if the specified URI is nullIllegalArgumentException
- if the specified URI is not compatible with this transportprotected HttpURLConnection openConnection(URL aUrl) throws IOException
aUrl
- the URL to which a connection should be openedIOException
- if an error occurs while opening the connectionprotected TLcdOWSInputStream handleResponse(URI aURI, ILcdOWSRequest aRequest, int aResponse, String aResponseMessage, Map aHeaderFields) throws TLcdOWSHttpTransportException
TLcdOWSHttpTransportException
,
initialized with the details of the error response (response code, message and header fields).
Subclasses can override this method to take other actions (for instance use a proxy,
retrieve authentication information, etc.), and still try to return a valid TLcdOWSInputStream
.aURI
- the URI that was requested and triggered the erroraRequest
- the OWS requestaResponse
- the HTTP response codeaResponseMessage
- the HTTP response messageaHeaderFields
- the HTTP header fieldsTLcdOWSHttpTransportException
- in all casesprotected void configureConnection(URI aURI, ILcdOWSRequest aRequest, HttpURLConnection aConnection) throws IOException
URLConnection.setConnectTimeout(int)
and URLConnection.setReadTimeout(int)
).HttpURLConnection.setInstanceFollowRedirects(boolean)
).Accept-Encoding
request property is set to gzip, deflate, compress
,
to allow servers to send compressed data. These encodings are specified in HTTP 1.1 (RFC 2616, section 3.5),
and are supported by default in this class.User-Agent
request property is set to Java version
,
to identify the source of the request. Some servers require to have a non-empty value for this property.Authorization
request
property is initialized with these credentials, following the HTTP Basic Authentication mechanism.Map
object associated with the OWS request
(see ILcdOWSRequest.getTransportData()
and
URLConnection.setRequestProperty(String, String)
).aURI
- the URI which is being accessedaRequest
- the request parametersaConnection
- the connection to configureIOException
- if an error occurs while configuring the connectionprotected void sendData(HttpURLConnection aConnection, ILcdOWSRequest aRequest, String aMethod) throws TLcdOWSTransportException
GET
or POST
.
In case of a GET request, the request is fully defined by the URL itself
(through the key-value parameters), so no further request data is sent to the connection.
In case of a POST request, the request is not part of the URL itself,
and needs to be written to the connection (see URLConnection.getOutputStream()
).aConnection
- the connection to write request data toaRequest
- the requestaMethod
- the request method, which must be GET
or POST
TLcdOWSTransportException
- in case of an unsupported request method or when sending
the request data fails