use of org.glassfish.jersey.media.multipart.FormDataMultiPart 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;
}
use of org.glassfish.jersey.media.multipart.FormDataMultiPart in project jersey by jersey.
the class Jersey2421Test method testMultiPartFeatureOnClient.
/**
* Test that multipart feature works on the client-side - custom connector checks presence of {@code boundary} parameter in
* the {@code Content-Type} header (the header is added to the request in MBW).
*/
@Test
public void testMultiPartFeatureOnClient() throws Exception {
final Client client = ClientBuilder.newClient(new ClientConfig().connectorProvider(new TestConnector())).register(MultiPartFeature.class);
final MultiPart entity = new FormDataMultiPart().bodyPart(new FormDataBodyPart(FormDataContentDisposition.name("part").build(), "CONTENT"));
final Response response = client.target("http://localhost").request().post(Entity.entity(entity, MediaType.MULTIPART_FORM_DATA_TYPE));
assertThat(response.getStatus(), is(200));
}
use of org.glassfish.jersey.media.multipart.FormDataMultiPart in project jersey by jersey.
the class MultiPartWebAppTest method testPart.
@Test
public void testPart() {
final WebTarget target = target().path("form/part");
final FormDataMultiPart mp = new FormDataMultiPart();
final FormDataBodyPart p = new FormDataBodyPart(FormDataContentDisposition.name("part").build(), "CONTENT");
mp.bodyPart(p);
final String s = target.request().post(Entity.entity(mp, MediaType.MULTIPART_FORM_DATA_TYPE), String.class);
assertEquals("CONTENT", s);
}
use of org.glassfish.jersey.media.multipart.FormDataMultiPart in project jersey by jersey.
the class MultiPartWebAppTest method testPartWithFileName.
@Test
public void testPartWithFileName() {
final WebTarget target = target().path("form/part-file-name");
final FormDataMultiPart mp = new FormDataMultiPart();
final FormDataBodyPart p = new FormDataBodyPart(FormDataContentDisposition.name("part").fileName("file").build(), "CONTENT");
mp.bodyPart(p);
final String s = target.request().post(Entity.entity(mp, MediaType.MULTIPART_FORM_DATA_TYPE), String.class);
assertEquals("CONTENT:file", s);
}
use of org.glassfish.jersey.media.multipart.FormDataMultiPart in project jersey by jersey.
the class FormDataMultiPartReaderWriterTest method testFormDataListTypesResource.
@Test
public void testFormDataListTypesResource() {
final Invocation.Builder request = target().path("FormDataListTypesResource").request("text/plain");
final FormDataMultiPart entity = new FormDataMultiPart().field("foo", "bar").field("foo", "bar2").field("baz", "bop").field("baz", "bop2");
final String response = request.put(Entity.entity(entity, "multipart/form-data"), String.class);
assertEquals("OK", response);
}
Aggregations