349110 MultiPartFilter records the content-type in request params

This commit is contained in:
Jan Bartel 2011-11-15 19:09:31 +11:00
parent 9beadb5f3a
commit be9597f8f2
2 changed files with 30 additions and 2 deletions

View File

@ -63,6 +63,7 @@ import org.eclipse.jetty.util.TypeUtil;
*/
public class MultiPartFilter implements Filter
{
public final static String CONTENT_TYPE_SUFFIX=".org.eclipse.jetty.servlet.contentType";
private final static String FILES ="org.eclipse.jetty.servlet.MultiPartFilter.files";
private File tempdir;
private boolean _deleteFiles;
@ -132,8 +133,11 @@ public class MultiPartFilter implements Filter
String content_disposition=null;
String content_transfer_encoding=null;
outer:while(!lastPart)
{
String type_content=null;
while(true)
{
// read a line
@ -155,7 +159,9 @@ public class MultiPartFilter implements Filter
if(key.equals("content-disposition"))
content_disposition=value;
else if(key.equals("content-transfer-encoding"))
content_transfer_encoding=value;
content_transfer_encoding=value;
else if (key.equals("content-type"))
type_content = value;
}
}
// Extract content-disposition
@ -207,6 +213,8 @@ public class MultiPartFilter implements Filter
out = new BufferedOutputStream(out, _fileOutputBuffer);
request.setAttribute(name,file);
params.add(name, filename);
if (type_content != null)
params.add(name+CONTENT_TYPE_SUFFIX, type_content);
if (_deleteFiles)
{
@ -330,6 +338,8 @@ public class MultiPartFilter implements Filter
{
bytes = ((ByteArrayOutputStream)out).toByteArray();
params.add(name,bytes);
if (type_content != null)
params.add(name+CONTENT_TYPE_SUFFIX, type_content);
}
}

View File

@ -15,6 +15,7 @@ package org.eclipse.jetty.servlets;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertNotNull;
import java.io.File;
import java.io.FileInputStream;
@ -39,6 +40,21 @@ public class MultipartFilterTest
private File _dir;
private ServletTester tester;
public static class TestServlet extends DumpServlet
{
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
{
assertNotNull(req.getParameter("fileup"));
assertNotNull(req.getParameter("fileup"+MultiPartFilter.CONTENT_TYPE_SUFFIX));
assertEquals(req.getParameter("fileup"+MultiPartFilter.CONTENT_TYPE_SUFFIX), "application/octet-stream");
super.doPost(req, resp);
}
}
@Before
public void setUp() throws Exception
{
@ -51,7 +67,7 @@ public class MultipartFilterTest
tester=new ServletTester();
tester.setContextPath("/context");
tester.setResourceBase(_dir.getCanonicalPath());
tester.addServlet(DumpServlet.class, "/");
tester.addServlet(TestServlet.class, "/");
FilterHolder multipartFilter = tester.addFilter(MultiPartFilter.class,"/*",FilterMapping.DEFAULT);
multipartFilter.setInitParameter("deleteFiles", "true");
tester.start();
@ -93,6 +109,7 @@ public class MultipartFilterTest
assertTrue(response.getMethod()==null);
assertEquals(HttpServletResponse.SC_OK,response.getStatus());
}
@Test
public void testPost() throws Exception
@ -124,6 +141,7 @@ public class MultipartFilterTest
assertEquals(HttpServletResponse.SC_OK,response.getStatus());
assertTrue(response.getContent().indexOf("brown cow")>=0);
}
@Test
public void testEncodedPost() throws Exception