public interface ForwardedRequests
Provides services for servlets that wish to delegate the generation of a response to another service/servlet.
It is common in RESTful APIs that update operations return the updated representation of the resource.
If a POST operation leads to the creation of a new resource then the response should include a Location header indicating the location of the new resource. It may also be convenient for the response to include the representation of the resource. To ensure the representation is cacheable the response should include a Content-Location header indicating that the response is an equivalent representation of the resource that would be obtained by performing a GET operation on the specified URI.
A servlet can use the ForwardedRequests to automate this process as follows:
@Provides
class SomeServlet extends HttpServlet {
@Inject
SomeServlet(ForwardedRequests requests) {
this.requests = requests;
}
protected void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
final String newResourceLocation = ...; // create and store a new resource, producing it's location
response.setLocation(newResourceLocation); // Indicate location of created resource
response.setStatus(201); // 'Created' status
requests.get(request,response); // generate a response that includes the current representation of the created resource
}
private final ForwardedRequests requests;
}
If a PUT operation updates the current representation of a resource then it may be convenient for the response to include the updated representation. To ensure the representation is cacheable the response should include a Content-Location header indicating that the response is an equivalent representation of the resource that would be obtained by performing a GET operation on the specified URI.
A servlet can use the ForwardedRequests to automate this process as follows:
@Provides
class SomeServlet extends HttpServlet {
@Inject
SomeServlet(ForwardedRequests requests) {
this.requests = requests;
}
protected void doPut(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
... ; // process the update request
requests.get(request,response); // generate a representation of the resource at the requests location
}
private final ForwardedRequests requests;
}
| Modifier and Type | Method and Description |
|---|---|
void |
get(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
Delegate the generation of a response by performing a GET operation on the specified resource.
|
void get(javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response)
throws javax.servlet.ServletException,
IOException
GET is specified by either:
Location header in the response parameter if such a header exists. The value MUST must be located within the context root of this request (This prevents this service being used for open-redirect based attacks). If the location falls outside the context root then a 500 Internal Server Error is raised.Location header does not exist then the location of the request is used.response parameter.request - The servlet request that has been processedresponse - The servlet response object where the output of the GET operation will be written.javax.servlet.ServletException - if the request cannot be handledIOException - if an I/O error occurs while reading the request or writing the response