Updated to latest servlet 4.0 draft

This commit is contained in:
Greg Wilkins 2017-05-03 11:04:38 +02:00
parent e18c90fbd4
commit ce8342f12a
11 changed files with 140 additions and 129 deletions

View File

@ -104,7 +104,7 @@ public class TrailersTest extends AbstractTest
{
Request jettyRequest = (Request)request;
// No trailers yet.
Assert.assertNull(jettyRequest.getTrailers());
Assert.assertNull(jettyRequest.getTrailerHttpFields());
trailerLatch.countDown();
@ -118,7 +118,7 @@ public class TrailersTest extends AbstractTest
}
// Now we have the trailers.
HttpFields trailers = jettyRequest.getTrailers();
HttpFields trailers = jettyRequest.getTrailerHttpFields();
Assert.assertNotNull(trailers);
Assert.assertNotNull(trailers.get("X-Trailer"));
}

View File

@ -123,10 +123,10 @@ public class PushedResourcesTest extends AbstractTest
}
else
{
baseRequest.getPushBuilder()
baseRequest.newPushBuilder()
.path(path1)
.push();
baseRequest.getPushBuilder()
baseRequest.newPushBuilder()
.path(path2)
.push();
response.getOutputStream().write(bytes);
@ -185,7 +185,7 @@ public class PushedResourcesTest extends AbstractTest
else if (target.equals(newPath))
response.getOutputStream().write(pushBytes);
else
baseRequest.getPushBuilder().path(oldPath).push();
baseRequest.newPushBuilder().path(oldPath).push();
}
});

View File

@ -47,12 +47,10 @@ public class PushBuilderImpl implements PushBuilder
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)
public PushBuilderImpl(Request request, HttpFields fields, String method, String queryString, String sessionId)
{
super();
_request = request;
@ -60,10 +58,9 @@ public class PushBuilderImpl implements PushBuilder
_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);
LOG.debug("PushBuilder({} {}?{} s={} c={})",_method,_request.getRequestURI(),_queryString,_sessionId);
}
/* ------------------------------------------------------------ */
@ -111,21 +108,6 @@ public class PushBuilderImpl implements PushBuilder
return this;
}
/* ------------------------------------------------------------ */
@Override
public boolean isConditional()
{
return _conditional;
}
/* ------------------------------------------------------------ */
@Override
public PushBuilder conditional(boolean conditional)
{
_conditional = conditional;
return this;
}
/* ------------------------------------------------------------ */
@Override
public Set<String> getHeaderNames()
@ -179,35 +161,6 @@ public class PushBuilderImpl implements PushBuilder
return this;
}
/* ------------------------------------------------------------ */
@Override
public String getEtag()
{
return _etag;
}
/* ------------------------------------------------------------ */
@Override
public PushBuilder etag(String etag)
{
_etag = etag;
return this;
}
/* ------------------------------------------------------------ */
@Override
public String getLastModified()
{
return _lastModified;
}
/* ------------------------------------------------------------ */
@Override
public PushBuilder lastModified(String lastModified)
{
_lastModified = lastModified;
return this;
}
/* ------------------------------------------------------------ */
@Override
@ -240,14 +193,6 @@ public class PushBuilderImpl implements PushBuilder
// _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);
@ -256,7 +201,6 @@ public class PushBuilderImpl implements PushBuilder
_request.getHttpChannel().getHttpTransport().push(push);
_path=null;
_etag=null;
_lastModified=null;
}

View File

@ -36,6 +36,7 @@ import java.util.Collection;
import java.util.Collections;
import java.util.Enumeration;
import java.util.EventListener;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
@ -59,8 +60,10 @@ import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpUpgradeHandler;
import javax.servlet.http.MappingMatch;
import javax.servlet.http.Part;
import javax.servlet.http.PushBuilder;
import javax.servlet.http.ServletMapping;
import org.eclipse.jetty.http.BadMessageException;
import org.eclipse.jetty.http.HostPortHttpField;
@ -219,7 +222,23 @@ public class Request implements HttpServletRequest
return metadata==null?null:metadata.getFields();
}
public HttpFields getTrailers()
/* ------------------------------------------------------------ */
@Override
public Map<String,String> getTrailers()
{
if (_trailers==null)
return Collections.emptyMap();
Map<String,String> trailers = new HashMap<>();
for (HttpField field : _trailers)
{
String key = field.getName().toLowerCase();
String value = trailers.get(key);
trailers.put(key,value==null?field.getValue():value+","+field.getValue());
}
return trailers;
}
public HttpFields getTrailerHttpFields()
{
return _trailers;
}
@ -243,14 +262,20 @@ public class Request implements HttpServletRequest
}
/* ------------------------------------------------------------ */
@Override
@Deprecated
public PushBuilder getPushBuilder()
{
return newPushBuilder();
}
/* ------------------------------------------------------------ */
@Override
public PushBuilder newPushBuilder()
{
if (!isPushSupported())
throw new IllegalStateException(String.format("%s,push=%b,channel=%s", this, isPush(), getHttpChannel()));
HttpFields fields = new HttpFields(getHttpFields().size()+5);
boolean conditional=false;
for (HttpField field : getHttpFields())
{
@ -275,7 +300,7 @@ public class Request implements HttpServletRequest
case IF_NONE_MATCH:
case IF_MODIFIED_SINCE:
conditional=true;
// TODO
continue;
default:
@ -301,7 +326,7 @@ public class Request implements HttpServletRequest
id=getRequestedSessionId();
}
PushBuilder builder = new PushBuilderImpl(this,fields,getMethod(),getQueryString(),id,conditional);
PushBuilder builder = new PushBuilderImpl(this,fields,getMethod(),getQueryString(),id);
builder.addHeader("referer",getRequestURL().toString());
// TODO process any set cookies
@ -2429,7 +2454,7 @@ public class Request implements HttpServletRequest
// TODO replace with overriden version from API
public Mapping getMapping()
public ServletMapping getMapping()
{
final PathSpec pathSpec = _pathSpec;
final MappingMatch match;
@ -2460,18 +2485,18 @@ public class Request implements HttpServletRequest
mapping = _servletPath;
break;
default:
match = MappingMatch.UNKNOWN;
match = null;
mapping = _servletPath;
break;
}
}
else
{
match = MappingMatch.UNKNOWN;
match = null;
mapping = _servletPath;
}
return new Mapping()
return new ServletMapping()
{
@Override
public String getMatchValue()
@ -2488,57 +2513,16 @@ public class Request implements HttpServletRequest
}
@Override
public MappingMatch getMatchType()
public String getServletName()
{
return Request.this.getServletName();
}
@Override
public MappingMatch getMappingMatch()
{
return match;
}
public String getServletName()
{
return null;
}
};
}
/**
* Represents how the request from which this object was obtained was mapped to
* the associated servlet.
* TODO replace with API version
* @since 4.0
*/
public interface Mapping {
/**
* @return The value that was matched or the empty String if not known.
*/
String getMatchValue();
/**
* @return The {@code url-pattern} that matched this request or the empty
* String if not known.
*/
String getPattern();
/**
* @return The type of match ({@link MappingMatch#UNKNOWN} if not known)
*/
MappingMatch getMatchType();
}
/**
* Represents the ways that a request can be mapped to a servlet
* TODO replace with API version
* @since 4.0
*/
public enum MappingMatch
{
CONTEXT_ROOT,
DEFAULT,
EXACT,
EXTENSION,
IMPLICIT,
PATH,
UNKNOWN
}
}

View File

@ -26,6 +26,7 @@ import java.util.Collections;
import java.util.EnumSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Supplier;
import java.util.stream.Collectors;
@ -1312,11 +1313,34 @@ public class Response implements HttpServletResponse
_out.resetBuffer();
}
public void setTrailers(Supplier<HttpFields> trailers)
@Deprecated
public void setTrailerHttpFields(Supplier<HttpFields> trailers)
{
this.trailers = trailers;
}
@Override
public void setTrailers(Supplier<Map<String,String>> trailers)
{
// TODO new for 4.0 - avoid transient supplier?
this.trailers = new Supplier<HttpFields>()
{
@Override
public HttpFields get()
{
Map<String,String> t = trailers.get();
if (t==null)
return null;
HttpFields fields = new HttpFields();
for (Map.Entry<String,String> e : t.entrySet())
{
fields.add(e.getKey(),e.getValue());
}
return fields;
}
};
}
public Supplier<HttpFields> getTrailers()
{
return trailers;

View File

@ -2723,6 +2723,14 @@ public class ContextHandler extends ScopedHandler implements Attributes, Gracefu
return null;
}
@Override
public ServletRegistration.Dynamic addJspFile(String servletName, String jspFile)
{
// TODO new in 4.0
LOG.warn(__unimplmented);
return null;
}
@Override
public javax.servlet.ServletRegistration.Dynamic addServlet(String servletName, Servlet servlet)
{
@ -2887,6 +2895,51 @@ public class ContextHandler extends ScopedHandler implements Attributes, Gracefu
{
return null;
}
@Override
public int getSessionTimeout()
{
// TODO new in 4.0
LOG.warn(__unimplmented);
return 0;
}
@Override
public void setSessionTimeout(int sessionTimeout)
{
// TODO new in 4.0
LOG.warn(__unimplmented);
}
@Override
public String getRequestCharacterEncoding()
{
// TODO new in 4.0
LOG.warn(__unimplmented);
return null;
}
@Override
public void setRequestCharacterEncoding(String encoding)
{
// TODO new in 4.0
LOG.warn(__unimplmented);
}
@Override
public String getResponseCharacterEncoding()
{
// TODO new in 4.0
LOG.warn(__unimplmented);
return null;
}
@Override
public void setResponseCharacterEncoding(String encoding)
{
// TODO new in 4.0
LOG.warn(__unimplmented);
}
}

View File

@ -431,6 +431,7 @@ public class ServletHandler extends ScopedHandler
// Get the base requests
final String old_servlet_path=baseRequest.getServletPath();
final String old_path_info=baseRequest.getPathInfo();
final PathSpec old_path_spec=baseRequest.getPathSpec();
DispatcherType type = baseRequest.getDispatcherType();
@ -482,6 +483,7 @@ public class ServletHandler extends ScopedHandler
{
baseRequest.setServletPath(old_servlet_path);
baseRequest.setPathInfo(old_path_info);
baseRequest.setPathSpec(old_path_spec);
}
}
}

View File

@ -267,7 +267,7 @@ public class PushCacheFilter implements Filter
// Push associated resources.
if (!conditional && !primaryResource._associated.isEmpty())
{
PushBuilder pushBuilder = jettyRequest.getPushBuilder();
PushBuilder pushBuilder = jettyRequest.newPushBuilder();
// Breadth-first push of associated resources.
Queue<PrimaryResource> queue = new ArrayDeque<>();

View File

@ -155,13 +155,15 @@ public class PushSessionCacheFilter implements Filter
// push any associated resources
if (baseRequest.isPushSupported() && !baseRequest.isPush() && !target._associated.isEmpty())
{
boolean conditional = baseRequest.getHttpFields().contains(HttpHeader.IF_NONE_MATCH) ||
baseRequest.getHttpFields().contains(HttpHeader.IF_MODIFIED_SINCE);
// Breadth-first push of associated resources.
Queue<Target> queue = new ArrayDeque<>();
queue.offer(target);
while (!queue.isEmpty())
{
Target parent = queue.poll();
PushBuilder builder = baseRequest.getPushBuilder();
PushBuilder builder = baseRequest.newPushBuilder();
builder.addHeader("X-Pusher", PushSessionCacheFilter.class.toString());
for (Target child : parent._associated.values())
{
@ -171,7 +173,9 @@ public class PushSessionCacheFilter implements Filter
if (LOG.isDebugEnabled())
LOG.debug("PUSH {} <- {}", path, uri);
builder.path(path).etag(child._etag).lastModified(child._lastModified).push();
builder.path(path)
.setHeader(HttpHeader.IF_NONE_MATCH.asString(),conditional?child._etag:null)
.setHeader(HttpHeader.IF_MODIFIED_SINCE.asString(),conditional?child._lastModified:null);
}
}
}

View File

@ -864,7 +864,7 @@
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.0-b01</version>
<version>4.0.0-b06-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>javax.websocket</groupId>

View File

@ -81,7 +81,7 @@ public class HttpTrailersTest extends AbstractTest
}
// Now the trailers can be accessed.
HttpFields trailers = jettyRequest.getTrailers();
HttpFields trailers = jettyRequest.getTrailerHttpFields();
Assert.assertNotNull(trailers);
Assert.assertEquals(trailerValue, trailers.get(trailerName));
}
@ -118,7 +118,7 @@ public class HttpTrailersTest extends AbstractTest
}
// Now the trailers can be accessed.
HttpFields trailers = jettyRequest.getTrailers();
HttpFields trailers = jettyRequest.getTrailerHttpFields();
Assert.assertNull(trailers);
}
});
@ -157,7 +157,7 @@ public class HttpTrailersTest extends AbstractTest
trailers.put(trailerName, trailerValue);
Response jettyResponse = (Response)response;
jettyResponse.setTrailers(() -> trailers);
jettyResponse.setTrailerHttpFields(() -> trailers);
if (content != null)
response.getOutputStream().write(content);
}
@ -199,7 +199,7 @@ public class HttpTrailersTest extends AbstractTest
HttpFields trailers = new HttpFields();
Response jettyResponse = (Response)response;
jettyResponse.setTrailers(() -> trailers);
jettyResponse.setTrailerHttpFields(() -> trailers);
}
});