package org.jboss.resteasy.experiment.undertow;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.nio.charset.Charset;
import java.nio.charset.CharsetEncoder;
import java.nio.charset.CodingErrorAction;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import javax.validation.constraints.NotNull;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.GET;
import javax.ws.rs.HeaderParam;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.client.Invocation;
import javax.ws.rs.core.Application;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.MessageBodyWriter;
import javax.ws.rs.ext.Provider;
import org.jboss.resteasy.client.jaxrs.ResteasyClient;
import org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder;
import org.jboss.resteasy.plugins.server.undertow.UndertowJaxrsServer;
import org.jboss.resteasy.spi.ResteasyDeployment;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.common.base.Joiner;
import com.google.common.base.Optional;
/**
*
* @author Ron Sigal
* @version $Revision: 1.1 $
*
* Created August 14, 2013
*/
public class Test1227c
{
private static UndertowJaxrsServer server;
@Provider
@Produces(MediaType.TEXT_PLAIN + "; charset=UTF-8")
public static class ListAsText implements MessageBodyWriter> {
private static final Joiner LINE_BUILDER = Joiner.on('\n').useForNull("");
@Override
public long getSize(final Iterable> t, final Class> type, final Type genericType, final Annotation[] annotations,
final MediaType mediaType) {
return -1L;
}
@Override
public boolean isWriteable(final Class> type, final Type genericType, final Annotation[] annotations,
final MediaType mediaType) {
return Iterable.class.isAssignableFrom(type);
}
@Override
public void writeTo(final Iterable> items, final Class> type, final Type genericType,
final Annotation[] annotations, final MediaType mediaType, final MultivaluedMap httpHeaders,
final OutputStream out) throws IOException {
if (items instanceof Collection) {
httpHeaders.putSingle("X-COUNT", Integer.toString(((Collection>) items).size()));
}
final Writer w = getWriter(out, mediaType);
LINE_BUILDER.appendTo(w, items);
w.flush();
}
@NotNull
protected Writer getWriter(final OutputStream out, final MediaType mediaType) {
return new OutputStreamWriter(out, getEncoder(mediaType));
}
@NotNull
protected static CharsetEncoder getEncoder(final MediaType mediaType) {
return createStrictEncoder(
Charset.forName(Optional.fromNullable(mediaType.getParameters().get("charset")).or("UTF-8")));
}
private static CharsetEncoder createStrictEncoder(final Charset encoding) {
return encoding.newEncoder().onMalformedInput(CodingErrorAction.REPORT)
.onUnmappableCharacter(CodingErrorAction.REPORT);
}
}
@ApplicationPath("")
public static class TestApp extends Application
{
@Override
public Set> getClasses()
{
HashSet> classes = new HashSet>();
classes.add(Root.class);
classes.add(ListAsText.class);
return classes;
}
}
@Path("hello")
public static class Root {
@GET
@Path("{type}")
public Object hello(@PathParam("type") final String type, @HeaderParam("Accept") final String accept)
throws Exception {
System.out.println("type: " + type);
System.out.println("accept: " + accept);
return Class.forName(type).newInstance();
}
}
//////////////////////////////////////////////////////////////////////////////
@BeforeClass
public static void before() throws Exception
{
server = new UndertowJaxrsServer().start();
ResteasyDeployment deployment = new ResteasyDeployment();
deployment.setDeploymentSensitiveFactoryEnabled(true);
deployment.setApplication(new TestApp());
deployment.start();
server.deploy(deployment);
}
@AfterClass
public static void after() throws Exception
{
server.stop();
}
//////////////////////////////////////////////////////////////////////////////
// curl -H "Accept: */*" 'http://localhost:8080/hello/java.util.TreeSet'
@Test
public void testWorks() throws Exception
{
ResteasyClient client = new ResteasyClientBuilder().build();
Invocation.Builder request = client.target("http://localhost:8081/hello/java.util.TreeSet").request();
request.accept(MediaType.valueOf("*/*"));
Response response = request.get();
String report = response.readEntity(String.class);
System.out.println("response: " + report);
System.out.println("X-COUNT: " + response.getHeaderString("X-COUNT"));
System.out.println("Content-Type: " + response.getHeaderString("Content-Type"));
Assert.assertEquals(200, response.getStatus());
client.close();
}
// curl -H "Accept: foo/bar,*/*" 'http://localhost:8080/hello/java.util.TreeSet'
@Test
public void testDoesntWorks() throws Exception
{
ResteasyClient client = new ResteasyClientBuilder().build();
Invocation.Builder request = client.target("http://localhost:8081/hello/java.util.TreeSet").request();
request.accept(MediaType.valueOf("foo/bar"));
request.accept(MediaType.valueOf("*/*"));
Response response = request.get();
String report = response.readEntity(String.class);
System.out.println("response: " + report);
System.out.println("X-COUNT: " + response.getHeaderString("X-COUNT"));
System.out.println("Content-Type: " + response.getHeaderString("Content-Type"));
Assert.assertEquals(200, response.getStatus());
client.close();
}
}