I have a java application that opens a browser and browses to a remote servlet that performs a few redirects. Additionally, the java application instantiates a Jetty Server on an open port and waits for the servlet to redirect the browser to it. I've verified that the URL that the servlet is instructing the browser to redirect to is correct (i.e. something like http://localhost:54321/callback), but I'm never hitting a breakpoint in the request handler of the concrete AbstractHandler I implemented to receive the redirect. I am running in a windows environment, but I'm not sure if that's what's causing my woes...
Below is the LocalServerReceiver implementation I'm using:
import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.mortbay.jetty.Connector;
import org.mortbay.jetty.Request;
import org.mortbay.jetty.Server;
import org.mortbay.jetty.handler.AbstractHandler;
public class LocalServerReceiver {
class CallbackHandler extends AbstractHandler {
@Override
public void handle(String target, HttpServletRequest request, HttpServletResponse response, int dispatch) throws IOException, ServletException {
if (!CALLBACK_PATH.equals(target)) {
return;
}
writeLandingHtml(response);
response.flushBuffer();
((Request) request).setHandled(true);
String error = request.getParameter("error");
if (error != null) {
// TODO Do something.
}
synchronized (LocalServerReceiver.this) {
LocalServerReceiver.this.notify();
}
}
private void writeLandingHtml(HttpServletResponse response) throws IOException {
response.setStatus(HttpServletResponse.SC_OK);
response.setContentType("text/html");
PrintWriter doc = response.getWriter();
doc.println("<html>");
doc.println("<body>");
doc.println("<script type='text/javascript'>");
doc.println("window.setTimeout(function() {");
doc.println(" window.open('', '_self', ''); window.close(); }, 1000);");
doc.println("if (window.opener) { window.opener.checkToken(); }");
doc.println("</script>");
doc.println("</body>");
doc.println("</html>");
}
}
private static final String CALLBACK_PATH = "/callback";
private Server _server;
public String getRedirectUrl() throws Exception {
int port = getUnusedPort();
_server = new Server(port);
for (Connector c : _server.getConnectors()) {
c.setHost("localhost");
}
_server.addHandler(new CallbackHandler());
_server.start();
return "http://localhost:" + port + CALLBACK_PATH;
}
public void waitForResponse() {
try {
wait();
} catch (InterruptedException e) {
// Should not happen
}
}
public void stop() throws Exception {
if (_server != null) {
_server.stop();
_server = null;
}
}
private static int getUnusedPort() throws IOException {
Socket s = new Socket();
s.bind(null);
try {
return s.getLocalPort();
} finally {
s.close();
}
}
}