Search in sources :

Example 1 with ClientBulkLookupRequest

use of io.kamax.mxisd.http.io.identity.ClientBulkLookupRequest in project mxisd by kamax-io.

the class BulkLookupHandler method handleRequest.

@Override
public void handleRequest(HttpServerExchange exchange) throws Exception {
    ClientBulkLookupRequest input = parseJsonTo(exchange, ClientBulkLookupRequest.class);
    BulkLookupRequest lookupRequest = new BulkLookupRequest();
    setRequesterInfo(lookupRequest, exchange);
    log.info("Got bulk lookup request from {} with client {} - Is recursive? {}", lookupRequest.getRequester(), lookupRequest.getUserAgent(), lookupRequest.isRecursive());
    List<ThreePidMapping> mappings = new ArrayList<>();
    for (List<String> mappingRaw : input.getThreepids()) {
        ThreePidMapping mapping = new ThreePidMapping();
        mapping.setMedium(mappingRaw.get(0));
        mapping.setValue(mappingRaw.get(1));
        mappings.add(mapping);
    }
    lookupRequest.setMappings(mappings);
    ClientBulkLookupAnswer answer = new ClientBulkLookupAnswer();
    answer.addAll(strategy.find(lookupRequest).get());
    log.info("Finished bulk lookup request from {}", lookupRequest.getRequester());
    respondJson(exchange, answer);
}
Also used : ThreePidMapping(io.kamax.mxisd.lookup.ThreePidMapping) BulkLookupRequest(io.kamax.mxisd.lookup.BulkLookupRequest) ClientBulkLookupRequest(io.kamax.mxisd.http.io.identity.ClientBulkLookupRequest) ClientBulkLookupRequest(io.kamax.mxisd.http.io.identity.ClientBulkLookupRequest) ArrayList(java.util.ArrayList) ClientBulkLookupAnswer(io.kamax.mxisd.http.io.identity.ClientBulkLookupAnswer)

Example 2 with ClientBulkLookupRequest

use of io.kamax.mxisd.http.io.identity.ClientBulkLookupRequest 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 + IsAPIv1.Base + "/bulk_lookup";
    try {
        HttpPost request = RestClientUtils.post(url, mappingRequest);
        try (CloseableHttpResponse response = client.execute(request)) {
            int statusCode = response.getStatusLine().getStatusCode();
            String body = EntityUtils.toString(response.getEntity());
            if (statusCode != 200) {
                log.warn("Could not perform lookup at {} due to HTTP return code: {}", url, statusCode);
                log.warn("Body: {}", body);
                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) HttpPost(org.apache.http.client.methods.HttpPost) InvalidResponseJsonException(io.kamax.mxisd.exception.InvalidResponseJsonException) ArrayList(java.util.ArrayList) ClientBulkLookupRequest(io.kamax.mxisd.http.io.identity.ClientBulkLookupRequest) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) IOException(java.io.IOException)

Aggregations

ClientBulkLookupRequest (io.kamax.mxisd.http.io.identity.ClientBulkLookupRequest)2 ThreePidMapping (io.kamax.mxisd.lookup.ThreePidMapping)2 ArrayList (java.util.ArrayList)2 InvalidResponseJsonException (io.kamax.mxisd.exception.InvalidResponseJsonException)1 ClientBulkLookupAnswer (io.kamax.mxisd.http.io.identity.ClientBulkLookupAnswer)1 BulkLookupRequest (io.kamax.mxisd.lookup.BulkLookupRequest)1 IOException (java.io.IOException)1 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)1 HttpPost (org.apache.http.client.methods.HttpPost)1