Class PushBuilder
- java.lang.Object
-
- org.glassfish.grizzly.http.server.http2.PushBuilder
-
public final class PushBuilder extends Object
Build a request to be pushed. This is based on Servlet 4.0's PushBuilder. According section 8.2 of RFC 7540, a promised request must be cacheable and safe without a request body.A PushBuilder is obtained by calling
Request.newPushBuilder(). Each call to this method will return a new instance of a PushBuilder based off the currentHttpServletRequest. Any mutations to the returned PushBuilder are not reflected on future returns.The instance is initialized as follows:
- The method is initialized to "GET"
- The existing request headers of the current
Requestare added to the builder, except for:- Conditional headers (eg. If-Modified-Since)
- Range headers
- Expect headers
- Authorization headers
- Referrer headers
- The
Request.getRequestedSessionId()value, unless at the time of the callRequest.getSession(boolean)has previously been called to create a newSession, in which case the new session ID will be used as the PushBuilder's requested session ID. The source of the requested session id will be the same as for the request - The Referer(sic) header will be set to
Request.getRequestURL()plus anyRequest.getQueryString() - If
Response.addCookie(Cookie)has been called on the associated response, then a corresponding Cookie header will be added to the PushBuilder, unless theCookie.getMaxAge()is <=0, in which case the Cookie will be removed from the builder.
The
pathmethod must be called on thePushBuilderinstance before the call topush(). Failure to do so must cause an exception to be thrown frompush(), as specified in that method.A PushBuilder can be customized by chained calls to mutator methods before the
push()method is called to initiate an asynchronous push request with the current state of the builder. After the call topush(), the builder may be reused for another push, however the path and conditional headers are cleared before returning frompush(). All other values are retained over calls topush().- Since:
- 2.3.30
-
-
Constructor Summary
Constructors Constructor Description PushBuilder(Request request)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description PushBuilderaddHeader(String name, String value)Add a request header to be used for the push.PushBuilderaddHeader(org.glassfish.grizzly.http.util.Header name, String value)Add a request header to be used for the push.StringgetHeader(String name)Return the header of the given name to be used for the push.Iterable<String>getHeaderNames()Return the set of header to be used for the push.StringgetMethod()Return the method to be used for the push.StringgetPath()Return the URI path to be used for the push.StringgetQueryString()Return the query string to be used for the push.StringgetSessionId()Return the SessionID to be used for the push.PushBuildermethod(String method)Set the method to be used for the push.PushBuilderpath(String path)Set the URI path to be used for the push.voidpush()Push a resource given the current state of the builder without blocking.PushBuilderqueryString(String queryString)Set the query string to be used for the push.PushBuilderremoveHeader(String name)Remove the named request header.PushBuilderremoveHeader(org.glassfish.grizzly.http.util.Header name)Remove the named request header.PushBuildersessionId(String sessionId)Set the SessionID to be used for the push.PushBuildersetHeader(String name, String value)Set a request header to be used for the push.PushBuildersetHeader(org.glassfish.grizzly.http.util.Header name, String value)Set a request header to be used for the push.
-
-
-
Constructor Detail
-
PushBuilder
public PushBuilder(Request request)
-
-
Method Detail
-
method
public PushBuilder method(String method)
Set the method to be used for the push.
- Parameters:
method- the method to be used for the push.- Returns:
- this builder.
- Throws:
NullPointerException- if the argument isnullIllegalArgumentException- if the argument is the empty String, or any non-cacheable or unsafe methods defined in RFC 7231, which are POST, PUT, DELETE, CONNECT, OPTIONS and TRACE.
-
queryString
public PushBuilder queryString(String queryString)
Set the query string to be used for the push.Will be appended to any query String included in a call to
path(String). Any duplicate parameters must be preserved. This method should be used instead of a query inpath(String)when multiplepush()calls are to be made with the same query string.- Parameters:
queryString- the query string to be used for the push.- Returns:
- this builder.
-
sessionId
public PushBuilder sessionId(String sessionId)
Set the SessionID to be used for the push. The session ID will be set in the same way it was on the associated request (ie as a cookie if the associated request used a cookie, or as a url parameter if the associated request used a url parameter). Defaults to the requested session ID or any newly assigned session id from a newly created session.- Parameters:
sessionId- the SessionID to be used for the push.- Returns:
- this builder.
-
setHeader
public PushBuilder setHeader(String name, String value)
Set a request header to be used for the push. If the builder has an existing header with the same name, its value is overwritten.
- Parameters:
name- The header name to setvalue- The header value to set- Returns:
- this builder.
-
setHeader
public PushBuilder setHeader(org.glassfish.grizzly.http.util.Header name, String value)
Set a request header to be used for the push. If the builder has an existing header with the same name, its value is overwritten.
- Parameters:
name- TheHeaderto setvalue- The header value to set- Returns:
- this builder.
-
addHeader
public PushBuilder addHeader(String name, String value)
Add a request header to be used for the push.
- Parameters:
name- The header name to addvalue- The header value to add- Returns:
- this builder.
-
addHeader
public PushBuilder addHeader(org.glassfish.grizzly.http.util.Header name, String value)
Add a request header to be used for the push.
- Parameters:
name- TheHeaderto addvalue- The header value to add- Returns:
- this builder.
-
removeHeader
public PushBuilder removeHeader(String name)
Remove the named request header. If the header does not exist, take no action.
- Parameters:
name- The name of the header to remove- Returns:
- this builder.
-
removeHeader
public PushBuilder removeHeader(org.glassfish.grizzly.http.util.Header name)
Remove the named request header. If the header does not exist, take no action.
- Parameters:
name- TheHeaderto remove- Returns:
- this builder.
-
path
public PushBuilder path(String path)
Set the URI path to be used for the push. The path may start with "/" in which case it is treated as an absolute path, otherwise it is relative to the context path of the associated request. There is no path default and path(String) must be called before every call topush(). If a query string is present in the argumentpath, its contents must be merged with the contents previously passed toqueryString, preserving duplicates.- Parameters:
path- the URI path to be used for the push, which may include a query string.- Returns:
- this builder.
-
push
public void push()
Push a resource given the current state of the builder without blocking.Push a resource based on the current state of the PushBuilder. Calling this method does not guarantee the resource will actually be pushed, since it is possible the client can decline acceptance of the pushed resource using the underlying HTTP/2 protocol.
Before returning from this method, the builder has its path set to null and all conditional headers removed. All other fields are left as is for possible reuse in another push.
- Throws:
IllegalStateException- if there was no call topathon this instance either between its instantiation or the last call topush()that did not throw an IllegalStateException.
-
getMethod
public String getMethod()
Return the method to be used for the push.- Returns:
- the method to be used for the push.
-
getQueryString
public String getQueryString()
Return the query string to be used for the push.- Returns:
- the query string to be used for the push.
-
getSessionId
public String getSessionId()
Return the SessionID to be used for the push.- Returns:
- the SessionID to be used for the push.
-
getHeaderNames
public Iterable<String> getHeaderNames()
Return the set of header to be used for the push.- Returns:
- the set of header to be used for the push.
-
getHeader
public String getHeader(String name)
Return the header of the given name to be used for the push.- Returns:
- the header of the given name to be used for the push.
-
getPath
public String getPath()
Return the URI path to be used for the push.- Returns:
- the URI path to be used for the push.
-
-