Skip to content

Commit

Permalink
[#1802] Adjust code to Jetty 12 SNAPSHOT (20230626)
Browse files Browse the repository at this point in the history
  • Loading branch information
grgrzybek committed Jun 26, 2023
1 parent de2df6d commit 1b26a2f
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 82 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@
import jakarta.servlet.SessionCookieConfig;
import jakarta.servlet.annotation.ServletSecurity;
import jakarta.servlet.http.HttpSessionAttributeListener;
import org.eclipse.jetty.ee.security.ConstraintAware;
import org.eclipse.jetty.ee.security.ConstraintMapping;
import org.eclipse.jetty.ee10.servlet.ErrorPageErrorHandler;
import org.eclipse.jetty.ee10.servlet.FilterHolder;
import org.eclipse.jetty.ee10.servlet.FilterMapping;
Expand All @@ -34,6 +32,8 @@
import org.eclipse.jetty.ee10.servlet.ServletHolder;
import org.eclipse.jetty.ee10.servlet.ServletMapping;
import org.eclipse.jetty.ee10.servlet.SessionHandler;
import org.eclipse.jetty.ee10.servlet.security.ConstraintAware;
import org.eclipse.jetty.ee10.servlet.security.ConstraintMapping;
import org.eclipse.jetty.ee10.servlet.security.ConstraintSecurityHandler;
import org.eclipse.jetty.http.HttpCookie;
import org.eclipse.jetty.http.PreEncodedHttpField;
Expand Down Expand Up @@ -754,11 +754,11 @@ public void visitServletContextModelChange(ServletContextModelChange change) {

LOG.info("Creating new Jetty context for {}", model);

PaxWebServletContextHandler sch = new PaxWebServletContextHandler(null, contextPath, configuration);
PaxWebServletContextHandler sch = new PaxWebServletContextHandler(contextPath, configuration);
// special, OSGi-aware org.eclipse.jetty.servlet.ServletHandler
sch.setServletHandler(new PaxWebServletHandler(default404Servlet, new OsgiSessionAttributeListener(sessionListenerModels)));
// setting "false" here will trigger 302 redirect when browsing to context without trailing "/"
sch.setAllowNullPathInfo(false);
sch.setAllowNullPathInContext(false);
// welcome files will be handled at default/resource servlet level and OsgiServletContext
sch.setWelcomeFiles(new String[0]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,11 @@ public class PaxWebServletContextHandler extends ServletContextHandler {
* {@code org.eclipse.jetty.webapp.WebAppContext} which does all the sort of XML/annotation configuration, but
* we take some of the mechanisms from {@code WebAppContext} if they're useful in Pax Web.
*
* @param parent
* @param contextPath
* @param configuration
*/
public PaxWebServletContextHandler(Handler.Container parent, String contextPath, Configuration configuration) {
super(parent, contextPath, true, true);
public PaxWebServletContextHandler(String contextPath, Configuration configuration) {
super(contextPath, true, true);

// TCCL of sessionManager timer threads will be set to thread of pax-web-jetty bundle, not to current TCCL
ScheduledExecutorScheduler executorScheduler = new ScheduledExecutorScheduler(getSessionHandler().toString() + "Timer", true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
package org.ops4j.pax.web.service.jetty.internal;

import org.eclipse.jetty.ee10.servlet.ServletContextRequest;
import org.eclipse.jetty.ee10.servlet.ServletHandler;
import org.eclipse.jetty.http.pathmap.MatchedResource;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.session.DefaultSessionIdManager;
Expand All @@ -34,7 +36,8 @@ public PaxWebSessionIdManager(Server server) {

public static String getSessionIdSuffix(Request r) {
ServletContextRequest req = Request.as(r, ServletContextRequest.class);
if (req.getMappedServlet() != null && req.getMappedServlet().getServletHolder() instanceof PaxWebServletHolder holder) {
MatchedResource<ServletHandler.MappedServlet> match = req.getMatchedResource();
if (match != null && match.getResource() != null && match.getResource().getServletHolder() instanceof PaxWebServletHolder holder) {
OsgiContextModel ocm = holder.getOsgiContextModel();
// we can't replace '/' to '_' because of how
// org.eclipse.jetty.server.session.FileSessionDataStore.initializeStore() analyzes the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

package org.ops4j.pax.web.service.jetty.internal.web;

//CHECKSTYLE:OFF
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
Expand All @@ -33,8 +32,6 @@
import java.util.Set;
import java.util.StringTokenizer;
import java.util.concurrent.CompletableFuture;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.stream.Collectors;

Expand All @@ -48,7 +45,6 @@
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpServletResponseWrapper;
import org.eclipse.jetty.ee10.servlet.ServletApiRequest;
import org.eclipse.jetty.ee10.servlet.ServletApiResponse;
import org.eclipse.jetty.ee10.servlet.ServletContextHandler;
import org.eclipse.jetty.ee10.servlet.ServletContextRequest;
import org.eclipse.jetty.ee10.servlet.ServletContextResponse;
Expand All @@ -71,13 +67,17 @@
import org.eclipse.jetty.io.ByteBufferInputStream;
import org.eclipse.jetty.io.ByteBufferPool;
import org.eclipse.jetty.server.Context;
import org.eclipse.jetty.server.HttpStream;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.ResourceService;
import org.eclipse.jetty.server.Response;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.ContextHandler;
import org.eclipse.jetty.util.*;
import org.eclipse.jetty.util.Blocker;
import org.eclipse.jetty.util.BufferUtil;
import org.eclipse.jetty.util.Callback;
import org.eclipse.jetty.util.IO;
import org.eclipse.jetty.util.StringUtil;
import org.eclipse.jetty.util.URIUtil;
import org.eclipse.jetty.util.resource.Resource;
import org.eclipse.jetty.util.resource.ResourceFactory;
import org.eclipse.jetty.util.resource.Resources;
Expand Down Expand Up @@ -227,7 +227,7 @@ public void init() throws ServletException
}
}

// for welcome file handling we need some _baseResource
// Pax Web: for welcome file handling we need some _baseResource
_baseResource = configureBaseResource();

List<CompressedContentFormat> precompressedFormats = parsePrecompressedFormats(getInitParameter("precompressed"),
Expand Down Expand Up @@ -477,7 +477,7 @@ protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws Se
else if (isPathInfoOnly())
encodedPathInContext = URIUtil.encodePath(req.getPathInfo());
else if (req instanceof ServletApiRequest apiRequest)
encodedPathInContext = Context.getPathInContext(req.getContextPath(), apiRequest.getServletContextRequest().getHttpURI().getCanonicalPath());
encodedPathInContext = Context.getPathInContext(req.getContextPath(), apiRequest.getRequest().getHttpURI().getCanonicalPath());
else
encodedPathInContext = Context.getPathInContext(req.getContextPath(), URIUtil.canonicalPath(req.getRequestURI()));

Expand Down Expand Up @@ -649,17 +649,6 @@ public boolean isSecure()
return _servletRequest.isSecure();
}

@Override
public boolean addErrorListener(Predicate<Throwable> onError)
{
return false;
}

@Override
public void addHttpStreamWrapper(Function<HttpStream, HttpStream> wrapper)
{
}

@Override
public Object removeAttribute(String name)
{
Expand Down Expand Up @@ -908,12 +897,7 @@ public HttpFields.Mutable getHeaders()

public ServletContextResponse getServletContextResponse()
{
if (_response instanceof ServletApiResponse)
{
ServletApiResponse apiResponse = (ServletApiResponse)_response;
return apiResponse.getResponse();
}
return null;
return ServletContextResponse.getServletContextResponse(_response);
}

@Override
Expand Down Expand Up @@ -1033,6 +1017,12 @@ public CompletableFuture<Void> writeInterim(int status, HttpFields headers)
{
return null;
}

@Override
public String toString()
{
return "%s@%x{%s,%s}".formatted(this.getClass().getSimpleName(), hashCode(), this._coreRequest, _response);
}
}

private class ServletResourceService extends ResourceService implements ResourceService.WelcomeFactory
Expand All @@ -1045,23 +1035,14 @@ private ServletResourceService(ServletContextHandler servletContextHandler)
}

@Override
public String getWelcomeTarget(Request coreRequest)
public String getWelcomeTarget(HttpContent content, Request coreRequest)
{
String[] welcomes = _servletContextHandler.getWelcomeFiles();
if (welcomes == null)
return null;

HttpServletRequest request = getServletRequest(coreRequest);
String pathInContext = Request.getPathInContext(coreRequest);
String includedServletPath = (String)request.getAttribute(RequestDispatcher.INCLUDE_SERVLET_PATH);
String requestTarget;
if (includedServletPath != null)
requestTarget = getIncludedPathInContext(request, includedServletPath, isPathInfoOnly());
else
requestTarget = isPathInfoOnly() ? request.getPathInfo() : pathInContext;

String welcomeTarget = null;
Resource base = _baseResource.resolve(requestTarget);
Resource base = content.getResource();
if (Resources.isReadableDirectory(base))
{
for (String welcome : welcomes)
Expand All @@ -1070,13 +1051,16 @@ public String getWelcomeTarget(Request coreRequest)

// If the welcome resource is a file, it has
// precedence over resources served by Servlets.
Resource welcomePath = base.resolve(welcome);
Resource welcomePath = content.getResource().resolve(welcome);
if (Resources.isReadableFile(welcomePath))
return welcomeInContext;

// Check whether a Servlet may serve the welcome resource.
if (_welcomeServletMode != WelcomeServletMode.NONE && welcomeTarget == null)
{
if (isPathInfoOnly() && !isIncluded(getServletRequest(coreRequest)))
welcomeTarget = URIUtil.addPaths(getServletRequest(coreRequest).getPathInfo(), welcome);

ServletHandler.MappedServlet entry = _servletContextHandler.getServletHandler().getMappedServlet(welcomeInContext);
// Is there a different Servlet that may serve the welcome resource?
if (entry != null && entry.getServletHolder().getServletInstance() != DefaultServlet.this)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ public class EmbeddedJettyHttp2Test {

@BeforeEach
public void resetState() {
decoder = new HpackDecoder(4096, 8192);
decoder = new HpackDecoder(8192);
decoder.setMaxTableCapacity(4096);
responses = new HashMap<>();
}

Expand Down Expand Up @@ -253,7 +254,6 @@ public void http2NioExchange() throws Exception {
final CountDownLatch latch = new CountDownLatch(3);

ServletContextHandler handler = new ServletContextHandler("/");
handler.setAllowNullPathInfo(true);
handler.setAllowNullPathInContext(true);
handler.addServlet(new ServletHolder("default-servlet", new HttpServlet() {
@Override
Expand Down Expand Up @@ -468,7 +468,6 @@ public void http2NioExchangeWithDirectUpgrade() throws Exception {
final CountDownLatch latch = new CountDownLatch(3);

ServletContextHandler handler = new ServletContextHandler("/");
handler.setAllowNullPathInfo(true);
handler.setAllowNullPathInContext(true);
handler.addServlet(new ServletHolder("default-servlet", new HttpServlet() {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -302,8 +302,7 @@ public void embeddedServerWithServletContextHandler() throws Exception {
// servlet context handler extends ContextHandler for easier ContextHandler with _handler = ServletHandler
// created ServletContextHandler will already have session, security handlers (depending on options) and
// ServletHandler and we can add servlets/filters through ServletContextHandler
ServletContextHandler handler1 = new ServletContextHandler(null, "/c1", ServletContextHandler.NO_SESSIONS);
handler1.setAllowNullPathInfo(true); // for ServletContextHandler
ServletContextHandler handler1 = new ServletContextHandler("/c1", ServletContextHandler.NO_SESSIONS);
handler1.setAllowNullPathInContext(true); // for ContextHandler
// this single method adds both ServletHolder and ServletMapping
// calling org.eclipse.jetty.servlet.ServletHandler.addServletWithMapping()
Expand All @@ -317,8 +316,7 @@ protected void service(HttpServletRequest req, HttpServletResponse resp) throws
}
}), "/");

ServletContextHandler handler2 = new ServletContextHandler(null, "/c2", ServletContextHandler.NO_SESSIONS);
handler2.setAllowNullPathInfo(true);
ServletContextHandler handler2 = new ServletContextHandler("/c2", ServletContextHandler.NO_SESSIONS);
handler2.setAllowNullPathInContext(true);
handler2.addServlet(new ServletHolder("default-servlet", new HttpServlet() {
@Override
Expand Down Expand Up @@ -388,7 +386,7 @@ public void embeddedServerWithServletContextHandlerAnd404Servlet() throws Except

ContextHandlerCollection chc = new ContextHandlerCollection();

ServletContextHandler h = new ServletContextHandler(null, "/c1", ServletContextHandler.NO_SESSIONS);
ServletContextHandler h = new ServletContextHandler("/c1", ServletContextHandler.NO_SESSIONS);
h.setAllowNullPathInContext(false);
h.addServlet(new ServletHolder("default-servlet", new ServletHandler.Default404Servlet()), "/*");

Expand Down Expand Up @@ -430,8 +428,7 @@ public void embeddedServerWithServletContextHandlerAndDynamicInitializers() thro

ContextHandlerCollection chc = new ContextHandlerCollection();

ServletContextHandler handler1 = new ServletContextHandler(null, "/c1", ServletContextHandler.NO_SESSIONS);
handler1.setAllowNullPathInfo(true);
ServletContextHandler handler1 = new ServletContextHandler("/c1", ServletContextHandler.NO_SESSIONS);
handler1.setAllowNullPathInContext(true);

// SCI that adds a ServletContextListener which tries to add ServletContextListener
Expand Down Expand Up @@ -521,8 +518,8 @@ public void embeddedServerWithJettyResourceServlet() throws Exception {
server.setConnectors(new Connector[] { connector });

ContextHandlerCollection chc = new ContextHandlerCollection();
ServletContextHandler handler1 = new ServletContextHandler(null, "/", ServletContextHandler.NO_SESSIONS);
handler1.setAllowNullPathInfo(true);
ServletContextHandler handler1 = new ServletContextHandler("/", ServletContextHandler.NO_SESSIONS);
handler1.setAllowNullPathInContext(true);

ServletHolder sh1 = new ServletHolder("default", new DefaultServlet());
sh1.setInitParameter("dirAllowed", "false");
Expand Down Expand Up @@ -564,12 +561,11 @@ public void embeddedServerWithServletContextHandlerAndOnlyFilter() throws Except

ContextHandlerCollection chc = new ContextHandlerCollection();

ServletContextHandler handler1 = new ServletContextHandler(null, "/c1", ServletContextHandler.NO_SESSIONS);
ServletContextHandler handler1 = new ServletContextHandler("/c1", ServletContextHandler.NO_SESSIONS);

// without "default 404 servlet", jetty won't invoke a "pipeline" that has only a filter.
handler1.getServletHandler().setEnsureDefaultServlet(true);

handler1.setAllowNullPathInfo(true);
handler1.setAllowNullPathInContext(true);
handler1.addFilter(new FilterHolder(new Filter() {
@Override
Expand Down Expand Up @@ -619,7 +615,7 @@ public void embeddedServerWithServletContextHandlerAddedAfterServerHasStarted()

ContextHandlerCollection chc = new ContextHandlerCollection();

ServletContextHandler handler1 = new ServletContextHandler(chc, "/c1", ServletContextHandler.NO_SESSIONS);
ServletContextHandler handler1 = new ServletContextHandler("/c1", ServletContextHandler.NO_SESSIONS);
handler1.addServlet(new ServletHolder("s1", new HttpServlet() {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws IOException {
Expand All @@ -630,6 +626,7 @@ protected void service(HttpServletRequest req, HttpServletResponse resp) throws
}
}), "/s1");

chc.addHandler(handler1);
server.setHandler(chc);
server.start();

Expand All @@ -648,8 +645,8 @@ protected void service(HttpServletRequest req, HttpServletResponse resp) throws

// add new context

ServletContextHandler handler2 = new ServletContextHandler(chc, "/c2", ServletContextHandler.NO_SESSIONS);
handler2.setAllowNullPathInfo(true);
ServletContextHandler handler2 = new ServletContextHandler("/c2", ServletContextHandler.NO_SESSIONS);
handler2.setAllowNullPathInContext(true);
handler2.addServlet(new ServletHolder("s1", new HttpServlet() {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws IOException {
Expand All @@ -659,6 +656,7 @@ protected void service(HttpServletRequest req, HttpServletResponse resp) throws
resp.getWriter().close();
}
}), "/s1");
chc.addHandler(handler2);
handler2.start();

// add new servlet to existing context
Expand Down Expand Up @@ -702,7 +700,6 @@ public void embeddedServerWithWebAppContext() throws Exception {
wac1.setContextPath("/app1");
// by default, null path info is not allowed and redirect (with added "/") is sent when requesting just
// the context URL
wac1.setAllowNullPathInfo(false);
wac1.setAllowNullPathInContext(false);
// when we don't pass handler collection (or handler wrapper) in constructor, we have to add this
// specialized context handler manually
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ public void webSockets() throws Exception {

ContextHandlerCollection chc = new ContextHandlerCollection();

ServletContextHandler sch = new ServletContextHandler(null, "/", ServletContextHandler.NO_SESSIONS);
sch.setAllowNullPathInfo(true);
ServletContextHandler sch = new ServletContextHandler("/", ServletContextHandler.NO_SESSIONS);
sch.setAllowNullPathInContext(true);
sch.addServlet(DefaultServlet.class, "/");

chc.addHandler(sch);
Expand Down
Loading

0 comments on commit 1b26a2f

Please sign in to comment.