Class TLcdOWSHttpTransport
- All Implemented Interfaces:
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.
- Since:
- 8.2
-
Constructor Summary
ConstructorDescriptionCreates 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. -
Method Summary
Modifier and TypeMethodDescriptionboolean
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.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.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.
-
Constructor Details
-
TLcdOWSHttpTransport
public TLcdOWSHttpTransport()Creates a new HTTP transport. By default,performRequest(TLcdOWSOperation, ILcdOWSRequest)
first tries GET requests instead of POST requests. -
TLcdOWSHttpTransport
Creates a new HTTP transport with the given username and password. These credentials will be used to authenticate at the server using HTTP Basic Authentication. By default,performRequest(TLcdOWSOperation, ILcdOWSRequest)
first tries GET requests instead of POST requests.- Parameters:
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 set- Since:
- 2015.1
-
TLcdOWSHttpTransport
public TLcdOWSHttpTransport(boolean aPreferPOST) Creates a new HTTP transport with the given request type preference.- Parameters:
aPreferPOST
- indicates whether POST requests are preferred above GET
-
TLcdOWSHttpTransport
Creates a new HTTP transport with the given credentials and request type preference. The credentials will be used to authenticate at the server using HTTP Basic Authentication.- Parameters:
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 setaPreferPOST
- indicates whether POST requests are preferred above GET- Since:
- 2015.1
-
-
Method Details
-
canPerformRequest
Description copied from interface: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 returnstrue
, it is still possible that callingILcdOWSTransport.performRequest(TLcdOWSOperation, ILcdOWSRequest)
throws an error (for example when the server is offline).The default implementation returns
true
.- Specified by:
canPerformRequest
in interfaceILcdOWSTransport
- Parameters:
aOperation
- The operationaRequest
- The request parameters- Returns:
false
when this transport cannot perform the request,true
when this transport probably can perform the request.
-
performRequest
public TLcdOWSInputStream performRequest(TLcdOWSOperation aOperation, ILcdOWSRequest aRequest) throws TLcdOWSTransportException Performs the specified operation passing in the parameters defined in the request object. Request properties to be used for the connection can be specified through the transport data of the request,ILcdOWSRequest.getTransportData()
, represented by aMap
object (seeURLConnection.getRequestProperties()
). The returnedTLcdOWSInputStream
contains, next to the MIME type and input stream, the response headers of the connection, represented as aMap
object (seeURLConnection.getHeaderFields()
).- Specified by:
performRequest
in interfaceILcdOWSTransport
- Parameters:
aOperation
- the operation to performaRequest
- the request parameters- Returns:
- a typed inputstream
- Throws:
TLcdOWSTransportException
- if an error occurs in the transport layer
-
isCompatibleURI
Determines whether the specified URI is compatible with this transport implementation. An URI is considered compatible if it has a scheme (seeURI.getScheme()
) that equals "http" or "https".- Specified by:
isCompatibleURI
in interfaceILcdOWSTransport
- Parameters:
aUri
- the URI to test- Returns:
- true if the specified URI is compatible; false otherwise
-
createDCP
Creates an OpenGIS Web Service DCP that can be used to access the specified URI using this transport. This method can be used to create aTLcdOWSOperation
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, aTLcdOWSDCP
is returned that is configured with aTLcdOWSHTTP
object consisting of a GET request method.- Specified by:
createDCP
in interfaceILcdOWSTransport
- Parameters:
aURI
- the URI- Returns:
- an OWS DCP object
- Throws:
NullPointerException
- if the specified URI is nullIllegalArgumentException
- if the specified URI is not compatible with this transport
-
openConnection
Opens a connection to the specified URL. This method can be overridden in order to redirect a URL to an HTTP proxy.- Parameters:
aUrl
- the URL to which a connection should be opened- Returns:
- an HttpURLConnection to the specified URL
- Throws:
IOException
- if an error occurs while opening the connection
-
handleResponse
protected TLcdOWSInputStream handleResponse(URI aURI, ILcdOWSRequest aRequest, int aResponse, String aResponseMessage, Map aHeaderFields) throws TLcdOWSHttpTransportException 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. By default, this method fails by throwing aTLcdOWSHttpTransportException
, 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 validTLcdOWSInputStream
.- Parameters:
aURI
- the URI that was requested and triggered the erroraRequest
- the OWS requestaResponse
- the HTTP response codeaResponseMessage
- the HTTP response messageaHeaderFields
- the HTTP header fields- Returns:
- a typed inputstream
- Throws:
TLcdOWSHttpTransportException
- in all cases
-
configureConnection
protected void configureConnection(URI aURI, ILcdOWSRequest aRequest, HttpURLConnection aConnection) throws IOException Configures the specified connection. This method allows subclasses to set specific request properties. The default implementation sets the following properties, in this order:- A connect and read timeout of 30s is set (see
URLConnection.setConnectTimeout(int)
andURLConnection.setReadTimeout(int)
). - HTTP redirects (requests with response code 3xx) should be automatically followed by the connection
(see
HttpURLConnection.setInstanceFollowRedirects(boolean)
). - The
Accept-Encoding
request property is set togzip, 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. - The
User-Agent
request property is set toJava version
, to identify the source of the request. Some servers require to have a non-empty value for this property. - If a username and password have been supplied to the constructor, the
Authorization
request property is initialized with these credentials, following the HTTP Basic Authentication mechanism. - Connection request properties that are contained in a
Map
object associated with the OWS request (seeILcdOWSRequest.getTransportData()
andURLConnection.setRequestProperty(String, String)
).
- Parameters:
aURI
- the URI which is being accessedaRequest
- the request parametersaConnection
- the connection to configure- Throws:
IOException
- if an error occurs while configuring the connection
- A connect and read timeout of 30s is set (see
-
sendData
protected void sendData(HttpURLConnection aConnection, ILcdOWSRequest aRequest, String aMethod) throws TLcdOWSTransportException Writes request data to the given connection, if required by the request method. The request method must be eitherGET
orPOST
. 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 (seeURLConnection.getOutputStream()
).- Parameters:
aConnection
- the connection to write request data toaRequest
- the requestaMethod
- the request method, which must beGET
orPOST
- Throws:
TLcdOWSTransportException
- in case of an unsupported request method or when sending the request data fails
-