/*
* Copyright (c) xlightweb.org, 2006 - 2009. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Please refer to the LGPL license at: http://www.gnu.org/copyleft/lesser.txt
* The latest copy of this software may be found on http://www.xlightweb.org/
*/
package org.xlightweb;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.channels.Channels;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.multipart.FilePart;
import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
import org.apache.commons.httpclient.methods.multipart.Part;
import org.apache.commons.httpclient.methods.multipart.StringPart;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.junit.Assert;
import org.junit.Test;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.xlightweb.client.HttpClient;
import org.xlightweb.server.HttpServer;
import org.xsocket.connection.BlockingConnection;
import org.xsocket.connection.IBlockingConnection;
/**
*
* @author grro@xlightweb.org
*/
public final class MultipartUploadTest {
public static void main(String[] args) throws Exception {
for (int i = 0; i < 1000; i++) {
new MultipartUploadTest().testClientMultiFile();
}
}
@Test
public void testServerSingleFile() throws Exception {
System.out.println("testServerSingleFile");
System.setProperty(IHttpExchange.SHOW_DETAILED_ERROR_KEY, "true");
HttpServer server = new HttpServer(new RequestHandler());
server.start();
PostMethod filePost = new PostMethod("http://localhost:" + server.getLocalPort() + "/");
org.apache.commons.httpclient.HttpClient client = new org.apache.commons.httpclient.HttpClient();
File file = QAUtil.createTestfile_4k();
Part part = new FilePart("file4k", file);
HttpMethodParams params = new HttpMethodParams();
params.setBooleanParameter("testb", false);
filePost.setRequestEntity(new MultipartRequestEntity(new Part[] {part }, params));
client.executeMethod(filePost);
if (filePost.getStatusCode() != 200) {
System.out.println(filePost.getResponseBodyAsString());
Assert.fail("error occured");
}
file.delete();
server.close();
}
@Test
public void testServerMultiFile() throws Exception {
System.out.println("testServerMultiFile");
HttpServer server = new HttpServer(new RequestHandler());
server.start();
PostMethod filePost = new PostMethod("http://localhost:" + server.getLocalPort() + "/");
org.apache.commons.httpclient.HttpClient client = new org.apache.commons.httpclient.HttpClient();
File file1 = QAUtil.createTestfile_40k();
File file2 = QAUtil.createTestfile_4k();
Part part1 = new FilePart("file40k", file1);
Part part2 = new FilePart("file4k", file2);
HttpMethodParams params = new HttpMethodParams();
params.setBooleanParameter("testb", false);
filePost.setRequestEntity(new MultipartRequestEntity(new Part[] {part1, part2 }, params));
client.executeMethod(filePost);
if (filePost.getStatusCode() != 200) {
Assert.fail("error occured");
}
file1.delete();
file2.delete();
server.close();
}
@Test
public void testAsyncServerMultiFile() throws Exception {
/*
AsyncRequestHandler hdl = new AsyncRequestHandler();
HttpServer server = new HttpServer(new AsyncRequestHandler());
server.start();
PostMethod filePost = new PostMethod("http://localhost:" + server.getLocalPort() + "/");
org.apache.commons.httpclient.HttpClient client = new org.apache.commons.httpclient.HttpClient();
Part part1 = new FilePart("file40k", new File(QAUtil.getNameTestfile_40k()));
Part part2 = new FilePart("file4k", new File(QAUtil.getNameTestfile_4k()));
HttpMethodParams params = new HttpMethodParams();
params.setBooleanParameter("testb", false);
filePost.setRequestEntity(new MultipartRequestEntity(new Part[] {part1, part2 }, params));
client.executeMethod(filePost);
if (filePost.getStatusCode() != 200) {
Assert.fail("error occured");
}
server.close();
*/
}
@Test
public void testServerMultiStringPartFile() throws Exception {
System.out.println("testServerMultiStringPartFile");
HttpServer server = new HttpServer(new RequestHandler());
server.start();
PostMethod filePost = new PostMethod("http://localhost:" + server.getLocalPort() + "/");
org.apache.commons.httpclient.HttpClient client = new org.apache.commons.httpclient.HttpClient();
Part part1 = new StringPart("part1", "0123456789\r\n");
Part part2 = new StringPart("part2", "567890\r\n");
Part part3 = new StringPart("part3", "3456\r\n");
HttpMethodParams params = new HttpMethodParams();
params.setBooleanParameter("testb", false);
filePost.setRequestEntity(new MultipartRequestEntity(new Part[] {part1, part2, part3 }, params));
client.executeMethod(filePost);
if (filePost.getStatusCode() != 200) {
Assert.fail("error occured");
}
server.close();
}
@Test
public void testServerFragementedMultiFile2() throws Exception {
System.out.println("testServerFragementedMultiFile2");
System.setProperty(IHttpExchange.SHOW_DETAILED_ERROR_KEY, "true");
HttpServer server = new HttpServer(new InvokeOnMessageRequestHandler());
server.start();
IBlockingConnection con = new BlockingConnection("localhost", server.getLocalPort());
con.setAutoflush(false);
con.write("POST / HTTP/1.1\r\n" +
"Host: localhost:" + server.getLocalPort() + "\r\n" +
"User-Agent: testclient\r\n" +
"Content-Type: multipart/form-data; boundary=0cr76rTHEafjLO4oJ-qL6MXaYBL8Na9C\r\n" +
"Content-Length: 384\r\n" +
"\r\n" +
"--0cr76rTHEafjLO4oJ-qL6MXaYBL8Na9C\r\n" +
"Content-Disposition: form-data; name=\"part1\"\r\n");
con.flush();
QAUtil.sleep(1000);
con.write("Content-Type: text/plain; charset=US-ASCII\r\n" +
"Content-Transfer-Encoding: 8bit\r\n" +
"\r\n" +
"0123456789\r\n\r\n" +
"--0cr76rTHEafjLO4oJ-qL6MXaYBL8Na9C\r\n" +
"Content-Disposition: form-data; name=\"part2\"\r\n");
con.flush();
QAUtil.sleep(1000);
con.write("Content-Type: text/plain; charset=US-ASCII\r\n" +
"Content-Transfer-Encoding: 8bit\r\n" +
"\r\n" +
"567890\r\n\r\n" +
"--0cr76rTHEafjLO4o");
con.flush();
QAUtil.sleep(1000);
con.write("J-qL6MXaYBL8Na9C--\r\n");
con.flush();
String header = con.readStringByDelimiter("\r\n\r\n");
Assert.assertTrue(header.indexOf("200 OK") != -1);
con.close();
server.close();
}
public void testServerFragementedMultiFile() throws Exception {
System.out.println("testServerFragementedMultiFile");
System.setProperty(IHttpExchange.SHOW_DETAILED_ERROR_KEY, "true");
HttpServer server = new HttpServer(new RequestHandler());
server.start();
IBlockingConnection con = new BlockingConnection("localhost", server.getLocalPort());
con.setAutoflush(false);
con.write("POST / HTTP/1.1\r\n" +
"Host: localhost:" + server.getLocalPort() + "\r\n" +
"User-Agent: testclient\r\n" +
"Content-Type: multipart/form-data; boundary=0cr76rTHEafjLO4oJ-qL6MXaYBL8Na9C\r\n" +
"Content-Length: 384\r\n" +
"\r\n" +
"--0cr76rTHEafjLO4oJ-qL6MXaYBL8Na9C\r\n" +
"Content-Disposition: form-data; name=\"part1\"\r\n");
con.flush();
QAUtil.sleep(1000);
con.write("Content-Type: text/plain; charset=US-ASCII\r\n" +
"Content-Transfer-Encoding: 8bit\r\n" +
"\r\n" +
"0123456789\r\n\r\n" +
"--0cr76rTHEafjLO4oJ-qL6MXaYBL8Na9C\r\n" +
"Content-Disposition: form-data; name=\"part2\"\r\n");
con.flush();
QAUtil.sleep(1000);
con.write("Content-Type: text/plain; charset=US-ASCII\r\n" +
"Content-Transfer-Encoding: 8bit\r\n" +
"\r\n" +
"567890\r\n\r\n" +
"--0cr76rTHEafjLO4o");
con.flush();
QAUtil.sleep(1000);
con.write("J-qL6MXaYBL8Na9C--\r\n");
con.flush();
String header = con.readStringByDelimiter("\r\n\r\n");
Assert.assertTrue(header.indexOf("200 OK") != -1);
con.close();
server.close();
}
@Test
public void testServerMultiFileAdd() throws Exception {
System.out.println("testServerMultiFileAdd");
HttpServer server = new HttpServer(new AddingRequestHandler());
server.start();
HttpClient httpClient = new HttpClient();
MultipartFormDataRequest req = new MultipartFormDataRequest("Http://localhost:" + server.getLocalPort() + "/");
File file1 = QAUtil.createTestfile_40k();
File file2 = QAUtil.createTestfile_4k();
req.addPart("file40k", file1);
req.addPart("file4k", file2);
IHttpResponse resp = httpClient.call(req);
Assert.assertEquals(200, resp.getStatus());
file1.delete();
file2.delete();
httpClient.close();
server.close();
}
@Test
public void testClientSingleFile() throws Exception {
System.out.println("testClientSingleFile");
WebContainer server = new WebContainer(new TestServlet());
server.start();
HttpClient httpClient = new HttpClient();
MultipartFormDataRequest req = new MultipartFormDataRequest("http://localhost:" + server.getLocalPort()+ "/");
String name = "file80byte";
File file = QAUtil.createTestfile_80byte();
req.addPart(name, file);
IHttpResponse resp = httpClient.call(req);
FileInputStream fis = new FileInputStream(file);
Content content = new Content(name, file.getName(), "text/html", fis);
fis.close();
String body = resp.getBlockingBody().toString();
Assert.assertEquals(content.toString() + "\r\n", body);
fis.close();
file.delete();
httpClient.close();
server.stop();
}
@Test
public void testClientStringAndFile() throws Exception {
System.out.println("testClientStringAndFile");
WebContainer server = new WebContainer(new TestServlet());
server.start();
HttpClient httpClient = new HttpClient();
MultipartFormDataRequest req = new MultipartFormDataRequest("http://localhost:" + server.getLocalPort()+ "/");
String name = "file40k";
File file = QAUtil.createTestfile_4k();
req.addPart(name, file);
req.addPart("test1", "I wake up early and ...");
req.addPart("test2", "... saw the light");
IHttpResponse resp = httpClient.call(req);
FileInputStream fis = new FileInputStream(file);
Content content = new Content(name, file.getName(), "text/html", fis);
fis.close();
String body = resp.getBlockingBody().toString();
Assert.assertEquals(content.toString() + "\r\n", body);
fis.close();
file.delete();
httpClient.close();
server.stop();
}
@Test
public void testClientMultiFile() throws Exception {
System.out.println("testClientMultiFile");
WebContainer server = new WebContainer(new TestServlet());
server.start();
HttpClient httpClient = new HttpClient();
MultipartFormDataRequest req = new MultipartFormDataRequest("http://localhost:" + server.getLocalPort()+ "/");
String name1 = "file4k";
File file1 = QAUtil.createTestfile_4k();
String name2 = "file40k";
File file2 = QAUtil.createTestfile_40k();
req.addPart(name1, file1);
req.addPart(name2, file2);
IHttpResponse resp = httpClient.call(req);
FileInputStream fis1 = new FileInputStream(file1);
Content content1 = new Content(name1, file1.getName(), "text/html", fis1);
fis1.close();
FileInputStream fis2 = new FileInputStream(file2);
Content content2 = new Content(name2, file2.getName(), "text/html", fis2);
fis2.close();
String body = resp.getBlockingBody().toString();
Assert.assertEquals(content1.toString() + "\r\n" + content2.toString() + "\r\n", body);
fis1.close();
file1.delete();
file2.delete();
httpClient.close();
server.stop();
}
private static final class RequestHandler implements IHttpRequestHandler {
public void onRequest(final IHttpExchange exchange) throws IOException, BadMessageException {
final StringBuilder sb = new StringBuilder();
IHttpRequest req = exchange.getRequest();
if (MultipartFormDataRequest.isMultipartFormDataRequest(req)) {
final MultipartFormDataRequest multipartReq = new MultipartFormDataRequest(req);
IBodyCompleteListener cl = new IBodyCompleteListener() {
public void onComplete() throws IOException {
Map<String, IPart> partMap = multipartReq.getPartMap();
for (String name : partMap.keySet()) {
IPart part = partMap.get(name);
String orgFilename = part.getDispositionParam("filename");
String contentType = part.getContentType();
InputStream is = Channels.newInputStream(partMap.get(name).getBlockingBody());
Content content = new Content((String) name, orgFilename, contentType, is);
is.close();
sb.append(content + "\r\n");
}
exchange.send(new HttpResponse(200, "text/plain", sb.toString()));
}
};
req.getNonBlockingBody().addCompleteListener(cl);
return;
}
exchange.sendError(500);
}
}
private static final class AsyncRequestHandler implements IHttpRequestHandler {
private final PartHandler partHandler = new PartHandler();
public void onRequest(final IHttpExchange exchange) throws IOException, BadMessageException {
IHttpRequest req = exchange.getRequest();
if (MultipartFormDataRequest.isMultipartFormDataRequest(req)) {
MultipartFormDataRequest multipartReq = new MultipartFormDataRequest(req);
multipartReq.setPartHandler(partHandler);
return;
}
exchange.sendError(500);
}
PartHandler getPartHandler() {
return partHandler;
}
}
private static final class PartHandler implements IPartHandler {
private final List<IPart> parts = new ArrayList<IPart>();
private boolean competePartOnly = true;
public void onPart(IPart part) throws IOException, BadMessageException {
parts.add(part);
competePartOnly = competePartOnly && part.getNonBlockingBody().isComplete();
}
boolean isCompletePartsOnly() {
return competePartOnly;
}
List<IPart> getParts() {
return parts;
}
}
@InvokeOn(InvokeOn.MESSAGE_RECEIVED)
private static final class InvokeOnMessageRequestHandler implements IHttpRequestHandler {
public void onRequest(IHttpExchange exchange) throws IOException, BadMessageException {
StringBuilder sb = new StringBuilder();
IHttpRequest req = exchange.getRequest();
if (MultipartFormDataRequest.isMultipartFormDataRequest(req)) {
MultipartFormDataRequest multipartReq = new MultipartFormDataRequest(req);
Map<String, IPart> partMap = multipartReq.getPartMap();
for (String name : partMap.keySet()) {
IPart part = partMap.get(name);
String orgFilename = part.getDispositionParam("filename");
String contentType = part.getContentType();
InputStream is = Channels.newInputStream(partMap.get(name).getBlockingBody());
Content content = new Content((String) name, orgFilename, contentType, is);
is.close();
sb.append(content + "\r\n");
}
exchange.send(new HttpResponse(200, "text/plain", sb.toString()));
}
exchange.sendError(500);
}
}
@InvokeOn(InvokeOn.MESSAGE_RECEIVED)
private static final class AddingRequestHandler implements IHttpRequestHandler {
public void onRequest(IHttpExchange exchange) throws IOException, BadMessageException {
IHttpRequest req = exchange.getRequest();
if (MultipartFormDataRequest.isMultipartFormDataRequest(req)) {
MultipartFormDataRequest multipartReq = new MultipartFormDataRequest(req);
String name3 = "file4kSec";
File file3 = QAUtil.createTestfile_4k();
try {
multipartReq.addPart(name3, file3);
exchange.send(new HttpResponse(500));
} catch (IOException expected) {
exchange.send(new HttpResponse(200));
}
file3.delete();
}
exchange.sendError(500);
}
}
private static final class TestServlet extends HttpServlet {
private static final long serialVersionUID = -5985963633008722396L;
private CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
@SuppressWarnings("unchecked")
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
StringBuilder sb = new StringBuilder();
MultipartHttpServletRequest multipartRequest = multipartResolver.resolveMultipart(req);
Map filemap = multipartRequest.getFileMap();
for (Object name : filemap.keySet()) {
MultipartFile mpf = (MultipartFile) filemap.get(name);
String orgFilename = mpf.getOriginalFilename();
String contentType = mpf.getContentType();
InputStream is = mpf.getInputStream();
Content content = new Content((String) name, orgFilename, contentType, is);
is.close();
sb.append(content + "\r\n");
}
resp.getWriter().write(sb.toString());
}
}
private static final class Content {
private String content;
public Content(String name, String filename, String contentType, InputStream is) throws IOException {
StringBuilder sb = new StringBuilder();
sb.append("name=" + name +"\r\n");
sb.append("filename=" + filename +"\r\n");
sb.append("contentType=" + contentType +"\r\n");
sb.append("content=");
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
int numRead=0;
while((numRead = is.read(buf)) != -1){
bos.write(buf, 0, numRead);
}
bos.close();
sb.append(bos.toString());
content = sb.toString();
}
@Override
public String toString() {
return content;
}
}
}