public class HttpRequestImpl extends Object implements HttpRequest
An HttpRequestImpl
can be modified after construction, e.g. by header(String, String)
.
These mutation methods return `this` for method chaining.
Constructor and Description |
---|
HttpRequestImpl(String method,
String uri,
HttpEntity entity)
Create an http request.
|
HttpRequestImpl(HttpRequest originRequest)
Copy an http request.
|
Instance Methods | |
---|---|
InetAddress |
ip()
Get the client IP.
|
boolean |
isHttps()
Whether this is an HTTPS request.
|
List<X509Certificate> |
certs()
Certificates of the client.
|
String |
method()
Get the request method.
|
String |
uri()
Get the request URI.
|
String |
uriPath()
The path component of the request URI.
|
String |
uriParam(String name)
Get the value of a parameter in the request URI.
|
String |
httpVersion()
Get the HTTP version.
|
HeaderMap |
headers()
Get the headers.
|
Map<String,String> |
cookies()
Get the request cookies.
|
HttpEntity |
entity()
Get the entity.
|
HttpRequestImpl |
ip(InetAddress ip)
Set the client IP.
|
HttpRequestImpl |
isHttps(boolean isHttps)
Set the
isHttps property.. |
HttpRequestImpl |
method(String method)
Set the request method.
|
HttpRequestImpl |
host(String host)
Set the "Host" header.
|
HttpRequestImpl |
uri(String uri)
Set the request URI.
|
HttpRequestImpl |
header(String name,
String value)
Set a header.
|
HttpRequestImpl |
cookie(String name,
String value)
Set a cookie.
|
HttpRequestImpl |
cookies(Map<String,String> cookies)
Set the cookies.
|
HttpRequestImpl |
entity(HttpEntity entity)
Set the entity.
|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
absoluteUri, acceptsGzip, bodyBytes, bodyString, cookie, header, host, scheme
public HttpRequestImpl(String method, String uri, HttpEntity entity)
See uri(String)
for permissible `uri` arguments.
It's preferred that `uri` is an absolute URI; otherwise,
the caller should set the host
afterwards.
Examples:
new HttpRequestImpl("GET", "https://example.com/", null); new HttpRequestImpl("GET", "/search?q=cat", null).host("localhost:8080"); new HttpRequestImpl("CONNECT", "proxy.com:8080", null); new HttpRequestImpl("OPTIONS", "*", null).host("example.com");
public HttpRequestImpl(HttpRequest originRequest)
This can be used for request transformation, for example to change the URI, add a header, etc. The origin request is treated as read-only and will not be modified.
public InetAddress ip()
See ip(java.net.InetAddress)
for changing the IP.
ip
in interface HttpRequest
public boolean isHttps()
See isHttps(boolean)
for changing this property.
isHttps
in interface HttpRequest
HttpRequest.scheme()
public List<X509Certificate> certs()
SSLSession.getPeerCertificates()
.certs
in interface HttpRequest
public String method()
See method(String)
for changing the method.
method
in interface HttpRequest
public String uri()
See uri(String)
for changing the URI.
uri
in interface HttpRequest
public String uriPath()
HttpRequest
The path is the substring before the first question mark ("?").
For example, if uri()=="/query?term=cat"
, then uriPath()=="/query"
.
uriPath
in interface HttpRequest
public String uriParam(String name)
HttpRequest
For example, if uri()=="/query?term=cat"
, then uriParam("term")=="cat"
.
The query component of the request URI is parsed as "application/x-www-form-urlencoded"
with UTF-8 charset. If there's any parsing error, this method returns null
.
See FormParser
for more options.
uriParam
in interface HttpRequest
public String httpVersion()
httpVersion
in interface HttpRequest
public HeaderMap headers()
The returned map is mutable; the request producer can manipulate it at will.
See also header(name,value)
.
headers
in interface HttpRequest
public Map<String,String> cookies()
The cookies are parsed from the "Cookie" header; see parent javadoc.
The returned Map is read-only. To modify cookies,
see cookie(String, String)
and cookies(java.util.Map)
methods;
or you can always set the "Cookie" header directly.
cookies
in interface HttpRequest
HttpRequest.cookie(String)
public HttpEntity entity()
See entity(HttpEntity)
for changing the entity;
entity
in interface HttpRequest
public HttpRequestImpl ip(InetAddress ip)
public HttpRequestImpl isHttps(boolean isHttps)
isHttps
property..public HttpRequestImpl method(String method)
public HttpRequestImpl host(String host)
public HttpRequestImpl uri(String uri) throws IllegalArgumentException
The behavior of this method depends on the request method
,
which should be settled before calling this method.
See HttpRequest.uri()
for permissible `uri` arguments.
In addition, this method also accepts absolute URI
,
which may change isHttps(boolean)
and host(String)
too.
Examples:
req.uri( "/search?q=cat" ); // path-query req.uri( "proxy.com:8080" ); // CONNECT req.uri( "*" ); // OPTIONS req.uri( "https://example.com/" ); // absolute URI
IllegalArgumentException
public HttpRequestImpl header(String name, String value)
If value==null
, the header will be removed.
public HttpRequestImpl cookie(String name, String value) throws IllegalArgumentException
If value==null
, the cookie will be removed.
See Cookie
for valid cookie name/value.
IllegalArgumentException
public HttpRequestImpl cookies(Map<String,String> cookies)
All existing cookies will be removed. This method sets a new "Cookie" header based on provided cookies.
See Cookie
for valid cookie name/value.
public HttpRequestImpl entity(HttpEntity entity)