Search in sources :

Example 21 with MediaType

use of javax.ws.rs.core.MediaType in project jersey by jersey.

the class FormDataMultiPartReaderWriterTest method testDefaultFormDataParamResource.

@Test
public void testDefaultFormDataParamResource() {
    final Invocation.Builder request = target().path("DefaultFormDataParamResource").request("text/plain");
    final MultiPartBean bean = new MultiPartBean("myname", "myvalue");
    final FormDataMultiPart entity = new FormDataMultiPart().field("bean", bean, new MediaType("x-application", "x-format"));
    final String response = request.put(Entity.entity(entity, "multipart/form-data"), String.class);
    assertEquals("OK", response);
}
Also used : Invocation(javax.ws.rs.client.Invocation) FormDataMultiPart(org.glassfish.jersey.media.multipart.FormDataMultiPart) MediaType(javax.ws.rs.core.MediaType) Test(org.junit.Test)

Example 22 with MediaType

use of javax.ws.rs.core.MediaType in project jersey by jersey.

the class MultiPartHeaderModificationTest method testLogMessage.

@Test
public void testLogMessage() {
    final WebTarget target = target().path("multipart/ten");
    MultiPartBean bean = new MultiPartBean("myname", "myvalue");
    MultiPart entity = new MultiPart().bodyPart(bean, new MediaType("x-application", "x-format")).bodyPart("", MediaType.APPLICATION_OCTET_STREAM_TYPE);
    final String UNSENT_HEADER_CHANGES = "Unsent header changes";
    try {
        target.request("text/plain").put(Entity.entity(entity, "multipart/mixed"), String.class);
        assertFalse("BadRequestException can not be thrown just in case JERSEY-2341 is not fixed.", messageLogged);
        LogRecord logRecord = findLogRecord(UNSENT_HEADER_CHANGES);
        assertNull(logRecord);
    } catch (BadRequestException brex) {
        assertTrue("BadRequestException can be thrown just in case JERSEY-2341 is not fixed.", messageLogged);
        LogRecord logRecord = findLogRecord(UNSENT_HEADER_CHANGES);
        assertNotNull("Missing LogRecord for message '" + UNSENT_HEADER_CHANGES + "'.", logRecord);
        assertThat(logRecord.getMessage(), containsString("MIME-Version"));
        assertThat(logRecord.getMessage(), containsString("Content-Type"));
    }
}
Also used : MultiPart(org.glassfish.jersey.media.multipart.MultiPart) LogRecord(java.util.logging.LogRecord) MediaType(javax.ws.rs.core.MediaType) BadRequestException(javax.ws.rs.BadRequestException) WebTarget(javax.ws.rs.client.WebTarget) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Test(org.junit.Test)

Example 23 with MediaType

use of javax.ws.rs.core.MediaType in project jersey by jersey.

the class FileDataBodyPartTest method testCreateFDBP.

@Test
public void testCreateFDBP() throws Exception {
    FileDataBodyPart fdbp = (FileDataBodyPart) bodyPart;
    assertNull(fdbp.getFormDataContentDisposition());
    assertNull(fdbp.getName());
    assertNull(fdbp.getValue());
    assertTrue(fdbp.isSimple());
    String name;
    File file = new File("pom.xml");
    name = "xml";
    fdbp = new FileDataBodyPart(name, file);
    MediaType expectedType = MediaType.APPLICATION_XML_TYPE;
    checkEntityAttributes(name, fdbp, file, expectedType);
    fdbp.setName(name);
    checkEntityAttributes(name, fdbp, file, expectedType);
    fdbp.setFileEntity(file);
    checkEntityAttributes(name, fdbp, file, expectedType);
    fdbp = new FileDataBodyPart(name, file, expectedType);
    checkEntityAttributes(name, fdbp, file, expectedType);
    fdbp.setFileEntity(file, expectedType);
    checkEntityAttributes(name, fdbp, file, expectedType);
    file = new File("pom.png");
    name = "png";
    fdbp = new FileDataBodyPart("png", file);
    expectedType = DefaultMediaTypePredictor.CommonMediaTypes.PNG.getMediaType();
    checkEntityAttributes(name, fdbp, file, expectedType);
    file = new File("pom.zip");
    fdbp = new FileDataBodyPart(name, file);
    expectedType = DefaultMediaTypePredictor.CommonMediaTypes.ZIP.getMediaType();
    checkEntityAttributes(name, fdbp, file, expectedType);
    file = new File("pom.avi");
    fdbp = new FileDataBodyPart(name, file);
    expectedType = DefaultMediaTypePredictor.CommonMediaTypes.AVI.getMediaType();
    checkEntityAttributes(name, fdbp, file, expectedType);
}
Also used : MediaType(javax.ws.rs.core.MediaType) File(java.io.File) Test(org.junit.Test) BodyPartTest(org.glassfish.jersey.media.multipart.BodyPartTest)

Example 24 with MediaType

use of javax.ws.rs.core.MediaType in project jersey by jersey.

the class StreamDataBodyPartTest method testCreateStreamEntityNameStreamFilename.

@Test
public void testCreateStreamEntityNameStreamFilename() {
    String expectedName = "foo";
    String expectedFilename = "bar.txt";
    MediaType expectedMediaType = StreamDataBodyPart.getDefaultMediaType();
    cut = new StreamDataBodyPart(expectedName, new ByteArrayInputStream(new byte[] {}), expectedFilename);
    boolean actualIsSimpleBodyPart = cut.isSimple();
    String actualName = cut.getName();
    String actualFilename = cut.getFilename();
    // MediaType is to be the default one.
    MediaType actualMediaType = cut.getMediaType();
    assertEquals(expectedName, actualName);
    assertEquals(expectedMediaType, actualMediaType);
    assertEquals(expectedFilename, actualFilename);
    assertFalse(actualIsSimpleBodyPart);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) MediaType(javax.ws.rs.core.MediaType) Test(org.junit.Test) BodyPartTest(org.glassfish.jersey.media.multipart.BodyPartTest)

Example 25 with MediaType

use of javax.ws.rs.core.MediaType in project jersey by jersey.

the class StreamDataBodyPartTest method testCreateStreamEntityNullMediaType.

@Test
public void testCreateStreamEntityNullMediaType() {
    MediaType expectedMediaType = StreamDataBodyPart.getDefaultMediaType();
    // MediaType is nullable - it takes the default value in such situation.
    cut = new StreamDataBodyPart("foo", new ByteArrayInputStream(new byte[] {}), "bar.txt", null);
    MediaType actualMediaType = cut.getMediaType();
    assertEquals(expectedMediaType, actualMediaType);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) MediaType(javax.ws.rs.core.MediaType) Test(org.junit.Test) BodyPartTest(org.glassfish.jersey.media.multipart.BodyPartTest)

Aggregations

MediaType (javax.ws.rs.core.MediaType)228 Test (org.junit.Test)112 ResponseBuilder (javax.ws.rs.core.Response.ResponseBuilder)29 Path (javax.ws.rs.Path)25 Produces (javax.ws.rs.Produces)24 WebApplicationException (javax.ws.rs.WebApplicationException)24 ByteArrayInputStream (java.io.ByteArrayInputStream)20 HashSet (java.util.HashSet)20 MediaTypeUtil.getAcceptableMediaType (org.apache.stanbol.commons.web.base.utils.MediaTypeUtil.getAcceptableMediaType)20 IOException (java.io.IOException)18 AcceptableMediaType (org.glassfish.jersey.message.internal.AcceptableMediaType)18 InputStream (java.io.InputStream)16 GET (javax.ws.rs.GET)16 ContainerResponse (org.glassfish.jersey.server.ContainerResponse)16 EntityhubLDPath (org.apache.stanbol.entityhub.ldpath.EntityhubLDPath)15 ArrayList (java.util.ArrayList)14 Consumes (javax.ws.rs.Consumes)14 HashMap (java.util.HashMap)13 Viewable (org.apache.stanbol.commons.web.viewable.Viewable)12 MultiPart (org.glassfish.jersey.media.multipart.MultiPart)12