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/01/19 14:50:06 UTC

[karaf] branch karaf-4.2.x updated (5f7605d -> b24c41b)

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

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


    from 5f7605d  [KARAF-6593] Replace references in client script to slf4j-api bundle jar with pax-logging-api and remove it from assembly. Set version of slf4j-api to 1.7.29 in parent pom.
     new 898bd4d  [KARAF-6603] Upgrade to Pax Web 7.2.14
     new b24c41b  [KARAF-6597] Add servlet upload example and corresponding itest.

The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 examples/karaf-servlet-example/README.md           | 18 +++++-
 .../src/main/feature/feature.xml                   |  6 ++
 .../pom.xml                                        |  6 +-
 .../karaf/examples/servlet/upload/Component.java}  | 32 +++++-----
 .../examples/servlet/upload/UploadServlet.java     | 70 ++++++++++++++++++++++
 examples/karaf-servlet-example/pom.xml             |  1 +
 .../karaf/itests/examples/ServletExampleTest.java  | 63 +++++++++++++++++++
 pom.xml                                            |  2 +-
 8 files changed, 176 insertions(+), 22 deletions(-)
 copy examples/karaf-servlet-example/{karaf-servlet-example-scr => karaf-servlet-example-upload}/pom.xml (91%)
 copy examples/{karaf-soap-example/karaf-soap-example-scr/src/main/java/org/apache/karaf/examples/soap/scr/SoapService.java => karaf-servlet-example/karaf-servlet-example-upload/src/main/java/org/apache/karaf/examples/servlet/upload/Component.java} (61%)
 create mode 100644 examples/karaf-servlet-example/karaf-servlet-example-upload/src/main/java/org/apache/karaf/examples/servlet/upload/UploadServlet.java


[karaf] 01/02: [KARAF-6603] Upgrade to Pax Web 7.2.14

Posted by jb...@apache.org.
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

commit 898bd4d8c65cbd56efc86465ce068d4950dc4f82
Author: Jean-Baptiste Onofré <jb...@apache.org>
AuthorDate: Sun Jan 19 14:16:36 2020 +0100

    [KARAF-6603] Upgrade to Pax Web 7.2.14
    
    (cherry picked from commit d028bd2468f2d07163b12f711e3056acb44ad2e5)
---
 pom.xml | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/pom.xml b/pom.xml
index 5ab0f20..a7a7a41 100644
--- a/pom.xml
+++ b/pom.xml
@@ -284,7 +284,7 @@
         <pax.base.version>1.5.1</pax.base.version>
         <pax.swissbox.version>1.8.3</pax.swissbox.version>
         <pax.url.version>2.6.2</pax.url.version>
-        <pax.web.version>7.2.13</pax.web.version>
+        <pax.web.version>7.2.14</pax.web.version>
         <pax.tinybundle.version>3.0.0</pax.tinybundle.version>
         <pax.jdbc.version>1.4.4</pax.jdbc.version>
         <pax.jms.version>1.0.6</pax.jms.version>


[karaf] 02/02: [KARAF-6597] Add servlet upload example and corresponding itest.

Posted by jb...@apache.org.
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

commit b24c41be121253b25cae879853e3473c35f728f5
Author: Jean-Baptiste Onofré <jb...@apache.org>
AuthorDate: Sun Jan 19 08:12:14 2020 +0100

    [KARAF-6597] Add servlet upload example and corresponding itest.
    
    (cherry picked from commit 70a831d0f4947c90cf68e7b69989d3ac5a24678c)
---
 examples/karaf-servlet-example/README.md           | 18 +++++-
 .../src/main/feature/feature.xml                   |  6 ++
 .../{ => karaf-servlet-example-upload}/pom.xml     | 40 +++++++++----
 .../karaf/examples/servlet/upload/Component.java   | 46 ++++++++++++++
 .../examples/servlet/upload/UploadServlet.java     | 70 ++++++++++++++++++++++
 examples/karaf-servlet-example/pom.xml             |  1 +
 .../karaf/itests/examples/ServletExampleTest.java  | 63 +++++++++++++++++++
 7 files changed, 230 insertions(+), 14 deletions(-)

diff --git a/examples/karaf-servlet-example/README.md b/examples/karaf-servlet-example/README.md
index c1d683f..b5f704d 100644
--- a/examples/karaf-servlet-example/README.md
+++ b/examples/karaf-servlet-example/README.md
@@ -84,4 +84,20 @@ karaf@root()> feature:install karaf-servlet-example-scr
 
 Whatever feature you use, you can access the servlet on the following URL:
 
-[http://localhost:8181/servlet-example]
\ No newline at end of file
+[http://localhost:8181/servlet-example]
+
+## Upload Servlet
+
+You can also find a upload servlet example using multipart data.
+
+You can install it with:
+
+```
+karaf@root()> feature:install karaf-servlet-example-upload
+```
+
+Then, you can use `curl` to upload data via this servlet:
+
+```
+curl --progress-bar -v -k -F file=/my/file http://127.0.0.1:8181/upload-example
+```
\ No newline at end of file
diff --git a/examples/karaf-servlet-example/karaf-servlet-example-features/src/main/feature/feature.xml b/examples/karaf-servlet-example/karaf-servlet-example-features/src/main/feature/feature.xml
index 1b1af79..1bbfa94 100644
--- a/examples/karaf-servlet-example/karaf-servlet-example-features/src/main/feature/feature.xml
+++ b/examples/karaf-servlet-example/karaf-servlet-example-features/src/main/feature/feature.xml
@@ -42,4 +42,10 @@
         <bundle>mvn:org.apache.karaf.examples/karaf-servlet-example-scr/${project.version}</bundle>
     </feature>
 
+    <feature name="karaf-servlet-example-upload" version="${project.version}">
+        <feature prerequisite="true">scr</feature>
+        <feature prerequisite="true">http</feature>
+        <bundle>mvn:org.apache.karaf.examples/karaf-servlet-example-upload/${project.version}</bundle>
+    </feature>
+
 </features>
diff --git a/examples/karaf-servlet-example/pom.xml b/examples/karaf-servlet-example/karaf-servlet-example-upload/pom.xml
similarity index 59%
copy from examples/karaf-servlet-example/pom.xml
copy to examples/karaf-servlet-example/karaf-servlet-example-upload/pom.xml
index c3163ba..f46f534 100644
--- a/examples/karaf-servlet-example/pom.xml
+++ b/examples/karaf-servlet-example/karaf-servlet-example-upload/pom.xml
@@ -23,21 +23,35 @@
 
     <parent>
         <groupId>org.apache.karaf.examples</groupId>
-        <artifactId>apache-karaf-examples</artifactId>
-        <version>4.2.8-SNAPSHOT</version>
+        <artifactId>karaf-servlet-example</artifactId>
+        <version>4.3.0-SNAPSHOT</version>
         <relativePath>../pom.xml</relativePath>
     </parent>
 
-    <artifactId>karaf-servlet-example</artifactId>
-    <name>Apache Karaf :: Examples :: Servlet</name>
-    <packaging>pom</packaging>
-
-    <modules>
-        <module>karaf-servlet-example-annotation</module>
-        <module>karaf-servlet-example-registration</module>
-        <module>karaf-servlet-example-blueprint</module>
-        <module>karaf-servlet-example-scr</module>
-        <module>karaf-servlet-example-features</module>
-    </modules>
+    <artifactId>karaf-servlet-example-upload</artifactId>
+    <name>Apache Karaf :: Examples :: Servlet :: Upload</name>
+    <packaging>bundle</packaging>
+
+    <dependencies>
+        <dependency>
+            <groupId>javax.servlet</groupId>
+            <artifactId>javax.servlet-api</artifactId>
+            <version>3.0.1</version>
+        </dependency>
+        <dependency>
+            <groupId>org.osgi</groupId>
+            <artifactId>osgi.cmpn</artifactId>
+            <version>6.0.0</version>
+        </dependency>
+    </dependencies>
+
+    <build>
+        <plugins>
+            <plugin>
+                <groupId>org.apache.felix</groupId>
+                <artifactId>maven-bundle-plugin</artifactId>
+            </plugin>
+        </plugins>
+    </build>
 
 </project>
\ No newline at end of file
diff --git a/examples/karaf-servlet-example/karaf-servlet-example-upload/src/main/java/org/apache/karaf/examples/servlet/upload/Component.java b/examples/karaf-servlet-example/karaf-servlet-example-upload/src/main/java/org/apache/karaf/examples/servlet/upload/Component.java
new file mode 100644
index 0000000..ffa208b
--- /dev/null
+++ b/examples/karaf-servlet-example/karaf-servlet-example-upload/src/main/java/org/apache/karaf/examples/servlet/upload/Component.java
@@ -0,0 +1,46 @@
+/*
+ *  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.upload;
+
+import org.osgi.service.component.annotations.Activate;
+import org.osgi.service.component.annotations.Deactivate;
+import org.osgi.service.component.annotations.Reference;
+import org.osgi.service.http.HttpService;
+
+import java.nio.file.Path;
+import java.nio.file.Paths;
+
+@org.osgi.service.component.annotations.Component
+public class Component {
+
+    @Reference
+    protected HttpService httpService;
+
+    @Activate
+    public void activate() throws Exception {
+        final String tmpDir = System.getProperty("java.io.tmpdir");
+        final Path uploadPath = Paths.get(tmpDir, "karaf", "upload");
+        uploadPath.toFile().mkdirs();
+        httpService.registerServlet("/upload-example", new UploadServlet(uploadPath), null, null);
+    }
+
+    @Deactivate
+    public void deactivate() throws Exception {
+        httpService.unregister("/upload-example");
+    }
+
+}
diff --git a/examples/karaf-servlet-example/karaf-servlet-example-upload/src/main/java/org/apache/karaf/examples/servlet/upload/UploadServlet.java b/examples/karaf-servlet-example/karaf-servlet-example-upload/src/main/java/org/apache/karaf/examples/servlet/upload/UploadServlet.java
new file mode 100644
index 0000000..81329f8
--- /dev/null
+++ b/examples/karaf-servlet-example/karaf-servlet-example-upload/src/main/java/org/apache/karaf/examples/servlet/upload/UploadServlet.java
@@ -0,0 +1,70 @@
+/*
+ *  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.upload;
+
+import javax.servlet.MultipartConfigElement;
+import javax.servlet.ServletException;
+import javax.servlet.ServletRegistration;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import javax.servlet.http.Part;
+import java.io.File;
+import java.io.IOException;
+import java.nio.file.Path;
+import java.util.Collection;
+import java.util.Map;
+
+public class UploadServlet extends HttpServlet {
+
+    /** The size threshold after which the file will be written to disk. */
+    private static final int FILE_SIZE_THRESHOLD = 1024 * 1024 * 2;
+
+    /** The maximum size allowed for uploaded files (-1L means unlimited). */
+    private static final long MAX_FILE_SIZE = 1024 * 1024 * 1024;
+
+    /** The maximum size allowed for "multipart/form-data" requests (-1L means unlimited). */
+    private static final long MAX_REQUEST_SIZE = 1024 * 1024 * 1024;
+
+    private final File tempDir;
+
+    public UploadServlet(final Path tempDir) {
+        this.tempDir = tempDir.toFile();
+    }
+
+    @Override
+    public void init() throws ServletException {
+        final MultipartConfigElement multipartConfigElement = new MultipartConfigElement(tempDir.getAbsolutePath(), MAX_FILE_SIZE, MAX_REQUEST_SIZE, FILE_SIZE_THRESHOLD);
+        for (final Map.Entry<String, ? extends ServletRegistration> entry : getServletContext().getServletRegistrations()
+                .entrySet()) {
+            final ServletRegistration reg = entry.getValue();
+            if (reg == null) {
+                continue;
+            }
+            if (reg instanceof ServletRegistration.Dynamic) {
+                final ServletRegistration.Dynamic regDyn = (ServletRegistration.Dynamic) reg;
+                regDyn.setMultipartConfig(multipartConfigElement);
+            }
+        }
+    }
+
+    @Override
+    public void doPost(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException {
+        final Collection<Part> parts = request.getParts();
+    }
+
+}
diff --git a/examples/karaf-servlet-example/pom.xml b/examples/karaf-servlet-example/pom.xml
index c3163ba..2b91318 100644
--- a/examples/karaf-servlet-example/pom.xml
+++ b/examples/karaf-servlet-example/pom.xml
@@ -37,6 +37,7 @@
         <module>karaf-servlet-example-registration</module>
         <module>karaf-servlet-example-blueprint</module>
         <module>karaf-servlet-example-scr</module>
+        <module>karaf-servlet-example-upload</module>
         <module>karaf-servlet-example-features</module>
     </modules>
 
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 4ef1037..27c541a 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
@@ -17,6 +17,7 @@
 package org.apache.karaf.itests.examples;
 
 import org.apache.karaf.itests.BaseTest;
+import org.junit.Assert;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.ops4j.pax.exam.junit.PaxExam;
@@ -24,7 +25,14 @@ import org.ops4j.pax.exam.spi.reactors.ExamReactorStrategy;
 import org.ops4j.pax.exam.spi.reactors.PerMethod;
 
 import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.FileWriter;
 import java.io.InputStreamReader;
+import java.io.OutputStream;
+import java.io.OutputStreamWriter;
+import java.io.PrintWriter;
 import java.net.HttpURLConnection;
 import java.net.URL;
 
@@ -97,4 +105,59 @@ public class ServletExampleTest extends BaseTest {
         verify();
     }
 
+    @Test
+    public void testUploadServlet() throws Exception {
+        setup();
+
+        installAndAssertFeature("karaf-servlet-example-upload");
+
+        String command = executeCommand("http:list");
+        while (!command.contains("upload-example")) {
+            Thread.sleep(200);
+            command = executeCommand("http:list");
+        }
+        System.out.println(command);
+
+        File file = new File(System.getProperty("karaf.data"), "test.txt");
+        FileWriter fileWriter = new FileWriter(file);
+        fileWriter.write("test");
+        fileWriter.flush();
+        fileWriter.close();
+
+        URL url = new URL("http://localhost:" + getHttpPort() + "/upload-example");
+        String boundary = "===" + System.currentTimeMillis() + "===";
+        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
+        connection.setRequestMethod("POST");
+        connection.setUseCaches(false);
+        connection.setDoInput(true);
+        connection.setDoOutput(true);
+        connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
+
+        OutputStream outputStream = connection.getOutputStream();
+
+        PrintWriter writer = new PrintWriter(new OutputStreamWriter(outputStream), true);
+        writer.append("--" + boundary).append("\r\n");
+        writer.append("Content-Disposition: form-data; name=\"test\"; filename=\"test.txt\"").append("\r\n");
+        writer.append("Content-Type: text/plain; charset=UTF-8").append("\r\n");
+        writer.append("Content-Transfer-Encoding: binary").append("\r\n");
+        writer.append("\r\n");
+        writer.flush();
+
+        FileInputStream fileInputStream = new FileInputStream(file);
+        byte[] buffer = new byte[1024];
+        int bytesRead = -1;
+        while ((bytesRead = fileInputStream.read(buffer)) != -1) {
+            outputStream.write(buffer, 0, bytesRead);
+        }
+        outputStream.flush();
+        fileInputStream.close();
+        writer.append("\r\n");
+        writer.append("\r\n");
+        writer.append("--" + boundary + "--").append("\r\n");
+        writer.flush();
+        writer.close();
+
+        Assert.assertEquals(200, connection.getResponseCode());
+    }
+
 }