Examples of MultipartEntityBuilder


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

    }
   
    @Test
    public void testMultipartPostWithParametersAndPayload() throws Exception {
        HttpPost post = new HttpPost("http://localhost:" + PORT_PATH + "/rest/customerservice/customers/multipart/123?query=abcd");
        MultipartEntityBuilder builder = MultipartEntityBuilder.create().setMode(HttpMultipartMode.STRICT);
        builder.addBinaryBody("part1", new File(this.getClass().getClassLoader().getResource("java.jpg").toURI()), ContentType.create("image/jpeg"), "java.jpg");
        builder.addBinaryBody("part2", new File(this.getClass().getClassLoader().getResource("java.jpg").toURI()), ContentType.create("image/jpeg"), "java.jpg");
        StringWriter sw = new StringWriter();
        jaxb.createMarshaller().marshal(new Customer(123, "Raul"), sw);
        builder.addTextBody("body", sw.toString(), ContentType.create("text/xml", Consts.UTF_8));
        post.setEntity(builder.build());
        HttpResponse response = httpclient.execute(post);
        assertEquals(200, response.getStatusLine().getStatusCode());
    }
View Full Code Here

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

    }
   
    @Test
    public void testMultipartPostWithoutParameters() throws Exception {
        HttpPost post = new HttpPost("http://localhost:" + PORT_PATH + "/rest/customerservice/customers/multipart");
        MultipartEntityBuilder builder = MultipartEntityBuilder.create().setMode(HttpMultipartMode.STRICT);
        builder.addBinaryBody("part1", new File(this.getClass().getClassLoader().getResource("java.jpg").toURI()), ContentType.create("image/jpeg"), "java.jpg");
        builder.addBinaryBody("part2", new File(this.getClass().getClassLoader().getResource("java.jpg").toURI()), ContentType.create("image/jpeg"), "java.jpg");
        StringWriter sw = new StringWriter();
        jaxb.createMarshaller().marshal(new Customer(123, "Raul"), sw);
        builder.addTextBody("body", sw.toString(), ContentType.create("text/xml", Consts.UTF_8));
        post.setEntity(builder.build());
        HttpResponse response = httpclient.execute(post);
        assertEquals(200, response.getStatusLine().getStatusCode());
    }
View Full Code Here

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

  @Autowired
  WxConfig config;

  public WxBaseItemMediaEntity remoteMediaUpload(String accessToken,
      WxMediaTypeEnum type, byte[] content) throws WxException {
    MultipartEntityBuilder entityBuilder = MultipartEntityBuilder.create();
    String typeString = null;
    switch (type) {
    case IMAGE:
    case THUMB:
    case VIDEO:
    case VOICE:
      typeString = type.toString().toLowerCase();
      break;
    case MUSIC:
    case DEFAULT:
    case PIC_DESC:
      throw new WxException("Not supported upload type : "
          + type.toString());
    default:
      break;
    }

    Map<String, String> params = WxUtil.getAccessTokenParams(accessToken);
    System.out.println(typeString);
    params.put("type", typeString);
    ContentBody contentBody = new ByteArrayBody(content, ContentType.MULTIPART_FORM_DATA, "name.jpg");
    entityBuilder.addPart("media", contentBody);
    MediaResultMapper result = WxUtil.sendRequest(
        config.getMediaUploadUrl(), HttpMethod.POST, params,
        entityBuilder.build(), MediaResultMapper.class);

    WxBaseItemMediaEntity resultEntity = null;
    switch (type) {
    case IMAGE:
      WxItemImageEntity imageEntity = new WxItemImageEntity();
View Full Code Here

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

  private void testRecord(String handler, int statusCode) throws IOException {
    // To follow redirect: .setRedirectStrategy(new LaxRedirectStrategy())
    HttpClient client = HttpClientBuilder.create().build();
    HttpPost post = new HttpPost("http://localhost:" + getServerPort()
        + "/kmf-content-api-test/" + handler);
    MultipartEntityBuilder multipartEntity = MultipartEntityBuilder
        .create();
    multipartEntity.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);

    File file = new File("small");
    URL small = new URL(VideoURLs.map.get("small-webm"));
    FileUtils.copyURLToFile(small, file);
    FileBody fb = new FileBody(file);
    multipartEntity.addPart("file", fb);

    HttpEntity httpEntity = multipartEntity.build();
    post.setEntity(httpEntity);

    EntityUtils.consume(httpEntity);
    HttpResponse response = client.execute(post);
    final int responseStatusCode = response.getStatusLine().getStatusCode();
View Full Code Here

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

      HttpPost post = new HttpPost("http://localhost:9998/controller/router");

      InputStream is = getClass().getResourceAsStream("/UploadTestFile.txt");

      MultipartEntityBuilder builder = MultipartEntityBuilder.create();
      ContentBody cbFile = new InputStreamBody(is,
          ContentType.create("text/plain"), "UploadTestFile.txt");
      builder.addPart("fileUpload", cbFile);
      builder.addPart("extTID", new StringBody("2", ContentType.DEFAULT_TEXT));
      builder.addPart("extAction", new StringBody("fileUploadService",
          ContentType.DEFAULT_TEXT));
      builder.addPart("extMethod", new StringBody("uploadTest",
          ContentType.DEFAULT_TEXT));
      builder.addPart("extType", new StringBody("rpc", ContentType.DEFAULT_TEXT));
      builder.addPart("extUpload", new StringBody("true", ContentType.DEFAULT_TEXT));

      builder.addPart(
          "name",
          new StringBody("Jimöäü", ContentType.create("text/plain",
              Charset.forName("UTF-8"))));
      builder.addPart("firstName",
          new StringBody("Ralph", ContentType.DEFAULT_TEXT));
      builder.addPart("age", new StringBody("25", ContentType.DEFAULT_TEXT));
      builder.addPart("email", new StringBody("test@test.ch",
          ContentType.DEFAULT_TEXT));

      post.setEntity(builder.build());
      response = client.execute(post);
      HttpEntity resEntity = response.getEntity();

      assertThat(resEntity).isNotNull();
      String responseString = EntityUtils.toString(resEntity);
View Full Code Here

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

                            reqBuilder.setEntity(e);
                        }
                        else if(bean instanceof ReqEntityMultipart) {
                            ReqEntityMultipart multipart = (ReqEntityMultipart)bean;
                           
                            MultipartEntityBuilder meb = MultipartEntityBuilder.create();
                           
                            // Format:
                            MultipartMode mpMode = multipart.getMode();
                            switch(mpMode) {
                                case BROWSER_COMPATIBLE:
                                    meb.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
                                    break;
                                case RFC_6532:
                                    meb.setMode(HttpMultipartMode.RFC6532);
                                    break;
                                case STRICT:
                                    meb.setMode(HttpMultipartMode.STRICT);
                                    break;
                            }
                           
                            // Parts:
                            for(ReqEntityPart part: multipart.getBody()) {
                                if(part instanceof ReqEntityStringPart) {
                                    ReqEntityStringPart p = (ReqEntityStringPart)part;
                                    String body = p.getPart();
                                    ContentType ct = p.getContentType();
                                    final StringBody sb;
                                    if(ct != null) {
                                        sb = new StringBody(body, HTTPClientUtil.getContentType(ct));
                                    }
                                    else {
                                        sb = new StringBody(body, org.apache.http.entity.ContentType.DEFAULT_TEXT);
                                    }
                                    meb.addPart(part.getName(), sb);
                                }
                                else if(part instanceof ReqEntityFilePart) {
                                    ReqEntityFilePart p = (ReqEntityFilePart)part;
                                    File body = p.getPart();
                                    ContentType ct = p.getContentType();
                                    final FileBody fb;
                                    if(ct != null) {
                                        fb = new FileBody(body, HTTPClientUtil.getContentType(ct), p.getFilename());
                                    }
                                    else {
                                        fb = new FileBody(body, org.apache.http.entity.ContentType.DEFAULT_BINARY, p.getFilename());
                                    }
                                    meb.addPart(p.getName(), fb);
                                }
                            }
                           
                            reqBuilder.setEntity(meb.build());
                        }
                       
                       
                    }
                    catch (UnsupportedEncodingException ex) {
View Full Code Here

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

                new StringResponseHandler());
    }

    private HttpEntity createHttpEntity(String name, URL archiveUrl) throws OpenESBClientException {
        try {
            MultipartEntityBuilder multipartEntity = MultipartEntityBuilder.create();
            multipartEntity.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
            multipartEntity.addPart(name, new FileBody(new File(archiveUrl.toURI())));

            return multipartEntity.build();
        } catch (URISyntaxException ex) {
            logger.log(Level.SEVERE, "Unable to create an HTTP entity to upload file : " + archiveUrl, ex);
            throw new OpenESBClientException("Unable to create an HTTP entity to upload file : " + archiveUrl, ex);
        }
    }
View Full Code Here

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

    public String uploadFile(String uri, String partName, String filename, String mimeType, byte[] contents, int expectedResponseCode) throws URISyntaxException, IOException {
        try (CloseableHttpClient client = HttpClients.createDefault()) {
            HttpPost post = new HttpPost(uri);
            ByteArrayBody contentBody = new ByteArrayBody(contents, ContentType.create(mimeType), filename);
            MultipartEntityBuilder builder = MultipartEntityBuilder.create();
            builder.addPart(partName, contentBody);
            post.setEntity(builder.build());
            HttpResponse response = client.execute(post);
            String result = EntityUtils.toString(response.getEntity());
            int statusCode = response.getStatusLine().getStatusCode();
            Assert.assertEquals(String.format("Invalid response code, %s", statusCode), expectedResponseCode, statusCode);
            return result;
View Full Code Here

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

    try {
      HttpPost post = new HttpPost("http://localhost:9998/controller/router");
      is = getClass().getResourceAsStream("/UploadTestFile.txt");

      MultipartEntityBuilder builder = MultipartEntityBuilder.create();
      ContentBody cbFile = new InputStreamBody(is,
          ContentType.create("text/plain"), "UploadTestFile.txt");
      builder.addPart("fileUpload", cbFile);
      builder.addPart("extTID", new StringBody("2", ContentType.DEFAULT_TEXT));
      builder.addPart("extAction", new StringBody("fileUploadController",
          ContentType.DEFAULT_TEXT));
      builder.addPart("extMethod", new StringBody("uploadTest",
          ContentType.DEFAULT_TEXT));
      builder.addPart("extType", new StringBody("rpc", ContentType.DEFAULT_TEXT));
      builder.addPart("extUpload", new StringBody("true", ContentType.DEFAULT_TEXT));

      builder.addPart(
          "name",
          new StringBody("Jimöäü", ContentType.create("text/plain",
              Charset.forName("UTF-8"))));
      builder.addPart("firstName",
          new StringBody("Ralph", ContentType.DEFAULT_TEXT));
      builder.addPart("age", new StringBody("25", ContentType.DEFAULT_TEXT));
      builder.addPart("email", new StringBody("test@test.ch",
          ContentType.DEFAULT_TEXT));

      post.setEntity(builder.build());
      response = client.execute(post);
      HttpEntity resEntity = response.getEntity();

      assertThat(resEntity).isNotNull();
      String responseString = EntityUtils.toString(resEntity);
View Full Code Here

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

   */
  public Result<PicUploadResult> uploadPic(String oAuthConsumerKey, String accessToken,
      String openid, String title, byte[] picture, String photoDesc, String albumId,
      Boolean mobile, Double lon, Double lat, Boolean needFeed, Integer successNum, Integer picNum) {
    HttpPost request = new HttpPost("https://graph.qq.com/photo/upload_pic");
    MultipartEntityBuilder builder =
        MultipartEntityBuilder.create().addBinaryBody("picture", picture,
            ContentType.create("image/" + title.substring(title.lastIndexOf(".") + 1)), title);
    builder.addTextBody("oauth_consumer_key", oAuthConsumerKey);
    builder.addTextBody("access_token", accessToken);
    builder.addTextBody("openid", openid);
    builder.addTextBody("format", "json");
    builder.addTextBody("title", title);
    if (photoDesc != null) {
      builder.addTextBody("photodesc", photoDesc);
    }
    if (albumId != null) {
      builder.addTextBody("albumid", albumId);
    }
    if (Boolean.TRUE.equals(mobile)) {
      builder.addTextBody("mobile", "1");
    }
    if (lon != null) {
      builder.addTextBody("x", lon.toString());
    }
    if (lat != null) {
      builder.addTextBody("y", lat.toString());
    }
    if (Boolean.FALSE.equals(needFeed)) {
      builder.addTextBody("needfeed", "0");
    }
    if (successNum != null) {
      builder.addTextBody("successnum", successNum.toString());
    }
    if (picNum != null) {
      builder.addTextBody("picnum", picNum.toString());
    }
    request.setEntity(builder.build());
    try {
      HttpResponse response = Http.CLIENT.execute(request);
      String json = IOUtils.toString(response.getEntity().getContent());
      return Result.parse(json, PicUploadResult.class);
    } catch (ClientProtocolException e) {
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. 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.