Skip to content

Commit

Permalink
[FLS] Complete migration to Jetty-12 and consider handlers again
Browse files Browse the repository at this point in the history
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
  • Loading branch information
HannesWell committed Dec 15, 2023
1 parent 12031b8 commit 377a387
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,24 @@
*
* 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
Expand All @@ -48,73 +45,36 @@ public JettyHandler(Function<NetRequest, NetResponse> 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();
}
public boolean handle(Request request, Response answer, Callback callback) throws Exception {
NetResponse response = handler.apply(new JettyRequest(request));
write(response, request, answer, callback);

Check warning on line 50 in bundles/org.eclipse.passage.lic.jetty/src/org/eclipse/passage/lic/internal/jetty/JettyHandler.java

View check run for this annotation

Codecov / codecov/patch

bundles/org.eclipse.passage.lic.jetty/src/org/eclipse/passage/lic/internal/jetty/JettyHandler.java#L49-L50

Added lines #L49 - L50 were not covered by tests
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$
private void write(NetResponse response, Request request, Response answer, Callback callback) throws IOException {
if (response.failed()) {
envelope.sendError(response.error().code(), response.error().message());
Error error = response.error();
Response.writeError(request, answer, callback, error.code(), error.message());
return;

Check warning on line 59 in bundles/org.eclipse.passage.lic.jetty/src/org/eclipse/passage/lic/internal/jetty/JettyHandler.java

View check run for this annotation

Codecov / codecov/patch

bundles/org.eclipse.passage.lic.jetty/src/org/eclipse/passage/lic/internal/jetty/JettyHandler.java#L57-L59

Added lines #L57 - L59 were not covered by tests
}
answer.setStatus(HttpStatus.OK_200);

Check warning on line 61 in bundles/org.eclipse.passage.lic.jetty/src/org/eclipse/passage/lic/internal/jetty/JettyHandler.java

View check run for this annotation

Codecov / codecov/patch

bundles/org.eclipse.passage.lic.jetty/src/org/eclipse/passage/lic/internal/jetty/JettyHandler.java#L61

Added line #L61 was not covered by tests
if (!response.carriesPayload()) {
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();
}
envelope.flushBuffer();
byte[] payload;
try {
payload = response.payload();
} catch (Exception e) {
throw new IOException(e);

Check warning on line 69 in bundles/org.eclipse.passage.lic.jetty/src/org/eclipse/passage/lic/internal/jetty/JettyHandler.java

View check run for this annotation

Codecov / codecov/patch

bundles/org.eclipse.passage.lic.jetty/src/org/eclipse/passage/lic/internal/jetty/JettyHandler.java#L67-L69

Added lines #L67 - L69 were not covered by tests
}
answer.getHeaders().put(HttpHeader.CONTENT_LENGTH, payload.length);
answer.getHeaders().put(HttpHeader.CONTENT_TYPE, contentType(response) + "; charset=UTF-8"); //$NON-NLS-1$
answer.write(true, ByteBuffer.wrap(payload), callback);
}

Check warning on line 74 in bundles/org.eclipse.passage.lic.jetty/src/org/eclipse/passage/lic/internal/jetty/JettyHandler.java

View check run for this annotation

Codecov / codecov/patch

bundles/org.eclipse.passage.lic.jetty/src/org/eclipse/passage/lic/internal/jetty/JettyHandler.java#L71-L74

Added lines #L71 - L74 were not covered by tests

private String contentType(NetResponse response) {
return response.contentType().contentType();

Check warning on line 77 in bundles/org.eclipse.passage.lic.jetty/src/org/eclipse/passage/lic/internal/jetty/JettyHandler.java

View check run for this annotation

Codecov / codecov/patch

bundles/org.eclipse.passage.lic.jetty/src/org/eclipse/passage/lic/internal/jetty/JettyHandler.java#L77

Added line #L77 was not covered by tests
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,44 +9,41 @@
*
* 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.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 parameters;

public JettyRequest(Request origin) {
Objects.requireNonNull(origin, "JettyRequest::origin"); //$NON-NLS-1$
this.origin = origin;
this.parameters = Request.extractQueryParameters(origin);

Check warning on line 32 in bundles/org.eclipse.passage.lic.jetty/src/org/eclipse/passage/lic/internal/jetty/JettyRequest.java

View check run for this annotation

Codecov / codecov/patch

bundles/org.eclipse.passage.lic.jetty/src/org/eclipse/passage/lic/internal/jetty/JettyRequest.java#L32

Added line #L32 was not covered by tests
}

@Override
public String parameter(String name) {
//FIXME: AF: most probably this is it
return Request.extractQueryParameters(origin).getValue(name);
return parameters.getValue(name);

Check warning on line 37 in bundles/org.eclipse.passage.lic.jetty/src/org/eclipse/passage/lic/internal/jetty/JettyRequest.java

View check run for this annotation

Codecov / codecov/patch

bundles/org.eclipse.passage.lic.jetty/src/org/eclipse/passage/lic/internal/jetty/JettyRequest.java#L37

Added line #L37 was not covered by tests
}

@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();
byte[] content = new byte[(int) length];
byte[] content = new byte[(int) origin.getLength()];

Check warning on line 42 in bundles/org.eclipse.passage.lic.jetty/src/org/eclipse/passage/lic/internal/jetty/JettyRequest.java

View check run for this annotation

Codecov / codecov/patch

bundles/org.eclipse.passage.lic.jetty/src/org/eclipse/passage/lic/internal/jetty/JettyRequest.java#L42

Added line #L42 was not covered by tests
try (InputStream stream = Request.asInputStream(origin)) {
stream.read(content);
}
return content;
}

}

0 comments on commit 377a387

Please sign in to comment.