gpt4 book ai didi

java - Windows 中的 jetty

转载 作者:行者123 更新时间:2023-11-30 11:47:22 25 4
gpt4 key购买 nike

我有一个 Java 应用程序可以打开浏览器并浏览到执行一些重定向的远程 servlet。此外,java 应用程序在开放端口上实例化 Jetty 服务器,并等待 servlet 将浏览器重定向到它。我已经验证了 servlet 指示浏览器重定向到的 URL 是正确的(即类似 http://localhost:54321/callback 的东西),但我从来没有在我实现接收重定向的具体 AbstractHandler 的请求处理程序中遇到断点.我在 Windows 环境中运行,但我不确定这是否是导致我遇到问题的原因...

下面是我正在使用的 LocalServerReceiver 实现:

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();
}
}
}

最佳答案

深入研究后,我意识到我需要在调用 java.lang.Object.wait() 之前让当前线程成为 LocalServerReceiver 对象的监视器的所有者,就像这样

public void waitForResponse() {
try {
synchronized (LocalServerReceiver.this) {
LocalServerReceiver.this.wait();
}
} catch (InterruptedException e) {
// Should not happen
}
}

完成此操作后,Jetty 开始按预期工作。

关于java - Windows 中的 jetty ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9346767/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com