diff options
author | ritchie <ritchie@gmx.at> | 2015-09-12 09:13:26 +0200 |
---|---|---|
committer | ritchie <ritchie@gmx.at> | 2015-09-12 09:13:26 +0200 |
commit | cebdea5194b468d09fa78f69c4b234c9a7acb080 (patch) | |
tree | 128f0142340adc4d4236110184a57a545a94d1cc | |
parent | afdc49fd4c3644346ece0ff66e0db41cc98aaf42 (diff) | |
download | nanohttpd-cebdea5194b468d09fa78f69c4b234c9a7acb080.tar.gz |
apache file upload integration #216 #219 (unit test not acive yet)
-rw-r--r-- | fileupload/pom.xml | 78 | ||||
-rw-r--r-- | fileupload/src/main/java/fi/iki/elonen/NanoFileUpload.java | 178 | ||||
-rw-r--r-- | fileupload/src/test/java/fi/iki/elonen/TestNanoFileUpLoad.java | 172 | ||||
-rw-r--r-- | nanolets/pom.xml | 2 |
4 files changed, 319 insertions, 111 deletions
diff --git a/fileupload/pom.xml b/fileupload/pom.xml index da60ecf..2ed65d3 100644 --- a/fileupload/pom.xml +++ b/fileupload/pom.xml @@ -1,39 +1,43 @@ <?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"> - - <parent> - <artifactId>nanohttpd-project</artifactId> - <groupId>org.nanohttpd</groupId> - <version>2.2.0-SNAPSHOT</version> - </parent> - <modelVersion>4.0.0</modelVersion> - - <artifactId>apache-fileupload-integration</artifactId> - <dependencies> - <dependency> - <groupId>org.nanohttpd</groupId> - <artifactId>nanohttpd</artifactId> - <version>2.2.0-SNAPSHOT</version> - <scope>provided</scope> - </dependency> - - <dependency> - <groupId>commons-fileupload</groupId> - <artifactId>commons-fileupload</artifactId> - <version>1.3.1</version> - <scope>provided</scope> - </dependency> - - <!-- this is just to pass the compilation. Servlets are not used in NanoHTTPD --> - <dependency> - <groupId>javax.servlet</groupId> - <artifactId>servlet-api</artifactId> - <version>2.5</version> - <scope>provided</scope> - </dependency> - </dependencies> - - +<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"> + <parent> + <artifactId>nanohttpd-project</artifactId> + <groupId>org.nanohttpd</groupId> + <version>2.2.0-SNAPSHOT</version> + </parent> + <modelVersion>4.0.0</modelVersion> + <artifactId>nanohttpd-apache-fileupload</artifactId> + <name>NanoHttpd-apache file upload integration</name> + <dependencies> + <dependency> + <groupId>org.nanohttpd</groupId> + <artifactId>nanohttpd</artifactId> + <version>2.2.0-SNAPSHOT</version> + <scope>provided</scope> + </dependency> + <dependency> + <groupId>commons-fileupload</groupId> + <artifactId>commons-fileupload</artifactId> + <version>1.3.1</version> + </dependency> + <dependency> + <groupId>javax.servlet</groupId> + <artifactId>servlet-api</artifactId> + <version>2.5</version> + <scope>provided</scope> + </dependency> + <dependency> + <groupId>org.apache.httpcomponents</groupId> + <artifactId>httpclient</artifactId> + <version>4.4.1</version> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.apache.httpcomponents</groupId> + <artifactId>httpmime</artifactId> + <version>4.4.1</version> + <scope>test</scope> + </dependency> + </dependencies> </project>
\ No newline at end of file diff --git a/fileupload/src/main/java/fi/iki/elonen/NanoFileUpload.java b/fileupload/src/main/java/fi/iki/elonen/NanoFileUpload.java index f51df3c..6dd06d5 100644 --- a/fileupload/src/main/java/fi/iki/elonen/NanoFileUpload.java +++ b/fileupload/src/main/java/fi/iki/elonen/NanoFileUpload.java @@ -1,5 +1,45 @@ package fi.iki.elonen; +/* + * #%L + * apache-fileupload-integration + * %% + * Copyright (C) 2012 - 2015 nanohttpd + * %% + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. Neither the name of the nanohttpd nor the names of its contributors + * may be used to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE + * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + * OF THE POSSIBILITY OF SUCH DAMAGE. + * #L% + */ + +import static fi.iki.elonen.NanoHTTPD.Method.POST; + +import java.io.IOException; +import java.io.InputStream; +import java.util.List; +import java.util.Map; + import org.apache.commons.fileupload.FileItem; import org.apache.commons.fileupload.FileItemFactory; import org.apache.commons.fileupload.FileItemIterator; @@ -8,82 +48,74 @@ import org.apache.commons.fileupload.FileUploadBase; import org.apache.commons.fileupload.FileUploadException; import org.apache.commons.fileupload.UploadContext; -import java.io.IOException; -import java.io.InputStream; -import java.util.List; -import java.util.Map; - /** - * Created by victor on 7/30/15. + * @author victor & ritchieGitHub */ public class NanoFileUpload extends FileUpload { - public static class NanoHttpdContext implements UploadContext { - - private NanoHTTPD.IHTTPSession session; - - public NanoHttpdContext(NanoHTTPD.IHTTPSession session) { - this.session = session; - } - - @Override - public long contentLength() { - long size; - try { - String cl1 = session.getHeaders().get("content-length"); - size = Long.parseLong(cl1); - } catch (NumberFormatException var4) { - size = -1L; - } - - return size; - } - - @Override - public String getCharacterEncoding() { - return "UTF-8"; - } - - @Override - public String getContentType() { - return this.session.getHeaders().get("content-type"); - } - - @Override - public int getContentLength() { - return (int)contentLength(); - } - - @Override - public InputStream getInputStream() throws IOException { - return session.getInputStream(); - } - } - - private static final String POST_METHOD = "POST"; - - public static final boolean isMultipartContent(NanoHTTPD.IHTTPSession session) { - return !"POST".equalsIgnoreCase(session.getMethod().toString()) - ? false: FileUploadBase.isMultipartContent(new NanoHttpdContext(session)); - } - - public NanoFileUpload() { - } - - public NanoFileUpload(FileItemFactory fileItemFactory) { - super(fileItemFactory); - } - - public List<FileItem> parseRequest(NanoHTTPD.IHTTPSession session) throws FileUploadException { - return this.parseRequest(new NanoHttpdContext(session)); - } - - public Map<String, List<FileItem>> parseParameterMap(NanoHTTPD.IHTTPSession session) throws FileUploadException { - return this.parseParameterMap(new NanoHttpdContext(session)); - } - - public FileItemIterator getItemIterator(NanoHTTPD.IHTTPSession session) throws FileUploadException, IOException { - return super.getItemIterator(new NanoHttpdContext(session)); - } + public static class NanoHttpdContext implements UploadContext { + + private NanoHTTPD.IHTTPSession session; + + public NanoHttpdContext(NanoHTTPD.IHTTPSession session) { + this.session = session; + } + + @Override + public long contentLength() { + long size; + try { + String cl1 = session.getHeaders().get("content-length"); + size = Long.parseLong(cl1); + } catch (NumberFormatException var4) { + size = -1L; + } + + return size; + } + + @Override + public String getCharacterEncoding() { + return "UTF-8"; + } + + @Override + public String getContentType() { + return this.session.getHeaders().get("content-type"); + } + + @Override + public int getContentLength() { + return (int) contentLength(); + } + + @Override + public InputStream getInputStream() throws IOException { + return session.getInputStream(); + } + } + + public static final boolean isMultipartContent(NanoHTTPD.IHTTPSession session) { + return session.getMethod() == POST && FileUploadBase.isMultipartContent(new NanoHttpdContext(session)); + } + + public NanoFileUpload() { + } + + public NanoFileUpload(FileItemFactory fileItemFactory) { + super(fileItemFactory); + } + + public List<FileItem> parseRequest(NanoHTTPD.IHTTPSession session) throws FileUploadException { + return this.parseRequest(new NanoHttpdContext(session)); + } + + public Map<String, List<FileItem>> parseParameterMap(NanoHTTPD.IHTTPSession session) throws FileUploadException { + return this.parseParameterMap(new NanoHttpdContext(session)); + } + + public FileItemIterator getItemIterator(NanoHTTPD.IHTTPSession session) throws FileUploadException, IOException { + return super.getItemIterator(new NanoHttpdContext(session)); + } } diff --git a/fileupload/src/test/java/fi/iki/elonen/TestNanoFileUpLoad.java b/fileupload/src/test/java/fi/iki/elonen/TestNanoFileUpLoad.java new file mode 100644 index 0000000..558ab01 --- /dev/null +++ b/fileupload/src/test/java/fi/iki/elonen/TestNanoFileUpLoad.java @@ -0,0 +1,172 @@ +package fi.iki.elonen; + +/* + * #%L + * NanoHttpd-apache file upload integration + * %% + * Copyright (C) 2012 - 2015 nanohttpd + * %% + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. Neither the name of the nanohttpd nor the names of its contributors + * may be used to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE + * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + * OF THE POSSIBILITY OF SUCH DAMAGE. + * #L% + */ + +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.net.InetAddress; +import java.util.List; +import java.util.Map; + +import org.apache.commons.fileupload.FileItem; +import org.apache.commons.fileupload.FileUploadException; +import org.apache.commons.fileupload.disk.DiskFileItemFactory; +import org.apache.http.HttpEntity; +import org.apache.http.HttpResponse; +import org.apache.http.client.methods.CloseableHttpResponse; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.client.methods.HttpTrace; +import org.apache.http.entity.ContentType; +import org.apache.http.entity.mime.HttpMultipartMode; +import org.apache.http.entity.mime.MultipartEntityBuilder; +import org.apache.http.entity.mime.content.FileBody; +import org.apache.http.entity.mime.content.StringBody; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClients; +import org.junit.After; +import org.junit.Before; +import org.junit.Ignore; +import org.junit.Test; + +public class TestNanoFileUpLoad { + + protected TestServer testServer; + + public static class TestServer extends NanoHTTPD { + + public Response response = newFixedLengthResponse(""); + + public String uri; + + public Method method; + + public Map<String, String> header; + + public Map<String, String> parms; + + public Map<String, List<FileItem>> files; + + public Map<String, List<String>> decodedParamters; + + public Map<String, List<String>> decodedParamtersFromParameter; + + public String queryParameterString; + + public TestServer() { + super(8192); + uploader = new NanoFileUpload(); + uploader.setFileItemFactory(new DiskFileItemFactory()); + } + + public HTTPSession createSession(TempFileManager tempFileManager, InputStream inputStream, OutputStream outputStream) { + return new HTTPSession(tempFileManager, inputStream, outputStream); + } + + public HTTPSession createSession(TempFileManager tempFileManager, InputStream inputStream, OutputStream outputStream, InetAddress inetAddress) { + return new HTTPSession(tempFileManager, inputStream, outputStream, inetAddress); + } + + @Override + public String decodePercent(String str) { + return super.decodePercent(str); + } + + NanoFileUpload uploader; + + @Override + public Response serve(IHTTPSession session) { + + this.uri = session.getUri(); + this.method = session.getMethod(); + this.header = session.getHeaders(); + this.parms = session.getParms(); + if (NanoFileUpload.isMultipartContent(session)) { + try { + files = uploader.parseParameterMap(session); + + } catch (FileUploadException e) { + e.printStackTrace(); + } + } + this.queryParameterString = session.getQueryParameterString(); + this.decodedParamtersFromParameter = decodeParameters(this.queryParameterString); + this.decodedParamters = decodeParameters(session.getQueryParameterString()); + return this.response; + } + + } + + @Test + public void testNormalRequest() throws Exception { + CloseableHttpClient httpclient = HttpClients.createDefault(); + HttpTrace httphead = new HttpTrace("http://localhost:8192/index.html"); + CloseableHttpResponse response = httpclient.execute(httphead); + response.close(); + + } + + @Ignore + @Test + public void testPostWithMultipartFormUpload() throws Exception { + CloseableHttpClient httpclient = HttpClients.createDefault(); + String textFileName = "src/test/java/fi/iki/elonen/TestNanoFileUpLoad.java"; + HttpPost post = new HttpPost("http://localhost:8192/uploadFile"); + FileBody fileBody = new FileBody(new File(textFileName), ContentType.DEFAULT_BINARY); + StringBody stringBody1 = new StringBody("Message 1", ContentType.MULTIPART_FORM_DATA); + + MultipartEntityBuilder builder = MultipartEntityBuilder.create(); + builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE); + builder.addPart("upfile", fileBody); + builder.addPart("text1", stringBody1); + HttpEntity entity = builder.build(); + // + post.setEntity(entity); + HttpResponse response = httpclient.execute(post); + response.toString(); + } + + @Before + public void setUp() throws IOException { + this.testServer = new TestServer(); + this.testServer.start(); + } + + @After + public void tearDown() { + this.testServer.stop(); + } + +} diff --git a/nanolets/pom.xml b/nanolets/pom.xml index 39af427..f6d3239 100644 --- a/nanolets/pom.xml +++ b/nanolets/pom.xml @@ -8,7 +8,7 @@ </parent> <artifactId>nanohttpd-nanolets</artifactId> <packaging>jar</packaging> - <name>NanoHttpd nano application server</name> + <name>NanoHttpd-nano application server</name> <dependencies> <dependency> <groupId>${project.groupId}</groupId> |