Search in sources :

Example 1 with InvalidJsonException

use of io.kamax.matrix.json.InvalidJsonException in project mxisd by kamax-io.

the class RestProfileProvider method doRequest.

private <T> Optional<T> doRequest(_MatrixID userId, Function<RestBackendConfig.ProfileEndpoints, Optional<String>> endpoint, Function<JsonProfileResult, Optional<T>> value) {
    Optional<String> url = endpoint.apply(cfg.getEndpoints().getProfile());
    if (!url.isPresent()) {
        return Optional.empty();
    }
    try {
        URIBuilder builder = new URIBuilder(url.get());
        HttpPost req = new HttpPost(builder.build());
        req.setEntity(new StringEntity(GsonUtil.get().toJson(new JsonProfileRequest(userId)), ContentType.APPLICATION_JSON));
        try (CloseableHttpResponse res = client.execute(req)) {
            int sc = res.getStatusLine().getStatusCode();
            if (sc == 404) {
                log.info("Got 404 - No result found");
                return Optional.empty();
            }
            if (sc != 200) {
                throw new InternalServerError("Unexpected backed status code: " + sc);
            }
            String body = IOUtils.toString(res.getEntity().getContent(), StandardCharsets.UTF_8);
            if (StringUtils.isBlank(body)) {
                log.warn("Backend response body is empty/blank, expected JSON object with profile key");
                return Optional.empty();
            }
            Optional<JsonObject> pJson = GsonUtil.findObj(GsonUtil.parseObj(body), "profile");
            if (!pJson.isPresent()) {
                log.warn("Backend response body is invalid, expected JSON object with profile key");
                return Optional.empty();
            }
            JsonProfileResult profile = gson.fromJson(pJson.get(), JsonProfileResult.class);
            return value.apply(profile);
        }
    } catch (JsonSyntaxException | InvalidJsonException e) {
        log.error("Unable to parse backend response as JSON", e);
        throw new InternalServerError(e);
    } catch (URISyntaxException e) {
        log.error("Unable to build a valid request URL", e);
        throw new InternalServerError(e);
    } catch (IOException e) {
        log.error("I/O Error during backend request", e);
        throw new InternalServerError();
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) JsonProfileRequest(io.kamax.mxisd.profile.JsonProfileRequest) JsonObject(com.google.gson.JsonObject) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) InternalServerError(io.kamax.mxisd.exception.InternalServerError) URIBuilder(org.apache.http.client.utils.URIBuilder) StringEntity(org.apache.http.entity.StringEntity) JsonSyntaxException(com.google.gson.JsonSyntaxException) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) InvalidJsonException(io.kamax.matrix.json.InvalidJsonException) JsonProfileResult(io.kamax.mxisd.profile.JsonProfileResult)

Example 2 with InvalidJsonException

use of io.kamax.matrix.json.InvalidJsonException in project mxisd by kamax-io.

the class WordpressRestBackend method validateConfig.

private void validateConfig() {
    log.info("Validating JWT auth endpoint");
    try (CloseableHttpResponse res = client.execute(new HttpGet(jwtEndpoint))) {
        int status = res.getStatusLine().getStatusCode();
        if (status != 200) {
            log.warn("JWT auth endpoint check failed: Got status code {}", status);
            return;
        }
        String data = EntityUtils.toString(res.getEntity());
        if (StringUtils.isBlank(data)) {
            log.warn("JWT auth endpoint check failed: Got no/empty body data");
        }
        JsonObject body = GsonUtil.parseObj(data);
        if (!body.has("namespace")) {
            log.warn("JWT auth endpoint check failed: invalid namespace");
        }
        log.info("JWT auth endpoint check succeeded");
    } catch (InvalidJsonException e) {
        log.warn("JWT auth endpoint check failed: Invalid JSON response: {}", e.getMessage());
    } catch (IOException e) {
        log.warn("JWT auth endpoint check failed: Could not read API endpoint: {}", e.getMessage());
    }
}
Also used : HttpGet(org.apache.http.client.methods.HttpGet) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) JsonObject(com.google.gson.JsonObject) InvalidJsonException(io.kamax.matrix.json.InvalidJsonException) IOException(java.io.IOException)

Example 3 with InvalidJsonException

use of io.kamax.matrix.json.InvalidJsonException in project mxisd by kamax-io.

the class HomeserverFederationResolver method resolveWellKnown.

private Optional<URL> resolveWellKnown(String domain) {
    log.debug("Performing Well-known lookup for {}", domain);
    HttpGet wnReq = new HttpGet("https://" + domain + "/.well-known/matrix/server");
    try (CloseableHttpResponse wnRes = client.execute(wnReq)) {
        int status = wnRes.getStatusLine().getStatusCode();
        if (status == 200) {
            try {
                JsonObject body = GsonUtil.parseObj(EntityUtils.toString(wnRes.getEntity()));
                String server = GsonUtil.getStringOrNull(body, "m.server");
                if (StringUtils.isNotBlank(server)) {
                    log.debug("Found well-known entry: {}", server);
                    return Optional.of(build(server));
                }
            } catch (InvalidJsonException e) {
                log.info("Could not parse well-known resource: {}", e.getMessage());
            }
        } else {
            log.info("Well-known did not return status code 200 but {}, ignoring", status);
        }
        return Optional.empty();
    } catch (IOException e) {
        throw new RuntimeException("Error while trying to lookup well-known for " + domain, e);
    }
}
Also used : HttpGet(org.apache.http.client.methods.HttpGet) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) JsonObject(com.google.gson.JsonObject) InvalidJsonException(io.kamax.matrix.json.InvalidJsonException) IOException(java.io.IOException)

Aggregations

JsonObject (com.google.gson.JsonObject)3 InvalidJsonException (io.kamax.matrix.json.InvalidJsonException)3 IOException (java.io.IOException)3 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)3 HttpGet (org.apache.http.client.methods.HttpGet)2 JsonSyntaxException (com.google.gson.JsonSyntaxException)1 InternalServerError (io.kamax.mxisd.exception.InternalServerError)1 JsonProfileRequest (io.kamax.mxisd.profile.JsonProfileRequest)1 JsonProfileResult (io.kamax.mxisd.profile.JsonProfileResult)1 URISyntaxException (java.net.URISyntaxException)1 HttpPost (org.apache.http.client.methods.HttpPost)1 URIBuilder (org.apache.http.client.utils.URIBuilder)1 StringEntity (org.apache.http.entity.StringEntity)1