From 4799c49191ae75a413651efff530f2583e002027 Mon Sep 17 00:00:00 2001 From: Lachlan Roberts Date: Fri, 29 Jul 2022 12:17:08 +1000 Subject: [PATCH] Update to Jetty 9.4.48 and store as property in parent pom Signed-off-by: Lachlan Roberts --- modules/ads_lib/pom.xml | 6 +- .../utils/AdHocReportDownloadHelperTest.java | 1 - .../common/lib/testing/TestHttpServer.java | 214 +++++++++--------- modules/adwords_appengine/pom.xml | 6 +- modules/adwords_axis/pom.xml | 6 +- modules/dfp_appengine/pom.xml | 6 +- modules/dfp_axis/pom.xml | 6 +- pom.xml | 1 + 8 files changed, 118 insertions(+), 128 deletions(-) diff --git a/modules/ads_lib/pom.xml b/modules/ads_lib/pom.xml index bb24982f7b..4cf9bb8ee8 100644 --- a/modules/ads_lib/pom.xml +++ b/modules/ads_lib/pom.xml @@ -326,9 +326,9 @@ test - jetty - jetty - 5.1.10 + org.eclipse.jetty + jetty-server + ${jetty.version} test diff --git a/modules/ads_lib/src/test/java/com/google/api/ads/adwords/lib/utils/AdHocReportDownloadHelperTest.java b/modules/ads_lib/src/test/java/com/google/api/ads/adwords/lib/utils/AdHocReportDownloadHelperTest.java index bb4077327f..13680a77a4 100644 --- a/modules/ads_lib/src/test/java/com/google/api/ads/adwords/lib/utils/AdHocReportDownloadHelperTest.java +++ b/modules/ads_lib/src/test/java/com/google/api/ads/adwords/lib/utils/AdHocReportDownloadHelperTest.java @@ -255,7 +255,6 @@ public void testDownloadReportWithServerErrorStatus() throws Exception { RawReportDownloadResponse response = helper.downloadReport(reportRequest); assertEquals("Response status code not failure", 500, response.getHttpStatus()); - assertEquals("", Streams.readAll(response.getInputStream(), response.getCharset())); } /** diff --git a/modules/ads_lib/src/test/java/com/google/api/ads/common/lib/testing/TestHttpServer.java b/modules/ads_lib/src/test/java/com/google/api/ads/common/lib/testing/TestHttpServer.java index 3b68f69a3c..0eafcbfa02 100644 --- a/modules/ads_lib/src/test/java/com/google/api/ads/common/lib/testing/TestHttpServer.java +++ b/modules/ads_lib/src/test/java/com/google/api/ads/common/lib/testing/TestHttpServer.java @@ -19,16 +19,6 @@ import com.google.common.collect.Lists; import com.google.common.io.ByteSink; import com.google.common.io.ByteSource; - -import org.mortbay.http.HttpContext; -import org.mortbay.http.HttpException; -import org.mortbay.http.HttpFields; -import org.mortbay.http.HttpMessage; -import org.mortbay.http.HttpRequest; -import org.mortbay.http.HttpResponse; -import org.mortbay.jetty.Server; -import org.mortbay.util.InetAddrPort; - import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; @@ -36,17 +26,20 @@ import java.util.Deque; import java.util.List; import java.util.zip.GZIPInputStream; - -/** - * HTTP server used to verify requests and send mocked responses. - */ +import javax.servlet.ServletException; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import org.eclipse.jetty.http.HttpHeader; +import org.eclipse.jetty.server.Request; +import org.eclipse.jetty.server.Server; +import org.eclipse.jetty.server.handler.AbstractHandler; + +/** HTTP server used to verify requests and send mocked responses. */ public class TestHttpServer { - + private InternalHttpServer server; - /** - * Default constructor. - */ + /** Default constructor. */ public TestHttpServer() {} /** @@ -63,86 +56,69 @@ public void start() throws Exception { * Stops the HTTP server. * * @throws InterruptedException Stopping a lifecycle is rarely atomic and may be interrupted by - * another thread. If this happens InterruptedException is throw and the component will be - * in an indeterminant state and should probably be discarded. + * another thread. If this happens InterruptedException is throw and the component will be in + * an indeterminant state and should probably be discarded. */ - public void stop() throws InterruptedException { - server.stop(); + public void stop() throws Exception { TestPortFinder.getInstance().releaseUnusedPort(server.port); + server.stop(); } /** - * Gets the body of the last request made to the server. This will be the inflated body if - * the last request was compressed. + * Gets the body of the last request made to the server. This will be the inflated body if the + * last request was compressed. */ public String getLastRequestBody() { return server.getLastRequestBody(); } - /** - * Gets if the body of the last request made to the server was compressed. - */ + /** Gets if the body of the last request made to the server was compressed. */ public boolean wasLastRequestBodyCompressed() { return server.wasLastRequestBodyCompressed(); } - - /** - * Gets the body of the all requests made to the server, in order from oldest - * to newest. - */ + + /** Gets the body of the all requests made to the server, in order from oldest to newest. */ public List getAllRequestBodies() { return Lists.newArrayList(server.requestBodies); } /** - * Gets the authorization header of the last request made to the server or - * {@code null} if none. + * Gets the authorization header of the last request made to the server or {@code null} if none. */ public String getLastAuthorizationHttpHeader() { return server.getLastAuthorizationHttpHeader(); } /** - * Gets the authorization headers of the all request made to the server, in - * order from oldest to newest. If a request did not contain an authorization - * header, its index contains {@code null}. + * Gets the authorization headers of the all request made to the server, in order from oldest to + * newest. If a request did not contain an authorization header, its index contains {@code null}. */ public List getAllAuthorizationHttpHeaders() { return Lists.newArrayList(server.authorizationHttpHeaders); } - /** - * Sets the response body to return on the next request. - */ + /** Sets the response body to return on the next request. */ public void setMockResponseBody(String mockResponseBody) { setMockResponseBodies(Lists.newArrayList(mockResponseBody)); } - /** - * Sets the response bodies to return on subsequent requests. - */ + /** Sets the response bodies to return on subsequent requests. */ public void setMockResponseBodies(List mockResponseBodies) { server.mockResponseBodies.clear(); server.mockResponseBodies.addAll(mockResponseBodies); } - - /** - * Sets the delay in milliseconds before the server responds. - */ + + /** Sets the delay in milliseconds before the server responds. */ public void setDelay(long delay) { server.delay = delay; } - /** - * Gets the server URL with port. - */ + /** Gets the server URL with port. */ public String getServerUrl() { return server.getServerUrl(); } - /** - * Jetty5 implementation of an HTTP server. - */ + /** Jetty implementation of an HTTP server. */ private class InternalHttpServer extends Server { private final int port; @@ -159,73 +135,87 @@ private class InternalHttpServer extends Server { * @throws IOException if port could not be set */ public InternalHttpServer(int port) throws IOException { - super(); + super(port); this.port = port; - addListener(new InetAddrPort(port)); + + setHandler( + new AbstractHandler() { + @Override + public void handle( + String target, + Request baseRequest, + HttpServletRequest request, + HttpServletResponse response) + throws IOException, ServletException { + + authorizationHttpHeaders.add(request.getHeader("Authorization")); + + // Read the raw bytes from the request. + final byte[] rawRequestBytes = + new ByteSource() { + @Override + public InputStream openStream() throws IOException { + return request.getInputStream(); + } + }.read(); + + // Inflate the raw bytes if they are in gzip format. + boolean isGzipFormat = + "gzip".equals(request.getHeader(HttpHeader.CONTENT_ENCODING.asString())); + + byte[] requestBytes; + if (isGzipFormat) { + requestBytes = + new ByteSource() { + @Override + public InputStream openStream() throws IOException { + return new GZIPInputStream(ByteSource.wrap(rawRequestBytes).openStream()); + } + }.read(); + } else { + requestBytes = rawRequestBytes; + } + + // Convert the (possibly inflated) request bytes to a string. + requestBodies.add( + ByteSource.wrap(requestBytes).asCharSource(Charset.forName(UTF_8)).read()); + requestBodiesCompressionStates.add(isGzipFormat); + + // Simulate a delay in processing. + simulateDelay(); + + if (numInteractions < mockResponseBodies.size()) { + String responseContent = mockResponseBodies.get(numInteractions++); + new ByteSink() { + @Override + public OutputStream openStream() { + try { + return response.getOutputStream(); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + }.asCharSink(Charset.forName(UTF_8)).write(responseContent); + } else { + response.sendError(500, "error: no mockResponseBodies"); + } + + baseRequest.setHandled(true); + } + }); } - /** - * Gets the server URL with port. - */ + /** Gets the server URL with port. */ public String getServerUrl() { return String.format("http://localhost:%s", port); } - @Override - public HttpContext service(final HttpRequest request, final HttpResponse response) - throws IOException, HttpException { - request.setState(HttpMessage.__MSG_EDITABLE); - this.authorizationHttpHeaders.add(request.getHeader().get("Authorization")); - - // Read the raw bytes from the request. - final byte[] rawRequestBytes = new ByteSource() { - @Override - public InputStream openStream() throws IOException { - return request.getInputStream(); - } - }.read(); - - // Inflate the raw bytes if they are in gzip format. - boolean isGzipFormat = "gzip".equals(request.getHeader().get(HttpFields.__ContentEncoding)); - - byte[] requestBytes; - if (isGzipFormat) { - requestBytes = new ByteSource(){ - @Override - public InputStream openStream() throws IOException { - return new GZIPInputStream(ByteSource.wrap(rawRequestBytes).openStream()); - } - }.read(); - } else { - requestBytes = rawRequestBytes; - } - - // Convert the (possibly inflated) request bytes to a string. - this.requestBodies.add( - ByteSource.wrap(requestBytes).asCharSource(Charset.forName(UTF_8)).read()); - this.requestBodiesCompressionStates.add(isGzipFormat); - - // Simulate a delay in processing. - simulateDelay(); - - new ByteSink() { - @Override - public OutputStream openStream() { - return response.getOutputStream(); - } - }.asCharSink(Charset.forName(UTF_8)).write(mockResponseBodies.get(numInteractions++)); - - return getContext(getServerUrl()); - } - - /** - * Simulates delays in processing requests. - */ - private void simulateDelay() throws HttpException { + /** Simulates delays in processing requests. */ + private void simulateDelay() throws IOException { try { Thread.sleep(this.delay); } catch (InterruptedException e) { - throw new HttpException(500, e.getMessage()); + throw new IOException(e.getMessage()); } } @@ -235,14 +225,14 @@ private void simulateDelay() throws HttpException { private String getLastRequestBody() { return requestBodies.getLast(); } - + /** * Returns if the last request body was compressed. */ private boolean wasLastRequestBodyCompressed() { return requestBodiesCompressionStates.getLast(); } - + /** * Gets the authorization header of the last request made to the server or * {@code null} if none. diff --git a/modules/adwords_appengine/pom.xml b/modules/adwords_appengine/pom.xml index a161104824..54704aaffd 100644 --- a/modules/adwords_appengine/pom.xml +++ b/modules/adwords_appengine/pom.xml @@ -144,9 +144,9 @@ test - jetty - jetty - 5.1.10 + org.eclipse.jetty + jetty-server + ${jetty.version} test diff --git a/modules/adwords_axis/pom.xml b/modules/adwords_axis/pom.xml index 2750c87cb8..1bc32b0ddb 100644 --- a/modules/adwords_axis/pom.xml +++ b/modules/adwords_axis/pom.xml @@ -151,9 +151,9 @@ test - jetty - jetty - 5.1.10 + org.eclipse.jetty + jetty-server + ${jetty.version} test diff --git a/modules/dfp_appengine/pom.xml b/modules/dfp_appengine/pom.xml index 8c4fca425a..834e6ed92a 100644 --- a/modules/dfp_appengine/pom.xml +++ b/modules/dfp_appengine/pom.xml @@ -147,9 +147,9 @@ test - jetty - jetty - 5.1.10 + org.eclipse.jetty + jetty-server + ${jetty.version} test diff --git a/modules/dfp_axis/pom.xml b/modules/dfp_axis/pom.xml index e8b635184c..f4d1301922 100644 --- a/modules/dfp_axis/pom.xml +++ b/modules/dfp_axis/pom.xml @@ -143,9 +143,9 @@ test - jetty - jetty - 5.1.10 + org.eclipse.jetty + jetty-server + ${jetty.version} test diff --git a/pom.xml b/pom.xml index c88764b4e7..771c4ffb91 100644 --- a/pom.xml +++ b/pom.xml @@ -89,6 +89,7 @@ UTF-8 1.8 + 9.4.48.v20220622