Skip to content

Commit

Permalink
Merge branch 'originalMaster' of https://github.com/vnnv/nanohttpd in…
Browse files Browse the repository at this point in the history
…to vnnv-originalMaster

Conflicts:
	pom.xml
  • Loading branch information
ritchieGitHub committed Sep 12, 2015
2 parents 350ed42 + 4d2c4ad commit 2ac451a
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 0 deletions.
39 changes: 39 additions & 0 deletions fileupload/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?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>
89 changes: 89 additions & 0 deletions fileupload/src/main/java/fi/iki/elonen/NanoFileUpload.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package fi.iki.elonen;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileItemIterator;
import org.apache.commons.fileupload.FileUpload;
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.
*/
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));
}

}
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
<module>websocket</module>
<module>markdown-plugin</module>
<module>nanolets</module>
<module>fileupload</module>
</modules>
<licenses>
<license>
Expand Down

0 comments on commit 2ac451a

Please sign in to comment.