Class RestRequest

java.lang.Object
org.elasticsearch.rest.RestRequest
All Implemented Interfaces:
Traceable, ToXContent.Params
Direct Known Subclasses:
FilteredRestRequest

public class RestRequest extends Object implements ToXContent.Params, Traceable
  • Field Details

    • SERVERLESS_REQUEST

      public static final String SERVERLESS_REQUEST
      Internal marker request parameter to indicate that a request was made in serverless mode. Use this parameter, together with OPERATOR_REQUEST if you need to toggle behavior for serverless, for example to enforce partial API restrictions (prevent request fields, omit response fields) for an API. Requests not made in serverless mode, will *not* have this parameter set. Given a request instance, you can use isServerlessRequest() to determine if the parameter is set or not. This is also available from ToXContent.Params. For example: params.paramAsBoolean(RestRequest.SERVERLESS_REQUEST, false)
      See Also:
    • OPERATOR_REQUEST

      public static final String OPERATOR_REQUEST
      Internal marker request parameter to indicate that a request was made by an operator user. Requests made by regular users (users without operator privileges), will *not* have this parameter set. Given a request instance, you can use isOperatorRequest() to determine if the parameter is set or not. This is also available from ToXContent.Params. For example: params.paramAsBoolean(RestRequest.OPERATOR_REQUEST, false)
      See Also:
    • INTERNAL_MARKER_REQUEST_PARAMETERS

      public static final Set<String> INTERNAL_MARKER_REQUEST_PARAMETERS
      Internal request parameters used as markers to indicate various operations modes such as serverless mode, or operator mode. These can never be set directly by end-users. Instead, they are set internally by Elasticsearch and must be supported by all request handlers.
  • Constructor Details

  • Method Details

    • isContentConsumed

      public boolean isContentConsumed()
    • request

      public static RestRequest request(XContentParserConfiguration parserConfig, HttpRequest httpRequest, HttpChannel httpChannel)
      Creates a new REST request.
      Throws:
      RestRequest.BadParameterException - if the parameters can not be decoded
      RestRequest.MediaTypeHeaderException - if the Content-Type or Accept header can not be parsed
    • requestWithoutParameters

      public static RestRequest requestWithoutParameters(XContentParserConfiguration parserConfig, HttpRequest httpRequest, HttpChannel httpChannel)
      Creates a new REST request. The path is not decoded so this constructor will not throw a RestRequest.BadParameterException.
      Throws:
      RestRequest.MediaTypeHeaderException - if the Content-Type or Accept header can not be parsed
    • method

      public RestRequest.Method method()
      Returns the HTTP method used in the REST request.
      Returns:
      the RestRequest.Method used in the REST request
      Throws:
      IllegalArgumentException - if the HTTP method is invalid
    • uri

      public String uri()
      The uri of the rest request, with the query string.
    • rawPath

      public String rawPath()
      The non decoded, raw path provided.
    • path

      public final String path()
      The path part of the URI (without the query string), decoded.
    • hasContent

      public boolean hasContent()
    • contentLength

      public int contentLength()
    • isFullContent

      public boolean isFullContent()
    • content

      public ReleasableBytesReference content()
      Returns a direct reference to the network buffer containing the request body. The HTTP layers will release their references to this buffer as soon as they have finished the synchronous steps of processing the request on the network thread, which will by default release the buffer back to the pool where it may be re-used for another request. If you need to keep the buffer alive past the end of these synchronous steps, acquire your own reference to this buffer and release it once it's no longer needed.
    • isStreamedContent

      public boolean isStreamedContent()
    • contentStream

      public HttpBody.Stream contentStream()
    • ensureContent

      public void ensureContent()
    • requiredContent

      public ReleasableBytesReference requiredContent()
      Returns reference to the network buffer of HTTP content or throw an exception if the body or content type is missing. See content().
    • header

      public final String header(String name)
      Get the value of the header or null if not found. This method only retrieves the first header value if multiple values are sent. Use of getAllHeaderValues(String) should be preferred
    • getAllHeaderValues

      public final List<String> getAllHeaderValues(String name)
      Get all values for the header or null if the header was not found
    • getHeaders

      public final Map<String,List<String>> getHeaders()
      Get all of the headers and values associated with the headers. Modifications of this map are not supported.
    • getRequestId

      public final long getRequestId()
    • getXContentType

      @Nullable public final XContentType getXContentType()
      The XContentType that was parsed from the Content-Type header. This value will be null in the case of a request without a valid Content-Type header, a request without content (hasContent(), or a plain text request
    • getHttpChannel

      public HttpChannel getHttpChannel()
    • getHttpRequest

      public HttpRequest getHttpRequest()
    • hasParam

      public final boolean hasParam(String key)
    • param

      public final String param(String key)
      Specified by:
      param in interface ToXContent.Params
    • param

      public final String param(String key, String defaultValue)
      Specified by:
      param in interface ToXContent.Params
    • params

      public RequestParams params()
    • repeatedParamAsList

      public List<String> repeatedParamAsList(String key)
      Returns all values for the given query parameter, preserving the order in which they appeared in the URL. This is useful for parameters that may be repeated (e.g. match[]=foo&match[]=bar). Unlike param(String), path parameters are not visible through this method.
      Parameters:
      key - the parameter name
      Returns:
      all values for the parameter, or an empty list if the parameter was not present
    • paramAsFloat

      public float paramAsFloat(String key, float defaultValue)
    • paramAsDouble

      public double paramAsDouble(String key, double defaultValue)
    • paramAsInt

      public int paramAsInt(String key, int defaultValue)
    • paramAsInteger

      public Integer paramAsInteger(String key, Integer defaultValue)
    • paramAsLong

      public long paramAsLong(String key, long defaultValue)
    • paramAsBoolean

      public boolean paramAsBoolean(String key, boolean defaultValue)
      Specified by:
      paramAsBoolean in interface ToXContent.Params
    • paramAsBoolean

      public Boolean paramAsBoolean(String key, Boolean defaultValue)
      Specified by:
      paramAsBoolean in interface ToXContent.Params
    • paramAsTime

      public TimeValue paramAsTime(String key, TimeValue defaultValue)
    • paramAsSize

      public ByteSizeValue paramAsSize(String key, ByteSizeValue defaultValue)
    • paramAsStringArray

      public String[] paramAsStringArray(String key, String[] defaultValue)
    • paramAsStringArrayOrEmptyIfAll

      public String[] paramAsStringArrayOrEmptyIfAll(String key)
    • contentParserConfig

      public XContentParserConfiguration contentParserConfig()
      Get the configuration that should be used to create XContentParser from this request.
    • contentParser

      public final XContentParser contentParser() throws IOException
      A parser for the contents of this request if there is a body, otherwise throws an ElasticsearchParseException. Use applyContentParser(CheckedConsumer) if you want to gracefully handle when the request doesn't have any contents. Use contentOrSourceParamParser() for requests that support specifying the request body in the source param.
      Throws:
      IOException
    • applyContentParser

      public final void applyContentParser(boolean includeSourceOnError, CheckedConsumer<XContentParser,IOException> applyParser) throws IOException
      If there is any content then call applyParser with the parser modified by includeSourceOnError, otherwise do nothing.
      Throws:
      IOException
    • applyContentParser

      public final void applyContentParser(CheckedConsumer<XContentParser,IOException> applyParser) throws IOException
      If there is any content then call applyParser with the parser, otherwise do nothing.
      Throws:
      IOException
    • hasContentOrSourceParam

      public final boolean hasContentOrSourceParam()
      Does this request have content or a source parameter? Use this instead of hasContent() if this RestHandler treats the source parameter like the body content.
    • contentOrSourceParamParser

      public final XContentParser contentOrSourceParamParser() throws IOException
      A parser for the contents of this request if it has contents, otherwise a parser for the source parameter if there is one, otherwise throws an ElasticsearchParseException. Use withContentOrSourceParamParserOrNull(CheckedConsumer) instead if you need to handle the absence request content gracefully.
      Throws:
      IOException
    • withContentOrSourceParamParserOrNull

      public final void withContentOrSourceParamParserOrNull(CheckedConsumer<XContentParser,IOException> withParser) throws IOException
      Call a consumer with the parser for the contents of this request if it has contents, otherwise with a parser for the source parameter if there is one, otherwise with null. Use contentOrSourceParamParser() if you should throw an exception back to the user when there isn't request content.
      Throws:
      IOException
    • contentOrSourceParam

      public final Tuple<XContentType,ReleasableBytesReference> contentOrSourceParam()
      Get the content of the request or the contents of the source param or throw an exception if both are missing. Prefer contentOrSourceParamParser() or withContentOrSourceParamParserOrNull(CheckedConsumer) if you need a parser.
    • getParsedAccept

      public ParsedMediaType getParsedAccept()
    • getParsedContentType

      public ParsedMediaType getParsedContentType()
    • parseContentType

      public static XContentType parseContentType(List<String> header)
      Parses the given content type string for the media type. This method currently ignores parameters.
    • getRestApiVersion

      public RestApiVersion getRestApiVersion()
      The requested version of the REST API.
    • hasExplicitRestApiVersion

      public boolean hasExplicitRestApiVersion()
    • markAsServerlessRequest

      public void markAsServerlessRequest()
    • isServerlessRequest

      public boolean isServerlessRequest()
    • markAsOperatorRequest

      public void markAsOperatorRequest()
    • isOperatorRequest

      public boolean isOperatorRequest()
    • getSpanId

      public String getSpanId()
      Description copied from interface: Traceable
      A consistent id for the span. Should be structured "[short-name]-[unique-id]" ie "request-abc1234"
      Specified by:
      getSpanId in interface Traceable