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);
}
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);
}
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);
}
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"));
}
}
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);
}
Aggregations