blob: ae87bea7a9d4872640883d339fdb44eba9b4d459 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
package com.orbekk.same;
import com.googlecode.jsonrpc4j.JsonRpcServer;
import com.orbekk.net.HttpUtil;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.handler.AbstractHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class RpcHandler extends AbstractHandler {
private Logger logger = LoggerFactory.getLogger(getClass());
private JsonRpcServer rpcServer;
private UrlReceiver urlReceiver;
public RpcHandler(JsonRpcServer rpcServer,
UrlReceiver urlReceiver) {
this.rpcServer = rpcServer;
this.urlReceiver = urlReceiver;
}
@Override
public synchronized void handle(String target, Request baseRequest,
HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
logger.info("Handling request to target: " + target);
if (urlReceiver != null) {
String sameServiceUrl = "http://" + request.getLocalAddr() +
":" + request.getLocalPort() + "/SameService.json";
urlReceiver.setUrl(sameServiceUrl);
urlReceiver = null;
}
if (target.equals("/ping")) {
int remotePort = Integer.parseInt(request.getParameter("port"));
String pongUrl = "http://" + request.getRemoteAddr() + ":" +
remotePort + "/pong";
logger.info("Got ping. Sending pong to {}", pongUrl);
HttpUtil.sendHttpRequest(pongUrl);
} else if (target.equals("/pong")) {
logger.info("Received pong from {}", request.getRemoteAddr());
} else {
rpcServer.handle(request, response);
}
baseRequest.setHandled(true);
}
}
|