Package org.elasticsearch.rest
Class RestRequest
- java.lang.Object
-
- org.elasticsearch.rest.RestRequest
-
- All Implemented Interfaces:
ToXContent.Params
public class RestRequest extends java.lang.Object implements ToXContent.Params
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classRestRequest.BadParameterExceptionstatic classRestRequest.ContentTypeHeaderExceptionstatic classRestRequest.Method
-
Constructor Summary
Constructors Modifier Constructor Description protectedRestRequest(NamedXContentRegistry xContentRegistry, java.util.Map<java.lang.String,java.lang.String> params, java.lang.String path, java.util.Map<java.lang.String,java.util.List<java.lang.String>> headers, HttpRequest httpRequest, HttpChannel httpChannel)protectedRestRequest(RestRequest restRequest)
-
Method Summary
Modifier and Type Method Description voidapplyContentParser(CheckedConsumer<XContentParser,java.io.IOException> applyParser)If there is any content then callapplyParserwith the parser, otherwise do nothing.BytesReferencecontent()protected BytesReferencecontent(boolean contentConsumed)Tuple<XContentType,BytesReference>contentOrSourceParam()Get the content of the request or the contents of thesourceparam or throw an exception if both are missing.XContentParsercontentOrSourceParamParser()A parser for the contents of this request if it has contents, otherwise a parser for thesourceparameter if there is one, otherwise throws anElasticsearchParseException.XContentParsercontentParser()A parser for the contents of this request if there is a body, otherwise throws anElasticsearchParseException.java.util.List<java.lang.String>getAllHeaderValues(java.lang.String name)Get all values for the header ornullif the header was not foundjava.util.Map<java.lang.String,java.util.List<java.lang.String>>getHeaders()Get all of the headers and values associated with the headers.HttpChannelgetHttpChannel()HttpRequestgetHttpRequest()NamedXContentRegistrygetXContentRegistry()Get theNamedXContentRegistrythat should be used to create parsers from this request.XContentTypegetXContentType()TheXContentTypethat was parsed from theContent-Typeheader.booleanhasContent()booleanhasContentOrSourceParam()Does this request have content or asourceparameter? Use this instead ofhasContent()if this RestHandler treats thesourceparameter like the body content.booleanhasParam(java.lang.String key)java.lang.Stringheader(java.lang.String name)Get the value of the header ornullif not found.booleanisContentConsumed()RestRequest.Methodmethod()Returns the HTTP method used in the REST request.java.lang.Stringparam(java.lang.String key)java.lang.Stringparam(java.lang.String key, java.lang.String defaultValue)booleanparamAsBoolean(java.lang.String key, boolean defaultValue)java.lang.BooleanparamAsBoolean(java.lang.String key, java.lang.Boolean defaultValue)floatparamAsFloat(java.lang.String key, float defaultValue)intparamAsInt(java.lang.String key, int defaultValue)longparamAsLong(java.lang.String key, long defaultValue)ByteSizeValueparamAsSize(java.lang.String key, ByteSizeValue defaultValue)java.lang.String[]paramAsStringArray(java.lang.String key, java.lang.String[] defaultValue)java.lang.String[]paramAsStringArrayOrEmptyIfAll(java.lang.String key)TimeValueparamAsTime(java.lang.String key, TimeValue defaultValue)java.util.Map<java.lang.String,java.lang.String>params()static XContentTypeparseContentType(java.util.List<java.lang.String> header)Parses the given content type string for the media type.java.lang.Stringpath()The path part of the URI (without the query string), decoded.java.lang.StringrawPath()The non decoded, raw path provided.static RestRequestrequest(NamedXContentRegistry xContentRegistry, HttpRequest httpRequest, HttpChannel httpChannel)Creates a new REST request.static RestRequestrequestWithoutParameters(NamedXContentRegistry xContentRegistry, HttpRequest httpRequest, HttpChannel httpChannel)Creates a new REST request.BytesReferencerequiredContent()java.lang.Stringuri()The uri of the rest request, with the query string.voidwithContentOrSourceParamParserOrNull(CheckedConsumer<XContentParser,java.io.IOException> withParser)Call a consumer with the parser for the contents of this request if it has contents, otherwise with a parser for thesourceparameter if there is one, otherwise withnull.
-
-
-
Constructor Detail
-
RestRequest
protected RestRequest(NamedXContentRegistry xContentRegistry, java.util.Map<java.lang.String,java.lang.String> params, java.lang.String path, java.util.Map<java.lang.String,java.util.List<java.lang.String>> headers, HttpRequest httpRequest, HttpChannel httpChannel)
-
RestRequest
protected RestRequest(RestRequest restRequest)
-
-
Method Detail
-
isContentConsumed
public boolean isContentConsumed()
-
request
public static RestRequest request(NamedXContentRegistry xContentRegistry, HttpRequest httpRequest, HttpChannel httpChannel)
Creates a new REST request. This method will throwRestRequest.BadParameterExceptionif the path cannot be decoded- Parameters:
xContentRegistry- the content registryhttpRequest- the http requesthttpChannel- the http channel- Throws:
RestRequest.BadParameterException- if the parameters can not be decodedRestRequest.ContentTypeHeaderException- if the Content-Type header can not be parsed
-
requestWithoutParameters
public static RestRequest requestWithoutParameters(NamedXContentRegistry xContentRegistry, HttpRequest httpRequest, HttpChannel httpChannel)
Creates a new REST request. The path is not decoded so this constructor will not throw aRestRequest.BadParameterException.- Parameters:
xContentRegistry- the content registryhttpRequest- the http requesthttpChannel- the http channel- Throws:
RestRequest.ContentTypeHeaderException- if the Content-Type header can not be parsed
-
method
public RestRequest.Method method()
Returns the HTTP method used in the REST request.- Returns:
- the
RestRequest.Methodused in the REST request - Throws:
java.lang.IllegalArgumentException- if the HTTP method is invalid
-
uri
public java.lang.String uri()
The uri of the rest request, with the query string.
-
rawPath
public java.lang.String rawPath()
The non decoded, raw path provided.
-
path
public final java.lang.String path()
The path part of the URI (without the query string), decoded.
-
hasContent
public boolean hasContent()
-
content
public BytesReference content()
-
content
protected BytesReference content(boolean contentConsumed)
-
requiredContent
public final BytesReference requiredContent()
- Returns:
- content of the request body or throw an exception if the body or content type is missing
-
header
public final java.lang.String header(java.lang.String name)
Get the value of the header ornullif not found. This method only retrieves the first header value if multiple values are sent. Use ofgetAllHeaderValues(String)should be preferred
-
getAllHeaderValues
public final java.util.List<java.lang.String> getAllHeaderValues(java.lang.String name)
Get all values for the header ornullif the header was not found
-
getHeaders
public final java.util.Map<java.lang.String,java.util.List<java.lang.String>> getHeaders()
Get all of the headers and values associated with the headers. Modifications of this map are not supported.
-
getXContentType
@Nullable public final XContentType getXContentType()
TheXContentTypethat was parsed from theContent-Typeheader. This value will benullin the case of a request without a validContent-Typeheader, a request without content (hasContent(), or a plain text request
-
getHttpChannel
public HttpChannel getHttpChannel()
-
getHttpRequest
public HttpRequest getHttpRequest()
-
hasParam
public final boolean hasParam(java.lang.String key)
-
param
public final java.lang.String param(java.lang.String key)
- Specified by:
paramin interfaceToXContent.Params
-
param
public final java.lang.String param(java.lang.String key, java.lang.String defaultValue)- Specified by:
paramin interfaceToXContent.Params
-
params
public java.util.Map<java.lang.String,java.lang.String> params()
-
paramAsFloat
public float paramAsFloat(java.lang.String key, float defaultValue)
-
paramAsInt
public int paramAsInt(java.lang.String key, int defaultValue)
-
paramAsLong
public long paramAsLong(java.lang.String key, long defaultValue)
-
paramAsBoolean
public boolean paramAsBoolean(java.lang.String key, boolean defaultValue)- Specified by:
paramAsBooleanin interfaceToXContent.Params
-
paramAsBoolean
public java.lang.Boolean paramAsBoolean(java.lang.String key, java.lang.Boolean defaultValue)- Specified by:
paramAsBooleanin interfaceToXContent.Params
-
paramAsSize
public ByteSizeValue paramAsSize(java.lang.String key, ByteSizeValue defaultValue)
-
paramAsStringArray
public java.lang.String[] paramAsStringArray(java.lang.String key, java.lang.String[] defaultValue)
-
paramAsStringArrayOrEmptyIfAll
public java.lang.String[] paramAsStringArrayOrEmptyIfAll(java.lang.String key)
-
getXContentRegistry
public NamedXContentRegistry getXContentRegistry()
Get theNamedXContentRegistrythat should be used to create parsers from this request.
-
contentParser
public final XContentParser contentParser() throws java.io.IOException
A parser for the contents of this request if there is a body, otherwise throws anElasticsearchParseException. UseapplyContentParser(CheckedConsumer)if you want to gracefully handle when the request doesn't have any contents. UsecontentOrSourceParamParser()for requests that support specifying the request body in thesourceparam.- Throws:
java.io.IOException
-
applyContentParser
public final void applyContentParser(CheckedConsumer<XContentParser,java.io.IOException> applyParser) throws java.io.IOException
If there is any content then callapplyParserwith the parser, otherwise do nothing.- Throws:
java.io.IOException
-
hasContentOrSourceParam
public final boolean hasContentOrSourceParam()
Does this request have content or asourceparameter? Use this instead ofhasContent()if this RestHandler treats thesourceparameter like the body content.
-
contentOrSourceParamParser
public final XContentParser contentOrSourceParamParser() throws java.io.IOException
A parser for the contents of this request if it has contents, otherwise a parser for thesourceparameter if there is one, otherwise throws anElasticsearchParseException. UsewithContentOrSourceParamParserOrNull(CheckedConsumer)instead if you need to handle the absence request content gracefully.- Throws:
java.io.IOException
-
withContentOrSourceParamParserOrNull
public final void withContentOrSourceParamParserOrNull(CheckedConsumer<XContentParser,java.io.IOException> withParser) throws java.io.IOException
Call a consumer with the parser for the contents of this request if it has contents, otherwise with a parser for thesourceparameter if there is one, otherwise withnull. UsecontentOrSourceParamParser()if you should throw an exception back to the user when there isn't request content.- Throws:
java.io.IOException
-
contentOrSourceParam
public final Tuple<XContentType,BytesReference> contentOrSourceParam()
Get the content of the request or the contents of thesourceparam or throw an exception if both are missing. PrefercontentOrSourceParamParser()orwithContentOrSourceParamParserOrNull(CheckedConsumer)if you need a parser.
-
parseContentType
public static XContentType parseContentType(java.util.List<java.lang.String> header)
Parses the given content type string for the media type. This method currently ignores parameters.
-
-