You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@karaf.apache.org by jb...@apache.org on 2020/12/09 08:54:40 UTC

[karaf] branch karaf-4.2.x updated: Add servlet multipart example

This is an automated email from the ASF dual-hosted git repository.

jbonofre pushed a commit to branch karaf-4.2.x
in repository https://gitbox.apache.org/repos/asf/karaf.git


The following commit(s) were added to refs/heads/karaf-4.2.x by this push:
     new e5dc4f4  Add servlet multipart example
e5dc4f4 is described below

commit e5dc4f4b588151a2058bc4198bf1377d9b097f0a
Author: jbonofre <jb...@apache.org>
AuthorDate: Fri Dec 4 15:55:14 2020 +0100

    Add servlet multipart example
    
    (cherry picked from commit b3fd8b7bdaab56531ff973d50505d9e49ce5ff75)
---
 .../servlet/annotation/ExampleServlet.java         |  1 +
 .../annotation/MultipartExampleServlet.java        | 59 ++++++++++++++++++++++
 .../karaf/itests/examples/ServletExampleTest.java  |  6 +++
 3 files changed, 66 insertions(+)

diff --git a/examples/karaf-servlet-example/karaf-servlet-example-annotation/src/main/java/org/apache/karaf/examples/servlet/annotation/ExampleServlet.java b/examples/karaf-servlet-example/karaf-servlet-example-annotation/src/main/java/org/apache/karaf/examples/servlet/annotation/ExampleServlet.java
index 498f159..349894d 100644
--- a/examples/karaf-servlet-example/karaf-servlet-example-annotation/src/main/java/org/apache/karaf/examples/servlet/annotation/ExampleServlet.java
+++ b/examples/karaf-servlet-example/karaf-servlet-example-annotation/src/main/java/org/apache/karaf/examples/servlet/annotation/ExampleServlet.java
@@ -17,6 +17,7 @@
 package org.apache.karaf.examples.servlet.annotation;
 
 import javax.servlet.ServletException;
+import javax.servlet.annotation.MultipartConfig;
 import javax.servlet.annotation.WebServlet;
 import javax.servlet.http.HttpServlet;
 import javax.servlet.http.HttpServletRequest;
diff --git a/examples/karaf-servlet-example/karaf-servlet-example-annotation/src/main/java/org/apache/karaf/examples/servlet/annotation/MultipartExampleServlet.java b/examples/karaf-servlet-example/karaf-servlet-example-annotation/src/main/java/org/apache/karaf/examples/servlet/annotation/MultipartExampleServlet.java
new file mode 100644
index 0000000..994a0e0
--- /dev/null
+++ b/examples/karaf-servlet-example/karaf-servlet-example-annotation/src/main/java/org/apache/karaf/examples/servlet/annotation/MultipartExampleServlet.java
@@ -0,0 +1,59 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one or more
+ *  contributor license agreements.  See the NOTICE file distributed with
+ *  this work for additional information regarding copyright ownership.
+ *  The ASF licenses this file to You under the Apache License, Version 2.0
+ *  (the "License"); you may not use this file except in compliance with
+ *  the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+package org.apache.karaf.examples.servlet.annotation;
+
+import javax.servlet.ServletException;
+import javax.servlet.annotation.MultipartConfig;
+import javax.servlet.annotation.WebServlet;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import javax.servlet.http.Part;
+import java.io.IOException;
+
+@WebServlet(value = "/multipart", name = "multipart")
+@MultipartConfig(fileSizeThreshold = 1024 * 1024 * 10, // 10MB
+        maxFileSize = 1024 * 1024 * 50, // 50MB
+        maxRequestSize = 1024 * 1024 * 100 // 100MB
+)
+public class MultipartExampleServlet extends HttpServlet {
+
+    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
+        // final Part filePart = request.getPart("exampleFile");
+
+        String fileName = null;
+        // get all parts from request and write it to the file on server
+        for (Part part : request.getParts()) {
+            fileName = getFileName(part);
+        }
+
+        response.getWriter().write("Part of file: " + fileName);
+    }
+
+    private String getFileName(Part part) {
+        String contentDisp = part.getHeader("content-disposition");
+        System.out.println("content-disposition header=" + contentDisp);
+        String[] tokens = contentDisp.split(";");
+        for (String token : tokens) {
+            if (token.trim().startsWith("name")) {
+                return token.substring(token.indexOf("=") + 2, token.length() - 1);
+            }
+        }
+        return "";
+    }
+
+}
diff --git a/itests/test/src/test/java/org/apache/karaf/itests/examples/ServletExampleTest.java b/itests/test/src/test/java/org/apache/karaf/itests/examples/ServletExampleTest.java
index f4568a2..fb48a7a 100644
--- a/itests/test/src/test/java/org/apache/karaf/itests/examples/ServletExampleTest.java
+++ b/itests/test/src/test/java/org/apache/karaf/itests/examples/ServletExampleTest.java
@@ -83,6 +83,12 @@ public class ServletExampleTest extends BaseTest {
 
         installAndAssertFeature("karaf-servlet-example-annotation");
 
+        String command = executeCommand("http:list");
+        while (!command.contains("servlet-example/multipart")) {
+            Thread.sleep(200);
+            command = executeCommand("http:list");
+        }
+
         verify();
     }