-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpBackendAddServerRequestHandler.java
More file actions
executable file
·40 lines (34 loc) · 1.25 KB
/
HttpBackendAddServerRequestHandler.java
File metadata and controls
executable file
·40 lines (34 loc) · 1.25 KB
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
import java.io.IOException;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.http.HttpException;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.MethodNotSupportedException;
import org.apache.http.protocol.HttpContext;
import org.apache.http.protocol.HttpRequestHandler;
public class HttpBackendAddServerRequestHandler implements HttpRequestHandler {
public void handle(final HttpRequest request, final HttpResponse response,
final HttpContext context) throws HttpException, IOException {
String method = request.getRequestLine().getMethod()
.toUpperCase(Locale.ENGLISH);
if (method.equals("GET")) {
final String target = request.getRequestLine().getUri();
Pattern p = Pattern.compile("/addserver\\?ip=(.*?)&port=(.*)$");
Matcher m = p.matcher(target);
if (m.find()) {
String ip = m.group(1);
int port = Integer.parseInt(m.group(2));
SSM.getInstance().addServer(ip, port);
response.setStatusCode(HttpStatus.SC_OK);
} else {
response.setStatusCode(HttpStatus.SC_BAD_REQUEST);
}
} else {
throw new MethodNotSupportedException(method
+ " method not supported\n");
}
}
}