Skip to content

Commit

Permalink
Upgrade to Jetty 12 ee8
Browse files Browse the repository at this point in the history
Signed-off-by: Olivier Lamy <olamy@apache.org>
  • Loading branch information
olamy committed Sep 12, 2023
1 parent 6736476 commit 47fd229
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 78 deletions.
27 changes: 18 additions & 9 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@
<properties>
<revision>6.14</revision>
<changelist>-SNAPSHOT</changelist>
<jetty.version>10.0.16</jetty.version>
<jetty.version>12.0.1</jetty.version>
<slf4j.version>2.0.9</slf4j.version>
<gitHubRepo>jenkinsci/winstone</gitHubRepo>
<maven.compiler.release>17</maven.compiler.release>
<maven.compiler.testRelease>17</maven.compiler.testRelease>
</properties>

<licenses>
Expand Down Expand Up @@ -222,6 +224,13 @@
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.eclipse.jetty.ee8</groupId>
<artifactId>jetty-ee8-bom</artifactId>
<version>${jetty.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest</artifactId>
Expand Down Expand Up @@ -274,28 +283,28 @@
<artifactId>jetty-server</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlet</artifactId>
<groupId>org.eclipse.jetty.ee8</groupId>
<artifactId>jetty-ee8-servlet</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-util</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-webapp</artifactId>
<groupId>org.eclipse.jetty.ee8</groupId>
<artifactId>jetty-ee8-webapp</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.jetty.http2</groupId>
<artifactId>http2-hpack</artifactId>
<artifactId>jetty-http2-hpack</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.jetty.http2</groupId>
<artifactId>http2-server</artifactId>
<artifactId>jetty-http2-server</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.jetty.websocket</groupId>
<artifactId>websocket-jetty-server</artifactId> <!-- or websocket-javax-server -->
<groupId>org.eclipse.jetty.ee8.websocket</groupId>
<artifactId>jetty-ee8-websocket-jetty-server</artifactId> <!-- or websocket-javax-server -->
<!-- until Jetty release including https://github.com/eclipse/jetty.project/pull/8199 -->
<exclusions>
<exclusion>
Expand Down
73 changes: 39 additions & 34 deletions src/main/java/winstone/HostConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@

import edu.umd.cs.findbugs.annotations.NonNull;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import org.eclipse.jetty.ee8.webapp.WebAppContext;
import org.eclipse.jetty.ee8.websocket.server.config.JettyWebSocketServletContainerInitializer;
import org.eclipse.jetty.http.MimeTypes;
import org.eclipse.jetty.security.LoginService;
import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.RequestLog;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.ContextHandlerCollection;
import org.eclipse.jetty.server.handler.RequestLogHandler;
import org.eclipse.jetty.webapp.WebAppContext;
import org.eclipse.jetty.websocket.server.config.JettyWebSocketServletContainerInitializer;

import winstone.cmdline.Option;

import javax.servlet.SessionTrackingMode;
Expand Down Expand Up @@ -55,7 +55,7 @@ public class HostConfiguration {
private Map<String, String> args;
private Map<String,WebAppContext> webapps;
private ClassLoader commonLibCL;
private MimeTypes mimeTypes = new MimeTypes();
private MimeTypes.Mutable mimeTypes = new MimeTypes.Mutable();
private final LoginService loginService;

public HostConfiguration(Server server, String hostname, ClassLoader commonLibCL,
Expand All @@ -75,23 +75,6 @@ public HostConfiguration(Server server, String hostname, ClassLoader commonLibCL
throw new IOException("Failed to setup authentication realm", err);
}

// Is this the single or multiple configuration ? Check args
File warfile = Option.WARFILE.get(this.args);
File webroot = Option.WEBROOT.get(this.args);

Handler handler;
// If single-webapp mode
if (webappsDir == null && ((warfile != null) || (webroot != null))) {
String prefix = Option.PREFIX.get(this.args);
if (prefix.endsWith("/")) // trim off the trailing '/' that Jetty doesn't like
prefix = prefix.substring(0,prefix.length()-1);
handler = configureAccessLog(create(getWebRoot(webroot,warfile), prefix),"webapp");
}
// Otherwise multi-webapp mode
else {
handler = initMultiWebappDir(webappsDir);
}

{// load additional mime types
loadBuiltinMimeTypes();
String types = Option.MIME_TYPES.get(this.args);
Expand All @@ -109,7 +92,31 @@ public HostConfiguration(Server server, String hostname, ClassLoader commonLibCL
}
}

server.setHandler(handler);
// Is this the single or multiple configuration ? Check args
File warfile = Option.WARFILE.get(this.args);
File webroot = Option.WEBROOT.get(this.args);

// If single-webapp mode
if (webappsDir == null && ((warfile != null) || (webroot != null))) {
String prefix = Option.PREFIX.get(this.args);
if (prefix.endsWith("/")) // trim off the trailing '/' that Jetty doesn't like
prefix = prefix.substring(0,prefix.length()-1);

WebAppContext webAppContext = create(getWebRoot(webroot,warfile), prefix, server);
server.setHandler(webAppContext);
}
// Otherwise multi-webapp mode
else {
ContextHandlerCollection contextHandlerCollection = initMultiWebappDir(webappsDir);
contextHandlerCollection.setServer(server);
server.setHandler(contextHandlerCollection);
}

// TODO I really have some doubt this is really used....
RequestLog requestLog = configureAccessLog("webapp");
if (requestLog != null) {
server.setRequestLog(requestLog);
}
Logger.log(Level.FINER, Launcher.RESOURCES, "HostConfig.InitComplete",
this.webapps.size() + "", this.webapps.keySet() + "");
}
Expand All @@ -131,27 +138,24 @@ private void loadBuiltinMimeTypes() {
* @param webAppName
* Unique name given to the access logger.
*/
private Handler configureAccessLog(Handler handler, String webAppName) {
private RequestLog configureAccessLog(String webAppName) {
try {
Class<? extends RequestLog> loggerClass = Option.ACCESS_LOGGER_CLASSNAME.get(args, RequestLog.class, commonLibCL);
if (loggerClass!=null) {
// Build the realm
Constructor<? extends RequestLog> loggerConstr = loggerClass.getConstructor(String.class, Map.class);
RequestLogHandler rlh = new RequestLogHandler();
rlh.setHandler(handler);
rlh.setRequestLog(loggerConstr.newInstance(webAppName, args));
return rlh;
return loggerConstr.newInstance(webAppName, args);
} else {
Logger.log(Level.FINER, Launcher.RESOURCES, "WebAppConfig.LoggerDisabled");
}
} catch (Throwable err) {
Logger.log(Level.SEVERE, Launcher.RESOURCES,
"WebAppConfig.LoggerError", "", err);
}
return handler;
return null;
}

private WebAppContext create(File app, String prefix) {
private WebAppContext create(File app, String prefix, Server server) {
WebAppContext wac = new WebAppContext(app.getAbsolutePath(),prefix) {
@Override
public void preConfigure() throws Exception {
Expand Down Expand Up @@ -185,10 +189,11 @@ public void postConfigure() throws Exception {
getSessionHandler().getSessionCache().setEvictionPolicy( sessionEviction );
}
};
wac.setServer(server);
JettyWebSocketServletContainerInitializer.configure(wac, null);
wac.getSecurityHandler().setLoginService(loginService);
wac.setThrowUnavailableOnStartupException(true); // if boot fails, abort the process instead of letting empty Jetty run
wac.setMimeTypes(mimeTypes);
mimeTypes.getMimeMap().forEach((extension, type) -> wac.getServer().getMimeTypes().addMimeMapping(extension, type));
wac.getSessionHandler().setSessionTrackingModes(Set.of(SessionTrackingMode.COOKIE));
wac.getSessionHandler().setSessionCookie(WinstoneSession.SESSION_COOKIE_NAME);
this.webapps.put(wac.getContextPath(),wac);
Expand Down Expand Up @@ -370,8 +375,8 @@ protected ContextHandlerCollection initMultiWebappDir(File webappsDir) {
String prefix = childName.equalsIgnoreCase("ROOT") ? "" : "/" + childName;
if (!this.webapps.containsKey(prefix)) {
try {
WebAppContext context = create(aChildren, prefix);
webApps.addHandler(configureAccessLog(context,childName));
WebAppContext context = create(aChildren, prefix, server);
//webApps.addHandler(configureAccessLog(context,childName));
Logger.log(Level.INFO, Launcher.RESOURCES, "HostConfig.DeployingWebapp", childName);
} catch (Throwable err) {
Logger.log(Level.SEVERE, Launcher.RESOURCES, "HostConfig.WebappInitError", prefix, err);
Expand All @@ -391,8 +396,8 @@ protected ContextHandlerCollection initMultiWebappDir(File webappsDir) {
}
try {
WebAppContext context = create(
getWebRoot(new File(webappsDir, outputName), aChildren), prefix);
webApps.addHandler(configureAccessLog(context,outputName));
getWebRoot(new File(webappsDir, outputName), aChildren), prefix, server);
//webApps.addHandler(configureAccessLog(context,outputName));
Logger.log(Level.INFO, Launcher.RESOURCES, "HostConfig.DeployingWebapp", childName);
} catch (Throwable err) {
Logger.log(Level.SEVERE, Launcher.RESOURCES, "HostConfig.WebappInitError", prefix, err);
Expand Down
12 changes: 3 additions & 9 deletions src/main/java/winstone/HttpsConnectorFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.server.handler.HandlerList;
import org.eclipse.jetty.server.handler.SecuredRedirectHandler;

import winstone.cmdline.Option;
Expand Down Expand Up @@ -43,14 +42,9 @@ public Connector start( Map<String, String> args, Server server) throws IOExcept
if(currentHandler == null) {
server.setHandler(new SecuredRedirectHandler());
} else {
if(currentHandler instanceof HandlerList) {
((HandlerList)currentHandler).addHandler(new SecuredRedirectHandler());
} else {
HandlerList handlers = new HandlerList();
handlers.addHandler(new SecuredRedirectHandler());
handlers.addHandler(currentHandler);
server.setHandler(handlers);
}
SecuredRedirectHandler securedRedirectHandler = new SecuredRedirectHandler();
securedRedirectHandler.setHandler(currentHandler);
server.setHandler(securedRedirectHandler);
}
}
configureSsl(args, server);
Expand Down
47 changes: 21 additions & 26 deletions src/main/java/winstone/accesslog/SimpleAccessLogger.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
package winstone.accesslog;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import org.eclipse.jetty.server.Authentication;
import org.eclipse.jetty.http.HttpFields;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.RequestLog;
import org.eclipse.jetty.server.Response;
Expand All @@ -25,6 +25,7 @@
import java.nio.file.Files;
import java.nio.file.InvalidPathException;
import java.nio.file.StandardOpenOption;
import java.security.Principal;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
Expand Down Expand Up @@ -97,43 +98,37 @@ public SimpleAccessLogger(String webAppName, Map<String, String> startupArgs)

@Override
public void log(Request request, Response response) {
String uriLine = request.getMethod() + " " + request.getRequestURI() + " " + request.getProtocol();
String uriLine = request.getMethod() + " " + request.getHttpURI().getPath() + " " + request.getConnectionMetaData().getProtocol();
int status = response.getStatus();
long size = response.getContentCount();
long size = Response.getContentBytesWritten(response);
String date;
synchronized (DF) {
date = DF.format(new Date());
}
Request.AuthenticationState authenticationState = Request.getAuthenticationState(request);
Principal principal = authenticationState == null ? null : authenticationState.getUserPrincipal();
String remoteUser = principal == null ? "-" : principal.getName();

// mimic https://github.com/eclipse/jetty.project/blob/jetty-9.4.0.v20161208/jetty-server/src/main/java/org/eclipse/jetty/server/AbstractNCSARequestLog.java#L130
Authentication authentication = request.getAuthentication();
String remoteUser;
if (authentication instanceof Authentication.User) {
Authentication.User user = (Authentication.User) authentication;
remoteUser = user.getUserIdentity().getUserPrincipal().getName();
} else {
remoteUser = null;
}

HttpFields httpFields = request.getHeaders();
String logLine = WinstoneResourceBundle.globalReplace(this.pattern, new String[][] {
{"###x-forwarded-for###", nvl(request.getHeader("X-Forwarded-For"))},
{"###x-forwarded-host###", nvl(request.getHeader("X-Forwarded-Host"))},
{"###x-forwarded-proto###", nvl(request.getHeader("X-Forwarded-Proto"))},
{"###x-forwarded-protocol###", nvl(request.getHeader("X-Forwarded-Protocol"))},
{"###x-forwarded-server###", nvl(request.getHeader("X-Forwarded-Server"))},
{"###x-forwarded-ssl###", nvl(request.getHeader("X-Forwarded-Ssl"))},
{"###x-requested-with###", nvl(request.getHeader("X-Requested-With"))},
{"###x-do-not-track###", nvl(request.getHeader("X-Do-Not-Track"))},
{"###dnt###", nvl(request.getHeader("DNT"))},
{"###via###", nvl(request.getHeader("Via"))},
{"###ip###", request.getRemoteHost()},
{"###x-forwarded-for###", nvl(httpFields.get("X-Forwarded-For"))},
{"###x-forwarded-host###", nvl(httpFields.get("X-Forwarded-Host"))},
{"###x-forwarded-proto###", nvl(httpFields.get("X-Forwarded-Proto"))},
{"###x-forwarded-protocol###", nvl(httpFields.get("X-Forwarded-Protocol"))},
{"###x-forwarded-server###", nvl(httpFields.get("X-Forwarded-Server"))},
{"###x-forwarded-ssl###", nvl(httpFields.get("X-Forwarded-Ssl"))},
{"###x-requested-with###", nvl(httpFields.get("X-Requested-With"))},
{"###x-do-not-track###", nvl(httpFields.get("X-Do-Not-Track"))},
{"###dnt###", nvl(httpFields.get("DNT"))},
{"###via###", nvl(httpFields.get("Via"))},
{"###ip###", Request.getRemoteAddr(request)},
{"###user###", nvl(remoteUser)},
{"###time###", "[" + date + "]"},
{"###uriLine###", uriLine},
{"###status###", "" + status},
{"###size###", "" + size},
{"###referer###", nvl(request.getHeader("Referer"))},
{"###userAgent###", nvl(request.getHeader("User-Agent"))}
{"###referer###", nvl(httpFields.get("Referer"))},
{"###userAgent###", nvl(httpFields.get("User-Agent"))}
});
this.outWriter.println(logLine);
}
Expand Down

0 comments on commit 47fd229

Please sign in to comment.