}
@Override
public void handle(HttpContext context) throws IOException {
HttpRequest req = context.getRequest();
HttpResponse resp = context.getResponse();
Iterator<String> parts = Splitter.on('/').omitEmptyStrings().split(Strings.nullToEmpty(req.getPathInfo())).iterator();
if (!parts.hasNext()) {
resp.sendError(400, String.format("Protocol version required (supports v%d)", PROTOCOL_VERSION));
return;
}
int protocolVersion;
try {
protocolVersion = Integer.parseInt(parts.next());
} catch (NumberFormatException e) {
resp.sendError(400, e.getLocalizedMessage());
return;
}
if (protocolVersion != PROTOCOL_VERSION) {
resp.sendError(501, String.format("Protocol version not implemented (supports v%d)", PROTOCOL_VERSION));
return;
}
if (!parts.hasNext()) {
handleHandshake(context);
return;
}
String transportIdAsString = parts.next();
TransportId transportId = TransportId.fromString(transportIdAsString);
if (transportId == null || !SUPPORTED.contains(transportId)) {
resp.sendError(400, "Unsupported transport " + transportIdAsString);
}
if (!parts.hasNext()) {
resp.sendError(400, "Socket id required");
return;
}
UUID sid;
try {
sid = UUID.fromString(parts.next());
} catch (IllegalArgumentException e) {
resp.sendError(400, "Invalid socket id");
return;
}
SocketImplementor socket = get(sid);
if (socket == null) {
resp.sendError(400, String.format("Socket %s does not exist", sid.toString()));
return;
}
Transport transport = createTransport(transportId, context, socket);
if (transport == null) {
resp.sendError(400, String.format("Socket %s does not exist", sid.toString()));
return;
}
transport.handle(15000); // TODO make configurable
}