diff --git a/pom.xml b/pom.xml index 5a3a7a6..b904a40 100644 --- a/pom.xml +++ b/pom.xml @@ -57,6 +57,9 @@ THE SOFTWARE. true Low 1.16 + + 2250.v03a_1295b_0a_30 + 17 diff --git a/src/test/java/jenkins/plugins/http_request/HttpRequestStepTest.java b/src/test/java/jenkins/plugins/http_request/HttpRequestStepTest.java index 65b50ab..f594a77 100644 --- a/src/test/java/jenkins/plugins/http_request/HttpRequestStepTest.java +++ b/src/test/java/jenkins/plugins/http_request/HttpRequestStepTest.java @@ -21,12 +21,12 @@ import java.util.ArrayList; import java.util.List; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; - import org.apache.commons.io.FilenameUtils; import org.apache.http.entity.ContentType; +import org.eclipse.jetty.http.HttpStatus; import org.eclipse.jetty.server.Request; +import org.eclipse.jetty.server.Response; +import org.eclipse.jetty.util.Callback; import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition; import org.jenkinsci.plugins.workflow.job.WorkflowJob; import org.jenkinsci.plugins.workflow.job.WorkflowRun; @@ -636,11 +636,11 @@ public void testPostBody() throws Exception { //configure server registerHandler("/doPostBody", HttpMode.POST, new SimpleHandler() { @Override - void doHandle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException { + boolean doHandle(Request request, Response response, Callback callback) throws IOException { assertEquals("POST", request.getMethod()); String body = requestBody(request); - body(response, HttpServletResponse.SC_OK, ContentType.TEXT_PLAIN, body); + return body(response, HttpStatus.OK_200, ContentType.TEXT_PLAIN, body, callback); } }); @@ -671,7 +671,7 @@ public void testFileUpload() throws Exception { final File testFolder = folder.newFolder(); File uploadFile = File.createTempFile("upload", ".zip", testFolder); String responseText = "File upload successful!"; - registerFileUpload(testFolder, uploadFile, responseText); + registerFileUpload(uploadFile, responseText); // Prepare HttpRequest WorkflowJob proj = j.jenkins.createProject(WorkflowJob.class, "uploadFile"); @@ -700,13 +700,12 @@ public void testFileUpload() throws Exception { @Test public void testFormData() throws Exception { - final File testFolder = folder.newFolder(); File projectRoot = Paths.get("").toAbsolutePath().toFile(); String responseText = "File upload successful!"; String json = "{\"foo\": \"bar\"}"; File file1 = new File(projectRoot, "src/test/resources/testdata/readme.txt"); File file2 = new File(projectRoot, "src/test/resources/testdata/small.zip"); - registerFormData(testFolder, json, file1, file2, responseText); + registerFormData(json, file1, file2, responseText); // Let's upload these files and a JSON String script = "node {\n" diff --git a/src/test/java/jenkins/plugins/http_request/HttpRequestTest.java b/src/test/java/jenkins/plugins/http_request/HttpRequestTest.java index b677fe9..56bdea1 100644 --- a/src/test/java/jenkins/plugins/http_request/HttpRequestTest.java +++ b/src/test/java/jenkins/plugins/http_request/HttpRequestTest.java @@ -19,22 +19,24 @@ import static org.junit.Assert.assertTrue; import java.io.File; -import java.io.IOException; import java.net.URL; import java.util.ArrayList; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; -import javax.servlet.http.Cookie; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; +import javax.servlet.ServletException; import org.apache.http.HttpResponse; import org.apache.http.ProtocolVersion; import org.apache.http.entity.StringEntity; import org.apache.http.message.BasicHttpResponse; +import org.eclipse.jetty.http.HttpCookie; +import org.eclipse.jetty.http.HttpStatus; import org.eclipse.jetty.server.Request; +import org.eclipse.jetty.server.Response; +import org.eclipse.jetty.util.Callback; +import org.eclipse.jetty.util.Fields; import org.junit.Assert; import org.junit.Assume; import org.junit.Rule; @@ -766,23 +768,31 @@ public void testFormAuthentication() throws Exception { registerHandler("/form-auth", HttpMode.POST, new SimpleHandler() { @Override - void doHandle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException { - String username = request.getParameter(paramUsername); - String password = request.getParameter(paramPassword); + boolean doHandle(Request request, Response response, Callback callback) throws ServletException { + Fields parameters; + try { + parameters = Request.getParameters(request); + } catch (Exception e) { + throw new ServletException(e); + } + String username = parameters.getValue(paramUsername); + String password = parameters.getValue(paramPassword); + if (!username.equals(valueUsername) || !password.equals(valuePassword)) { - response.setStatus(401); - return; + Response.writeError(request, response, callback, HttpStatus.UNAUTHORIZED_401); + return true; } - response.addCookie(new Cookie(sessionName, "ok")); - okAllIsWell(response); + HttpCookie cookie = HttpCookie.build(sessionName, "ok").build(); + Response.addCookie(response, cookie); + return okAllIsWell(response, callback); } }); registerHandler("/test-auth", HttpMode.GET, new SimpleHandler() { @Override - void doHandle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException { + boolean doHandle(Request request, Response response, Callback callback) { String jsessionValue = ""; - Cookie[] cookies = request.getCookies(); - for (Cookie cookie : cookies) { + List cookies = Request.getCookies(request); + for (HttpCookie cookie : cookies) { if (cookie.getName().equals(sessionName)) { jsessionValue = cookie.getValue(); break; @@ -790,10 +800,10 @@ void doHandle(String target, Request baseRequest, HttpServletRequest request, Ht } if (!jsessionValue.equals("ok")) { - response.setStatus(401); - return; + Response.writeError(request, response, callback, HttpStatus.UNAUTHORIZED_401); + return true; } - okAllIsWell(response); + return okAllIsWell(response, callback); } }); @@ -982,7 +992,7 @@ public void testFileUpload() throws Exception { final File testFolder = folder.newFolder(); File uploadFile = File.createTempFile("upload", ".zip", testFolder); String responseText = "File upload successful!"; - registerFileUpload(testFolder, uploadFile, responseText); + registerFileUpload(uploadFile, responseText); // Prepare HttpRequest HttpRequest httpRequest = new HttpRequest(baseURL() + "/uploadFile"); diff --git a/src/test/java/jenkins/plugins/http_request/HttpRequestTestBase.java b/src/test/java/jenkins/plugins/http_request/HttpRequestTestBase.java index 75ede04..f799cf9 100644 --- a/src/test/java/jenkins/plugins/http_request/HttpRequestTestBase.java +++ b/src/test/java/jenkins/plugins/http_request/HttpRequestTestBase.java @@ -1,24 +1,33 @@ package jenkins.plugins.http_request; -import java.io.BufferedReader; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.both; +import static org.hamcrest.Matchers.greaterThanOrEqualTo; +import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.lessThan; + import java.io.IOException; +import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.servlet.ServletException; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; import org.apache.http.entity.ContentType; +import org.eclipse.jetty.http.HttpHeader; +import org.eclipse.jetty.http.HttpStatus; +import org.eclipse.jetty.io.Content; import org.eclipse.jetty.server.Connector; import org.eclipse.jetty.server.Handler; import org.eclipse.jetty.server.Request; +import org.eclipse.jetty.server.Response; import org.eclipse.jetty.server.Server; import org.eclipse.jetty.server.ServerConnector; import org.eclipse.jetty.server.handler.ContextHandler; import org.eclipse.jetty.server.handler.DefaultHandler; +import org.eclipse.jetty.util.Callback; import org.junit.After; import org.junit.AfterClass; import org.junit.Before; @@ -31,7 +40,6 @@ import com.cloudbees.plugins.credentials.SystemCredentialsProvider; import com.cloudbees.plugins.credentials.domains.Domain; import com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl; -import org.apache.commons.io.IOUtils; /** * @author Martin d'Anjou @@ -89,34 +97,35 @@ public void cleanHandlers() { } } - public static abstract class SimpleHandler extends DefaultHandler { + public static abstract class SimpleHandler extends Handler.Abstract { @Override - public final void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { - doHandle(target, baseRequest, request, response); - baseRequest.setHandled(true); + public final boolean handle(Request request, Response response, Callback callback) throws IOException, ServletException { + return doHandle(request, response, callback); } - String requestBody(HttpServletRequest request) throws IOException { - try (BufferedReader reader = request.getReader()) { - return IOUtils.toString(reader); - } + String requestBody(Request request) throws IOException { + return Content.Source.asString(request, StandardCharsets.UTF_8); } - void okAllIsWell(HttpServletResponse response) throws IOException { - okText(response, ALL_IS_WELL); + boolean okAllIsWell(Response response, Callback callback) { + return okText(response, ALL_IS_WELL, callback); } - void okText(HttpServletResponse response, String body) throws IOException { - body(response, HttpServletResponse.SC_OK, ContentType.TEXT_PLAIN, body); + boolean okText(Response response, String body, Callback callback) { + return body(response, HttpStatus.OK_200, ContentType.TEXT_PLAIN, body, callback); } - void body(HttpServletResponse response, int status, ContentType contentType, String body) throws IOException { - response.setContentType(contentType != null ? contentType.toString() : ""); + boolean body(Response response, int status, ContentType contentType, String body, Callback callback) { + assertThat(status, is(both(greaterThanOrEqualTo(200)).and(lessThan(300)))); + if (contentType != null) { + response.getHeaders().add(HttpHeader.CONTENT_TYPE, contentType.toString()); + } response.setStatus(status); - response.getWriter().append(body); + Content.Sink.write(response, true, body, callback); + return true; } - abstract void doHandle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException; + abstract boolean doHandle(Request request, Response response, Callback callback) throws IOException, ServletException; } private static final class ServerRunning { @@ -134,17 +143,17 @@ private ServerRunning() throws Exception { context.setContextPath("/"); context.setHandler(new DefaultHandler() { @Override - public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { + public boolean handle(Request request, Response response, Callback callback) throws Exception { + String target = request.getHttpURI().getPath(); Map handlerByMethod = handlersByMethodByTarget.get(target); if (handlerByMethod != null) { Handler handler = handlerByMethod.get(HttpMode.valueOf(request.getMethod())); if (handler != null) { - handler.handle(target, baseRequest, request, response); - return; + return handler.handle(request, response, callback); } } - super.handle(target, baseRequest, request, response); + return super.handle(request, response, callback); } }); server.setHandler(context); diff --git a/src/test/java/jenkins/plugins/http_request/Registers.java b/src/test/java/jenkins/plugins/http_request/Registers.java index 7103f43..7215a03 100644 --- a/src/test/java/jenkins/plugins/http_request/Registers.java +++ b/src/test/java/jenkins/plugins/http_request/Registers.java @@ -12,19 +12,20 @@ import java.util.Base64; import java.util.Enumeration; import java.util.Map; +import java.util.concurrent.ExecutionException; -import javax.servlet.MultipartConfigElement; import javax.servlet.ServletException; -import javax.servlet.ServletRequest; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; -import javax.servlet.http.Part; -import org.apache.commons.io.IOUtils; import org.apache.http.HttpHeaders; import org.apache.http.entity.ContentType; -import org.eclipse.jetty.server.MultiPartFormInputStream; +import org.eclipse.jetty.http.HttpHeader; +import org.eclipse.jetty.http.HttpStatus; +import org.eclipse.jetty.http.MultiPart; +import org.eclipse.jetty.http.MultiPartConfig; +import org.eclipse.jetty.http.MultiPartFormData; import org.eclipse.jetty.server.Request; +import org.eclipse.jetty.server.Response; +import org.eclipse.jetty.util.Callback; import jenkins.plugins.http_request.HttpRequestTestBase.SimpleHandler; @@ -36,12 +37,12 @@ public class Registers { static void registerRequestChecker(final HttpMode method) { registerHandler("/do" + method.name(), method, new SimpleHandler() { @Override - void doHandle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException { + boolean doHandle(Request request, Response response, Callback callback) { assertEquals(method.name(), request.getMethod()); - String query = request.getQueryString(); + String query = request.getHttpURI().getQuery(); assertNull(query); - okAllIsWell(response); + return okAllIsWell(response, callback); } }); } @@ -49,10 +50,10 @@ void doHandle(String target, Request baseRequest, HttpServletRequest request, Ht static void registerContentTypeRequestChecker(final MimeType mimeType, final HttpMode httpMode, final String responseMessage) { registerHandler("/incoming_" + mimeType.toString(), httpMode, new SimpleHandler() { @Override - void doHandle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException { + boolean doHandle(Request request, Response response, Callback callback) throws IOException { assertEquals(httpMode.name(), request.getMethod()); - Enumeration headers = request.getHeaders(HttpHeaders.CONTENT_TYPE); + Enumeration headers = request.getHeaders().getValues(HttpHeaders.CONTENT_TYPE); if (mimeType == MimeType.NOT_SET) { assertFalse(headers.hasMoreElements()); } else { @@ -63,10 +64,10 @@ void doHandle(String target, Request baseRequest, HttpServletRequest request, Ht assertEquals(mimeType.getContentType().toString(), value); } - String query = request.getQueryString(); + String query = request.getHttpURI().getQuery(); assertNull(query); String body = responseMessage != null ? responseMessage : requestBody(request); - body(response, HttpServletResponse.SC_OK, mimeType.getContentType(), body); + return body(response, HttpStatus.OK_200, mimeType.getContentType(), body, callback); } }); } @@ -74,10 +75,11 @@ void doHandle(String target, Request baseRequest, HttpServletRequest request, Ht static void registerAcceptedTypeRequestChecker(final MimeType mimeType) { registerHandler("/accept_" + mimeType.toString(), HttpMode.GET, new SimpleHandler() { @Override - void doHandle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException { + boolean doHandle(Request request, Response response, Callback callback) { assertEquals("GET", request.getMethod()); - Enumeration headers = request.getHeaders(HttpHeaders.ACCEPT); + Enumeration headers = request.getHeaders().getValues(HttpHeaders.ACCEPT); + if (mimeType == MimeType.NOT_SET) { assertFalse(headers.hasMoreElements()); @@ -88,9 +90,9 @@ void doHandle(String target, Request baseRequest, HttpServletRequest request, Ht assertEquals(mimeType.getValue(), value); } - String query = request.getQueryString(); + String query = request.getHttpURI().getQuery(); assertNull(query); - okAllIsWell(response); + return okAllIsWell(response, callback); } }); } @@ -99,12 +101,13 @@ static void registerTimeout() { // Timeout, do not respond! registerHandler("/timeout", HttpMode.GET, new SimpleHandler() { @Override - void doHandle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) { + boolean doHandle(Request request, Response response, Callback callback) { try { Thread.sleep(10000); } catch (InterruptedException ex) { // do nothing the sleep will be interrupted when the test ends } + return true; } }); } @@ -113,10 +116,15 @@ static void registerReqAction() { // Accept the form authentication registerHandler("/reqAction", HttpMode.GET, new SimpleHandler() { @Override - void doHandle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException { + boolean doHandle(Request request, Response response, Callback callback) throws ServletException { assertEquals("GET", request.getMethod()); - Map parameters = request.getParameterMap(); + Map parameters; + try { + parameters = Request.getParameters(request).toStringArrayMap(); + } catch (Exception e) { + throw new ServletException(e); + } assertEquals(2, parameters.size()); assertTrue(parameters.containsKey("param1")); @@ -128,7 +136,7 @@ void doHandle(String target, Request baseRequest, HttpServletRequest request, Ht value = parameters.get("param2"); assertEquals(1, value.length); assertEquals("value2", value[0]); - okAllIsWell(response); + return okAllIsWell(response, callback); } }); } @@ -137,8 +145,8 @@ static void registerFormAuth() { // Check the form authentication registerHandler("/formAuth", HttpMode.GET, new SimpleHandler() { @Override - void doHandle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException { - okAllIsWell(response); + boolean doHandle(Request request, Response response, Callback callback) { + return okAllIsWell(response, callback); } }); } @@ -147,8 +155,9 @@ static void registerFormAuthBad() { // Check the form authentication header registerHandler("/formAuthBad", HttpMode.GET, new SimpleHandler() { @Override - void doHandle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException { - body(response, HttpServletResponse.SC_BAD_REQUEST, ContentType.TEXT_PLAIN, "Not allowed"); + boolean doHandle(Request request, Response response, Callback callback) { + Response.writeError(request, response, callback, HttpStatus.BAD_REQUEST_400, "Not allowed"); + return true; } }); } @@ -157,8 +166,8 @@ static void registerBasicAuth() { // Check the basic authentication header registerHandler("/basicAuth", HttpMode.GET, new SimpleHandler() { @Override - void doHandle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException { - Enumeration headers = request.getHeaders(HttpHeaders.AUTHORIZATION); + boolean doHandle(Request request, Response response, Callback callback) { + Enumeration headers = request.getHeaders().getValues(HttpHeaders.AUTHORIZATION); String value = headers.nextElement(); assertFalse(headers.hasMoreElements()); @@ -169,7 +178,7 @@ void doHandle(String target, Request baseRequest, HttpServletRequest request, Ht assertEquals("username1", usernamePassword[0]); assertEquals("password1", usernamePassword[1]); - okAllIsWell(response); + return okAllIsWell(response, callback); } }); } @@ -178,12 +187,12 @@ static void registerCheckRequestBodyWithTag() { // Check that request body is present and that the containing parameter ${Tag} has been resolved to "trunk" registerHandler("/checkRequestBodyWithTag", HttpMode.POST, new SimpleHandler() { @Override - void doHandle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException { + boolean doHandle(Request request, Response response, Callback callback) throws IOException { assertEquals("POST", request.getMethod()); String requestBody = requestBody(request); assertEquals("cleanupDir=D:/continuousIntegration/deployments/Daimler/trunk/standalone", requestBody); - okAllIsWell(response); + return okAllIsWell(response, callback); } }); } @@ -192,8 +201,8 @@ static void registerCustomHeaders() { // Check the custom headers registerHandler("/customHeaders", HttpMode.GET, new SimpleHandler() { @Override - void doHandle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException { - Enumeration headers = request.getHeaders("customHeader"); + boolean doHandle(Request request, Response response, Callback callback) { + Enumeration headers = request.getHeaders().getValues("customHeader"); String value1 = headers.nextElement(); String value2 = headers.nextElement(); @@ -202,7 +211,7 @@ void doHandle(String target, Request baseRequest, HttpServletRequest request, Ht assertEquals("value1", value1); assertEquals("value2", value2); - okAllIsWell(response); + return okAllIsWell(response, callback); } }); } @@ -211,12 +220,13 @@ static void registerInvalidStatusCode() { // Return an invalid status code registerHandler("/invalidStatusCode", HttpMode.GET, new SimpleHandler() { @Override - void doHandle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException { + boolean doHandle(Request request, Response response, Callback callback) { assertEquals("GET", request.getMethod()); - String query = request.getQueryString(); + String query = request.getHttpURI().getQuery(); assertNull(query); - body(response, HttpServletResponse.SC_BAD_REQUEST, ContentType.TEXT_PLAIN, "Throwing status 400 for test"); + Response.writeError(request, response, callback, HttpStatus.BAD_REQUEST_400, "Throwing status 400 for test"); + return true; } }); } @@ -225,18 +235,19 @@ static void registerCustomHeadersResolved() { // Check if the parameters in custom headers have been resolved registerHandler("/customHeadersResolved", HttpMode.POST, new SimpleHandler() { @Override - void doHandle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException { - Enumeration headers = request.getHeaders("resolveCustomParam"); + boolean doHandle(Request request, Response response, Callback callback) { + Enumeration headers = request.getHeaders().getValues("resolveCustomParam"); + String value = headers.nextElement(); assertFalse(headers.hasMoreElements()); assertEquals("trunk", value); - headers = request.getHeaders("resolveEnvParam"); + headers = request.getHeaders().getValues("resolveEnvParam"); value = headers.nextElement(); assertFalse(headers.hasMoreElements()); assertEquals("C:/path/to/my/workspace", value); - okAllIsWell(response); + return okAllIsWell(response, callback); } }); } @@ -245,17 +256,23 @@ static void registerCheckBuildParameters() { // Check that exactly one build parameter is passed registerHandler("/checkBuildParameters", HttpMode.GET, new SimpleHandler() { @Override - void doHandle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException { + boolean doHandle(Request request, Response response, Callback callback) throws ServletException { assertEquals("GET", request.getMethod()); - Map parameters = request.getParameterMap(); + Map parameters; + try { + parameters = Request.getParameters(request).toStringArrayMap(); + } catch (Exception e) { + throw new ServletException(e); + } + assertEquals(1, parameters.size()); assertTrue(parameters.containsKey("foo")); String[] value = parameters.get("foo"); assertEquals(1, value.length); assertEquals("value", value[0]); - okAllIsWell(response); + return okAllIsWell(response, callback); } }); } @@ -264,112 +281,92 @@ static void registerCheckRequestBody() { // Check that request body is present and equals to TestRequestBody registerHandler("/checkRequestBody", HttpMode.POST, new SimpleHandler() { @Override - void doHandle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException { + boolean doHandle(Request request, Response response, Callback callback) throws IOException { assertEquals("POST", request.getMethod()); String requestBody = requestBody(request); assertEquals("TestRequestBody", requestBody); - okAllIsWell(response); + return okAllIsWell(response, callback); } }); } - static void registerFileUpload(final File testFolder, final File uploadFile, final String responseText) { + static void registerFileUpload(final File uploadFile, final String responseText) { registerHandler("/uploadFile", HttpMode.POST, new SimpleHandler() { private static final String MULTIPART_FORMDATA_TYPE = "multipart/form-data"; - private void enableMultipartSupport(HttpServletRequest request, MultipartConfigElement multipartConfig) { - request.setAttribute(Request.__MULTIPART_CONFIG_ELEMENT, multipartConfig); - } - - private boolean isMultipartRequest(ServletRequest request) { - return request.getContentType() != null && request.getContentType().startsWith(MULTIPART_FORMDATA_TYPE); + private boolean isMultipartRequest(Request request) { + return request.getHeaders().get(HttpHeader.CONTENT_TYPE) != null && request.getHeaders().get(HttpHeader.CONTENT_TYPE).startsWith(MULTIPART_FORMDATA_TYPE); } @Override - void doHandle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { + boolean doHandle(Request request, Response response, Callback callback) throws ServletException { assertEquals("POST", request.getMethod()); assertTrue(isMultipartRequest(request)); - MultipartConfigElement multipartConfig = new MultipartConfigElement(testFolder.getAbsolutePath()); - enableMultipartSupport(request, multipartConfig); - + MultiPartFormData.Parts parts; try { - Part part = request.getPart("file-name"); - assertNotNull(part); - assertEquals(uploadFile.length(), part.getSize()); - assertEquals(uploadFile.getName(), part.getSubmittedFileName()); - assertEquals(MimeType.APPLICATION_ZIP.getValue(), part.getContentType()); - - body(response, HttpServletResponse.SC_CREATED, ContentType.TEXT_PLAIN, responseText); - } finally { - String MULTIPART = "org.eclipse.jetty.servlet.MultiPartFile.multiPartInputStream"; - MultiPartFormInputStream multipartInputStream = (MultiPartFormInputStream) request.getAttribute(MULTIPART); - if (multipartInputStream != null) { - multipartInputStream.deleteParts(); - } + String contentType = request.getHeaders().get(HttpHeader.CONTENT_TYPE); + parts = MultiPartFormData.from(request, request, contentType, new MultiPartConfig.Builder().build()).get(); + } catch (InterruptedException | ExecutionException e) { + throw new ServletException(e); } + + MultiPart.Part part = parts.getFirst("file-name"); + assertNotNull(part); + assertEquals(uploadFile.length(), part.getLength()); + assertEquals(uploadFile.getName(), part.getFileName()); + assertEquals(MimeType.APPLICATION_ZIP.getValue(), part.getHeaders().get(HttpHeader.CONTENT_TYPE)); + + return body(response, HttpStatus.CREATED_201, ContentType.TEXT_PLAIN, responseText, callback); } }); } - static void registerFormData(final File testFolder, String content, final File file1, + static void registerFormData(String content, final File file1, File file2, final String responseText) { registerHandler("/formData", HttpMode.POST, new SimpleHandler() { private static final String MULTIPART_FORMDATA_TYPE = "multipart/form-data"; - private void enableMultipartSupport(HttpServletRequest request, - MultipartConfigElement multipartConfig) { - request.setAttribute(Request.__MULTIPART_CONFIG_ELEMENT, multipartConfig); - } - - private boolean isMultipartRequest(ServletRequest request) { - return request.getContentType() != null - && request.getContentType().startsWith(MULTIPART_FORMDATA_TYPE); + private boolean isMultipartRequest(Request request) { + return request.getHeaders().get(HttpHeader.CONTENT_TYPE) != null + && request.getHeaders().get(HttpHeader.CONTENT_TYPE).startsWith(MULTIPART_FORMDATA_TYPE); } @Override - void doHandle(String target, Request baseRequest, HttpServletRequest request, - HttpServletResponse response) throws IOException, ServletException { + boolean doHandle(Request request, Response response, Callback callback) throws ServletException { assertEquals("POST", request.getMethod()); assertTrue(isMultipartRequest(request)); - MultipartConfigElement multipartConfig = - new MultipartConfigElement(testFolder.getAbsolutePath()); - enableMultipartSupport(request, multipartConfig); - + MultiPartFormData.Parts parts; try { - Part file1Part = request.getPart("file1"); - assertNotNull(file1Part); - assertEquals(file1.length(), file1Part.getSize()); - assertEquals(file1.getName(), file1Part.getSubmittedFileName()); - assertEquals(MimeType.TEXT_PLAIN.getValue(), file1Part.getContentType()); - - Part file2Part = request.getPart("file2"); - assertNotNull(file2Part); - assertEquals(file2.length(), file2Part.getSize()); - assertEquals(file2.getName(), file2Part.getSubmittedFileName()); - assertEquals(MimeType.APPLICATION_ZIP.getValue(), file2Part.getContentType()); - - Part modelPart = request.getPart("model"); - assertNotNull(modelPart); - assertEquals(content, - IOUtils.toString(modelPart.getInputStream(), StandardCharsets.UTF_8)); - assertEquals(MimeType.APPLICATION_JSON.getValue(), modelPart.getContentType()); - - // So far so good - body(response, HttpServletResponse.SC_CREATED, ContentType.TEXT_PLAIN, - responseText); - } finally { - String MULTIPART = - "org.eclipse.jetty.servlet.MultiPartFile.multiPartInputStream"; - MultiPartFormInputStream multipartInputStream = - (MultiPartFormInputStream) request.getAttribute(MULTIPART); - if (multipartInputStream != null) { - multipartInputStream.deleteParts(); - } + String contentType = request.getHeaders().get(HttpHeader.CONTENT_TYPE); + parts = MultiPartFormData.from(request, request, contentType, new MultiPartConfig.Builder().build()).get(); + } catch (InterruptedException | ExecutionException e) { + throw new ServletException(e); } + + MultiPart.Part file1Part = parts.getFirst("file1"); + assertNotNull(file1Part); + assertEquals(file1.length(), file1Part.getLength()); + assertEquals(file1.getName(), file1Part.getFileName()); + assertEquals(MimeType.TEXT_PLAIN.getValue(), file1Part.getHeaders().get(HttpHeader.CONTENT_TYPE)); + + MultiPart.Part file2Part = parts.getFirst("file2"); + assertNotNull(file2Part); + assertEquals(file2.length(), file2Part.getLength()); + assertEquals(file2.getName(), file2Part.getFileName()); + assertEquals(MimeType.APPLICATION_ZIP.getValue(), file2Part.getHeaders().get(HttpHeader.CONTENT_TYPE)); + + MultiPart.Part modelPart = parts.getFirst("model"); + assertNotNull(modelPart); + assertEquals(content, modelPart.getContentAsString(StandardCharsets.UTF_8)); + assertEquals(MimeType.APPLICATION_JSON.getValue(), modelPart.getHeaders().get(HttpHeader.CONTENT_TYPE)); + + // So far so good + return body(response, HttpStatus.CREATED_201, ContentType.TEXT_PLAIN, + responseText, callback); } }); } @@ -379,17 +376,17 @@ static void registerUnwrappedPutFileUpload(final File uploadFile, final String r private static final String MULTIPART_FORMDATA_TYPE = "multipart/form-data"; - private boolean isMultipartRequest(ServletRequest request) { - return request.getContentType() != null && request.getContentType().startsWith(MULTIPART_FORMDATA_TYPE); + private boolean isMultipartRequest(Request request) { + return request.getHeaders().get(HttpHeader.CONTENT_TYPE) != null && request.getHeaders().get(HttpHeader.CONTENT_TYPE).startsWith(MULTIPART_FORMDATA_TYPE); } @Override - void doHandle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException { + boolean doHandle(Request request, Response response, Callback callback) { assertEquals("PUT", request.getMethod()); assertFalse(isMultipartRequest(request)); - assertEquals(uploadFile.length(), request.getContentLength()); - assertEquals(MimeType.APPLICATION_ZIP.getValue(), request.getContentType()); - body(response, HttpServletResponse.SC_CREATED, ContentType.TEXT_PLAIN, responseText); + assertEquals(uploadFile.length(), request.getLength()); + assertEquals(MimeType.APPLICATION_ZIP.getValue(), request.getHeaders().get(HttpHeader.CONTENT_TYPE)); + return body(response, HttpStatus.CREATED_201, ContentType.TEXT_PLAIN, responseText, callback); } }); }