From fcb5f8c2c487bb3fa1e63b2d5a56c2fdcf016b08 Mon Sep 17 00:00:00 2001 From: Hannes Wellmann Date: Wed, 13 Dec 2023 14:53:19 +0100 Subject: [PATCH] [FLS] Complete migration to Jetty-12 and consider handlers again This completes the migration of Passage's JettyHandler and JettyRequest to Jetty-12 so that response handlers are considered again and the FLS works again. Jetty-12 migration guide https://eclipse.dev/jetty/documentation/jetty-12/programming-guide/index.html#pg-migration-11-to-12-servlet-to-handler --- .../lic/internal/jetty/JettyHandler.java | 91 +++++-------------- .../lic/internal/jetty/JettyRequest.java | 16 ++-- 2 files changed, 32 insertions(+), 75 deletions(-) diff --git a/bundles/org.eclipse.passage.lic.jetty/src/org/eclipse/passage/lic/internal/jetty/JettyHandler.java b/bundles/org.eclipse.passage.lic.jetty/src/org/eclipse/passage/lic/internal/jetty/JettyHandler.java index 5ac87520f..a5f204b0b 100644 --- a/bundles/org.eclipse.passage.lic.jetty/src/org/eclipse/passage/lic/internal/jetty/JettyHandler.java +++ b/bundles/org.eclipse.passage.lic.jetty/src/org/eclipse/passage/lic/internal/jetty/JettyHandler.java @@ -9,27 +9,25 @@ * * Contributors: * ArSysOp - initial API and implementation + * Hannes Wellmann (IILS mbH) - Complete Migration to Jetty-12 *******************************************************************************/ package org.eclipse.passage.lic.internal.jetty; import java.io.IOException; -import java.io.PrintWriter; -import java.nio.charset.Charset; +import java.nio.ByteBuffer; import java.util.Objects; import java.util.function.Function; -import javax.servlet.http.HttpServletResponse; - -import org.eclipse.jetty.http.HttpField; import org.eclipse.jetty.http.HttpFields; import org.eclipse.jetty.http.HttpHeader; -import org.eclipse.jetty.io.Content; +import org.eclipse.jetty.http.HttpStatus; import org.eclipse.jetty.server.Handler; import org.eclipse.jetty.server.Request; import org.eclipse.jetty.server.Response; import org.eclipse.jetty.util.Callback; import org.eclipse.passage.lic.internal.net.api.handle.NetRequest; import org.eclipse.passage.lic.internal.net.api.handle.NetResponse; +import org.eclipse.passage.lic.internal.net.api.handle.NetResponse.Error; /** * There is one single instance of the handler for a server. All of the rest in @@ -49,71 +47,32 @@ public JettyHandler(Function handler) { @Override public boolean handle(Request request, Response response, Callback callback) throws Exception { - response.setStatus(200); - long contentLength = -1; - for (HttpField field : request.getHeaders()) { - HttpHeader header = field.getHeader(); - if (header == null) { - continue; - } - switch (header) { - case CONTENT_LENGTH: - response.getHeaders().add(field); - contentLength = field.getLongValue(); - break; - case CONTENT_TYPE: - response.getHeaders().add(field); - break; - case TRAILER: - response.setTrailersSupplier(HttpFields.build()); - break; - case TRANSFER_ENCODING: - contentLength = Long.MAX_VALUE; - break; - } - } - if (contentLength > 0) { - //FIXME: AF: here we should somehow consider our "handler" function - Content.copy(request, response, Response.newTrailersChunkProcessor(response), callback); - } else { - callback.succeeded(); - } + NetResponse netResponse = handler.apply(new JettyRequest(request)); + write(netResponse, request, response, callback); return true; } -// @Override -// public void handle(String target, Request request, HttpServletRequest wrapper, HttpServletResponse envelope) -// throws IOException, ServletException { -// write(response(wrapper), envelope); -// request.setHandled(true); -// } - - private NetResponse response(Request request) { - return handler.apply(new JettyRequest(request)); - } - - private void write(NetResponse response, HttpServletResponse envelope) throws IOException { - envelope.setContentType(response.contentType().contentType()); - envelope.setCharacterEncoding("UTF-8"); //$NON-NLS-1$ - if (response.failed()) { - envelope.sendError(response.error().code(), response.error().message()); - return; - } - envelope.setStatus(HttpServletResponse.SC_OK); - if (response.carriesPayload()) { - byte[] payload; - try { - payload = response.payload(); - } catch (Exception e) { - throw new IOException(e); - } - envelope.setContentLength(payload.length); - try (PrintWriter out = envelope.getWriter()) { - out.write(new String(payload, Charset.forName("UTF-8"))); //$NON-NLS-1$ - out.flush(); + private void write(NetResponse netResponse, Request request, Response response, Callback callback) + throws IOException { + if (netResponse.failed()) { + Error error = netResponse.error(); + Response.writeError(request, response, callback, error.code(), error.message()); + } else { + response.setStatus(HttpStatus.OK_200); + if (netResponse.carriesPayload()) { + byte[] payload; + try { + payload = netResponse.payload(); + } catch (Exception e) { + throw new IOException(e); + } + String contentType = netResponse.contentType().contentType(); + HttpFields.Mutable responseHeaders = response.getHeaders(); + responseHeaders.put(HttpHeader.CONTENT_LENGTH, payload.length); + responseHeaders.put(HttpHeader.CONTENT_TYPE, contentType + "; charset=UTF-8"); //$NON-NLS-1$ + response.write(true, ByteBuffer.wrap(payload), callback); } - envelope.flushBuffer(); } } diff --git a/bundles/org.eclipse.passage.lic.jetty/src/org/eclipse/passage/lic/internal/jetty/JettyRequest.java b/bundles/org.eclipse.passage.lic.jetty/src/org/eclipse/passage/lic/internal/jetty/JettyRequest.java index f3e4431c2..08cdd666e 100644 --- a/bundles/org.eclipse.passage.lic.jetty/src/org/eclipse/passage/lic/internal/jetty/JettyRequest.java +++ b/bundles/org.eclipse.passage.lic.jetty/src/org/eclipse/passage/lic/internal/jetty/JettyRequest.java @@ -9,6 +9,7 @@ * * Contributors: * ArSysOp - initial API and implementation + * Hannes Wellmann (IILS mbH) - Complete Migration to Jetty-12 *******************************************************************************/ package org.eclipse.passage.lic.internal.jetty; @@ -16,37 +17,34 @@ import java.io.InputStream; import java.util.Objects; -import org.eclipse.jetty.http.HttpField; -import org.eclipse.jetty.http.HttpHeader; import org.eclipse.jetty.server.Request; +import org.eclipse.jetty.util.Fields; import org.eclipse.passage.lic.internal.net.api.handle.NetRequest; public final class JettyRequest implements NetRequest { private final Request origin; + private final Fields queryParameters; public JettyRequest(Request origin) { Objects.requireNonNull(origin, "JettyRequest::origin"); //$NON-NLS-1$ this.origin = origin; + this.queryParameters = Request.extractQueryParameters(origin); } @Override public String parameter(String name) { - //FIXME: AF: most probably this is it - return Request.extractQueryParameters(origin).getValue(name); + return queryParameters.getValue(name); } @Override public byte[] content() throws IOException { - //FIXME: AF: field can be null - HttpField field = origin.getHeaders().getField(HttpHeader.CONTENT_LENGTH); - //FIXME: AF: they expect long here - long length = field.getLongValue(); + long length = origin.getLength(); byte[] content = new byte[(int) length]; try (InputStream stream = Request.asInputStream(origin)) { stream.read(content); } return content; } - + }