Package org.apache.sling.servlethelpers.internalrequests

# 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 Summary

Fields
Modifier and Type
Field
Description
`protected Reader`
`bodyReader`
`protected String`
`contentType`
`static final String`
`DEFAULT_METHOD`
`protected boolean`
`explicitStatusCheck`
`protected String`
`extension`
`protected final org.slf4j.Logger`
`log`
`static final String`
`MDC_KEY`
An slf4j MDC value is set at this key with request information.
`protected Map<String,Object>`
`parameters`
`protected final String`
`path`
`protected String`
`requestMethod`
`protected final org.apache.sling.api.resource.ResourceResolver`
`resourceResolver`
`protected String`
`selectorString`

## Constructor Summary

Constructors
Modifier
Constructor
Description
`protected`
`BaseInternalRequest(@NotNull org.apache.sling.api.resource.ResourceResolver resourceResolver, @NotNull String path)`
Clients use subclasses of this one

## Method Summary

Modifier and Type
Method
Description
`protected abstract void`
`assertRequestExecuted()`
`protected void`
`checkNotNull(Class<?> clazz, Object candidate)`
`protected void`
`checkNotNull(String info, Object candidate)`
`abstract BaseInternalRequest`
`checkResponseContentType(String contentType)`
After executing the request, checks that the response content-type is as expected.
`BaseInternalRequest`
`checkStatus(int... acceptableValues)`
After executing the request, checks that the request status is one of the supplied values.
`abstract BaseInternalRequest`
`execute()`
`protected abstract org.apache.sling.api.resource.Resource`
`getExecutionResource()`
Provide the Resource to use to execute the request
`abstract String`
`getResponseAsString()`
`abstract int`
`getStatus()`
Return the response status.
`BaseInternalRequest`
`withBody(Reader bodyContent)`
Use the supplied Reader as the request's body content
`BaseInternalRequest`
`withContentType(String contentType)`
Set the HTTP request's Content-Type
`BaseInternalRequest`
`withExtension(String extension)`
Sets the optional extension of the internal request, which influence the Servlet/Script resolution.
`BaseInternalRequest`
`withParameter(String key, Object value)`
Set a request parameter
`BaseInternalRequest`
`withParameters(Map<String,Object> additionalParameters)`
Add the supplied request parameters to the current ones
`BaseInternalRequest`
`withRequestMethod(String method)`
Set the HTTP request method to use - defaults to GET
`BaseInternalRequest`
`withSelectors(String... selectors)`
Sets the optional selectors of the internal request, which influence the Servlet/Script resolution.

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

## 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:
  - 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 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`