Package com.sun.jersey.multipart

Examples of com.sun.jersey.multipart.MultiPart


    // Test sending a completely empty MultiPart
    public void testSix() {
        WebResource.Builder builder = client.resource(getUri())
                .path("multipart/six").type("multipart/mixed").accept("text/plain");
        try {
            String result = builder.post(String.class, new MultiPart());
            fail("Should have thrown an exception about zero body parts");
        } catch (ClientHandlerException e) {
            assertNotNull(e.getCause());
            assertEquals(WebApplicationException.class, e.getCause().getClass());
            WebApplicationException wae = (WebApplicationException) e.getCause();
View Full Code Here


    public void testTen() {
        WebResource.Builder builder = client.resource(getUri())
                .path("multipart/ten").accept("text/plain").type("multipart/mixed");
        try {
            MultiPartBean bean = new MultiPartBean("myname", "myvalue");
            MultiPart entity = new MultiPart().
                    bodyPart(bean, new MediaType("x-application", "x-format")).
                    bodyPart("", MediaType.APPLICATION_OCTET_STREAM_TYPE);
            String response = builder.put(String.class, entity);
            if (!response.startsWith("SUCCESS:")) {
                fail("Response is '" + response + "'");
View Full Code Here

    // Echo back the multipart that was sent
    public void testTwelve() {
        WebResource.Builder builder = client.resource(getUri())
                .path("multipart/twelve").accept("multipart/mixed").type("multipart/mixed");
        try {
            MultiPart entity = new MultiPart().
              bodyPart("CONTENT", MediaType.TEXT_PLAIN_TYPE);
            MultiPart response = builder.put(MultiPart.class, entity);
            String actual = response.getBodyParts().get(0).getEntityAs(String.class);
            assertEquals("CONTENT", actual);
            response.cleanup();
        } catch (UniformInterfaceException e) {
            report(e);
            fail("Caught exception: " + e);
        }
View Full Code Here

    // Call clean up explicitly
    public void testThirteen() {
        WebResource.Builder builder = client.resource(getUri())
                .path("multipart/thirteen").accept("multipart/mixed").type("multipart/mixed");
        try {
            MultiPart entity = new MultiPart().
              bodyPart("CONTENT", MediaType.TEXT_PLAIN_TYPE);
            String response = builder.put(String.class, entity);
            assertEquals("cleanup", response);
        } catch (UniformInterfaceException e) {
            report(e);
View Full Code Here

        }
        String expected = sb.toString();
        WebResource.Builder builder = client.resource(getUri())
                .path("multipart/eleven").accept("multipart/mixed").type("multipart/mixed");
        try {
            MultiPart entity = new MultiPart().
              bodyPart(expected, MediaType.TEXT_PLAIN_TYPE);
            MultiPart response = builder.put(MultiPart.class, entity);
            String actual = response.getBodyParts().get(0).getEntityAs(String.class);
            assertEquals("Length for multiplier " + multiplier, expected.length(), actual.length());
            assertEquals("Content for multiplier " + multiplier, expected, actual);
            response.cleanup();
        } catch (UniformInterfaceException e) {
            report(e);
            fail("Caught exception: " + e + " for multiplier " + multiplier);
        }
    }
View Full Code Here

    @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();
    }
View Full Code Here

    @Path("two")
    @GET
    @Produces("multipart/mixed")
    public Response two() {
        // Exercise builder pattern with default content type
        return Response.ok(new MultiPart().
                             bodyPart("This is the first segment", new MediaType("text", "plain")).
                             bodyPart("<outer><inner>value</inner></outer>", new MediaType("text", "xml"))).build();
    }
View Full Code Here

    @GET
    @Produces("multipart/mixed")
    public Response three() {
        // Exercise builder pattern with explicit content type
        MultiPartBean bean = new MultiPartBean("myname", "myvalue");
        return Response.ok(new MultiPart().
                             type(new MediaType("multipart", "mixed")).
                             bodyPart("This is the first segment", new MediaType("text", "plain")).
                             bodyPart(bean, new MediaType("x-application", "x-format"))).build();
    }
View Full Code Here

            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();
    }
View Full Code Here

        MIMEMessage mm = new MIMEMessage(stream,
                mediaType.getParameters().get("boundary"),
                mimeConfig);

        boolean formData = false;
        MultiPart multiPart = null;
        if (MediaTypes.typeEquals(mediaType, MediaType.MULTIPART_FORM_DATA_TYPE)) {
            multiPart = new FormDataMultiPart();
            formData = true;
        } else {
            multiPart = new MultiPart();
        }

        multiPart.setProviders(providers);

        MultivaluedMap<String,String> mpHeaders = multiPart.getHeaders();
        for (Map.Entry<String,List<String>> entry : headers.entrySet()) {
            List<String> values = entry.getValue();
            for (String value : values) {
                mpHeaders.add(entry.getKey(), value);
            }
        }

        if (!formData) {
            multiPart.setMediaType(mediaType);
        }

        for (MIMEPart mp : mm.getAttachments()) {
            BodyPart bodyPart = null;
            if (formData) {
                bodyPart = new FormDataBodyPart();
            } else {
                bodyPart = new BodyPart();
            }

            // Configure providers
            bodyPart.setProviders(providers);

            // Copy headers
            for (Header h : mp.getAllHeaders()) {
                bodyPart.getHeaders().add(h.getName(), h.getValue());
            }

            try {
                String contentType = bodyPart.getHeaders().getFirst("Content-Type");
                if (contentType != null)
                    bodyPart.setMediaType(MediaType.valueOf(contentType));

                bodyPart.getContentDisposition();
            } catch (IllegalArgumentException ex) {
                throw new WebApplicationException(ex, Status.BAD_REQUEST);
            }

            // Copy data into a BodyPartEntity structure
            bodyPart.setEntity(new BodyPartEntity(mp));
            // Add this BodyPart to our MultiPart
            multiPart.getBodyParts().add(bodyPart);
        }
       
        return multiPart;
    }
View Full Code Here

TOP

Related Classes of com.sun.jersey.multipart.MultiPart

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.