Class InternalRequest

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

      • checkNotNull

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

        protected void checkNotNull​(Class<?> clazz,
                                    Object candidate)
      • 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.
      • 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
      • 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 unless checkStatus(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 unless checkStatus(int...) has been called before.
        Throws:
        IOException - if the request hasn't been executed yet or if the status check fails.