views:

48

answers:

1

Hi chaps, I have an embedded servlet which I use in unit tests, looks like this:

public class UnitTestWebservices extends AbstractHandler {

    private Server server;

    private Map<Route,String> data = new HashMap<Route,String>();

    public UnitTestWebservices(int port) throws Exception {

        server = new Server(port);
        server.setHandler(this);
        server.start();

    }

    public void handle(String url, HttpServletRequest request, HttpServletResponse response, int arg3) throws IOException, ServletException {

        final Route route = Route.valueOf(request.getMethod(), url);

        final String content = data.get(route);

        if(content != null) {
            final ServletOutputStream stream = response.getOutputStream();

            stream.print(content);
            stream.flush();
            stream.close();
        }
        else {
            response.sendError(HttpServletResponse.SC_NOT_FOUND);
        }
    }

    ....
}

That's written using version 6.1.24 of Jetty. I tried switching over to use Jetty 7.1.1.v20100517, and updated that code to this:

public class UnitTestWebservices extends AbstractHandler {

    private Server server;

    private Map<Route,String> data = new HashMap<Route,String>();

    public UnitTestWebservices(int port) throws Exception {

        server = new Server(port);
        server.setHandler(this);
        server.start();

    }


    public void handle(String url, Request request, HttpServletRequest servletRequest, HttpServletResponse response) throws IOException, ServletException {

        final Route route = Route.valueOf(request.getMethod(), url);

        final String content = data.get(route);

        request.setHandled(true);

        response.setContentType("application/json");

        if(content != null) {
            response.setStatus(HttpServletResponse.SC_OK);
            final Writer stream = response.getWriter();

            stream.append(content);
        }
        else {
            response.sendError(HttpServletResponse.SC_NOT_FOUND);
        }
    }
}

But whenever I tried to access make a request to the server it would hang indefinitely. Has anyone experienced anything similar?. It also printed this into the log:

log4j:WARN No appenders could be found for logger (org.eclipse.jetty.util.log).
log4j:WARN Please initialize the log4j system properly.
org.eclipse.jetty.server.Server@670655dd STOPPED
 +-UnitTestWebservices@50ef5502 started
A: 

Seems that it might have been a problem with that specific version. I changed to version 7.0.2.v20100331 with this code

public class UnitTestWebservices extends AbstractHandler {

    private Server server;

    private Map<Route,String> data = new HashMap<Route,String>();

    public UnitTestWebservices(int port) throws Exception {

        server = new Server(port);
        server.setHandler(this);
        server.start();

    }

    public void handle(String url, final Request rawRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {

        final Route route = Route.valueOf(request.getMethod(), url);

        final String content = data.get(route);

        if(content != null) {
            final ServletOutputStream stream = response.getOutputStream();

            stream.print(content);
            stream.flush();
            stream.close();
        }
        else {
            response.sendError(HttpServletResponse.SC_NOT_FOUND);
        }
    }
}

and everything seems to work wonderfully now.

Ceilingfish