import org.restlet.resource.*;
import org.restlet.*;
import org.restlet.representation.*;
import org.restlet.data.*;
import org.restlet.routing.*;
import java.io.*;

public class Test extends Application {
    /**
     * Binds TestResource to /test.
     */
    public synchronized Restlet createInboundRoot() {
        Router router;

        router = new Router(getContext());
        router.attach("/test", TestResource.class);

        return router;
    }

    /**
     * Resource that only supports POST and aborts the client request immediately.
     */
    public static class TestResource extends ServerResource {
        @Post
        public void test() {
            getRequest().abort();
        }
    }

    /**
     * InputStream that counts the number of bytes that were read from it.
     */
    public static class CountingInputStream extends InputStream {
        private int count;

        public int read() {
            if(count > 100)
                return -1;
            count++;
            return 'a';
        }

        public int getCount() {
            return count;
        }
    }

    public static void main(String[] args) throws Exception {
        Component           component;
        CountingInputStream in;

        // Starts the server.
        component = new Component();
        component.getServers().add(Protocol.HTTP, 8182);
        component.getDefaultHost().attach(new Test());
        component.start();

        // Submits a CountingInputStream to TestResource.
        in = new CountingInputStream();
        new Client(new Context(), Protocol.HTTP).handle(new Request(Method.POST, new Reference("http://localhost:8182/test"),
                                                                    new InputRepresentation(in)));

        // This should be 0, but is 101.
        System.out.println("Consumed: " + in.getCount());
        System.exit(0);
    }
}
