Skip to content

Instantly share code, notes, and snippets.

@psamsotha
Last active November 8, 2018 07:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save psamsotha/f54d2050b3946b2bdc1cb8f94c01f3f0 to your computer and use it in GitHub Desktop.
Save psamsotha/f54d2050b3946b2bdc1cb8f94c01f3f0 to your computer and use it in GitHub Desktop.
import java.io.InputStream;
import java.net.URI;
import java.util.logging.Logger;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;
import org.glassfish.jersey.client.ClientConfig;
import org.glassfish.jersey.filter.LoggingFilter;
import org.glassfish.jersey.media.multipart.FormDataBodyPart;
import org.glassfish.jersey.media.multipart.FormDataContentDisposition;
import org.glassfish.jersey.media.multipart.FormDataMultiPart;
import org.glassfish.jersey.media.multipart.FormDataParam;
import org.glassfish.jersey.media.multipart.MultiPart;
import org.glassfish.jersey.media.multipart.MultiPartFeature;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.test.JerseyTest;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class MultiPartTest extends JerseyTest {
@Path("a")
public static class AResource {
@POST
@Produces(MediaType.TEXT_PLAIN)
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response post(@FormDataParam("file") FormDataBodyPart body) {
FormDataBodyPart bodyPart = new FormDataBodyPart(
"file", body.getValueAs(InputStream.class), body.getMediaType());
bodyPart.setFormDataContentDisposition(body.getFormDataContentDisposition());
MultiPart multiPart = new FormDataMultiPart()
.bodyPart(bodyPart);
Response response = ClientBuilder.newClient()
.register(MultiPartFeature.class)
.register(new LoggingFilter(Logger.getAnonymousLogger(), true))
.target("http://localhost:8080/b")
.request()
.post(Entity.entity(multiPart, multiPart.getMediaType()));
return Response.status(response.getStatus())
.entity(response.readEntity(InputStream.class))
.type(response.getMediaType())
.build();
}
}
@Path("b")
public static class BResource {
@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.TEXT_PLAIN)
public Response post(@FormDataParam("file") String file,
@FormDataParam("file") FormDataContentDisposition fileDetail) {
return Response.ok(file + ":" + fileDetail.getFileName()).build();
}
}
@Override
public void configureClient(ClientConfig config) {
config.register(MultiPartFeature.class);
}
@Override
public URI getBaseUri() {
return URI.create("http://localhost:8080");
}
@Override
public ResourceConfig configure() {
return new ResourceConfig()
.register(AResource.class)
.register(BResource.class)
.register(MultiPartFeature.class)
.register(new LoggingFilter(Logger.getAnonymousLogger(), true))
.register(new ExceptionMapper<Throwable>() {
@Override
public Response toResponse(Throwable t) {
t.printStackTrace();
return Response.serverError().build();
}
});
}
@Test
public void testIt() {
FormDataBodyPart bodyPart = new FormDataBodyPart("file", "Hello World", MediaType.TEXT_PLAIN_TYPE);
bodyPart.setFormDataContentDisposition(FormDataContentDisposition.name("file").fileName("data.txt").build());
MultiPart multiPart = new FormDataMultiPart()
.bodyPart(bodyPart);
Response response = target("a")
.request()
.post(Entity.entity(multiPart, multiPart.getMediaType()));
String data = response.readEntity(String.class);
System.out.println(data);
assertEquals("Hello World:data.txt", data);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment