Search in sources :

Example 1 with LookupBulkResponseJson

use of io.kamax.mxisd.backend.rest.LookupBulkResponseJson in project mxisd by kamax-io.

the class ExecIdentityStore method populate.

@Override
public List<ThreePidMapping> populate(List<ThreePidMapping> mappings) {
    Processor<List<ThreePidMapping>> p = new Processor<>();
    p.withConfig(cfg.getLookup().getBulk());
    p.addInput(JsonType, () -> {
        JsonArray tpids = GsonUtil.asArray(mappings.stream().map(mapping -> GsonUtil.get().toJsonTree(new ThreePid(mapping.getMedium(), mapping.getValue()))).collect(Collectors.toList()));
        return GsonUtil.get().toJson(GsonUtil.makeObj("lookup", tpids));
    });
    p.addInput(PlainType, () -> {
        StringBuilder input = new StringBuilder();
        for (ThreePidMapping mapping : mappings) {
            input.append(mapping.getMedium()).append("\t").append(mapping.getValue()).append(System.lineSeparator());
        }
        return input.toString();
    });
    p.addSuccessMapper(JsonType, output -> {
        if (StringUtils.isBlank(output)) {
            return Collections.emptyList();
        }
        LookupBulkResponseJson response = GsonUtil.get().fromJson(output, LookupBulkResponseJson.class);
        return response.getLookup().stream().map(item -> {
            ThreePidMapping mapping = new ThreePidMapping();
            mapping.setMedium(item.getMedium());
            mapping.setValue(item.getAddress());
            if (UserIdType.Localpart.is(item.getId().getType())) {
                mapping.setValue(MatrixID.asAcceptable(item.getId().getValue(), mxCfg.getDomain()).getId());
                return mapping;
            }
            if (UserIdType.MatrixID.is(item.getId().getType())) {
                mapping.setValue(MatrixID.asAcceptable(item.getId().getValue()).getId());
                return mapping;
            }
            throw new InternalServerError("Invalid user type: " + item.getId().getType());
        }).collect(Collectors.toList());
    });
    p.withFailureDefault(output -> Collections.emptyList());
    return p.execute();
}
Also used : JsonArray(com.google.gson.JsonArray) ThreePidMapping(io.kamax.mxisd.lookup.ThreePidMapping) JsonParseException(com.google.gson.JsonParseException) GsonUtil(io.kamax.matrix.json.GsonUtil) LookupBulkResponseJson(io.kamax.mxisd.backend.rest.LookupBulkResponseJson) MatrixConfig(io.kamax.mxisd.config.MatrixConfig) LoggerFactory(org.slf4j.LoggerFactory) MatrixID(io.kamax.matrix.MatrixID) StringUtils(org.apache.commons.lang3.StringUtils) IThreePidProvider(io.kamax.mxisd.lookup.provider.IThreePidProvider) SingleLookupRequest(io.kamax.mxisd.lookup.SingleLookupRequest) UserIdType(io.kamax.mxisd.UserIdType) LookupSingleResponseJson(io.kamax.mxisd.backend.rest.LookupSingleResponseJson) SingleLookupReply(io.kamax.mxisd.lookup.SingleLookupReply) Logger(org.slf4j.Logger) ThreePidMapping(io.kamax.mxisd.lookup.ThreePidMapping) io.kamax.matrix._MatrixID(io.kamax.matrix._MatrixID) Collectors(java.util.stream.Collectors) ThreePid(io.kamax.matrix.ThreePid) Objects(java.util.Objects) JsonArray(com.google.gson.JsonArray) InternalServerError(io.kamax.mxisd.exception.InternalServerError) List(java.util.List) ExecConfig(io.kamax.mxisd.config.ExecConfig) UserID(io.kamax.mxisd.UserID) Optional(java.util.Optional) Collections(java.util.Collections) LookupBulkResponseJson(io.kamax.mxisd.backend.rest.LookupBulkResponseJson) List(java.util.List) ThreePid(io.kamax.matrix.ThreePid) InternalServerError(io.kamax.mxisd.exception.InternalServerError)

Aggregations

JsonArray (com.google.gson.JsonArray)1 JsonParseException (com.google.gson.JsonParseException)1 MatrixID (io.kamax.matrix.MatrixID)1 ThreePid (io.kamax.matrix.ThreePid)1 io.kamax.matrix._MatrixID (io.kamax.matrix._MatrixID)1 GsonUtil (io.kamax.matrix.json.GsonUtil)1 UserID (io.kamax.mxisd.UserID)1 UserIdType (io.kamax.mxisd.UserIdType)1 LookupBulkResponseJson (io.kamax.mxisd.backend.rest.LookupBulkResponseJson)1 LookupSingleResponseJson (io.kamax.mxisd.backend.rest.LookupSingleResponseJson)1 ExecConfig (io.kamax.mxisd.config.ExecConfig)1 MatrixConfig (io.kamax.mxisd.config.MatrixConfig)1 InternalServerError (io.kamax.mxisd.exception.InternalServerError)1 SingleLookupReply (io.kamax.mxisd.lookup.SingleLookupReply)1 SingleLookupRequest (io.kamax.mxisd.lookup.SingleLookupRequest)1 ThreePidMapping (io.kamax.mxisd.lookup.ThreePidMapping)1 IThreePidProvider (io.kamax.mxisd.lookup.provider.IThreePidProvider)1 Collections (java.util.Collections)1 List (java.util.List)1 Objects (java.util.Objects)1