use of io.kamax.mxisd.exception.RemoteHomeServerException in project mxisd by kamax-io.
the class RoomInviteHandler method handleRequest.
@Override
public void handleRequest(HttpServerExchange exchange) {
String accessToken = getAccessToken(exchange);
String whoamiUri = dns.transform(URI.create(exchange.getRequestURL()).resolve(URI.create("/_matrix/client/r0/account/whoami"))).toString();
log.info("Who Am I URL: {}", whoamiUri);
HttpGet whoAmIReq = new HttpGet(whoamiUri);
whoAmIReq.addHeader("Authorization", "Bearer " + accessToken);
_MatrixID uId;
try (CloseableHttpResponse whoAmIRes = client.execute(whoAmIReq)) {
int sc = whoAmIRes.getStatusLine().getStatusCode();
String body = EntityUtils.toString(whoAmIRes.getEntity());
if (sc != 200) {
log.warn("Unable to get caller identity from Homeserver - Status code: {}", sc);
log.debug("Body: {}", body);
throw new RemoteHomeServerException(body);
}
JsonObject json = GsonUtil.parseObj(body);
Optional<String> uIdRaw = GsonUtil.findString(json, "user_id");
if (!uIdRaw.isPresent()) {
throw new RemoteHomeServerException("No User ID provided when checking identity");
}
uId = MatrixID.asAcceptable(uIdRaw.get());
} catch (IOException e) {
InternalServerError ex = new InternalServerError(e);
log.error("Ref {}: Unable to fetch caller identity from Homeserver", ex.getReference());
throw ex;
}
log.info("Processing room invite from {}", uId.getId());
JsonObject reqBody = parseJsonObject(exchange);
if (!invMgr.canInvite(uId, reqBody)) {
throw new NotAllowedException("Your account is not allowed to invite that address");
}
log.info("Invite was allowing, relaying to the Homeserver");
proxyPost(exchange, reqBody, client, dns);
}
use of io.kamax.mxisd.exception.RemoteHomeServerException in project mxisd by kamax-io.
the class RegistrationManager method execute.
public RegistrationReply execute(URI target, JsonObject request) {
HttpPost registerProxyRq = RestClientUtils.post(resolveProxyUrl(target), GsonUtil.get(), request);
try (CloseableHttpResponse response = client.execute(registerProxyRq)) {
int status = response.getStatusLine().getStatusCode();
if (status == 200) {
// The user managed to register. We check if it had a session
String sessionId = GsonUtil.findObj(request, "auth").flatMap(auth -> GsonUtil.findString(auth, "session")).orElse("");
if (StringUtils.isEmpty(sessionId)) {
// No session ID was provided. This is an edge case we do not support for now as investigation is needed
// to ensure how and when this happens.
HttpPost newSessReq = RestClientUtils.post(resolveProxyUrl(target), GsonUtil.get(), new JsonObject());
try (CloseableHttpResponse newSessRes = client.execute(newSessReq)) {
RegistrationReply reply = new RegistrationReply();
reply.setStatus(newSessRes.getStatusLine().getStatusCode());
reply.setBody(GsonUtil.parseObj(EntityUtils.toString(newSessRes.getEntity())));
return reply;
}
}
}
throw new NotImplementedException("Registration");
} catch (IOException e) {
throw new RemoteHomeServerException(e.getMessage());
}
}
Aggregations