Package org.apache.http.entity.mime

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


    String apiUrl = cloudinary.cloudinaryApiUrl(action, options);

    HttpClient client = new DefaultHttpClient();

    HttpPost postMethod = new HttpPost(apiUrl);
    MultipartEntity multipart = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
    // Remove blank parameters
    for (Map.Entry<String, Object> param : params.entrySet()) {
      if (param.getValue() instanceof String) {
        String value = (String) param.getValue();
        if (StringUtils.isNotBlank(value)) {
          multipart.addPart(param.getKey(), new StringBody(value));
        }
      } else if (param.getValue() instanceof Collection) {
        for (Object value : (Collection) param.getValue()) {
          multipart.addPart(param.getKey()+"[]", new StringBody(Cloudinary.asString(value)));         
        }
      }
    }

    if (file instanceof String && !((String) file).matches("^https?:.*")) {
      file = new File((String) file);
    }
    if (file instanceof File) {
      multipart.addPart("file", new FileBody((File) file));
    } else if (file instanceof String) {
      multipart.addPart("file", new StringBody((String) file));
    }
    postMethod.setEntity(multipart);

    HttpResponse response = client.execute(postMethod);
    int code = response.getStatusLine().getStatusCode();
View Full Code Here


        return uploadContentBody(url, field, contentBody);
    }

    public JsonObject uploadContentBody(String url, String field, ContentBody contentBody) throws IOException {
        HttpPost httpPost = new HttpPost(url);
        MultipartEntity multipartEntity = new MultipartEntity();
        multipartEntity.addPart(field, contentBody);
        httpPost.setEntity(multipartEntity);
        JsonObject uploadResponse = executeRequest(httpPost, false).getAsJsonObject();
        JsonObject jsonObject = saveWallPhoto(
                uploadResponse.getAsJsonPrimitive("server").getAsString(),
                uploadResponse.getAsJsonPrimitive("photo").getAsString(),
View Full Code Here

    public static Test suite() {
        return new TestSuite(TestMultipartFormHttpEntity.class);
    }

    public void testExplictContractorParams() throws Exception {
        MultipartEntity entity = new MultipartEntity(
                HttpMultipartMode.BROWSER_COMPATIBLE,
                "whatever",
                CharsetUtil.getCharset("UTF-8"));

        assertNull(entity.getContentEncoding());
        assertNotNull(entity.getContentType());
        Header header = entity.getContentType();
        HeaderElement[] elems = header.getElements();
        assertNotNull(elems);
        assertEquals(1, elems.length);

        HeaderElement elem = elems[0];
View Full Code Here

        assertNotNull(p2);
        assertEquals("UTF-8", p2.getValue());
    }

    public void testImplictContractorParams() throws Exception {
        MultipartEntity entity = new MultipartEntity();
        assertNull(entity.getContentEncoding());
        assertNotNull(entity.getContentType());
        Header header = entity.getContentType();
        HeaderElement[] elems = header.getElements();
        assertNotNull(elems);
        assertEquals(1, elems.length);

        HeaderElement elem = elems[0];
View Full Code Here

        NameValuePair p2 = elem.getParameterByName("charset");
        assertNull(p2);
    }

    public void testRepeatable() throws Exception {
        MultipartEntity entity = new MultipartEntity();
        entity.addPart("p1", new StringBody("blah blah"));
        entity.addPart("p2", new StringBody("yada yada"));
        assertTrue(entity.isRepeatable());
        assertFalse(entity.isChunked());
        assertFalse(entity.isStreaming());

        long len = entity.getContentLength();
        assertTrue(len == entity.getContentLength());

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

        byte[] bytes = out.toByteArray();
        assertNotNull(bytes);
        assertTrue(bytes.length == len);

        assertTrue(len == entity.getContentLength());

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

        bytes = out.toByteArray();
        assertNotNull(bytes);
        assertTrue(bytes.length == len);
View Full Code Here

        assertNotNull(bytes);
        assertTrue(bytes.length == len);
    }

    public void testNonRepeatable() throws Exception {
        MultipartEntity entity = new MultipartEntity();
        entity.addPart("p1", new InputStreamBody(
                new ByteArrayInputStream("blah blah".getBytes()), null));
        entity.addPart("p2", new InputStreamBody(
                new ByteArrayInputStream("yada yada".getBytes()), null));
        assertFalse(entity.isRepeatable());
        assertTrue(entity.isChunked());
        assertTrue(entity.isStreaming());

        assertTrue(entity.getContentLength() == -1);
    }
View Full Code Here

      // Prepare HTTP request
      HttpClient client = new DefaultHttpClient();
      client.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
     
      HttpPost post = new HttpPost("https://rink.hockeyapp.net/api/2/apps");
      MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
     
      // Set headers and parameters
      post.addHeader("X-HockeyAppToken", properties.get("hockeyAppToken"));
      entity.addPart("ipa", new FileBody(
          new File(appPath + "/" + properties.get("appName") + ".ipa"),
              "application/zip"));
      entity.addPart("dsym", new FileBody(
          new File(appPath + "/" + properties.get("appName") + ".dSYM.zip"),
          "application/zip"));     
      entity.addPart("notes",
          new StringBody(properties.get("releaseNotes"),
              "text/plain", Charset.forName("UTF-8")));     
      post.setEntity(entity);
                 
      // Run the request
View Full Code Here

        //It is a secret, that Berlin is the capital of Germany
        String extraTextConent = TEXT_CONTENT +
                "\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
View Full Code Here

    @Test
    public void testContentBeforeMetadata() throws IOException{
        final UriRef contentItemId = new UriRef("http://www.example.com/test.html");
        String rdfContentType = SupportedFormat.RDF_XML;
        String rdfContent = getDummyRdfMetadata(contentItemId, rdfContentType);
        MultipartEntity contentItem = new MultipartEntity(null, null ,UTF8);
        //first the content -> illegal
        contentItem.addPart(
            "content", //the name MUST BE "content"
            new StringBody(HTML_CONTENT,"text/html",UTF8));
        //after that the metadata
        contentItem.addPart(
            "metadata", //the name MUST BE "metadata"
            new StringBody(rdfContent,rdfContentType,UTF8));

        String receivedContent = executor.execute(
            builder.buildPostRequest(getEndpoint())
View Full Code Here

    @Test
    public void testMissingContent() throws IOException{
        final UriRef contentItemId = new UriRef("http://www.example.com/test.html");
        String rdfContentType = SupportedFormat.RDF_XML;
        String rdfContent = getDummyRdfMetadata(contentItemId, rdfContentType);
        MultipartEntity contentItem = new MultipartEntity(null, null ,UTF8);
        //after that the metadata
        contentItem.addPart(
            "metadata", //the name MUST BE "metadata"
            new StringBody(rdfContent,rdfContentType,UTF8));

        String receivedContent = executor.execute(
            builder.buildPostRequest(getEndpoint())
View Full Code Here

TOP

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

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.