I had a class: MockServerHandler
I use in my itests using Jetty 9 which I had to migrate to Jetty 12.
Before the migration (Jetty 9):
public class MockServerHandler extends DefaultHandler {
@Override
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
try {
log.debug("Got request for: {} request type {} ", target, request.getMethod());
handleDownload(request, response);
} catch (Exception e) {
log.error("Mock handler caught and exception", e);
}
}
private void handleDownload(HttpServletRequest request, HttpServletResponse response) {
byte[] byteBuffer = getContentToReturn();
long lastModified = getLastModified();
String contentType = getContentType();
int returnCode = getReturnCode();
response.setStatus(returnCode);
if (lastModified > 0L) {
response.setDateHeader(HttpHeader.LAST_MODIFIED.asString(), lastModified);
}
response.setContentType(contentType);
response.setContentLength(byteBuffer.length);
OutputStream outputStream = response.getOutputStream();
outputStream.write(byteBuffer, 0, byteBuffer.length);
response.flushBuffer();
outputStream.close();
}
}
After bumping to Jetty 12:
public class MockServerHandler extends DefaultHandler {
@Override
public boolean handle(Request request, Response response, Callback callback)
throws IOException, ServletException {
try {
String target = request.getHttpURI().getPath();
log.debug("Got request for: {} request type {} ", target, request.getMethod());
handleDownload(request, response);
callback.succeeded();
} catch (Exception e) {
callback.failed(e);
response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
log.error("Mock handler caught and exception", e);
}
return true;
}
private void handleDownload(Request request, Response response) {
byte[] byteBuffer = getContentToReturn();
long lastModified = getLastModified();
String contentType = getContentType();
int returnCode = getReturnCode();
response.setStatus(returnCode);
if (lastModified > 0L) {
response.getHeaders().addDateField(HttpHeader.LAST_MODIFIED.asString(), lastModified);
}
response.getHeaders().add(HttpHeader.CONTENT_TYPE, contentType);
OutputStream outputStream = Content.Sink.asOutputStream(response);
outputStream.write(byteBuffer, 0, byteBuffer.length);
outputStream.flush();
outputStream.close();
}
}
After these changes my integration tests started to fail. Some due to read timeout, others expect UTF-8 encoded response body and suddenly fail complaining the first byte isn’t compliant, and more.
I’m clearly missing something, as I tried to make the least amount of changes possible to retain behavior.
I noticed DefaultHandler
passes super(InvocationType.NON_BLOCKING);
which I did not expect (it wasn’t non-blocking on Jetty 9, right?). I tried extending Handler.Abstract
directly but that didn’t change anything.
Am I violating the new Jetty contract somehow? I’ve read the docs carefully but couldn’t see anything I am doing blatantly wrong.
Any advice to understand how it works and what I’m missing is very much appreciated.
1