Search in sources :

Example 1 with SingleLookupReply

use of io.kamax.mxisd.lookup.SingleLookupReply in project mxisd by kamax-io.

the class RestThreePidProvider method find.

// TODO refactor common code
@Override
public Optional<SingleLookupReply> find(SingleLookupRequest request) {
    String endpoint = cfg.getEndpoints().getIdentity().getSingle();
    HttpUriRequest req = RestClientUtils.post(endpoint, gson, "lookup", new LookupSingleRequestJson(request.getType(), request.getThreePid()));
    try (CloseableHttpResponse res = client.execute(req)) {
        int status = res.getStatusLine().getStatusCode();
        if (status < 200 || status >= 300) {
            log.warn("REST endpoint {} answered with status {}, no binding found", endpoint, status);
            return Optional.empty();
        }
        Optional<LookupSingleResponseJson> responseOpt = parser.parseOptional(res, "lookup", LookupSingleResponseJson.class);
        return responseOpt.map(lookupSingleResponseJson -> new SingleLookupReply(request, getMxId(lookupSingleResponseJson.getId())));
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) SingleLookupReply(io.kamax.mxisd.lookup.SingleLookupReply) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) IOException(java.io.IOException)

Example 2 with SingleLookupReply

use of io.kamax.mxisd.lookup.SingleLookupReply in project mxisd by kamax-io.

the class SqlThreePidProvider method find.

@Override
public Optional<SingleLookupReply> find(SingleLookupRequest request) {
    log.info("SQL lookup");
    String stmtSql = StringUtils.defaultIfBlank(cfg.getIdentity().getMedium().get(request.getType()), cfg.getIdentity().getQuery());
    log.info("SQL query: {}", stmtSql);
    try (Connection conn = pool.get()) {
        try (PreparedStatement stmt = conn.prepareStatement(stmtSql)) {
            stmt.setString(1, request.getType().toLowerCase());
            stmt.setString(2, request.getThreePid().toLowerCase());
            try (ResultSet rSet = stmt.executeQuery()) {
                while (rSet.next()) {
                    String uid = rSet.getString("uid");
                    log.info("Found match: {}", uid);
                    if (StringUtils.equals("uid", cfg.getIdentity().getType())) {
                        log.info("Resolving as localpart");
                        return Optional.of(new SingleLookupReply(request, new MatrixID(uid, mxCfg.getDomain())));
                    }
                    if (StringUtils.equals("mxid", cfg.getIdentity().getType())) {
                        log.info("Resolving as MXID");
                        return Optional.of(new SingleLookupReply(request, new MatrixID(uid)));
                    }
                    log.info("Identity type is unknown, skipping");
                }
                log.info("No match found in SQL");
                return Optional.empty();
            }
        }
    } catch (SQLException e) {
        throw new RuntimeException(e);
    }
}
Also used : SingleLookupReply(io.kamax.mxisd.lookup.SingleLookupReply) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) io.kamax.matrix._MatrixID(io.kamax.matrix._MatrixID) MatrixID(io.kamax.matrix.MatrixID)

Example 3 with SingleLookupReply

use of io.kamax.mxisd.lookup.SingleLookupReply in project mxisd by kamax-io.

the class MemoryIdentityStore method find.

@Override
public Optional<SingleLookupReply> find(SingleLookupRequest request) {
    logger.info("Performing lookup {} of type {}", request.getThreePid(), request.getType());
    ThreePid req = new ThreePid(request.getType(), request.getThreePid());
    for (MemoryIdentityConfig id : cfg.getIdentities()) {
        for (MemoryThreePid threepid : id.getThreepids()) {
            if (req.equals(new ThreePid(threepid.getMedium(), threepid.getAddress()))) {
                return Optional.of(new SingleLookupReply(request, new MatrixID(id.getUsername(), mxCfg.getDomain())));
            }
        }
    }
    return Optional.empty();
}
Also used : SingleLookupReply(io.kamax.mxisd.lookup.SingleLookupReply) MemoryIdentityConfig(io.kamax.mxisd.config.memory.MemoryIdentityConfig) io.kamax.matrix._ThreePid(io.kamax.matrix._ThreePid) ThreePid(io.kamax.matrix.ThreePid) MemoryThreePid(io.kamax.mxisd.config.memory.MemoryThreePid) MemoryThreePid(io.kamax.mxisd.config.memory.MemoryThreePid) MatrixID(io.kamax.matrix.MatrixID) io.kamax.matrix._MatrixID(io.kamax.matrix._MatrixID)

Aggregations

SingleLookupReply (io.kamax.mxisd.lookup.SingleLookupReply)3 MatrixID (io.kamax.matrix.MatrixID)2 io.kamax.matrix._MatrixID (io.kamax.matrix._MatrixID)2 ThreePid (io.kamax.matrix.ThreePid)1 io.kamax.matrix._ThreePid (io.kamax.matrix._ThreePid)1 MemoryIdentityConfig (io.kamax.mxisd.config.memory.MemoryIdentityConfig)1 MemoryThreePid (io.kamax.mxisd.config.memory.MemoryThreePid)1 IOException (java.io.IOException)1 Connection (java.sql.Connection)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)1 HttpUriRequest (org.apache.http.client.methods.HttpUriRequest)1