Search in sources :

Example 1 with InvalidResponseJsonException

use of io.kamax.mxisd.exception.InvalidResponseJsonException in project mxisd by kamax-io.

the class RemoteIdentityServerFetcher method find.

@Override
public List<ThreePidMapping> find(String remote, List<ThreePidMapping> mappings) {
    List<ThreePidMapping> mappingsFound = new ArrayList<>();
    ClientBulkLookupRequest mappingRequest = new ClientBulkLookupRequest();
    mappingRequest.setMappings(mappings);
    String url = remote + "/_matrix/identity/api/v1/bulk_lookup";
    CloseableHttpClient client = HttpClients.createDefault();
    try {
        HttpPost request = RestClientUtils.post(url, mappingRequest);
        try (CloseableHttpResponse response = client.execute(request)) {
            if (response.getStatusLine().getStatusCode() != 200) {
                log.info("Could not perform lookup at {} due to HTTP return code: {}", url, response.getStatusLine().getStatusCode());
                return mappingsFound;
            }
            ClientBulkLookupRequest input = parser.parse(response, ClientBulkLookupRequest.class);
            for (List<String> mappingRaw : input.getThreepids()) {
                ThreePidMapping mapping = new ThreePidMapping();
                mapping.setMedium(mappingRaw.get(0));
                mapping.setValue(mappingRaw.get(1));
                mapping.setMxid(mappingRaw.get(2));
                mappingsFound.add(mapping);
            }
        }
    } catch (IOException e) {
        log.warn("Unable to fetch remote lookup data: {}", e.getMessage());
    } catch (InvalidResponseJsonException e) {
        log.info("HTTP response from {} was empty/invalid", remote);
    }
    return mappingsFound;
}
Also used : ThreePidMapping(io.kamax.mxisd.lookup.ThreePidMapping) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpPost(org.apache.http.client.methods.HttpPost) InvalidResponseJsonException(io.kamax.mxisd.exception.InvalidResponseJsonException) ArrayList(java.util.ArrayList) ClientBulkLookupRequest(io.kamax.mxisd.controller.identity.v1.ClientBulkLookupRequest) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) IOException(java.io.IOException)

Aggregations

ClientBulkLookupRequest (io.kamax.mxisd.controller.identity.v1.ClientBulkLookupRequest)1 InvalidResponseJsonException (io.kamax.mxisd.exception.InvalidResponseJsonException)1 ThreePidMapping (io.kamax.mxisd.lookup.ThreePidMapping)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)1 HttpPost (org.apache.http.client.methods.HttpPost)1 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)1