Java Code Examples for com.ning.http.client.multipart.StringPart

The following examples show how to use com.ning.http.client.multipart.StringPart. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: qconfig   Source File: QConfigAdminClient.java    License: MIT License 6 votes vote down vote up
@Override
protected AsyncHttpClient.BoundRequestBuilder buildRequest(String url) throws IOException {
    AsyncHttpClient.BoundRequestBuilder builder = HttpClientHolder.INSTANCE.preparePost(url);
    builder.addQueryParam(Constants.TOKEN_NAME, token)
            .addQueryParam(Constants.GROUP_NAME, meta.getGroupName())
            .addQueryParam(Constants.DATAID_NAME, meta.getFileName())
            .addQueryParam(Constants.VERSION_NAME, String.valueOf(versionProfile.getVersion()))
            .addQueryParam(Constants.BUILD_GROUP, EnvironmentAware.determinedEnv())
            .addQueryParam(Constants.FILE_PROFILE, versionProfile.getProfile())
            .addQueryParam(Constants.API_VERSION, API_V)
            .addQueryParam(Constants.ISPUBLIC, String.valueOf(isPublic))
            .addQueryParam(Constants.OPERATOR, operator)
            .addQueryParam(Constants.ISDIRECTPUBLISH, String.valueOf(directPublish))
            .addQueryParam(Constants.DESCRIPTION, description)
            .addBodyPart(new StringPart(Constants.CONTENT, data, "text/html", Charsets.UTF_8));
    return builder;
}
 
Example 2
@Test
public void testMultipartBody() {
    String filepath = "src/test/java/com/yahoo/parsec/clients/ParsecAsyncHttpRequestTest.java";
    builder.addBodyPart("abc", "def");
    builder.addBodyPart("def", new byte[]{1,2,3}, null);
    builder.addBodyPart("myFile", new File(filepath), null);
    builder.addBodyPart("withContentType", "{\"abc\":\"def\"}", "application/json", StandardCharsets.UTF_8);
    List<Part> parts = builder.build().getNingRequest().getParts();

    assertEquals(parts.get(0).getName(), "abc");
    assertEquals(((StringPart)parts.get(0)).getValue(), "def");
    assertEquals(parts.get(1).getName(), "def");
    assertEquals(((ByteArrayPart)parts.get(1)).getBytes(), new byte[]{1, 2, 3});
    assertEquals(parts.get(2).getName(), "myFile");
    assertEquals(((FilePart)parts.get(2)).getFile().getName(), "ParsecAsyncHttpRequestTest.java");
    assertEquals(parts.get(3).getName(), "withContentType");
    assertEquals(((StringPart)parts.get(3)).getValue(), "{\"abc\":\"def\"}");
}
 
Example 3
Source Project: parsec-libraries   Source File: ParsecEqualsUtilTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testPartEquals() throws Exception {
    Assert.assertTrue(
        ParsecEqualsUtil.partEquals(
            new ByteArrayPart("abc", new byte[] {1,2,3}), new ByteArrayPart("abc", new byte[] {1,2,3})
        )
    );

    Assert.assertFalse(
        ParsecEqualsUtil.partEquals(
            new ByteArrayPart("abc", new byte[] {1,2,3}), new ByteArrayPart("abc", new byte[] {4,5,6})
        )
    );

    File file1 = new File("src/test/java/com/yahoo/parsec/clients/ParsecEqualsUtilTest.java"),
        file2 = new File("src/test/java/com/yahoo/parsec/clients/ParsecAsyncHttpRequestTest.java");

    Assert.assertTrue(ParsecEqualsUtil.partEquals(new FilePart("abc", file1), new FilePart("abc", file1)));
    Assert.assertFalse(ParsecEqualsUtil.partEquals(new FilePart("abc", file1), new FilePart("def", file1)));
    Assert.assertFalse(ParsecEqualsUtil.partEquals(new FilePart("abc", file1), new FilePart("abc", file2)));
    Assert.assertTrue(ParsecEqualsUtil.partEquals(new StringPart("abc", "def"), new StringPart("abc", "def")));
    Assert.assertFalse(ParsecEqualsUtil.partEquals(new StringPart("abc", "def"), new StringPart("def", "def")));
    Assert.assertFalse(ParsecEqualsUtil.partEquals(new StringPart("abc", "def"), new StringPart("abc", "abc")));
}
 
Example 4
Source Project: parsec-libraries   Source File: ParsecEqualsUtilTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testPartListEquals() throws Exception {
    List<Part> partList1 = Arrays.asList(
        new ByteArrayPart("abc", new byte[] {1,2,3}),
        new FilePart("abc", new File("src/test/java/com/yahoo/parsec/clients/ParsecEqualsUtilTest.java")),
        new StringPart("abc", "def")
    );

    List<Part> partList2 = new ArrayList<>();
    partList2.addAll(partList1);
    Assert.assertTrue(ParsecEqualsUtil.partListEquals(partList1, partList2));

    partList2.add(new StringPart("def", "def"));
    Assert.assertFalse(ParsecEqualsUtil.partListEquals(partList1, partList2));

    partList2.remove(1);
    Assert.assertFalse(ParsecEqualsUtil.partListEquals(partList1, partList2));
}
 
Example 5
Source Project: parsec-libraries   Source File: ParsecEqualsUtil.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Ning Part equals.
 *
 * @param lhs lhs
 * @param rhs rhs
 *
 * @return true when two Ning Part are equal by value
 */
static boolean partEquals(final Part lhs, final Part rhs) {
    if (!lhs.equals(rhs)) {
        if (lhs.getClass() != rhs.getClass()) {
            return false;
        }

        if (!new EqualsBuilder()
            .append(lhs.getCharset(), rhs.getCharset())
            .append(lhs.getContentId(), rhs.getContentId())
            .append(lhs.getContentType(), rhs.getContentType())
            .append(lhs.getDispositionType(), rhs.getDispositionType())
            .append(lhs.getName(), rhs.getName())
            .append(lhs.getTransferEncoding(), rhs.getTransferEncoding())
            .isEquals()) {
            return false;
        }

        switch (lhs.getClass().getSimpleName()) {
            case "ByteArrayPart":
                if (!Arrays.equals(((ByteArrayPart) lhs).getBytes(), ((ByteArrayPart) rhs).getBytes())) {
                    return false;
                }
                break;
            case "FilePart":
                if (!((FilePart) lhs).getFile().equals(((FilePart) rhs).getFile())) {
                    return false;
                }
                break;
            case "StringPart":
                if (!((StringPart) lhs).getValue().equals(((StringPart) rhs).getValue())) {
                    return false;
                }
                break;
            default:
                return false;
        }
    }
    return true;
}
 
Example 6
Source Project: slack-client   Source File: MultipartHttpRequest.java    License: Apache License 2.0 4 votes vote down vote up
public Builder addStringPart(@Nonnull String name, @Nonnull String value, @Nonnull Charset charset,
                             @Nullable String contentType) {
  parts.add(new StringPart(Preconditions.checkNotNull(name), Preconditions.checkNotNull(value),
      contentType, Preconditions.checkNotNull(charset)));
  return this;
}
 
Example 7
Source Project: parsec-libraries   Source File: ParsecEqualsUtilTest.java    License: Apache License 2.0 4 votes vote down vote up
@Test
public void testNingRequestEquals() throws Exception {
    Assert.assertTrue(
        ParsecEqualsUtil.ningRequestEquals(
            new RequestBuilder().setUrl("http://localhost:4080").build(),
            new RequestBuilder().setUrl("http://localhost:4080").build()
        )
    );

    Assert.assertFalse(
        ParsecEqualsUtil.ningRequestEquals(
            new RequestBuilder().setUrl("http://localhost:4080")
                .setBody(Arrays.asList(new byte[] {1,2,3}))
                .build(),
            new RequestBuilder().setUrl("http://localhost:4080")
                .build()
        )
    );

    Assert.assertFalse(
        ParsecEqualsUtil.ningRequestEquals(
            new RequestBuilder().setUrl("http://localhost:4080")
                .addBodyPart(new StringPart("abc", "def"))
                .build(),
            new RequestBuilder().setUrl("http://localhost:4080")
                .build()
        )
    );

    Assert.assertFalse(
        ParsecEqualsUtil.ningRequestEquals(
            new RequestBuilder().setUrl("http://localhost:4080")
                .setProxyServer(new ProxyServer("localhost", 8080))
                .build(),
            new RequestBuilder().setUrl("http://localhost:4080")
                .build()
        )
    );

    Assert.assertFalse(
        ParsecEqualsUtil.ningRequestEquals(
            new RequestBuilder().setUrl("http://localhost:4080")
                .build(),
            new RequestBuilder().setUrl("http://localhost:4080")
                .setBody(Arrays.asList(new byte[] {1,2,3}))
                .build()
        )
    );

    Assert.assertFalse(
        ParsecEqualsUtil.ningRequestEquals(
            new RequestBuilder().setUrl("http://localhost:4080")
                .build(),
            new RequestBuilder().setUrl("http://localhost:4080")
                .addBodyPart(new StringPart("abc", "def"))
                .build()
        )
    );

    Assert.assertFalse(
        ParsecEqualsUtil.ningRequestEquals(
            new RequestBuilder().setUrl("http://localhost:4080")
                .build(),
            new RequestBuilder().setUrl("http://localhost:4080")
                .setProxyServer(new ProxyServer("localhost", 8080))
                .build()
        )
    );
}
 
Example 8
Source Project: parsec-libraries   Source File: ParsecAsyncHttpRequest.java    License: Apache License 2.0 2 votes vote down vote up
/**
 * add body part for multipart/form-data upload.
 * @param name part name
 * @param value part value
 * @return {@link ParsecAsyncHttpRequest.Builder}
 */
public Builder addBodyPart(String name, String value) {
    Part part = new StringPart(name, value);
    bodyParts.add(part);
    return this;
}
 
Example 9
Source Project: parsec-libraries   Source File: ParsecAsyncHttpRequest.java    License: Apache License 2.0 2 votes vote down vote up
/**
 * add body part for multipart/form-data upload.
 * @param name part name
 * @param value part value
 * @param contentType content type
 * @param charset content charset
 * @return {@link ParsecAsyncHttpRequest.Builder}
 */
public Builder addBodyPart(String name, String value, String contentType, Charset charset) {
    Part part = new StringPart(name, value, contentType, charset);
    bodyParts.add(part);
    return this;
}