Search in sources :

Example 11 with BodyPartEntity

use of org.glassfish.jersey.media.multipart.BodyPartEntity in project jersey by jersey.

the class MultiPartResource method ten.

@Path("ten")
@PUT
@Consumes("multipart/mixed")
@Produces("text/plain")
public Response ten(MultiPart mp) {
    if (!(mp.getBodyParts().size() == 2)) {
        return Response.ok("FAILED:  Body part count is " + mp.getBodyParts().size() + " instead of 2").build();
    } else if (!(mp.getBodyParts().get(1).getEntity() instanceof BodyPartEntity)) {
        return Response.ok("FAILED:  Second body part is " + mp.getBodyParts().get(1).getClass().getName() + " instead of BodyPartEntity").build();
    }
    BodyPartEntity bpe = (BodyPartEntity) mp.getBodyParts().get(1).getEntity();
    try {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        InputStream stream = bpe.getInputStream();
        byte[] buffer = new byte[2048];
        while (true) {
            int n = stream.read(buffer);
            if (n < 0) {
                break;
            }
            baos.write(buffer, 0, n);
        }
        if (baos.toByteArray().length > 0) {
            return Response.ok("FAILED:  Second body part had " + baos.toByteArray().length + " bytes instead of 0").build();
        }
        return Response.ok("SUCCESS:  All tests passed").build();
    } catch (IOException e) {
        return Response.ok("FAILED:  Threw IOException").build();
    }
}
Also used : InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) BodyPartEntity(org.glassfish.jersey.media.multipart.BodyPartEntity) Path(javax.ws.rs.Path) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) PUT(javax.ws.rs.PUT)

Example 12 with BodyPartEntity

use of org.glassfish.jersey.media.multipart.BodyPartEntity in project jersey by jersey.

the class MultiPartReaderClientSide method readMultiPart.

protected MultiPart readMultiPart(final Class<MultiPart> type, final Type genericType, final Annotation[] annotations, MediaType mediaType, final MultivaluedMap<String, String> headers, final InputStream stream) throws IOException, MIMEParsingException {
    mediaType = unquoteMediaTypeParameters(mediaType, "boundary");
    final MIMEMessage mimeMessage = new MIMEMessage(stream, mediaType.getParameters().get("boundary"), mimeConfig);
    final boolean formData = MediaTypes.typeEqual(mediaType, MediaType.MULTIPART_FORM_DATA_TYPE);
    final MultiPart multiPart = formData ? new FormDataMultiPart() : new MultiPart();
    final MessageBodyWorkers workers = messageBodyWorkers.get();
    multiPart.setMessageBodyWorkers(workers);
    final MultivaluedMap<String, String> multiPartHeaders = multiPart.getHeaders();
    for (final Map.Entry<String, List<String>> entry : headers.entrySet()) {
        final List<String> values = entry.getValue();
        for (final String value : values) {
            multiPartHeaders.add(entry.getKey(), value);
        }
    }
    final boolean fileNameFix;
    if (!formData) {
        multiPart.setMediaType(mediaType);
        fileNameFix = false;
    } else {
        // see if the User-Agent header corresponds to some version of MS Internet Explorer
        // if so, need to set fileNameFix to true to handle issue http://java.net/jira/browse/JERSEY-759
        final String userAgent = headers.getFirst(HttpHeaders.USER_AGENT);
        fileNameFix = userAgent != null && userAgent.contains(" MSIE ");
    }
    for (final MIMEPart mimePart : getMimeParts(mimeMessage)) {
        final BodyPart bodyPart = formData ? new FormDataBodyPart(fileNameFix) : new BodyPart();
        // Configure providers.
        bodyPart.setMessageBodyWorkers(workers);
        // Copy headers.
        for (final Header header : mimePart.getAllHeaders()) {
            bodyPart.getHeaders().add(header.getName(), header.getValue());
        }
        try {
            final String contentType = bodyPart.getHeaders().getFirst("Content-Type");
            if (contentType != null) {
                bodyPart.setMediaType(MediaType.valueOf(contentType));
            }
            bodyPart.getContentDisposition();
        } catch (final IllegalArgumentException ex) {
            throw new BadRequestException(ex);
        }
        // Copy data into a BodyPartEntity structure.
        bodyPart.setEntity(new BodyPartEntity(mimePart));
        // Add this BodyPart to our MultiPart.
        multiPart.getBodyParts().add(bodyPart);
    }
    return multiPart;
}
Also used : MessageBodyWorkers(org.glassfish.jersey.message.MessageBodyWorkers) BodyPart(org.glassfish.jersey.media.multipart.BodyPart) FormDataBodyPart(org.glassfish.jersey.media.multipart.FormDataBodyPart) BodyPartEntity(org.glassfish.jersey.media.multipart.BodyPartEntity) MultiPart(org.glassfish.jersey.media.multipart.MultiPart) FormDataMultiPart(org.glassfish.jersey.media.multipart.FormDataMultiPart) Header(org.jvnet.mimepull.Header) MIMEMessage(org.jvnet.mimepull.MIMEMessage) FormDataBodyPart(org.glassfish.jersey.media.multipart.FormDataBodyPart) FormDataMultiPart(org.glassfish.jersey.media.multipart.FormDataMultiPart) BadRequestException(javax.ws.rs.BadRequestException) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) MultivaluedMap(javax.ws.rs.core.MultivaluedMap) MIMEPart(org.jvnet.mimepull.MIMEPart)

Example 13 with BodyPartEntity

use of org.glassfish.jersey.media.multipart.BodyPartEntity 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 14 with BodyPartEntity

use of org.glassfish.jersey.media.multipart.BodyPartEntity in project jersey by jersey.

the class MultiPartResource method eleven.

// Echo back a body part whose content may or may not exceed the size
// of the buffer threshold
@Path("eleven")
@PUT
@Consumes("multipart/mixed")
@Produces("multipart/mixed")
public Response eleven(MultiPart multiPart) throws IOException {
    BodyPartEntity bpe = (BodyPartEntity) multiPart.getBodyParts().get(0).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);
    }
    return Response.ok(new MultiPart().bodyPart(sb.toString(), MediaType.TEXT_PLAIN_TYPE)).type(new MediaType("multipart", "mixed")).build();
}
Also used : MultiPart(org.glassfish.jersey.media.multipart.MultiPart) 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) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) PUT(javax.ws.rs.PUT)

Example 15 with BodyPartEntity

use of org.glassfish.jersey.media.multipart.BodyPartEntity in project jahia by Jahia.

the class ProvisioningResource method executeMultipart.

@POST
@Consumes({ MediaType.MULTIPART_FORM_DATA })
public Response executeMultipart(@FormDataParam("script") FormDataBodyPart script, @FormDataParam("file") List<FormDataBodyPart> files) {
    List<File> tmpFiles = new ArrayList<>();
    List<String> result = new ArrayList<>();
    try {
        String scriptAsString = IOUtils.toString(script.getEntityAs(BodyPartEntity.class).getInputStream(), StandardCharsets.UTF_8);
        List<Map<String, Object>> sc = getService().parseScript(scriptAsString, script.getMediaType().getSubtype());
        Map<String, FileSystemResource> resources = new HashMap<>();
        if (files != null) {
            for (FormDataBodyPart file : files) {
                // Creating temp file in order to define a specific name
                final BodyPartEntity entity = file.getEntityAs(BodyPartEntity.class);
                File tmpFile = File.createTempFile("tmp-", "." + StringUtils.substringAfterLast(file.getFormDataContentDisposition().getFileName(), "."));
                tmpFiles.add(tmpFile);
                entity.moveTo(tmpFile);
                resources.put(file.getFormDataContentDisposition().getFileName(), new FileSystemResource(tmpFile));
            }
        }
        Map<String, Object> context = new HashMap<>();
        context.put("resources", resources);
        context.put("result", result);
        getService().executeScript(sc, context);
    } catch (Exception e) {
        logger.error("Cannot execute script", e);
        return Response.serverError().entity(e.getMessage()).build();
    } finally {
        for (File tmpFile : tmpFiles) {
            FileUtils.deleteQuietly(tmpFile);
        }
    }
    return Response.status(Response.Status.OK).entity(result).type(MediaType.APPLICATION_JSON).build();
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) FileSystemResource(org.springframework.core.io.FileSystemResource) BodyPartEntity(org.glassfish.jersey.media.multipart.BodyPartEntity) FormDataBodyPart(org.glassfish.jersey.media.multipart.FormDataBodyPart) File(java.io.File) HashMap(java.util.HashMap) Map(java.util.Map) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes)

Aggregations

BodyPartEntity (org.glassfish.jersey.media.multipart.BodyPartEntity)18 Path (javax.ws.rs.Path)10 InputStream (java.io.InputStream)9 Consumes (javax.ws.rs.Consumes)9 Produces (javax.ws.rs.Produces)9 IOException (java.io.IOException)8 BodyPart (org.glassfish.jersey.media.multipart.BodyPart)7 Map (java.util.Map)6 PUT (javax.ws.rs.PUT)6 MediaType (javax.ws.rs.core.MediaType)6 FormDataBodyPart (org.glassfish.jersey.media.multipart.FormDataBodyPart)6 List (java.util.List)5 POST (javax.ws.rs.POST)5 File (java.io.File)4 InputStreamReader (java.io.InputStreamReader)4 HashMap (java.util.HashMap)4 MultivaluedMap (javax.ws.rs.core.MultivaluedMap)4 ApiOperation (io.swagger.annotations.ApiOperation)3 ApiResponses (io.swagger.annotations.ApiResponses)3 ArrayList (java.util.ArrayList)3