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. TheServletInternalRequestandSlingInternalRequestsubclasses 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 StringcontentTypestatic StringDEFAULT_METHODprotected Stringextensionprotected org.slf4j.Loggerlogstatic StringMDC_KEYAn slf4j MDC value is set at this key with request information.protected Stringpathprotected StringrequestMethodprotected org.apache.sling.api.resource.ResourceResolverresourceResolverprotected StringselectorString
-
Constructor Summary
Constructors Modifier Constructor Description protectedInternalRequest(@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 voidassertRequestExecuted()protected voidcheckNotNull(Class<?> clazz, Object candidate)protected voidcheckNotNull(String info, Object candidate)InternalRequestcheckResponseContentType(String contentType)After executing the request, checks that the response content-type is as expected.InternalRequestcheckStatus(int... acceptableValues)After executing the request, checks that the request status is one of the supplied values.protected abstract voiddelegateExecute(org.apache.sling.api.SlingHttpServletRequest request, org.apache.sling.api.SlingHttpServletResponse response, org.apache.sling.api.resource.ResourceResolver resourceResolver)Execute the supplied RequestInternalRequestexecute()Execute the internal request.protected abstract org.apache.sling.api.resource.ResourcegetExecutionResource()Provide the Resource to use to execute the requestorg.apache.sling.api.SlingHttpServletResponsegetResponse()Return the response object.StringgetResponseAsString()Return the response as a String.intgetStatus()Return the response status.InternalRequestwithBody(Reader bodyContent)Use the supplied Reader as the request's body contentInternalRequestwithContentType(String contentType)Set the HTTP request's Content-TypeInternalRequestwithExtension(String extension)Sets the optional extension of the internal request, which influence the Servlet/Script resolution.InternalRequestwithParameter(String key, Object value)Set a request parameterInternalRequestwithParameters(Map<String,Object> additionalParameters)Add the supplied request parameters to the current onesInternalRequestwithRequestMethod(String method)Set the HTTP request method to use - defaults to GETInternalRequestwithSelectors(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, IOExceptionExecute the supplied Request- Throws:
javax.servlet.ServletExceptionIOException
-
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 IOExceptionReturn 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 IOExceptionReturn 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.
-
-