Class InternalRequest
- java.lang.Object
-
- org.apache.sling.servlethelpers.internalrequests.InternalRequest
-
- Direct Known Subclasses:
ServletInternalRequest
,SlingInternalRequest
public abstract class InternalRequest extends Object
Fluent helper for Sling internal requests. TheServletInternalRequest
andSlingInternalRequest
subclasses provide two modes for executing the internal requests, one that's very similar to the way Sling executes an HTTP request and another one that's faster by calling Servlets or Scripts directly.
-
-
Field Summary
Fields Modifier and Type Field Description protected String
contentType
static String
DEFAULT_METHOD
protected String
extension
protected org.slf4j.Logger
log
static String
MDC_KEY
An slf4j MDC value is set at this key with request information.protected String
path
protected String
requestMethod
protected org.apache.sling.api.resource.ResourceResolver
resourceResolver
protected String
selectorString
-
Constructor Summary
Constructors Modifier Constructor Description protected
InternalRequest(@NotNull org.apache.sling.api.resource.ResourceResolver resourceResolver, @NotNull String path)
Clients use subclasses of this one
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected void
assertRequestExecuted()
protected void
checkNotNull(Class<?> clazz, Object candidate)
protected void
checkNotNull(String info, Object candidate)
InternalRequest
checkResponseContentType(String contentType)
After executing the request, checks that the response content-type is as expected.InternalRequest
checkStatus(int... acceptableValues)
After executing the request, checks that the request status is one of the supplied values.protected abstract void
delegateExecute(org.apache.sling.api.SlingHttpServletRequest request, org.apache.sling.api.SlingHttpServletResponse response, org.apache.sling.api.resource.ResourceResolver resourceResolver)
Execute the supplied RequestInternalRequest
execute()
Execute the internal request.protected abstract org.apache.sling.api.resource.Resource
getExecutionResource()
Provide the Resource to use to execute the requestorg.apache.sling.api.SlingHttpServletResponse
getResponse()
Return the response object.String
getResponseAsString()
Return the response as a String.int
getStatus()
Return the response status.InternalRequest
withBody(Reader bodyContent)
Use the supplied Reader as the request's body contentInternalRequest
withContentType(String contentType)
Set the HTTP request's Content-TypeInternalRequest
withExtension(String extension)
Sets the optional extension of the internal request, which influence the Servlet/Script resolution.InternalRequest
withParameter(String key, Object value)
Set a request parameterInternalRequest
withParameters(Map<String,Object> additionalParameters)
Add the supplied request parameters to the current onesInternalRequest
withRequestMethod(String method)
Set the HTTP request method to use - defaults to GETInternalRequest
withSelectors(String... selectors)
Sets the optional selectors of the internal request, which influence the Servlet/Script resolution.
-
-
-
Field Detail
-
resourceResolver
protected final org.apache.sling.api.resource.ResourceResolver resourceResolver
-
path
protected final String path
-
selectorString
protected String selectorString
-
extension
protected String extension
-
requestMethod
protected String requestMethod
-
contentType
protected String contentType
-
log
protected final org.slf4j.Logger log
-
DEFAULT_METHOD
public static final String DEFAULT_METHOD
- See Also:
- Constant Field Values
-
MDC_KEY
public static final String MDC_KEY
An slf4j MDC value is set at this key with request information. That's useful for troubleshooting when using multiple internal requests in the context of a single HTTP request.
-
-
Constructor Detail
-
InternalRequest
protected InternalRequest(@NotNull @NotNull org.apache.sling.api.resource.ResourceResolver resourceResolver, @NotNull @NotNull String path)
Clients use subclasses of this one
-
-
Method Detail
-
withRequestMethod
public InternalRequest withRequestMethod(String method)
Set the HTTP request method to use - defaults to GET
-
withContentType
public InternalRequest withContentType(String contentType)
Set the HTTP request's Content-Type
-
withBody
public InternalRequest withBody(Reader bodyContent)
Use the supplied Reader as the request's body content
-
withSelectors
public InternalRequest withSelectors(String... selectors)
Sets the optional selectors of the internal request, which influence the Servlet/Script resolution.
-
withExtension
public InternalRequest withExtension(String extension)
Sets the optional extension of the internal request, which influence the Servlet/Script resolution.
-
withParameter
public InternalRequest withParameter(String key, Object value)
Set a request parameter
-
withParameters
public InternalRequest withParameters(Map<String,Object> additionalParameters)
Add the supplied request parameters to the current ones
-
execute
public final InternalRequest execute() throws IOException
Execute the internal request. Can be called right after creating it, if no options need to be set.- Throws:
IOException
- if the request was already executed, or if an error occurs during execution.
-
getExecutionResource
protected abstract org.apache.sling.api.resource.Resource getExecutionResource()
Provide the Resource to use to execute the request
-
delegateExecute
protected abstract void delegateExecute(org.apache.sling.api.SlingHttpServletRequest request, org.apache.sling.api.SlingHttpServletResponse response, org.apache.sling.api.resource.ResourceResolver resourceResolver) throws javax.servlet.ServletException, IOException
Execute the supplied Request- Throws:
javax.servlet.ServletException
IOException
-
assertRequestExecuted
protected void assertRequestExecuted() throws IOException
- Throws:
IOException
-
checkStatus
public InternalRequest checkStatus(int... acceptableValues) throws IOException
After executing the request, checks that the request status is one of the supplied values. If this is not called before methods that access the response, a check for a 200 OK status is done automatically unless this was called with no arguments before. This makes sure a status check is done or explicitly disabled.- Parameters:
acceptableValues
- providing no values means "don't care"- Throws:
IOException
- if status doesn't match any of these values
-
checkResponseContentType
public InternalRequest checkResponseContentType(String contentType) throws IOException
After executing the request, checks that the response content-type is as expected.- Throws:
IOException
- if the actual content-type doesn't match the expected one
-
getStatus
public int getStatus() throws IOException
Return the response status. The execute method must be called before this one.- Throws:
IOException
- if the request hasn't been executed yet
-
getResponse
public org.apache.sling.api.SlingHttpServletResponse getResponse() throws IOException
Return the response object. The execute method must be called before this one. A check for "200 OK" status is done automatically unlesscheckStatus(int...)
has been called before.- Throws:
IOException
- if the request hasn't been executed yet or if the status check fails.
-
getResponseAsString
public String getResponseAsString() throws IOException
Return the response as a String. The execute method must be called before this one. A check for "200 OK" status is done automatically unlesscheckStatus(int...)
has been called before.- Throws:
IOException
- if the request hasn't been executed yet or if the status check fails.
-
-