Search in sources :

Example 56 with MediaType

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

the class InboundEvent method readData.

/**
     * Read event data as a given generic type.
     *
     * @param type      generic type to be used for event data de-serialization.
     * @param mediaType {@link MediaType media type} to be used for event data de-serialization.
     * @return event data de-serialized as an instance of a given type.
     * @throws javax.ws.rs.ProcessingException when provided type can't be read. The thrown exception wraps the original cause.
     * @since 2.3
     */
public <T> T readData(GenericType<T> type, MediaType mediaType) {
    final MediaType effectiveMediaType = mediaType == null ? this.mediaType : mediaType;
    final MessageBodyReader reader = messageBodyWorkers.getMessageBodyReader(type.getRawType(), type.getType(), annotations, mediaType);
    if (reader == null) {
        throw new MessageBodyProviderNotFoundException(LocalizationMessages.EVENT_DATA_READER_NOT_FOUND());
    }
    return readAndCast(type, effectiveMediaType, reader);
}
Also used : MessageBodyReader(javax.ws.rs.ext.MessageBodyReader) MessageBodyProviderNotFoundException(org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException) MediaType(javax.ws.rs.core.MediaType)

Example 57 with MediaType

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

the class FormDataMultiPartReaderWriterTest method testMediaTypeWithBoundaryResource.

@Test
public void testMediaTypeWithBoundaryResource() {
    final Map<String, String> parameters = new HashMap<>();
    parameters.put("boundary", "XXXX_YYYY");
    final MediaType mediaType = new MediaType(MediaType.MULTIPART_FORM_DATA_TYPE.getType(), MediaType.MULTIPART_FORM_DATA_TYPE.getSubtype(), parameters);
    final FormDataMultiPart entity = new FormDataMultiPart().field("submit", "OK");
    final Invocation.Builder request = target().path("MediaTypeWithBoundaryResource").request("text/plain");
    final String response = request.put(Entity.entity(entity, mediaType), String.class);
    assertEquals("OK", response);
}
Also used : Invocation(javax.ws.rs.client.Invocation) HashMap(java.util.HashMap) MediaType(javax.ws.rs.core.MediaType) FormDataMultiPart(org.glassfish.jersey.media.multipart.FormDataMultiPart) Test(org.junit.Test)

Example 58 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 59 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 60 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)

Aggregations

MediaType (javax.ws.rs.core.MediaType)477 Test (org.junit.Test)184 Path (javax.ws.rs.Path)44 Produces (javax.ws.rs.Produces)44 ByteArrayInputStream (java.io.ByteArrayInputStream)42 WebApplicationException (javax.ws.rs.WebApplicationException)41 IOException (java.io.IOException)40 Response (javax.ws.rs.core.Response)40 InputStream (java.io.InputStream)38 ArrayList (java.util.ArrayList)31 ResponseBuilder (javax.ws.rs.core.Response.ResponseBuilder)31 Type (java.lang.reflect.Type)30 Consumes (javax.ws.rs.Consumes)27 MultivaluedMap (javax.ws.rs.core.MultivaluedMap)27 GET (javax.ws.rs.GET)25 OutputStream (java.io.OutputStream)23 Annotation (java.lang.annotation.Annotation)23 HashSet (java.util.HashSet)22 Locale (java.util.Locale)22 HashMap (java.util.HashMap)21