Search in sources :

Example 11 with BodyPart

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);
    }
}
Also used : BodyPart(org.glassfish.jersey.media.multipart.BodyPart) MultiPart(org.glassfish.jersey.media.multipart.MultiPart) MediaType(javax.ws.rs.core.MediaType) WebTarget(javax.ws.rs.client.WebTarget) IOException(java.io.IOException) ParseException(java.text.ParseException) Test(org.junit.Test)

Example 12 with BodyPart

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();
}
Also used : BodyPart(org.glassfish.jersey.media.multipart.BodyPart) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) MediaType(javax.ws.rs.core.MediaType) BodyPartEntity(org.glassfish.jersey.media.multipart.BodyPartEntity) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) PUT(javax.ws.rs.PUT)

Example 13 with BodyPart

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();
}
Also used : BodyPart(org.glassfish.jersey.media.multipart.BodyPart) MultiPart(org.glassfish.jersey.media.multipart.MultiPart) MediaType(javax.ws.rs.core.MediaType) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 14 with BodyPart

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;
}
Also used : FileDataBodyPart(org.glassfish.jersey.media.multipart.file.FileDataBodyPart) BodyPart(org.glassfish.jersey.media.multipart.BodyPart) FormDataBodyPart(org.glassfish.jersey.media.multipart.FormDataBodyPart) StreamDataBodyPart(org.glassfish.jersey.media.multipart.file.StreamDataBodyPart) MultiPart(org.glassfish.jersey.media.multipart.MultiPart) FormDataBodyPart(org.glassfish.jersey.media.multipart.FormDataBodyPart) FileDataBodyPart(org.glassfish.jersey.media.multipart.file.FileDataBodyPart)

Example 15 with BodyPart

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());
}
Also used : BodyPart(org.glassfish.jersey.media.multipart.BodyPart) FileAlreadyExistsException(org.apache.hadoop.fs.FileAlreadyExistsException) WebApplicationException(javax.ws.rs.WebApplicationException) DataSet(com.thinkbiganalytics.kylo.catalog.rest.model.DataSet) BadRequestException(javax.ws.rs.BadRequestException) InternalServerErrorException(javax.ws.rs.InternalServerErrorException) DataSetFile(com.thinkbiganalytics.kylo.catalog.rest.model.DataSetFile) CatalogException(com.thinkbiganalytics.kylo.catalog.CatalogException) BadRequestException(javax.ws.rs.BadRequestException) FileAlreadyExistsException(org.apache.hadoop.fs.FileAlreadyExistsException) InternalServerErrorException(javax.ws.rs.InternalServerErrorException) NotFoundException(javax.ws.rs.NotFoundException) WebApplicationException(javax.ws.rs.WebApplicationException) BodyPartEntity(org.glassfish.jersey.media.multipart.BodyPartEntity) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Aggregations

BodyPart (org.glassfish.jersey.media.multipart.BodyPart)15 MultiPart (org.glassfish.jersey.media.multipart.MultiPart)11 MediaType (javax.ws.rs.core.MediaType)9 IOException (java.io.IOException)6 ParseException (java.text.ParseException)5 Path (javax.ws.rs.Path)5 Produces (javax.ws.rs.Produces)5 BodyPartEntity (org.glassfish.jersey.media.multipart.BodyPartEntity)5 Test (org.junit.Test)5 WebTarget (javax.ws.rs.client.WebTarget)4 FormDataBodyPart (org.glassfish.jersey.media.multipart.FormDataBodyPart)3 ApiOperation (io.swagger.annotations.ApiOperation)2 ApiResponses (io.swagger.annotations.ApiResponses)2 List (java.util.List)2 Map (java.util.Map)2 BadRequestException (javax.ws.rs.BadRequestException)2 Consumes (javax.ws.rs.Consumes)2 GET (javax.ws.rs.GET)2 InternalServerErrorException (javax.ws.rs.InternalServerErrorException)2 NotFoundException (javax.ws.rs.NotFoundException)2