Package org.apache.pivot.web
Class Query<V>
- java.lang.Object
-
- org.apache.pivot.util.concurrent.Task<V>
-
- org.apache.pivot.io.IOTask<V>
-
- org.apache.pivot.web.Query<V>
-
- Type Parameters:
V
- The type of the value retrieved or sent via the query. For GET operations, it isObject
; for POST operations, the type isURL
. For PUT and DELETE, it isVoid
.
- Direct Known Subclasses:
DeleteQuery
,GetQuery
,PostQuery
,PutQuery
public abstract class Query<V> extends IOTask<V>
Abstract base class for web queries. A web query is an asynchronous operation that executes one of the following HTTP methods:- GET
- POST
- PUT
- DELETE
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
Query.Method
Supported HTTP methods.static class
Query.Status
Query status codes.-
Nested classes/interfaces inherited from class org.apache.pivot.io.IOTask
IOTask.MonitoredInputStream, IOTask.MonitoredOutputStream
-
-
Field Summary
Fields Modifier and Type Field Description static int
DEFAULT_PORT
-
Fields inherited from class org.apache.pivot.io.IOTask
bytesReceived, bytesSent
-
Fields inherited from class org.apache.pivot.util.concurrent.Task
abort, DEFAULT_EXECUTOR_SERVICE, timeout
-
-
Constructor Summary
Constructors Constructor Description Query(java.lang.String hostname, int port, java.lang.String path, boolean secure, java.util.concurrent.ExecutorService executorService)
Creates a new web query.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected java.lang.Object
execute(Query.Method method, java.lang.Object value)
long
getBytesExpected()
Gets the number of bytes that are expected to be received from the server in the body of the server's HTTP response.long
getBytesReceived()
Gets the number of bytes that have been received from the server in the body of the server's HTTP response.long
getBytesSent()
Gets the number of bytes that have been sent in the body of this query's HTTP request.java.lang.String
getHostname()
javax.net.ssl.HostnameVerifier
getHostnameVerifier()
java.net.URL
getLocation()
abstract Query.Method
getMethod()
QueryDictionary
getParameters()
Returns the web query's parameter dictionary.java.lang.String
getPath()
int
getPort()
java.net.Proxy
getProxy()
Gets the proxy associated with this query.ListenerList<QueryListener<V>>
getQueryListeners()
QueryDictionary
getRequestHeaders()
Returns the web query's request header dictionary.QueryDictionary
getResponseHeaders()
Returns the web query's response header dictionary.Serializer<?>
getSerializer()
Returns the serializer used to stream the value passed to or from the web query.int
getStatus()
Returns the status of the most recent execution.boolean
isSecure()
void
setHostnameVerifier(javax.net.ssl.HostnameVerifier hostnameVerifier)
void
setProxy(java.net.Proxy proxy)
Sets the proxy associated with this query.void
setSerializer(Serializer<?> serializer)
Sets the serializer used to stream the value passed to or from the web query.-
Methods inherited from class org.apache.pivot.util.concurrent.Task
abort, execute, execute, execute, getBackgroundThread, getExecutorService, getFault, getResult, getTimeout, isPending, setTimeout
-
-
-
-
Field Detail
-
DEFAULT_PORT
public static final int DEFAULT_PORT
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
Query
public Query(java.lang.String hostname, int port, java.lang.String path, boolean secure, java.util.concurrent.ExecutorService executorService)
Creates a new web query.- Parameters:
hostname
- Name of the host to contact for this web query.port
- Port number on that host.path
- The resource path on the host that is the target of this query.secure
- A flag to say whether to usehttp
orhttps
as the protocol.executorService
- The executor to use for running the query (in the background).- Throws:
java.lang.IllegalArgumentException
- if theURL
cannot be constructed.
-
-
Method Detail
-
getMethod
public abstract Query.Method getMethod()
-
getHostname
public java.lang.String getHostname()
-
getPath
public java.lang.String getPath()
-
getPort
public int getPort()
-
isSecure
public boolean isSecure()
-
getHostnameVerifier
public javax.net.ssl.HostnameVerifier getHostnameVerifier()
-
setHostnameVerifier
public void setHostnameVerifier(javax.net.ssl.HostnameVerifier hostnameVerifier)
-
getProxy
public java.net.Proxy getProxy()
Gets the proxy associated with this query.- Returns:
- This query's proxy, or
null
if the query is using the default JVM proxy settings
-
setProxy
public void setProxy(java.net.Proxy proxy)
Sets the proxy associated with this query.- Parameters:
proxy
- This query's proxy, ornull
to use the default JVM proxy settings
-
getLocation
public java.net.URL getLocation()
-
getParameters
public QueryDictionary getParameters()
Returns the web query's parameter dictionary. Parameters are passed via the query string of the web query's URL.- Returns:
- The current set of query parameters.
-
getRequestHeaders
public QueryDictionary getRequestHeaders()
Returns the web query's request header dictionary. Request headers are passed via HTTP headers when the query is executed.- Returns:
- The current set of request headers.
-
getResponseHeaders
public QueryDictionary getResponseHeaders()
Returns the web query's response header dictionary. Response headers are returned via HTTP headers when the query is executed.- Returns:
- The current set of response headers.
-
getStatus
public int getStatus()
Returns the status of the most recent execution.- Returns:
- An HTTP code representing the most recent execution status.
-
getSerializer
public Serializer<?> getSerializer()
Returns the serializer used to stream the value passed to or from the web query. By default, an instance ofJSONSerializer
is used.- Returns:
- The current serializer.
-
setSerializer
public void setSerializer(Serializer<?> serializer)
Sets the serializer used to stream the value passed to or from the web query.- Parameters:
serializer
- The serializer (must be non-null).- Throws:
java.lang.IllegalArgumentException
- if the input isnull
.
-
getBytesSent
public long getBytesSent()
Gets the number of bytes that have been sent in the body of this query's HTTP request. This will only be non-zero for POST and PUT requests, as GET and DELETE requests send no content to the server.For POST and PUT requests, this number will increment in between the
connected
andrequestSent
phases of theQueryListener
lifecycle methods. Interested listeners can poll for this value during that phase.- Returns:
- Number of bytes sent by POST or PUT requests, or 0 for GET and DELETE.
-
getBytesReceived
public long getBytesReceived()
Gets the number of bytes that have been received from the server in the body of the server's HTTP response. This will generally only be non-zero for GET requests, as POST, PUT, and DELETE requests generally don't solicit response content from the server.This number will increment in between the
requestSent
andresponseReceived
phases of theQueryListener
lifecycle methods. Interested listeners can poll for this value during that phase.- Returns:
- The number of bytes received.
-
getBytesExpected
public long getBytesExpected()
Gets the number of bytes that are expected to be received from the server in the body of the server's HTTP response. This value reflects theContent-Length
HTTP response header and is thus merely an expectation. The actual total number of bytes that will be received is not known for certain until the full response has been received.If the server did not specify a
Content-Length
HTTP response header, a value of-1
will be returned to indicate that this value is unknown.- Returns:
- The expected number of bytes to received based on the content length.
-
execute
protected java.lang.Object execute(Query.Method method, java.lang.Object value) throws QueryException
- Throws:
QueryException
-
getQueryListeners
public ListenerList<QueryListener<V>> getQueryListeners()
- Returns:
- The query listener list.
-
-