migrating an embedded jetty server from v6 to v7

Posted by Ceilingfish on Stack Overflow See other posts from Stack Overflow or by Ceilingfish
Published on 2010-05-26T11:46:25Z Indexed on 2010/05/26 11:51 UTC
Read the original article Hit count: 407

Filed under:
|
|

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

© Stack Overflow or respective owner

Related posts about java

Related posts about jetty