Package org.apache.http.entity.mime

Examples of org.apache.http.entity.mime.HttpMultipart


            writer.append("some random whatever");
        } finally {
            writer.close();
        }

        HttpMultipart multipart = new HttpMultipart("form-data", Charset.forName("UTF-8"), "foo");
        FormBodyPart p1 = new FormBodyPart(
                "field1",
                new InputStreamBody(new FileInputStream(tmpfile), s1 + ".tmp"));
        FormBodyPart p2 = new FormBodyPart(
                "field2",
                new InputStreamBody(new FileInputStream(tmpfile), s2 + ".tmp"));

        multipart.addBodyPart(p1);
        multipart.addBodyPart(p2);

        multipart.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);

        ByteArrayOutputStream out = new ByteArrayOutputStream();
        multipart.writeTo(out);
        out.close();

        String expected =
            "--foo\r\n" +
            "Content-Disposition: form-data; name=\"field1\"; " +
                "filename=\"" + s1 + ".tmp\"\r\n" +
            "Content-Type: application/octet-stream\r\n" +
            "\r\n" +
            "some random whatever\r\n" +
            "--foo\r\n" +
            "Content-Disposition: form-data; name=\"field2\"; " +
                "filename=\"" + s2 + ".tmp\"\r\n" +
            "Content-Type: application/octet-stream\r\n" +
            "\r\n" +
            "some random whatever\r\n" +
            "--foo--\r\n";
        String s = out.toString("UTF-8");
        Assert.assertEquals(expected, s);
        Assert.assertEquals(-1, multipart.getTotalLength());

        tmpfile.delete();
    }
View Full Code Here


    @Test
    public void testMultipartFormStringPartsMultiCharsets() throws Exception {
        String s1 = constructString(SWISS_GERMAN_HELLO);
        String s2 = constructString(RUSSIAN_HELLO);

        HttpMultipart multipart = new HttpMultipart("form-data", "foo");
        FormBodyPart p1 = new FormBodyPart(
                "field1",
                new StringBody(s1, Charset.forName("ISO-8859-1")));
        FormBodyPart p2 = new FormBodyPart(
                "field2",
                new StringBody(s2, Charset.forName("KOI8-R")));

        multipart.addBodyPart(p1);
        multipart.addBodyPart(p2);

        ByteArrayOutputStream out1 = new ByteArrayOutputStream();
        multipart.writeTo(out1);
        out1.close();

        ByteArrayOutputStream out2 = new ByteArrayOutputStream();

        out2.write((
            "--foo\r\n" +
            "Content-Disposition: form-data; name=\"field1\"\r\n" +
            "Content-Type: text/plain; charset=ISO-8859-1\r\n" +
            "Content-Transfer-Encoding: 8bit\r\n" +
            "\r\n").getBytes("US-ASCII"));
        out2.write(s1.getBytes("ISO-8859-1"));
        out2.write(("\r\n" +
            "--foo\r\n" +
            "Content-Disposition: form-data; name=\"field2\"\r\n" +
            "Content-Type: text/plain; charset=KOI8-R\r\n" +
            "Content-Transfer-Encoding: 8bit\r\n" +
            "\r\n").getBytes("US-ASCII"));
        out2.write(s2.getBytes("KOI8-R"));
        out2.write(("\r\n" +
            "--foo--\r\n").getBytes("US-ASCII"));
        out2.close();

        byte[] actual = out1.toByteArray();
        byte[] expected = out2.toByteArray();

        Assert.assertEquals(expected.length, actual.length);
        for (int i = 0; i < actual.length; i++) {
            Assert.assertEquals(expected[i], actual[i]);
        }
        Assert.assertEquals(expected.length, multipart.getTotalLength());
    }
View Full Code Here

public class TestMultipartForm {

    @Test
    public void testMultipartFormStringParts() throws Exception {
        HttpMultipart multipart = new HttpMultipart("form-data", "foo");
        FormBodyPart p1 = new FormBodyPart(
                "field1",
                new StringBody("this stuff"));
        FormBodyPart p2 = new FormBodyPart(
                "field2",
                new StringBody("that stuff", Charset.forName("US-ASCII")));
        FormBodyPart p3 = new FormBodyPart(
                "field3",
                new StringBody("all kind of stuff"));

        multipart.addBodyPart(p1);
        multipart.addBodyPart(p2);
        multipart.addBodyPart(p3);

        ByteArrayOutputStream out = new ByteArrayOutputStream();
        multipart.writeTo(out);
        out.close();

        String expected =
            "--foo\r\n" +
            "Content-Disposition: form-data; name=\"field1\"\r\n" +
            "Content-Type: text/plain; charset=" +
                Charset.defaultCharset() + "\r\n" +
            "Content-Transfer-Encoding: 8bit\r\n" +
            "\r\n" +
            "this stuff\r\n" +
            "--foo\r\n" +
            "Content-Disposition: form-data; name=\"field2\"\r\n" +
            "Content-Type: text/plain; charset=US-ASCII\r\n" +
            "Content-Transfer-Encoding: 8bit\r\n" +
            "\r\n" +
            "that stuff\r\n" +
            "--foo\r\n" +
            "Content-Disposition: form-data; name=\"field3\"\r\n" +
            "Content-Type: text/plain; charset=" +
                Charset.defaultCharset() + "\r\n" +
            "Content-Transfer-Encoding: 8bit\r\n" +
            "\r\n" +
            "all kind of stuff\r\n" +
            "--foo--\r\n";
        String s = out.toString("US-ASCII");
        Assert.assertEquals(expected, s);
        Assert.assertEquals(s.length(), multipart.getTotalLength());
    }
View Full Code Here

            writer.append("some random whatever");
        } finally {
            writer.close();
        }

        HttpMultipart multipart = new HttpMultipart("form-data", "foo");
        FormBodyPart p1 = new FormBodyPart(
                "field1",
                new FileBody(tmpfile));
        FormBodyPart p2 = new FormBodyPart(
                "field2",
                new InputStreamBody(new FileInputStream(tmpfile), "file.tmp"));

        multipart.addBodyPart(p1);
        multipart.addBodyPart(p2);

        ByteArrayOutputStream out = new ByteArrayOutputStream();
        multipart.writeTo(out);
        out.close();

        String expected =
            "--foo\r\n" +
            "Content-Disposition: form-data; name=\"field1\"; " +
                "filename=\"" + tmpfile.getName() + "\"\r\n" +
            "Content-Type: application/octet-stream\r\n" +
            "Content-Transfer-Encoding: binary\r\n" +
            "\r\n" +
            "some random whatever\r\n" +
            "--foo\r\n" +
            "Content-Disposition: form-data; name=\"field2\"; " +
                "filename=\"file.tmp\"\r\n" +
            "Content-Type: application/octet-stream\r\n" +
            "Content-Transfer-Encoding: binary\r\n" +
            "\r\n" +
            "some random whatever\r\n" +
            "--foo--\r\n";
        String s = out.toString("US-ASCII");
        Assert.assertEquals(expected, s);
        Assert.assertEquals(-1, multipart.getTotalLength());

        tmpfile.delete();
    }
View Full Code Here

            }
            //(1) setting the correct header
            String contentType = String.format("%s/%s; charset=%s; boundary=%s",
                mediaType.getType(),mediaType.getSubtype(),charset.toString(),CONTENT_ITEM_BOUNDARY);
            httpHeaders.putSingle(HttpHeaders.CONTENT_TYPE,contentType);
            HttpMultipart entity = new HttpMultipart("from-data", charset ,CONTENT_ITEM_BOUNDARY);
            //(2) serialising the metadata
            if(!isOmitMetadata(properties)){
                entity.addBodyPart(new FormBodyPart("metadata", new ClerezzaContentBody(
                    ci.getUri().getUnicodeString(), ci.getMetadata(),
                    rdfFormat)));
            }
            //(3) serialising the Content (Bloby)
            //(3.a) Filter based on parameter
            List<Entry<UriRef,Blob>> includedBlobs = filterBlobs(ci, properties);
            //(3.b) Serialise the filtered
            if(!includedBlobs.isEmpty()) {
                HttpMultipart content = new HttpMultipart("alternate", UTF8 ,"contentParts");
                for(Entry<UriRef,Blob> entry : includedBlobs){
                    content.addBodyPart(new FormBodyPart(entry.getKey().getUnicodeString(),
                        new BlobContentBody(entry.getValue()))); //no file name
                }
                //add all the blobs
                entity.addBodyPart(new FormBodyPart("content",new MultipartContentBody(content, null)));
            } //else no content to include
View Full Code Here

                "\nIt is a secret, that the city of Berlin is the capital of Germany since 1990.";
       
        //The multipart entity for the contentItem
        MultipartEntity contentItem = new MultipartEntity(null, null ,UTF8);
        //The multipart/alternate mime part for the parsed content versions
        HttpMultipart content = new HttpMultipart("alternate", UTF8 ,"contentParts");
        //add the content part to the contentItem
        contentItem.addPart(
            "content", //the name MUST BE "content"
            new MultipartContentBody(content));
        //now add the content (ordering is important, because the first
        //part will be assumed the original document and all following are
        //assumed alternate - transformed - versions
        content.addBodyPart(new FormBodyPart(
            "http://www.example.com/test.html", //the id of the content
            new StringBody(HTML_CONTENT, "text/html", UTF8)));
        content.addBodyPart(new FormBodyPart(
            "http://www.example.com/test.txt",
            new StringBody(extraTextConent, "text/plain", UTF8)));
       
        String receivedContent = executor.execute(
            builder.buildPostRequest(getEndpoint())
View Full Code Here

        Header header = new Header();
        header.addField(
                Field.parse("Content-Type: multipart/form-data; boundary=foo"));
        message.setHeader(header);
       
        HttpMultipart multipart = new HttpMultipart();
        multipart.setParent(message);
        BodyPart p1 = new BodyPart();
        Header h1 = new Header();
        h1.addField(Field.parse("Content-Type: text/plain"));
        p1.setHeader(h1);
        p1.setBody(new StringBody("this stuff"));
        BodyPart p2 = new BodyPart();
        Header h2 = new Header();
        h2.addField(Field.parse("Content-Type: text/plain"));
        p2.setHeader(h2);
        p2.setBody(new StringBody("that stuff"));
        BodyPart p3 = new BodyPart();
        Header h3 = new Header();
        h3.addField(Field.parse("Content-Type: text/plain"));
        p3.setHeader(h3);
        p3.setBody(new StringBody("all kind of stuff"));

        multipart.addBodyPart(p1);
        multipart.addBodyPart(p2);
        multipart.addBodyPart(p3);
       
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        multipart.writeTo(out);
        out.close();
       
        String expected = "\r\n" +
            "--foo\r\n" +
            "Content-Type: text/plain\r\n" +
            "\r\n" +
            "this stuff\r\n" +
            "--foo\r\n" +
            "Content-Type: text/plain\r\n" +
            "\r\n" +
            "that stuff\r\n" +
            "--foo\r\n" +
            "Content-Type: text/plain\r\n" +
            "\r\n" +
            "all kind of stuff\r\n" +
            "--foo--\r\n" +
            "\r\n";
        String s = out.toString("US-ASCII");
        assertEquals(expected, s);
        assertEquals(s.length(), multipart.getTotalLength());
    }
View Full Code Here

        Header header = new Header();
        header.addField(
                Field.parse("Content-Type: multipart/form-data; boundary=foo"));
        message.setHeader(header);
       
        HttpMultipart multipart = new HttpMultipart();
        multipart.setParent(message);
        FormBodyPart p1 = new FormBodyPart(
                "field1",
                new StringBody("this stuff"));
        FormBodyPart p2 = new FormBodyPart(
                "field2",
                new StringBody("that stuff", Charset.forName("US-ASCII")));
        FormBodyPart p3 = new FormBodyPart(
                "field3",
                new StringBody("all kind of stuff"));
       
        multipart.addBodyPart(p1);
        multipart.addBodyPart(p2);
        multipart.addBodyPart(p3);
       
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        multipart.writeTo(out);
        out.close();
       
        String expected = "\r\n" +
            "--foo\r\n" +
            "Content-Disposition: form-data; name=\"field1\"\r\n" +
            "Content-Type: text/plain; charset=" +
                Charset.defaultCharset() + "\r\n" +
            "Content-Transfer-Encoding: 8bit\r\n" +
            "\r\n" +
            "this stuff\r\n" +
            "--foo\r\n" +
            "Content-Disposition: form-data; name=\"field2\"\r\n" +
            "Content-Type: text/plain; charset=US-ASCII\r\n" +
            "Content-Transfer-Encoding: 8bit\r\n" +
            "\r\n" +
            "that stuff\r\n" +
            "--foo\r\n" +
            "Content-Disposition: form-data; name=\"field3\"\r\n" +
            "Content-Type: text/plain; charset=" +
                Charset.defaultCharset() + "\r\n" +
            "Content-Transfer-Encoding: 8bit\r\n" +
            "\r\n" +
            "all kind of stuff\r\n" +
            "--foo--\r\n" +
            "\r\n";
        String s = out.toString("US-ASCII");
        assertEquals(expected, s);
        assertEquals(s.length(), multipart.getTotalLength());
    }
View Full Code Here

            writer.append("some random whatever");
        } finally {
            writer.close();
        }
       
        HttpMultipart multipart = new HttpMultipart();
        multipart.setParent(message);
        FormBodyPart p1 = new FormBodyPart(
                "field1",
                new FileBody(tmpfile));
        FormBodyPart p2 = new FormBodyPart(
                "field2",
                new InputStreamBody(new FileInputStream(tmpfile), "file.tmp"));
       
        multipart.addBodyPart(p1);
        multipart.addBodyPart(p2);
       
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        multipart.writeTo(out);
        out.close();
       
        String expected = "\r\n" +
            "--foo\r\n" +
            "Content-Disposition: form-data; name=\"field1\"; " +
                "filename=\"" + tmpfile.getName() + "\"\r\n" +
            "Content-Type: application/octet-stream\r\n" +
            "Content-Transfer-Encoding: binary\r\n" +
            "\r\n" +
            "some random whatever\r\n" +
            "--foo\r\n" +
            "Content-Disposition: form-data; name=\"field2\"; " +
                "filename=\"file.tmp\"\r\n" +
            "Content-Type: application/octet-stream\r\n" +
            "Content-Transfer-Encoding: binary\r\n" +
            "\r\n" +
            "some random whatever\r\n" +
            "--foo--\r\n" +
            "\r\n";
        String s = out.toString("US-ASCII");
        assertEquals(expected, s);
        assertEquals(-1, multipart.getTotalLength());
       
        tmpfile.delete();
    }
View Full Code Here

            writer.append("some random whatever");
        } finally {
            writer.close();
        }
       
        HttpMultipart multipart = new HttpMultipart();
        multipart.setParent(message);
        FormBodyPart p1 = new FormBodyPart(
                "field1",
                new FileBody(tmpfile));
        FormBodyPart p2 = new FormBodyPart(
                "field2",
                new InputStreamBody(new FileInputStream(tmpfile), "file.tmp"));
       
        multipart.addBodyPart(p1);
        multipart.addBodyPart(p2);
       
        multipart.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
       
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        multipart.writeTo(out);
        out.close();
       
        String expected = "\r\n" +
            "--foo\r\n" +
            "Content-Disposition: form-data; name=\"field1\"; " +
                "filename=\"" + tmpfile.getName() + "\"\r\n" +
            "\r\n" +
            "some random whatever\r\n" +
            "--foo\r\n" +
            "Content-Disposition: form-data; name=\"field2\"; " +
                "filename=\"file.tmp\"\r\n" +
            "\r\n" +
            "some random whatever\r\n" +
            "--foo--\r\n" +
            "\r\n";
        String s = out.toString("US-ASCII");
        assertEquals(expected, s);
        assertEquals(-1, multipart.getTotalLength());
       
        tmpfile.delete();
    }
View Full Code Here

TOP

Related Classes of org.apache.http.entity.mime.HttpMultipart

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.