Package org.apache.pivot.web.server
Class QueryServlet
- java.lang.Object
-
- javax.servlet.GenericServlet
-
- javax.servlet.http.HttpServlet
-
- org.apache.pivot.web.server.QueryServlet
-
- All Implemented Interfaces:
java.io.Serializable
,javax.servlet.Servlet
,javax.servlet.ServletConfig
public abstract class QueryServlet extends javax.servlet.http.HttpServlet
Abstract base class for query servlets.Note that for better security, you should only use BinarySerializer in QueryServlet if you're sure the incoming requests will only come from trusted sources.- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
QueryServlet.Path
Immutable string sequence representing a query path.
-
Constructor Summary
Constructors Constructor Description QueryServlet()
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected abstract Serializer<?>
createSerializer(Query.Method method, QueryServlet.Path path)
Creates a serializer that will be used to serialize the current request data.protected void
dispose()
Disposes any resources allocated inprepare()
.protected void
doDelete(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
protected void
doDelete(QueryServlet.Path path)
Handles an HTTP DELETE request.protected void
doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
protected java.lang.Object
doGet(QueryServlet.Path path)
Handles an HTTP GET request.protected void
doHead(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
protected void
doOptions(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
protected void
doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
protected java.net.URL
doPost(QueryServlet.Path path, java.lang.Object value)
Handles an HTTP POST request.protected void
doPut(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
protected boolean
doPut(QueryServlet.Path path, java.lang.Object value)
Handles an HTTP GET request.protected void
doTrace(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
java.lang.String
getContextPath()
Returns the portion of the request URL representing the context path.java.lang.String
getHostname()
Gets the host name that was requested.java.net.URL
getLocation()
Returns the location of this servlet.QueryDictionary
getParameters()
Returns the servlet's parameter dictionary, which holds the values passed in the HTTP request query string.int
getPort()
Returns the Internet Protocol (IP) port number of the interface on which the request was received.java.lang.String
getProtocol()
Returns the name of the HTTP protocol that the request is using.QueryDictionary
getRequestHeaders()
Returns the servlet's request header dictionary, which holds the HTTP request headers.QueryDictionary
getResponseHeaders()
Returns the servlet's response header dictionary, which holds the HTTP response headers that will be sent back to the client.java.lang.String
getServletPath()
Returns the portion of the request URL representing the servlet path.boolean
isSecure()
Tells whether the request has been encrypted over HTTPS.protected void
prepare()
Prepares a servlet for request execution.protected void
service(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
protected void
validate(Query.Method method, QueryServlet.Path path)
Validates a servlet for request execution.
-
-
-
Method Detail
-
getHostname
public java.lang.String getHostname()
Gets the host name that was requested.- Returns:
- The host name from the request.
-
getPort
public int getPort()
Returns the Internet Protocol (IP) port number of the interface on which the request was received.- Returns:
- The port number from the request.
-
getContextPath
public java.lang.String getContextPath()
Returns the portion of the request URL representing the context path.- Returns:
- The context path from the request URL.
-
getServletPath
public java.lang.String getServletPath()
Returns the portion of the request URL representing the servlet path.- Returns:
- The servlet path parsed out of the request URL.
-
isSecure
public boolean isSecure()
Tells whether the request has been encrypted over HTTPS.- Returns:
true
if the request was sent over HTTPS,false
otherwise (regular HTTP).
-
getProtocol
public java.lang.String getProtocol()
Returns the name of the HTTP protocol that the request is using.- Returns:
- The
"http"
or"https"
protocol string depending on theisSecure()
setting.
-
getLocation
public java.net.URL getLocation()
Returns the location of this servlet.- Returns:
- The
URL
associated with this servlet.
-
getParameters
public QueryDictionary getParameters()
Returns the servlet's parameter dictionary, which holds the values passed in the HTTP request query string.- Returns:
- The set of parameters passed in this query.
-
getRequestHeaders
public QueryDictionary getRequestHeaders()
Returns the servlet's request header dictionary, which holds the HTTP request headers.- Returns:
- The set of request headers set in this query.
-
getResponseHeaders
public QueryDictionary getResponseHeaders()
Returns the servlet's response header dictionary, which holds the HTTP response headers that will be sent back to the client.- Returns:
- The current set of response headers to return to the client.
-
prepare
protected void prepare() throws javax.servlet.ServletException
Prepares a servlet for request execution. This method is called immediately prior to thevalidate(Query.Method, Path)
method.The default implementation is a no-op.
- Throws:
javax.servlet.ServletException
- on any kind of error.
-
dispose
protected void dispose() throws javax.servlet.ServletException
Disposes any resources allocated inprepare()
. This method is guaranteed to be called even if the HTTP handler method throws.The default implementation is a no-op.
- Throws:
javax.servlet.ServletException
- on any kind of error.
-
validate
protected void validate(Query.Method method, QueryServlet.Path path) throws QueryException
Validates a servlet for request execution. This method is called immediately prior to the HTTP handler method.The default implementation is a no-op.
- Parameters:
method
- The type of query this is.path
- The path to the server resources.- Throws:
QueryException
- if there is a problem, Houston.
-
doGet
protected java.lang.Object doGet(QueryServlet.Path path) throws QueryException
Handles an HTTP GET request. The default implementation throws an HTTP 405 query exception.- Parameters:
path
- The request path.- Returns:
- The result of the GET.
- Throws:
QueryException
- on any error.
-
doPost
protected java.net.URL doPost(QueryServlet.Path path, java.lang.Object value) throws QueryException
Handles an HTTP POST request. The default implementation throws an HTTP 405 query exception.- Parameters:
path
- The path of this request.value
- The value parsed from the POST request data.- Returns:
- A URL containing the location of the created resource, or
null
if operation did not result in the creation of a resource. - Throws:
QueryException
- on errors.
-
doPut
protected boolean doPut(QueryServlet.Path path, java.lang.Object value) throws QueryException
Handles an HTTP GET request. The default implementation throws an HTTP 405 query exception.- Parameters:
path
- The server path for this request.value
- The value parsed from the PUT request data.- Returns:
true
if the operation resulted in the creation of a resource;false
, otherwise.- Throws:
QueryException
- on any error.
-
doDelete
protected void doDelete(QueryServlet.Path path) throws QueryException
Handles an HTTP DELETE request. The default implementation throws an HTTP 405 query exception.- Parameters:
path
- The server path for this request.- Throws:
QueryException
- if there was a problem.
-
createSerializer
protected abstract Serializer<?> createSerializer(Query.Method method, QueryServlet.Path path) throws QueryException
Creates a serializer that will be used to serialize the current request data.- Parameters:
method
- Type of query this serializer will apply to.path
- The server path this is intended for.- Returns:
- The newly created serializer for this request.
- Throws:
QueryException
- if there is a problem.
-
service
protected void service(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws java.io.IOException, javax.servlet.ServletException
- Overrides:
service
in classjavax.servlet.http.HttpServlet
- Throws:
java.io.IOException
javax.servlet.ServletException
-
doGet
protected final void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws java.io.IOException, javax.servlet.ServletException
- Overrides:
doGet
in classjavax.servlet.http.HttpServlet
- Throws:
java.io.IOException
javax.servlet.ServletException
-
doPost
protected final void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws java.io.IOException, javax.servlet.ServletException
- Overrides:
doPost
in classjavax.servlet.http.HttpServlet
- Throws:
java.io.IOException
javax.servlet.ServletException
-
doPut
protected final void doPut(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws java.io.IOException, javax.servlet.ServletException
- Overrides:
doPut
in classjavax.servlet.http.HttpServlet
- Throws:
java.io.IOException
javax.servlet.ServletException
-
doDelete
protected final void doDelete(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws java.io.IOException, javax.servlet.ServletException
- Overrides:
doDelete
in classjavax.servlet.http.HttpServlet
- Throws:
java.io.IOException
javax.servlet.ServletException
-
doHead
protected final void doHead(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws java.io.IOException, javax.servlet.ServletException
- Overrides:
doHead
in classjavax.servlet.http.HttpServlet
- Throws:
java.io.IOException
javax.servlet.ServletException
-
doOptions
protected final void doOptions(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws java.io.IOException, javax.servlet.ServletException
- Overrides:
doOptions
in classjavax.servlet.http.HttpServlet
- Throws:
java.io.IOException
javax.servlet.ServletException
-
doTrace
protected final void doTrace(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws java.io.IOException, javax.servlet.ServletException
- Overrides:
doTrace
in classjavax.servlet.http.HttpServlet
- Throws:
java.io.IOException
javax.servlet.ServletException
-
-