Last active
January 26, 2016 13:38
-
-
Save psamsotha/3c44023743812377fcc5 to your computer and use it in GitHub Desktop.
Stack Overflow http://stackoverflow.com/q/35013157/2587435
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.IOException; | |
import java.io.InputStream; | |
import java.io.OutputStream; | |
import java.lang.annotation.Annotation; | |
import java.lang.reflect.Type; | |
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.WebApplicationException; | |
import javax.ws.rs.client.Entity; | |
import javax.ws.rs.core.MediaType; | |
import javax.ws.rs.core.MultivaluedMap; | |
import javax.ws.rs.core.Response; | |
import javax.ws.rs.ext.MessageBodyReader; | |
import javax.ws.rs.ext.MessageBodyWriter; | |
import org.glassfish.jersey.client.ClientConfig; | |
import org.glassfish.jersey.filter.LoggingFilter; | |
import org.glassfish.jersey.message.internal.ReaderWriter; | |
import org.glassfish.jersey.server.ResourceConfig; | |
import org.glassfish.jersey.test.JerseyTest; | |
import org.junit.Test; | |
import static junit.framework.Assert.assertEquals; | |
/** | |
* Stack Overflow http://stackoverflow.com/q/35013157/2587435 | |
* | |
* @author Paul Samsotha | |
*/ | |
public class HeadersTest extends JerseyTest { | |
public static class HeaderEntity { | |
public String header; | |
public String entity; | |
public HeaderEntity(String header, String entity) { | |
this.header = header; | |
this.entity = entity; | |
} | |
} | |
private static final String HEADER = "X-Custom-Header"; | |
@Produces("application/x-custom") | |
public static class HeaderEntityWriter implements MessageBodyWriter<HeaderEntity> { | |
@Override | |
public boolean isWriteable(Class<?> type, Type genericType, | |
Annotation[] annotations, MediaType mediaType) { | |
return HeaderEntity.class == type; | |
} | |
@Override | |
public long getSize(HeaderEntity t, Class<?> type, Type genericType, | |
Annotation[] annotations, MediaType mediaType) { | |
return -1; | |
} | |
@Override | |
public void writeTo(HeaderEntity entity, Class<?> type, Type genericType, | |
Annotation[] annotations, MediaType mediaType, | |
MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) | |
throws IOException, WebApplicationException { | |
httpHeaders.putSingle(HEADER, entity.header); | |
entityStream.write(entity.entity.getBytes()); | |
} | |
} | |
@Consumes("application/x-custom") | |
public static class HeaderEntityReader implements MessageBodyReader<HeaderEntity> { | |
@Override | |
public boolean isReadable(Class<?> type, Type genericType, | |
Annotation[] annotations, MediaType mediaType) { | |
return HeaderEntity.class == type; | |
} | |
@Override | |
public HeaderEntity readFrom(Class<HeaderEntity> type, Type genericType, | |
Annotation[] annotations, MediaType mediaType, | |
MultivaluedMap<String, String> httpHeaders, InputStream entityStream) | |
throws IOException, WebApplicationException { | |
String s = ReaderWriter.readFromAsString(entityStream, MediaType.TEXT_PLAIN_TYPE); | |
return new HeaderEntity(httpHeaders.getFirst(HEADER), s); | |
} | |
} | |
@Path("test") | |
public static class TestResource { | |
@POST | |
@Consumes("application/x-custom") | |
public String post(HeaderEntity entity) { | |
return entity.header + ":" + entity.entity; | |
} | |
} | |
@Override | |
public ResourceConfig configure() { | |
return new ResourceConfig(TestResource.class) | |
.register(HeaderEntityReader.class) | |
.register(new LoggingFilter(Logger.getAnonymousLogger(), true)); | |
} | |
@Override | |
public void configureClient(ClientConfig config) { | |
config.register(HeaderEntityWriter.class); | |
} | |
@Test | |
public void shouldReturnHeaderAndEntity() { | |
Response response = target("test").request() | |
.post(Entity.entity(new HeaderEntity("header", "entity"), "application/x-custom")); | |
assertEquals(200, response.getStatus()); | |
assertEquals("header:entity", response.readEntity(String.class)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment