Class BaseInternalRequest
java.lang.Object
org.apache.sling.servlethelpers.internalrequests.BaseInternalRequest
- Direct Known Subclasses:
InternalRequest,JakartaInternalRequest
Fluent helper for Sling internal requests.
The
ServletInternalRequest and SlingInternalRequest
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
FieldsModifier and TypeFieldDescriptionprotected Readerprotected Stringstatic final Stringprotected booleanprotected Stringprotected final org.slf4j.Loggerstatic final StringAn slf4j MDC value is set at this key with request information.protected final Stringprotected Stringprotected final org.apache.sling.api.resource.ResourceResolverprotected String -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedBaseInternalRequest(@NotNull org.apache.sling.api.resource.ResourceResolver resourceResolver, @NotNull String path) Clients use subclasses of this one -
Method Summary
Modifier and TypeMethodDescriptionprotected abstract voidprotected voidcheckNotNull(Class<?> clazz, Object candidate) protected voidcheckNotNull(String info, Object candidate) abstract BaseInternalRequestcheckResponseContentType(String contentType) After executing the request, checks that the response content-type is as expected.checkStatus(int... acceptableValues) After executing the request, checks that the request status is one of the supplied values.abstract BaseInternalRequestexecute()protected abstract org.apache.sling.api.resource.ResourceProvide the Resource to use to execute the requestabstract Stringabstract intReturn the response status.Use the supplied Reader as the request's body contentwithContentType(String contentType) Set the HTTP request's Content-TypewithExtension(String extension) Sets the optional extension of the internal request, which influence the Servlet/Script resolution.withParameter(String key, Object value) Set a request parameterwithParameters(Map<String, Object> additionalParameters) Add the supplied request parameters to the current oneswithRequestMethod(String method) Set the HTTP request method to use - defaults to GETwithSelectors(String... selectors) Sets the optional selectors of the internal request, which influence the Servlet/Script resolution.
-
Field Details
-
resourceResolver
protected final org.apache.sling.api.resource.ResourceResolver resourceResolver -
path
-
selectorString
-
extension
-
requestMethod
-
contentType
-
bodyReader
-
explicitStatusCheck
protected boolean explicitStatusCheck -
parameters
-
log
protected final org.slf4j.Logger log -
DEFAULT_METHOD
- See Also:
-
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 Details
-
BaseInternalRequest
protected BaseInternalRequest(@NotNull @NotNull org.apache.sling.api.resource.ResourceResolver resourceResolver, @NotNull @NotNull String path) Clients use subclasses of this one
-
-
Method Details
-
checkNotNull
-
checkNotNull
-
withRequestMethod
Set the HTTP request method to use - defaults to GET -
withContentType
Set the HTTP request's Content-Type -
withBody
Use the supplied Reader as the request's body content -
withSelectors
Sets the optional selectors of the internal request, which influence the Servlet/Script resolution. -
withExtension
Sets the optional extension of the internal request, which influence the Servlet/Script resolution. -
withParameter
Set a request parameter -
withParameters
Add the supplied request parameters to the current ones -
execute
- Throws:
IOException
-
getExecutionResource
protected abstract org.apache.sling.api.resource.Resource getExecutionResource()Provide the Resource to use to execute the request -
assertRequestExecuted
- Throws:
IOException
-
checkResponseContentType
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
-
checkStatus
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
-
getStatus
Return the response status. The execute method must be called before this one.- Throws:
IOException- if the request hasn't been executed yet
-
getResponseAsString
- Throws:
IOException
-