Class BaseInternalRequest

java.lang.Object
org.apache.sling.servlethelpers.internalrequests.BaseInternalRequest
Direct Known Subclasses:
InternalRequest, JakartaInternalRequest

public abstract class BaseInternalRequest extends Object
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 Details

    • 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
    • bodyReader

      protected Reader bodyReader
    • explicitStatusCheck

      protected boolean explicitStatusCheck
    • parameters

      protected Map<String,Object> parameters
    • log

      protected final org.slf4j.Logger log
    • DEFAULT_METHOD

      public static final String DEFAULT_METHOD
      See Also:
    • 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 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

      protected void checkNotNull(String info, Object candidate)
    • checkNotNull

      protected void checkNotNull(Class<?> clazz, Object candidate)
    • withRequestMethod

      public BaseInternalRequest withRequestMethod(String method)
      Set the HTTP request method to use - defaults to GET
    • withContentType

      public BaseInternalRequest withContentType(String contentType)
      Set the HTTP request's Content-Type
    • withBody

      public BaseInternalRequest withBody(Reader bodyContent)
      Use the supplied Reader as the request's body content
    • withSelectors

      public BaseInternalRequest withSelectors(String... selectors)
      Sets the optional selectors of the internal request, which influence the Servlet/Script resolution.
    • withExtension

      public BaseInternalRequest withExtension(String extension)
      Sets the optional extension of the internal request, which influence the Servlet/Script resolution.
    • withParameter

      public BaseInternalRequest withParameter(String key, Object value)
      Set a request parameter
    • withParameters

      public BaseInternalRequest withParameters(Map<String,Object> additionalParameters)
      Add the supplied request parameters to the current ones
    • execute

      public abstract BaseInternalRequest execute() throws IOException
      Throws:
      IOException
    • getExecutionResource

      protected abstract org.apache.sling.api.resource.Resource getExecutionResource()
      Provide the Resource to use to execute the request
    • assertRequestExecuted

      protected abstract void assertRequestExecuted() throws IOException
      Throws:
      IOException
    • checkResponseContentType

      public abstract BaseInternalRequest 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
    • checkStatus

      public BaseInternalRequest 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
    • getStatus

      public abstract 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
    • getResponseAsString

      public abstract String getResponseAsString() throws IOException
      Throws:
      IOException