use of org.glassfish.jersey.media.multipart.BodyPart in project jersey by jersey.
the class MultiPartReaderWriterTest method testThree.
@Test
public void testThree() {
final WebTarget target = target().path("multipart/three");
try {
final MultiPart result = target.request("multipart/mixed").get(MultiPart.class);
checkMediaType(new MediaType("multipart", "mixed"), result.getMediaType());
assertEquals(2, result.getBodyParts().size());
final BodyPart part1 = result.getBodyParts().get(0);
checkMediaType(new MediaType("text", "plain"), part1.getMediaType());
checkEntity("This is the first segment", (BodyPartEntity) part1.getEntity());
final BodyPart part2 = result.getBodyParts().get(1);
checkMediaType(new MediaType("x-application", "x-format"), part2.getMediaType());
final MultiPartBean entity = part2.getEntityAs(MultiPartBean.class);
assertEquals("myname", entity.getName());
assertEquals("myvalue", entity.getValue());
result.getParameterizedHeaders();
result.cleanup();
} catch (final IOException | ParseException e) {
e.printStackTrace(System.out);
fail("Caught exception: " + e);
}
}
use of org.glassfish.jersey.media.multipart.BodyPart in project jersey by jersey.
the class MultiPartResource method four.
@Path("four")
@PUT
@Produces("text/plain")
public Response four(MultiPart multiPart) throws IOException {
if (!(multiPart.getBodyParts().size() == 2)) {
return Response.ok("FAILED: Number of body parts is " + multiPart.getBodyParts().size() + " instead of 2").build();
}
BodyPart part0 = multiPart.getBodyParts().get(0);
if (!(part0.getMediaType().equals(new MediaType("text", "plain")))) {
return Response.ok("FAILED: First media type is " + part0.getMediaType()).build();
}
BodyPart part1 = multiPart.getBodyParts().get(1);
if (!(part1.getMediaType().equals(new MediaType("x-application", "x-format")))) {
return Response.ok("FAILED: Second media type is " + part1.getMediaType()).build();
}
BodyPartEntity bpe = (BodyPartEntity) part0.getEntity();
StringBuilder sb = new StringBuilder();
InputStream stream = bpe.getInputStream();
InputStreamReader reader = new InputStreamReader(stream);
char[] buffer = new char[2048];
while (true) {
int n = reader.read(buffer);
if (n < 0) {
break;
}
sb.append(buffer, 0, n);
}
if (!sb.toString().equals("This is the first segment")) {
return Response.ok("FAILED: First part name = " + sb.toString()).build();
}
MultiPartBean bean = part1.getEntityAs(MultiPartBean.class);
if (!bean.getName().equals("myname")) {
return Response.ok("FAILED: Second part name = " + bean.getName()).build();
}
if (!bean.getValue().equals("myvalue")) {
return Response.ok("FAILED: Second part value = " + bean.getValue()).build();
}
return Response.ok("SUCCESS: All tests passed").build();
}
use of org.glassfish.jersey.media.multipart.BodyPart in project jersey by jersey.
the class MultiPartResource method one.
@Path("one")
@GET
@Produces("multipart/mixed")
public Response one() {
MultiPart entity = new MultiPart();
// Exercise manually adding part(s) to the bodyParts property
BodyPart part = new BodyPart("This is the only segment", new MediaType("text", "plain"));
entity.getBodyParts().add(part);
return Response.ok(entity).type("multipart/mixed").build();
}
use of org.glassfish.jersey.media.multipart.BodyPart in project streamline by hortonworks.
the class RestIntegrationTest method getMultiPart.
private MultiPart getMultiPart(ResourceTestElement resourceToTest, Object entity) {
MultiPart multiPart = new MultiPart();
BodyPart filePart = new FileDataBodyPart(resourceToTest.fileNameHeader, resourceToTest.fileToUpload);
BodyPart entityPart = new FormDataBodyPart(resourceToTest.entityNameHeader, entity, MediaType.APPLICATION_JSON_TYPE);
multiPart.bodyPart(filePart).bodyPart(entityPart);
return multiPart;
}
use of org.glassfish.jersey.media.multipart.BodyPart in project kylo by Teradata.
the class DataSetController method postUpload.
@POST
@Path("{id}/uploads")
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation("Uploads a file for the data set.")
@ApiResponses({ @ApiResponse(code = 200, message = "Uploaded file info", response = DataSetFile.class), @ApiResponse(code = 400, message = "Invalid filename", response = RestResponseStatus.class), @ApiResponse(code = 404, message = "Data set does not exist", response = RestResponseStatus.class), @ApiResponse(code = 409, message = "A file already exists with the same name", response = RestResponseStatus.class), @ApiResponse(code = 500, message = "Failed to upload file", response = RestResponseStatus.class) })
public Response postUpload(@PathParam("id") @UUID final String dataSetId, @Nonnull final FormDataMultiPart form) {
log.entry(dataSetId, form);
final List<BodyPart> bodyParts = form.getBodyParts();
if (bodyParts.size() != 1) {
log.debug("Wrong number of form parts for uploading to dataset {}: {}", dataSetId, bodyParts.size());
throw new BadRequestException(getMessage("catalog.dataset.postUpload.missingBodyPart"));
}
final DataSet dataSet = findDataSet(dataSetId, true);
final DataSetFile file;
try {
final BodyPart part = bodyParts.get(0);
log.debug("Upload file [{}] for dataset {}", part.getContentDisposition().getFileName(), dataSetId);
file = fileManager.createUpload(dataSet, part.getContentDisposition().getFileName(), part.getEntityAs(BodyPartEntity.class).getInputStream());
} catch (final FileAlreadyExistsException e) {
log.debug("Filename conflict for uploaded file [{}] for dataset {}: {}", bodyParts.get(0).getContentDisposition().getFileName(), dataSetId, e, e);
throw new WebApplicationException(getMessage("catalog.dataset.postUpload.fileExists"), Response.Status.CONFLICT);
} catch (final IllegalArgumentException e) {
log.debug("Invalid filename [{}] for uploaded file for dataset {}: {}", bodyParts.get(0).getContentDisposition().getFileName(), dataSetId, e, e);
throw new BadRequestException(getMessage("catalog.dataset.invalidFileName"));
} catch (final Exception e) {
log.error("Failed to save file for dataset {}: {}", dataSetId, e, e);
throw new InternalServerErrorException(getMessage("catalog.dataset.postUpload.error"));
}
return log.exit(Response.ok(file).build());
}
Aggregations