public abstract class InternalRequest
extends java.lang.Object
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.Modifier and Type | Field and Description |
---|---|
protected java.lang.String |
contentType |
static java.lang.String |
DEFAULT_METHOD |
protected java.lang.String |
extension |
protected org.slf4j.Logger |
log |
static java.lang.String |
MDC_KEY
An slf4j MDC value is set at this key with request information.
|
protected java.lang.String |
path |
protected java.lang.String |
requestMethod |
protected org.apache.sling.api.resource.ResourceResolver |
resourceResolver |
protected java.lang.String |
selectorString |
Modifier | Constructor and Description |
---|---|
protected |
InternalRequest(@NotNull org.apache.sling.api.resource.ResourceResolver resourceResolver,
@NotNull java.lang.String path)
Clients use subclasses of this one
|
Modifier and Type | Method and Description |
---|---|
protected void |
assertRequestExecuted() |
protected void |
checkNotNull(java.lang.Class<?> clazz,
java.lang.Object candidate) |
protected void |
checkNotNull(java.lang.String info,
java.lang.Object candidate) |
InternalRequest |
checkResponseContentType(java.lang.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 Request
|
InternalRequest |
execute()
Execute the internal request.
|
protected abstract org.apache.sling.api.resource.Resource |
getExecutionResource()
Provide the Resource to use to execute the request
|
org.apache.sling.api.SlingHttpServletResponse |
getResponse()
Return the response object.
|
java.lang.String |
getResponseAsString()
Return the response as a String.
|
int |
getStatus()
Return the response status.
|
InternalRequest |
withBody(java.io.Reader bodyContent)
Use the supplied Reader as the request's body content
|
InternalRequest |
withContentType(java.lang.String contentType)
Set the HTTP request's Content-Type
|
InternalRequest |
withExtension(java.lang.String extension)
Sets the optional extension of the internal request, which influence
the Servlet/Script resolution.
|
InternalRequest |
withParameter(java.lang.String key,
java.lang.Object value)
Set a request parameter
|
InternalRequest |
withParameters(java.util.Map<java.lang.String,java.lang.Object> additionalParameters)
Add the supplied request parameters to the current ones
|
InternalRequest |
withRequestMethod(java.lang.String method)
Set the HTTP request method to use - defaults to GET
|
InternalRequest |
withSelectors(java.lang.String... selectors)
Sets the optional selectors of the internal request, which influence
the Servlet/Script resolution.
|
protected final org.apache.sling.api.resource.ResourceResolver resourceResolver
protected final java.lang.String path
protected java.lang.String selectorString
protected java.lang.String extension
protected java.lang.String requestMethod
protected java.lang.String contentType
protected final org.slf4j.Logger log
public static final java.lang.String DEFAULT_METHOD
public static final java.lang.String MDC_KEY
protected InternalRequest(@NotNull @NotNull org.apache.sling.api.resource.ResourceResolver resourceResolver, @NotNull @NotNull java.lang.String path)
protected void checkNotNull(java.lang.String info, java.lang.Object candidate)
protected void checkNotNull(java.lang.Class<?> clazz, java.lang.Object candidate)
public InternalRequest withRequestMethod(java.lang.String method)
public InternalRequest withContentType(java.lang.String contentType)
public InternalRequest withBody(java.io.Reader bodyContent)
public InternalRequest withSelectors(java.lang.String... selectors)
public InternalRequest withExtension(java.lang.String extension)
public InternalRequest withParameter(java.lang.String key, java.lang.Object value)
public InternalRequest withParameters(java.util.Map<java.lang.String,java.lang.Object> additionalParameters)
public final InternalRequest execute() throws java.io.IOException
java.io.IOException
- if the request was already executed,
or if an error occurs during execution.protected abstract org.apache.sling.api.resource.Resource getExecutionResource()
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, java.io.IOException
javax.servlet.ServletException
java.io.IOException
protected void assertRequestExecuted() throws java.io.IOException
java.io.IOException
public InternalRequest checkStatus(int... acceptableValues) throws java.io.IOException
acceptableValues
- providing no values means "don't care"java.io.IOException
- if status doesn't match any of these valuespublic InternalRequest checkResponseContentType(java.lang.String contentType) throws java.io.IOException
java.io.IOException
- if the actual content-type doesn't match the expected onepublic int getStatus() throws java.io.IOException
java.io.IOException
- if the request hasn't been executed yetpublic org.apache.sling.api.SlingHttpServletResponse getResponse() throws java.io.IOException
checkStatus(int...)
has
been called before.java.io.IOException
- if the request hasn't been executed yet or if the status
check fails.public java.lang.String getResponseAsString() throws java.io.IOException
checkStatus(int...)
has
been called before.java.io.IOException
- if the request hasn't been executed yet or if the status
check fails.Copyright © 2014–2021 wcm.io. All rights reserved.