Search in sources :

Example 11 with IFileItem

use of com.helger.web.fileupload.IFileItem in project ph-web by phax.

the class DiskFileItemTest method _createFileItem.

/**
 * Create a FileItem with the specified content bytes.
 */
private IFileItem _createFileItem(final byte[] contentBytes) {
    final IFileItemFactory factory = new DiskFileItemFactory(THRESHOLD);
    final String textFieldName = "textField";
    final IFileItem item = factory.createItem(textFieldName, CT_TEXT, true, "My File Name");
    try (final OutputStream os = item.getOutputStream()) {
        os.write(contentBytes);
    } catch (final IOException e) {
        fail("Unexpected IOException" + e);
    }
    return item;
}
Also used : OutputStream(java.io.OutputStream) NonBlockingByteArrayOutputStream(com.helger.commons.io.stream.NonBlockingByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) IFileItem(com.helger.web.fileupload.IFileItem) IOException(java.io.IOException) IFileItemFactory(com.helger.web.fileupload.IFileItemFactory)

Example 12 with IFileItem

use of com.helger.web.fileupload.IFileItem in project ph-web by phax.

the class ServletFileUploadTest method testFileUpload.

@Test
public void testFileUpload() throws FileUploadException {
    final List<IFileItem> fileItems = parseUpload("-----1234\r\n" + "Content-Disposition: form-data; name=\"file\"; filename=\"foo.tab\"\r\n" + "Content-Type: text/whatever\r\n" + "\r\n" + "This is the content of the file\n" + "\r\n" + "-----1234\r\n" + "Content-Disposition: form-data; name=\"field\"\r\n" + "\r\n" + "fieldValue\r\n" + "-----1234\r\n" + "Content-Disposition: form-data; name=\"multi\"\r\n" + "\r\n" + "value1\r\n" + "-----1234\r\n" + "Content-Disposition: form-data; name=\"multi\"\r\n" + "\r\n" + "value2\r\n" + "-----1234--\r\n");
    assertEquals(4, fileItems.size());
    final IFileItem file = fileItems.get(0);
    assertEquals("file", file.getFieldName());
    assertFalse(file.isFormField());
    assertEquals("This is the content of the file\n", file.getString());
    assertEquals("text/whatever", file.getContentType());
    assertEquals("foo.tab", file.getName());
    final IFileItem field = fileItems.get(1);
    assertEquals("field", field.getFieldName());
    assertTrue(field.isFormField());
    assertEquals("fieldValue", field.getString());
    final IFileItem multi0 = fileItems.get(2);
    assertEquals("multi", multi0.getFieldName());
    assertTrue(multi0.isFormField());
    assertEquals("value1", multi0.getString());
    final IFileItem multi1 = fileItems.get(3);
    assertEquals("multi", multi1.getFieldName());
    assertTrue(multi1.isFormField());
    assertEquals("value2", multi1.getString());
}
Also used : IFileItem(com.helger.web.fileupload.IFileItem) Test(org.junit.Test)

Example 13 with IFileItem

use of com.helger.web.fileupload.IFileItem in project ph-web by phax.

the class ServletFileUploadTest method testIE5MacBug.

/**
 * Internet Explorer 5 for the Mac has a bug where the carriage return is
 * missing on any boundary line immediately preceding an input with
 * type=image. (type=submit does not have the bug.)
 *
 * @throws FileUploadException
 *         In case of error
 */
@Test
public void testIE5MacBug() throws FileUploadException {
    final List<IFileItem> fileItems = parseUpload("-----1234\r\n" + "Content-Disposition: form-data; name=\"field1\"\r\n" + "\r\n" + "fieldValue\r\n" + // NOTE \r
    "-----1234\n" + // missing
    "Content-Disposition: form-data; name=\"submitName.x\"\r\n" + "\r\n" + "42\r\n" + // NOTE \r
    "-----1234\n" + // missing
    "Content-Disposition: form-data; name=\"submitName.y\"\r\n" + "\r\n" + "21\r\n" + "-----1234\r\n" + "Content-Disposition: form-data; name=\"field2\"\r\n" + "\r\n" + "fieldValue2\r\n" + "-----1234--\r\n");
    assertEquals(4, fileItems.size());
    final IFileItem field1 = fileItems.get(0);
    assertEquals("field1", field1.getFieldName());
    assertTrue(field1.isFormField());
    assertEquals("fieldValue", field1.getString());
    final IFileItem submitX = fileItems.get(1);
    assertEquals("submitName.x", submitX.getFieldName());
    assertTrue(submitX.isFormField());
    assertEquals("42", submitX.getString());
    final IFileItem submitY = fileItems.get(2);
    assertEquals("submitName.y", submitY.getFieldName());
    assertTrue(submitY.isFormField());
    assertEquals("21", submitY.getString());
    final IFileItem field2 = fileItems.get(3);
    assertEquals("field2", field2.getFieldName());
    assertTrue(field2.isFormField());
    assertEquals("fieldValue2", field2.getString());
}
Also used : IFileItem(com.helger.web.fileupload.IFileItem) Test(org.junit.Test)

Example 14 with IFileItem

use of com.helger.web.fileupload.IFileItem in project ph-web by phax.

the class ServletFileUploadTest method testFILEUPLOAD62.

/**
 * Test for
 * <a href="http://issues.apache.org/jira/browse/FILEUPLOAD-62">FILEUPLOAD-62
 * </a>
 *
 * @throws Exception
 *         In case of error
 */
@Test
public void testFILEUPLOAD62() throws Exception {
    final String contentType = "multipart/form-data; boundary=AaB03x";
    final String request = "--AaB03x\r\n" + "content-disposition: form-data; name=\"field1\"\r\n" + "\r\n" + "Joe Blow\r\n" + "--AaB03x\r\n" + "content-disposition: form-data; name=\"pics\"\r\n" + "Content-type: multipart/mixed; boundary=BbC04y\r\n" + "\r\n" + "--BbC04y\r\n" + "Content-disposition: attachment; filename=\"file1.txt\"\r\n" + "Content-Type: text/plain\r\n" + "\r\n" + "... contents of file1.txt ...\r\n" + "--BbC04y\r\n" + "Content-disposition: attachment; filename=\"file2.gif\"\r\n" + "Content-type: image/gif\r\n" + "Content-Transfer-Encoding: binary\r\n" + "\r\n" + "...contents of file2.gif...\r\n" + "--BbC04y--\r\n" + "--AaB03x--";
    final List<IFileItem> fileItems = parseUpload(request.getBytes(StandardCharsets.US_ASCII), contentType);
    assertEquals(3, fileItems.size());
    final IFileItem item0 = fileItems.get(0);
    assertEquals("field1", item0.getFieldName());
    assertNull(item0.getName());
    assertEquals("Joe Blow", new String(item0.directGet(), StandardCharsets.ISO_8859_1));
    final IFileItem item1 = fileItems.get(1);
    assertEquals("pics", item1.getFieldName());
    assertEquals("file1.txt", item1.getName());
    assertEquals("... contents of file1.txt ...", new String(item1.directGet(), StandardCharsets.ISO_8859_1));
    final IFileItem item2 = fileItems.get(2);
    assertEquals("pics", item2.getFieldName());
    assertEquals("file2.gif", item2.getName());
    assertEquals("...contents of file2.gif...", new String(item2.directGet(), StandardCharsets.ISO_8859_1));
}
Also used : IFileItem(com.helger.web.fileupload.IFileItem) Test(org.junit.Test)

Example 15 with IFileItem

use of com.helger.web.fileupload.IFileItem in project ph-web by phax.

the class ServletFileUploadTest method testFoldedHeaders.

/**
 * Test for
 * <a href="http://issues.apache.org/jira/browse/FILEUPLOAD-111">FILEUPLOAD
 * -111</a>
 *
 * @throws FileUploadException
 *         In case of error
 */
@Test
public void testFoldedHeaders() throws FileUploadException {
    final List<IFileItem> fileItems = parseUpload("-----1234\r\n" + "Content-Disposition: form-data; name=\"file\"; filename=\"foo.tab\"\r\n" + "Content-Type: text/whatever\r\n" + "\r\n" + "This is the content of the file\n" + "\r\n" + "-----1234\r\n" + "Content-Disposition: form-data; \r\n" + "\tname=\"field\"\r\n" + "\r\n" + "fieldValue\r\n" + "-----1234\r\n" + "Content-Disposition: form-data;\r\n" + "     name=\"multi\"\r\n" + "\r\n" + "value1\r\n" + "-----1234\r\n" + "Content-Disposition: form-data; name=\"multi\"\r\n" + "\r\n" + "value2\r\n" + "-----1234--\r\n");
    assertEquals(4, fileItems.size());
    final IFileItem file = fileItems.get(0);
    assertEquals("file", file.getFieldName());
    assertFalse(file.isFormField());
    assertEquals("This is the content of the file\n", file.getString());
    assertEquals("text/whatever", file.getContentType());
    assertEquals("foo.tab", file.getName());
    final IFileItem field = fileItems.get(1);
    assertEquals("field", field.getFieldName());
    assertTrue(field.isFormField());
    assertEquals("fieldValue", field.getString());
    final IFileItem multi0 = fileItems.get(2);
    assertEquals("multi", multi0.getFieldName());
    assertTrue(multi0.isFormField());
    assertEquals("value1", multi0.getString());
    final IFileItem multi1 = fileItems.get(3);
    assertEquals("multi", multi1.getFieldName());
    assertTrue(multi1.isFormField());
    assertEquals("value2", multi1.getString());
}
Also used : IFileItem(com.helger.web.fileupload.IFileItem) Test(org.junit.Test)

Aggregations

IFileItem (com.helger.web.fileupload.IFileItem)21 Test (org.junit.Test)11 HCNodeList (com.helger.html.hc.impl.HCNodeList)6 Locale (java.util.Locale)6 IOException (java.io.IOException)5 CommonsArrayList (com.helger.commons.collection.impl.CommonsArrayList)4 PDTFromString (com.helger.commons.datetime.PDTFromString)4 PDTToString (com.helger.commons.datetime.PDTToString)4 ISMLConfigurationManager (com.helger.peppol.app.mgr.ISMLConfigurationManager)4 ISMLConfiguration (com.helger.peppol.domain.ISMLConfiguration)4 ClientTransportException (com.sun.xml.ws.client.ClientTransportException)4 UnknownHostException (java.net.UnknownHostException)4 CertificateExpiredException (java.security.cert.CertificateExpiredException)4 CertificateNotYetValidException (java.security.cert.CertificateNotYetValidException)4 SSLSocketFactory (javax.net.ssl.SSLSocketFactory)4 ManageServiceMetadataServiceCaller (com.helger.peppol.smlclient.ManageServiceMetadataServiceCaller)3 NonBlockingByteArrayOutputStream (com.helger.commons.io.stream.NonBlockingByteArrayOutputStream)2 SimpleURL (com.helger.commons.url.SimpleURL)2 HCUL (com.helger.html.hc.html.grouping.HCUL)2 MockHttpServletRequest (com.helger.servlet.mock.MockHttpServletRequest)2