Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion examples/karaf-servlet-example/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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]
[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
```
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<!--

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.
-->

<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.apache.karaf.examples</groupId>
<artifactId>karaf-servlet-example</artifactId>
<version>4.3.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

<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>
Original file line number Diff line number Diff line change
@@ -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");
}

}
Original file line number Diff line number Diff line change
@@ -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();
}

}
1 change: 1 addition & 0 deletions examples/karaf-servlet-example/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,22 @@
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;
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;

Expand Down Expand Up @@ -97,4 +105,59 @@ public void testWithScr() throws Exception {
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());
}

}
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down