Java Code Examples for org.glassfish.jersey.media.multipart.MultiPart#bodyPart()

The following examples show how to use org.glassfish.jersey.media.multipart.MultiPart#bodyPart() . 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 File: SchemaRegistryClient.java    From registry with Apache License 2.0 8 votes vote down vote up
@Override
public String uploadFile(InputStream inputStream) {
    MultiPart multiPart = new MultiPart();
    BodyPart filePart = new StreamDataBodyPart("file", inputStream, "file");
    multiPart.bodyPart(filePart);
    return runRetryableBlock((SchemaRegistryTargets targets) -> {
        try {
            return login.doAction(new PrivilegedAction<String>() {
                @Override
                public String run() {
                    return targets.filesTarget.request()
                            .post(Entity.entity(multiPart, MediaType.MULTIPART_FORM_DATA), String.class);
                }
            });
        } catch (LoginException | ProcessingException e) {
            throw new RegistryRetryableException(e);
        }
    });
}
 
Example 2
Source File: ContestSubmissionServiceIntegrationTests.java    From judgels with GNU General Public License v2.0 6 votes vote down vote up
private Response submit(String contestJid, String token) {
    MultiPart multiPart = new MultiPart();
    multiPart.setMediaType(MULTIPART_FORM_DATA_TYPE);
    multiPart.bodyPart(new FormDataBodyPart("contestJid", contestJid));
    multiPart.bodyPart(new FormDataBodyPart("problemJid", PROBLEM_1_JID));
    multiPart.bodyPart(new FormDataBodyPart("gradingLanguage", "Cpp11"));
    multiPart.bodyPart(new FormDataBodyPart(
            FormDataContentDisposition.name("sourceFiles.source").fileName("solution.cpp").build(),
            "int main() {}".getBytes(),
            APPLICATION_OCTET_STREAM_TYPE));

    return webTarget
            .path("/api/v2/contests/submissions/programming")
            .request()
            .header(AUTHORIZATION, "Bearer " + token)
            .post(Entity.entity(multiPart, multiPart.getMediaType()));
}
 
Example 3
Source File: SchemaRegistryClient.java    From nifi with Apache License 2.0 6 votes vote down vote up
@Override
public String uploadFile(InputStream inputStream) {
    MultiPart multiPart = new MultiPart();
    BodyPart filePart = new StreamDataBodyPart("file", inputStream, "file");
    multiPart.bodyPart(filePart);
    try {
        return login.doAction(new PrivilegedAction<String>() {
            @Override
            public String run() {
                return currentSchemaRegistryTargets().filesTarget.request()
                        .post(Entity.entity(multiPart, MediaType.MULTIPART_FORM_DATA), String.class);
            }
        });
    } catch (LoginException e) {
        throw new RegistryRetryableException(e);
    }
}
 
Example 4
Source File: IntegrationTest.java    From timbuctoo with GNU General Public License v3.0 6 votes vote down vote up
private Response multipartPost(String path, File resource, String mediaType, Map<String, String> arguments)
  throws ParseException {
  MultiPart formdata = new MultiPart();
  arguments.forEach((key, value) -> formdata.bodyPart(new FormDataBodyPart(key, value)));

  formdata.bodyPart(new FormDataBodyPart(
    new FormDataContentDisposition(
      "form-data; name=\"file\"; filename=\"" + resource.getName().replace("\"", "") + "\""
    ),
    resource,
    MediaType.valueOf(mediaType)
  ));

  Response result = call(path)
    .post(Entity.entity(formdata, "multipart/form-data; boundary=Boundary_1_498293219_1483974344746"));
  return result;
}
 
Example 5
Source File: ContestFileServiceIntegrationTests.java    From judgels with GNU General Public License v2.0 5 votes vote down vote up
private Response uploadFile(Contest contest, String token, String filename, String content) {
    MultiPart multiPart = new MultiPart();
    multiPart.setMediaType(MULTIPART_FORM_DATA_TYPE);
    multiPart.bodyPart(new FormDataBodyPart(
            FormDataContentDisposition.name("file").fileName(filename).build(),
            content.getBytes(),
            APPLICATION_OCTET_STREAM_TYPE));

    return webTarget
            .path("/api/v2/contests/" + contest.getJid() + "/files")
            .request()
            .header(AUTHORIZATION, "Bearer " + token)
            .post(Entity.entity(multiPart, multiPart.getMediaType()));
}
 
Example 6
Source File: JerseyHttpClient.java    From karate with MIT License 5 votes vote down vote up
@Override
public Entity getEntity(List<MultiPartItem> items, String mediaType) {
    MultiPart multiPart = new MultiPart();
    for (MultiPartItem item : items) {
        if (item.getValue() == null || item.getValue().isNull()) {
            continue;
        }
        String name = item.getName();
        String filename = item.getFilename();
        ScriptValue sv = item.getValue();
        String ct = item.getContentType();
        if (ct == null) {
            ct = HttpUtils.getContentType(sv);
        }
        MediaType itemType = MediaType.valueOf(ct);
        if (name == null) { // most likely multipart/mixed
            BodyPart bp = new BodyPart().entity(sv.getAsString()).type(itemType);
            multiPart.bodyPart(bp);
        } else if (filename != null) {
            StreamDataBodyPart part = new StreamDataBodyPart(name, sv.getAsStream(), filename, itemType);
            multiPart.bodyPart(part);
        } else {
            multiPart.bodyPart(new FormDataBodyPart(name, sv.getAsString(), itemType));
        }
    }
    return Entity.entity(multiPart, mediaType);
}
 
Example 7
Source File: MattermostClient.java    From mattermost4j with Apache License 2.0 5 votes vote down vote up
@Override
public ApiResponse<Boolean> setProfileImage(String userId, Path imageFilePath) {
  MultiPart multiPart = new MultiPart();
  multiPart.setMediaType(MediaType.MULTIPART_FORM_DATA_TYPE);

  FileDataBodyPart body = new FileDataBodyPart("image", imageFilePath.toFile());
  multiPart.bodyPart(body);

  return doApiPostMultiPart(getUserProfileImageRoute(userId), multiPart).checkStatusOk();
}
 
Example 8
Source File: ApiResourceTest.java    From gravitee-management-rest-api with Apache License 2.0 5 votes vote down vote up
public void shouldUploadApiMedia() {
    StreamDataBodyPart filePart = new StreamDataBodyPart("file",
            this.getClass().getResourceAsStream("/media/logo.svg"), "logo.svg", MediaType.valueOf("image/svg+xml"));
    final MultiPart multiPart = new MultiPart(MediaType.MULTIPART_FORM_DATA_TYPE);
    multiPart.bodyPart(filePart);
    final Response response = target(API + "/media/upload").request().post(entity(multiPart, multiPart.getMediaType()));
    assertEquals(OK_200, response.getStatus());
}
 
Example 9
Source File: ApiResourceTest.java    From gravitee-management-rest-api with Apache License 2.0 5 votes vote down vote up
public void shouldNotUploadApiMediaBecauseWrongMediaType() {
    StreamDataBodyPart filePart = new StreamDataBodyPart("file",
            this.getClass().getResourceAsStream("/media/logo.svg"), "logo.svg", MediaType.APPLICATION_OCTET_STREAM_TYPE);
    final MultiPart multiPart = new MultiPart(MediaType.MULTIPART_FORM_DATA_TYPE);
    multiPart.bodyPart(filePart);
    final Response response = target(API + "/media/upload").request().post(entity(multiPart, multiPart.getMediaType()));
    assertEquals(BAD_REQUEST_400, response.getStatus());
    final String message = response.readEntity(String.class);
    assertTrue(message, message.contains("File format unauthorized"));
}
 
Example 10
Source File: ApiResourceTest.java    From gravitee-management-rest-api with Apache License 2.0 5 votes vote down vote up
private void shouldNotUpdateApiMediaBecauseXSS(final String fileName) {
    final StreamDataBodyPart filePart = new StreamDataBodyPart("file",
            this.getClass().getResourceAsStream("/media/" + fileName), fileName, IMAGE_SVG_XML_TYPE);
    final MultiPart multiPart = new MultiPart(MediaType.MULTIPART_FORM_DATA_TYPE);
    multiPart.bodyPart(filePart);
    final Response response = target(API + "/media/upload").request().post(entity(multiPart, multiPart.getMediaType()));
    assertEquals(BAD_REQUEST_400, response.getStatus());
    final String message = response.readEntity(String.class);
    assertTrue(message, message.contains("Invalid image format"));
}
 
Example 11
Source File: BfCoordWorkHelper.java    From batfish with Apache License 2.0 4 votes vote down vote up
private static void addFileMultiPart(MultiPart multiPart, String key, String filename) {
  multiPart.bodyPart(
      new FormDataBodyPart(key, new File(filename), MediaType.APPLICATION_OCTET_STREAM_TYPE));
}
 
Example 12
Source File: BfCoordWorkHelper.java    From batfish with Apache License 2.0 4 votes vote down vote up
private static void addTextMultiPart(MultiPart multiPart, String key, String value) {
  multiPart.bodyPart(new FormDataBodyPart(key, value, MediaType.TEXT_PLAIN_TYPE));
}