cleaned up PushBuilder API

This commit is contained in:
Greg Wilkins 2015-03-11 12:06:51 +11:00
parent c5541cb3bd
commit 0592ecf3e9
4 changed files with 466 additions and 156 deletions

View File

@ -20,167 +20,165 @@ package org.eclipse.jetty.server;
import java.util.Set;
import org.eclipse.jetty.http.HttpField;
import org.eclipse.jetty.http.HttpFields;
import org.eclipse.jetty.http.HttpHeader;
import org.eclipse.jetty.http.HttpMethod;
import org.eclipse.jetty.http.HttpURI;
import org.eclipse.jetty.http.MetaData;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/* ------------------------------------------------------------ */
/**
/** Build a request to be pushed.
* A PushBuilder is obtained by calling {@link Request#getPushBuilder()}
* which creates an initialises the builder as follows:<ul>
* <li> Each call to getPushBuilder() will return a new instance of a
* PushBuilder based off the Request. Any mutations to the
* returned PushBuilder are not reflected on future returns.</li>
* <li>The method is initialised to "GET"</li>
* <li>The requests headers are added to the Builder, except for:<ul>
* <li>Conditional headers (eg. If-Modified-Since)
* <li>Range headers
* <li>Expect headers
* <li>Authorisation headers
* <li>Referrer headers
* </ul></li>
* <li>If the request was Authenticated, an Authorisation header will
* be set with a container generated token that will result in equivalent
* Authorisation for the pushed request</li>
* <li>The query string from {@link ServletRequest#getQueryString()}
* <li>The {@link ServletRequest#getRequestedSessionId()} value, unless at the time
* of the call {@link ServletRequest#getSession(boolean)}
* has previously been called to create a new {@link HttpSession}, in
* which case the new session ID will be used as the PushBuilders
* requested session ID. The source of the requested session id will be the
* same as for the request</li>
* <li>The Referer header will be set to {@link ServletRequest#getRequestURL()}
* plus any {@link ServletRequest#getQueryString()} </li>
* <li>If {@link HttpServletResponse#addCookie(Cookie)} has been called
* on the associated response, then a corresponding Cookie header will be added
* to the PushBuilder, unless the {@link Cookie#getMaxAge()} is <=0, in which
* case the Cookie will be removed from the builder.</li>
* <li>If this request has has the conditional headers If-Modified-Since or
* If-None-Match then the {@link #isConditional()} header is set to true.</li>
* </ul>
* <p>A PushBuilder can be customised by chained calls to mutator methods before the
* {@link #push()} method is called to initiate a push request with the current state
* of the builder. After the call to {@link #push()}, the builder may be reused for
* another push, however the {@link #path(String)}, {@link #etag(String)} and
* {@link #lastModified(String)} values will have been nulled. All other
* values are retained over calls to {@link #push()}.
*/
public class PushBuilder
{
private static final Logger LOG = Log.getLogger(PushBuilder.class);
private final static HttpField JettyPush = new HttpField("x-http2-push","PushBuilder");
private final Request _request;
private final HttpFields _fields;
private String _method;
private String _queryString;
private String _sessionId;
private boolean _conditional;
public PushBuilder(Request request, HttpFields fields, String method, String queryString, String sessionId, boolean conditional)
{
super();
_request = request;
_fields = fields;
_method = method;
_queryString = queryString;
_sessionId = sessionId;
_conditional = conditional;
_fields.add(JettyPush);
if (LOG.isDebugEnabled())
LOG.debug("PushBuilder({} {}?{} s={} c={})",_method,_request.getRequestURI(),_queryString,_sessionId,_conditional);
}
public String getMethod()
{
return _method;
}
public PushBuilder setMethod(String method)
{
_method = method;
return this;
}
public String getQueryString()
{
return _queryString;
}
public PushBuilder setQueryString(String queryString)
{
_queryString = queryString;
return this;
}
public String getSessionId()
{
return _sessionId;
}
public PushBuilder setSessionId(String sessionId)
{
_sessionId = sessionId;
return this;
}
public boolean isConditional()
{
return _conditional;
}
public PushBuilder setConditional(boolean conditional)
{
_conditional = conditional;
return this;
}
public Set<String> getHeaderNames()
{
return _fields.getFieldNamesCollection();
}
public String getHeader(String name)
{
return _fields.get(name);
}
public PushBuilder setHeader(String name,String value)
{
_fields.put(name,value);
return this;
}
public void addHeader(String name,String value)
{
_fields.add(name,value);
}
/* ------------------------------------------------------------ */
/** Push a resource.
* Equivalent to {@link #push(String, String, String)} with null etag and
* lastModified.
* @param uriInContext
public interface PushBuilder
{
/** Set the method to be used for the push.
* Defaults to GET.
* @param method the method to be used for the push.
* @return this builder.
*/
public void push(String uriInContext)
{
push(uriInContext,null,null);
}
public abstract PushBuilder method(String method);
/* ------------------------------------------------------------ */
/** Set the query string to be used for the push.
* Defaults to the requests query string.
* Will be appended to any query String included in a call to {@link #path(String)}. This
* method should be used instead of a query in {@link #path(String)} when multiple
* {@link #push()} calls are to be made with the same query string, or to remove a
* query string obtained from the associated request.
* @param queryString the query string to be used for the push.
* @return this builder.
*/
public abstract PushBuilder queryString(String queryString);
/** 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.
* @param sessionId the SessionID to be used for the push.
* @return this builder.
*/
public abstract PushBuilder sessionId(String sessionId);
/** Set if the request is to be conditional.
* If the request is conditional, any available values from {@link #etag(String)} or
* {@link #lastModified(String)} will be set in the appropriate headers. If the request
* is not conditional, then etag and lastModified values are ignored.
* Defaults to true if the associated request was conditional.
* @param conditional true if the push request is conditional
* @return this builder.
*/
public abstract PushBuilder conditional(boolean conditional);
/** Set a header to be used for the push.
* @param name The header name to set
* @param value The header value to set
* @return this builder.
*/
public abstract PushBuilder setHeader(String name, String value);
/** Add a header to be used for the push.
* @param name The header name to add
* @param value The header value to add
* @return this builder.
*/
public abstract PushBuilder addHeader(String name, String value);
/** 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 {@link #path(String)} must be called
* before every call to {@link #push()}
* @param the URI path to be used for the push, which may include a
* query string.
* @return this builder.
*/
public abstract PushBuilder path(String path);
/** Set the etag to be used for conditional pushes.
* The etag will be used only if {@link #isConditional()} is true.
* Defaults to no etag. The value is nulled after every call to
* {@link #push()}
* @param the etag to be used for the push.
* @return this builder.
*/
public abstract PushBuilder etag(String etag);
/** Set the last modified date to be used for conditional pushes.
* The last modified date will be used only if {@link #isConditional()} is true.
* Defaults to no date. The value is nulled after every call to
* {@link #push()}
* @param the last modified date to be used for the push.
* @return this builder.
* */
public abstract PushBuilder lastModified(String lastModified);
/** Push a resource.
* Push a resource based on the current state of the PushBuilder. If {@link #isConditional()}
* is true and an etag or lastModified value is provided, then an appropriate conditional header
* will be generated. If an etag and lastModified value are provided only an If-None-Match header
* will be generated. If both an etag and lastModified value are provided only an If-None-Match header
* will be generated. If the builder has a session ID, then the pushed request
* will include the session ID either as a Cookie or as a URI parameter as appropriate.The builders
* will include the session ID either as a Cookie or as a URI parameter as appropriate. The builders
* query string is merged with any passed query string.
* @param path The URI of the resource to push.
* @param etag The etag for the resource or null if not available
* @param lastModified The last modified date of the resource or null if not available
* @throws IllegalArgumentException if the method set expects a request
* body (eg POST)
* After initiating the push, the builder has its path, etag and lastModified fields nulled. All
* other fields are left as is for possible reuse in another push.
* @throws IllegalArgumentException if the method set expects a request body (eg POST)
*/
public void push(String path,String etag,String lastModified)
{
if (HttpMethod.POST.is(_method) || HttpMethod.PUT.is(_method))
throw new IllegalStateException("Bad Method "+_method);
String query=_queryString;
int q=path.indexOf('?');
if (q>=0)
{
query=path.substring(q+1)+'&'+query;
path=path.substring(0,q);
}
String param=null;
if (_sessionId!=null && _request.isRequestedSessionIdFromURL())
param="jsessionid="+_sessionId;
if (_conditional)
{
if (etag!=null)
_fields.add(HttpHeader.IF_NONE_MATCH,etag);
else if (lastModified!=null)
_fields.add(HttpHeader.IF_MODIFIED_SINCE,lastModified);
}
HttpURI uri = HttpURI.createHttpURI(_request.getScheme(),_request.getServerName(),_request.getServerPort(),path,param,query,null);
MetaData.Request push = new MetaData.Request(_method,uri,_request.getHttpVersion(),_fields);
if (LOG.isDebugEnabled())
LOG.debug("Push {} {} inm={} ims={}",_method,uri,_fields.get(HttpHeader.IF_NONE_MATCH),_fields.get(HttpHeader.IF_MODIFIED_SINCE));
_request.getHttpChannel().getHttpTransport().push(push);
}
}
public abstract void push();
public abstract String getMethod();
public abstract String getQueryString();
public abstract String getSessionId();
public abstract boolean isConditional();
public abstract Set<String> getHeaderNames();
public abstract String getHeader(String name);
public abstract String getPath();
public abstract String getEtag();
public abstract String getLastModified();
}

View File

@ -0,0 +1,311 @@
//
// ========================================================================
// Copyright (c) 1995-2015 Mort Bay Consulting Pty. Ltd.
// ------------------------------------------------------------------------
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution.
//
// The Eclipse Public License is available at
// http://www.eclipse.org/legal/epl-v10.html
//
// The Apache License v2.0 is available at
// http://www.opensource.org/licenses/apache2.0.php
//
// You may elect to redistribute this code under either of these licenses.
// ========================================================================
//
package org.eclipse.jetty.server;
import java.util.Set;
import org.eclipse.jetty.http.HttpField;
import org.eclipse.jetty.http.HttpFields;
import org.eclipse.jetty.http.HttpHeader;
import org.eclipse.jetty.http.HttpMethod;
import org.eclipse.jetty.http.HttpURI;
import org.eclipse.jetty.http.MetaData;
import org.eclipse.jetty.util.URIUtil;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;
/* ------------------------------------------------------------ */
/**
*/
public class PushBuilderImpl implements PushBuilder
{
private static final Logger LOG = Log.getLogger(PushBuilderImpl.class);
private final static HttpField JettyPush = new HttpField("x-http2-push","PushBuilder");
private final Request _request;
private final HttpFields _fields;
private String _method;
private String _queryString;
private String _sessionId;
private boolean _conditional;
private String _path;
private String _etag;
private String _lastModified;
public PushBuilderImpl(Request request, HttpFields fields, String method, String queryString, String sessionId, boolean conditional)
{
super();
_request = request;
_fields = fields;
_method = method;
_queryString = queryString;
_sessionId = sessionId;
_conditional = conditional;
_fields.add(JettyPush);
if (LOG.isDebugEnabled())
LOG.debug("PushBuilder({} {}?{} s={} c={})",_method,_request.getRequestURI(),_queryString,_sessionId,_conditional);
}
/* ------------------------------------------------------------ */
/**
* @see org.eclipse.jetty.server.PushBuilder#getMethod()
*/
@Override
public String getMethod()
{
return _method;
}
/* ------------------------------------------------------------ */
/**
* @see org.eclipse.jetty.server.PushBuilder#method(java.lang.String)
*/
@Override
public PushBuilder method(String method)
{
_method = method;
return this;
}
/* ------------------------------------------------------------ */
/**
* @see org.eclipse.jetty.server.PushBuilder#getQueryString()
*/
@Override
public String getQueryString()
{
return _queryString;
}
/* ------------------------------------------------------------ */
/**
* @see org.eclipse.jetty.server.PushBuilder#queryString(java.lang.String)
*/
@Override
public PushBuilder queryString(String queryString)
{
_queryString = queryString;
return this;
}
/* ------------------------------------------------------------ */
/**
* @see org.eclipse.jetty.server.PushBuilder#getSessionId()
*/
@Override
public String getSessionId()
{
return _sessionId;
}
/* ------------------------------------------------------------ */
/**
* @see org.eclipse.jetty.server.PushBuilder#sessionId(java.lang.String)
*/
@Override
public PushBuilder sessionId(String sessionId)
{
_sessionId = sessionId;
return this;
}
/* ------------------------------------------------------------ */
/**
* @see org.eclipse.jetty.server.PushBuilder#isConditional()
*/
@Override
public boolean isConditional()
{
return _conditional;
}
/* ------------------------------------------------------------ */
/**
* @see org.eclipse.jetty.server.PushBuilder#conditional(boolean)
*/
@Override
public PushBuilder conditional(boolean conditional)
{
_conditional = conditional;
return this;
}
/* ------------------------------------------------------------ */
/**
* @see org.eclipse.jetty.server.PushBuilder#getHeaderNames()
*/
@Override
public Set<String> getHeaderNames()
{
return _fields.getFieldNamesCollection();
}
/* ------------------------------------------------------------ */
/**
* @see org.eclipse.jetty.server.PushBuilder#getHeader(java.lang.String)
*/
@Override
public String getHeader(String name)
{
return _fields.get(name);
}
/* ------------------------------------------------------------ */
/**
* @see org.eclipse.jetty.server.PushBuilder#setHeader(java.lang.String, java.lang.String)
*/
@Override
public PushBuilder setHeader(String name,String value)
{
_fields.put(name,value);
return this;
}
/* ------------------------------------------------------------ */
/**
* @see org.eclipse.jetty.server.PushBuilder#addHeader(java.lang.String, java.lang.String)
*/
@Override
public PushBuilder addHeader(String name,String value)
{
_fields.add(name,value);
return this;
}
/* ------------------------------------------------------------ */
/**
* @see org.eclipse.jetty.server.PushBuilder#getPath()
*/
@Override
public String getPath()
{
return _path;
}
/* ------------------------------------------------------------ */
/**
* @see org.eclipse.jetty.server.PushBuilder#path(java.lang.String)
*/
@Override
public PushBuilder path(String path)
{
_path = path;
return this;
}
/* ------------------------------------------------------------ */
/**
* @see org.eclipse.jetty.server.PushBuilder#getEtag()
*/
@Override
public String getEtag()
{
return _etag;
}
/* ------------------------------------------------------------ */
/**
* @see org.eclipse.jetty.server.PushBuilder#etag(java.lang.String)
*/
@Override
public PushBuilder etag(String etag)
{
_etag = etag;
return this;
}
/* ------------------------------------------------------------ */
/**
* @see org.eclipse.jetty.server.PushBuilder#getLastModified()
*/
@Override
public String getLastModified()
{
return _lastModified;
}
/* ------------------------------------------------------------ */
/**
* @see org.eclipse.jetty.server.PushBuilder#lastModified(java.lang.String)
*/
@Override
public PushBuilder lastModified(String lastModified)
{
_lastModified = lastModified;
return this;
}
/* ------------------------------------------------------------ */
/* ------------------------------------------------------------ */
/**
* @see org.eclipse.jetty.server.PushBuilder#push()
*/
@Override
public void push()
{
if (HttpMethod.POST.is(_method) || HttpMethod.PUT.is(_method))
throw new IllegalStateException("Bad Method "+_method);
if (_path==null || _path.length()==0)
throw new IllegalStateException("Bad Path "+_path);
String path=_path;
String query=_queryString;
int q=path.indexOf('?');
if (q>=0)
{
query=(query!=null && query.length()>0)?(_path.substring(q+1)+'&'+query):_path.substring(q+1);
path=_path.substring(0,q);
}
if (!path.startsWith("/"))
path=URIUtil.addPaths(_request.getContextPath(),path);
String param=null;
if (_sessionId!=null)
{
if (_request.isRequestedSessionIdFromURL())
param="jsessionid="+_sessionId;
// TODO else
// _fields.add("Cookie","JSESSIONID="+_sessionId);
}
if (_conditional)
{
if (_etag!=null)
_fields.add(HttpHeader.IF_NONE_MATCH,_etag);
else if (_lastModified!=null)
_fields.add(HttpHeader.IF_MODIFIED_SINCE,_lastModified);
}
HttpURI uri = HttpURI.createHttpURI(_request.getScheme(),_request.getServerName(),_request.getServerPort(),_path,param,query,null);
MetaData.Request push = new MetaData.Request(_method,uri,_request.getHttpVersion(),_fields);
if (LOG.isDebugEnabled())
LOG.debug("Push {} {} inm={} ims={}",_method,uri,_fields.get(HttpHeader.IF_NONE_MATCH),_fields.get(HttpHeader.IF_MODIFIED_SINCE));
_request.getHttpChannel().getHttpTransport().push(push);
_path=null;
_etag=null;
_lastModified=null;
}
}

View File

@ -258,7 +258,7 @@ public class Request implements HttpServletRequest
* to the PushBuilder, unless the {@link Cookie#getMaxAge()} is <=0, in which
* case the Cookie will be removed from the builder.</li>
* <li>If this request has has the conditional headers If-Modified-Since or
* If-None-Match then the {@link PushBuilder#isConditional()} header is set
* If-None-Match then the {@link PushBuilderImpl#isConditional()} header is set
* to true.
* </ul>
*
@ -328,7 +328,7 @@ public class Request implements HttpServletRequest
id=getRequestedSessionId();
}
PushBuilder builder = new PushBuilder(this,fields,getMethod(),getQueryString(),id,conditional);
PushBuilder builder = new PushBuilderImpl(this,fields,getMethod(),getQueryString(),id,conditional);
builder.addHeader("referer",getRequestURL().toString());
// TODO process any set cookies

View File

@ -168,13 +168,14 @@ public class PushSessionCacheFilter implements Filter
if (baseRequest.isPushSupported() && target._associated.size()>0)
{
PushBuilder builder = baseRequest.getPushBuilder();
builder.addHeader("X-Pusher",PushSessionCacheFilter.class.toString());
for (Target associated : target._associated.values())
{
String path = associated._path;
if (LOG.isDebugEnabled())
LOG.debug("PUSH {} <- {}",path,uri);
builder.push(path,associated._etag,associated._lastModified);
builder.path(path).etag(associated._etag).lastModified(associated._lastModified).push();
}
}