Start structural port from Spring Boot to Undertow
This commit is contained in:
@@ -24,7 +24,7 @@ import com.google.gson.Gson;
|
||||
import com.google.gson.JsonSyntaxException;
|
||||
import io.kamax.matrix.MatrixID;
|
||||
import io.kamax.matrix._MatrixID;
|
||||
import io.kamax.mxisd.controller.identity.v1.io.SingeLookupReplyJson;
|
||||
import io.kamax.mxisd.http.io.identity.SingeLookupReplyJson;
|
||||
|
||||
import java.time.Instant;
|
||||
|
||||
|
||||
42
src/main/java/io/kamax/mxisd/lookup/ThreePidProviders.java
Normal file
42
src/main/java/io/kamax/mxisd/lookup/ThreePidProviders.java
Normal file
@@ -0,0 +1,42 @@
|
||||
/*
|
||||
* mxisd - Matrix Identity Server Daemon
|
||||
* Copyright (C) 2018 Kamax Sàrl
|
||||
*
|
||||
* https://www.kamax.io/
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package io.kamax.mxisd.lookup;
|
||||
|
||||
import io.kamax.mxisd.lookup.provider.IThreePidProvider;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.function.Supplier;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class ThreePidProviders {
|
||||
|
||||
private static final List<Supplier<? extends IThreePidProvider>> suppliers = new ArrayList<>();
|
||||
|
||||
public static void register(Supplier<? extends IThreePidProvider> supplier) {
|
||||
suppliers.add(supplier);
|
||||
}
|
||||
|
||||
public static List<? extends IThreePidProvider> get() {
|
||||
return suppliers.stream().map(Supplier::get).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -28,24 +28,23 @@ import io.kamax.mxisd.lookup.fetcher.IBridgeFetcher;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
@Component
|
||||
public class BridgeFetcher implements IBridgeFetcher {
|
||||
|
||||
private Logger log = LoggerFactory.getLogger(BridgeFetcher.class);
|
||||
private transient final Logger log = LoggerFactory.getLogger(BridgeFetcher.class);
|
||||
|
||||
@Autowired
|
||||
private RecursiveLookupBridgeConfig cfg;
|
||||
|
||||
@Autowired
|
||||
private RemoteIdentityServerFetcher fetcher;
|
||||
|
||||
public BridgeFetcher(RecursiveLookupBridgeConfig cfg, RemoteIdentityServerFetcher fetcher) {
|
||||
this.cfg = cfg;
|
||||
this.fetcher = fetcher;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<SingleLookupReply> find(SingleLookupRequest request) {
|
||||
Optional<String> mediumUrl = Optional.ofNullable(cfg.getMappings().get(request.getType()));
|
||||
|
||||
@@ -30,24 +30,23 @@ import io.kamax.mxisd.matrix.IdentityServerUtils;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.concurrent.ForkJoinPool;
|
||||
import java.util.concurrent.RecursiveTask;
|
||||
|
||||
@Component
|
||||
class DnsLookupProvider implements IThreePidProvider {
|
||||
|
||||
private Logger log = LoggerFactory.getLogger(DnsLookupProvider.class);
|
||||
private transient final Logger log = LoggerFactory.getLogger(DnsLookupProvider.class);
|
||||
|
||||
@Autowired
|
||||
private MatrixConfig mxCfg;
|
||||
|
||||
@Autowired
|
||||
private MatrixConfig cfg;
|
||||
private IRemoteIdentityServerFetcher fetcher;
|
||||
|
||||
public DnsLookupProvider(MatrixConfig cfg, IRemoteIdentityServerFetcher fetcher) {
|
||||
this.cfg = cfg;
|
||||
this.fetcher = fetcher;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEnabled() {
|
||||
return true;
|
||||
@@ -74,7 +73,7 @@ class DnsLookupProvider implements IThreePidProvider {
|
||||
|
||||
// TODO use caching mechanism
|
||||
private Optional<String> findIdentityServerForDomain(String domain) {
|
||||
if (StringUtils.equals(mxCfg.getDomain(), domain)) {
|
||||
if (StringUtils.equals(cfg.getDomain(), domain)) {
|
||||
log.info("We are authoritative for {}, no remote lookup", domain);
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
@@ -28,27 +28,25 @@ import io.kamax.mxisd.lookup.ThreePidMapping;
|
||||
import io.kamax.mxisd.lookup.fetcher.IRemoteIdentityServerFetcher;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
@Component
|
||||
class ForwarderProvider implements IThreePidProvider {
|
||||
public class ForwarderProvider implements IThreePidProvider {
|
||||
|
||||
private Logger log = LoggerFactory.getLogger(ForwarderProvider.class);
|
||||
private transient final Logger log = LoggerFactory.getLogger(ForwarderProvider.class);
|
||||
|
||||
@Autowired
|
||||
private ForwardConfig cfg;
|
||||
|
||||
@Autowired
|
||||
private MatrixConfig mxCfg;
|
||||
|
||||
@Autowired
|
||||
private IRemoteIdentityServerFetcher fetcher;
|
||||
|
||||
public ForwarderProvider(ForwardConfig cfg, MatrixConfig mxCfg, IRemoteIdentityServerFetcher fetcher) {
|
||||
this.cfg = cfg;
|
||||
this.mxCfg = mxCfg;
|
||||
this.fetcher = fetcher;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEnabled() {
|
||||
return true;
|
||||
|
||||
@@ -24,8 +24,8 @@ import com.google.gson.Gson;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonParseException;
|
||||
import io.kamax.matrix.json.GsonUtil;
|
||||
import io.kamax.mxisd.controller.identity.v1.ClientBulkLookupRequest;
|
||||
import io.kamax.mxisd.exception.InvalidResponseJsonException;
|
||||
import io.kamax.mxisd.http.io.identity.ClientBulkLookupRequest;
|
||||
import io.kamax.mxisd.lookup.SingleLookupReply;
|
||||
import io.kamax.mxisd.lookup.SingleLookupRequest;
|
||||
import io.kamax.mxisd.lookup.ThreePidMapping;
|
||||
@@ -41,10 +41,6 @@ import org.apache.http.impl.client.CloseableHttpClient;
|
||||
import org.apache.http.util.EntityUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Lazy;
|
||||
import org.springframework.context.annotation.Scope;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URISyntaxException;
|
||||
@@ -52,19 +48,20 @@ import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
@Component
|
||||
@Scope("prototype")
|
||||
@Lazy
|
||||
public class RemoteIdentityServerFetcher implements IRemoteIdentityServerFetcher {
|
||||
|
||||
private Logger log = LoggerFactory.getLogger(RemoteIdentityServerFetcher.class);
|
||||
private transient final Logger log = LoggerFactory.getLogger(RemoteIdentityServerFetcher.class);
|
||||
|
||||
// FIXME remove
|
||||
private Gson gson = new Gson();
|
||||
private GsonParser parser = new GsonParser(gson);
|
||||
|
||||
@Autowired
|
||||
private CloseableHttpClient client;
|
||||
|
||||
public RemoteIdentityServerFetcher(CloseableHttpClient client) {
|
||||
this.client = client;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isUsable(String remote) {
|
||||
return IdentityServerUtils.isUsable(remote);
|
||||
|
||||
@@ -21,18 +21,14 @@
|
||||
package io.kamax.mxisd.lookup.strategy;
|
||||
|
||||
import edazdarevic.commons.net.CIDRUtils;
|
||||
import io.kamax.mxisd.config.BulkLookupConfig;
|
||||
import io.kamax.mxisd.config.RecursiveLookupConfig;
|
||||
import io.kamax.mxisd.config.MxisdConfig;
|
||||
import io.kamax.mxisd.exception.ConfigurationException;
|
||||
import io.kamax.mxisd.lookup.*;
|
||||
import io.kamax.mxisd.lookup.fetcher.IBridgeFetcher;
|
||||
import io.kamax.mxisd.lookup.provider.IThreePidProvider;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import java.net.UnknownHostException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
@@ -40,38 +36,31 @@ import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Component
|
||||
public class RecursivePriorityLookupStrategy implements LookupStrategy {
|
||||
|
||||
private Logger log = LoggerFactory.getLogger(RecursivePriorityLookupStrategy.class);
|
||||
private transient final Logger log = LoggerFactory.getLogger(RecursivePriorityLookupStrategy.class);
|
||||
|
||||
private RecursiveLookupConfig cfg;
|
||||
private BulkLookupConfig bulkCfg;
|
||||
private MxisdConfig.Lookup cfg;
|
||||
private List<IThreePidProvider> providers;
|
||||
private IBridgeFetcher bridge;
|
||||
|
||||
private List<CIDRUtils> allowedCidr = new ArrayList<>();
|
||||
|
||||
@Autowired
|
||||
public RecursivePriorityLookupStrategy(RecursiveLookupConfig cfg, BulkLookupConfig bulkCfg, List<IThreePidProvider> providers, IBridgeFetcher bridge) {
|
||||
public RecursivePriorityLookupStrategy(MxisdConfig.Lookup cfg, List<? extends IThreePidProvider> providers, IBridgeFetcher bridge) {
|
||||
this.cfg = cfg;
|
||||
this.bulkCfg = bulkCfg;
|
||||
this.bridge = bridge;
|
||||
this.providers = providers.stream().filter(p -> {
|
||||
log.info("3PID Provider {} is enabled: {}", p.getClass().getSimpleName(), p.isEnabled());
|
||||
return p.isEnabled();
|
||||
}).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
@PostConstruct
|
||||
private void build() throws UnknownHostException {
|
||||
try {
|
||||
log.info("Found {} providers", providers.size());
|
||||
providers.forEach(p -> log.info("\t- {}", p.getClass().getName()));
|
||||
providers.sort((o1, o2) -> Integer.compare(o2.getPriority(), o1.getPriority()));
|
||||
|
||||
log.info("Recursive lookup enabled: {}", cfg.isEnabled());
|
||||
for (String cidr : cfg.getAllowedCidr()) {
|
||||
log.info("Recursive lookup enabled: {}", cfg.getRecursive().isEnabled());
|
||||
for (String cidr : cfg.getRecursive().getAllowedCidr()) {
|
||||
log.info("{} is allowed for recursion", cidr);
|
||||
allowedCidr.add(new CIDRUtils(cidr));
|
||||
}
|
||||
@@ -84,7 +73,7 @@ public class RecursivePriorityLookupStrategy implements LookupStrategy {
|
||||
boolean canRecurse = false;
|
||||
|
||||
try {
|
||||
if (cfg.isEnabled()) {
|
||||
if (cfg.getRecursive().isEnabled()) {
|
||||
log.debug("Checking {} CIDRs for recursion", allowedCidr.size());
|
||||
for (CIDRUtils cidr : allowedCidr) {
|
||||
if (cidr.isInRange(source)) {
|
||||
@@ -170,10 +159,10 @@ public class RecursivePriorityLookupStrategy implements LookupStrategy {
|
||||
}
|
||||
|
||||
if (
|
||||
cfg.getBridge() != null &&
|
||||
cfg.getBridge().getEnabled() &&
|
||||
(!cfg.getBridge().getRecursiveOnly() || isAllowedForRecursive(request.getRequester()))
|
||||
) {
|
||||
cfg.getRecursive().getBridge() != null &&
|
||||
cfg.getRecursive().getBridge().getEnabled() &&
|
||||
(!cfg.getRecursive().getBridge().getRecursiveOnly() || isAllowedForRecursive(request.getRequester()))
|
||||
) {
|
||||
log.info("Using bridge failover for lookup");
|
||||
Optional<SingleLookupReply> lookupDataOpt = bridge.find(request);
|
||||
log.info("Found 3PID mapping: {medium: '{}', address: '{}', mxid: '{}'}",
|
||||
@@ -197,7 +186,7 @@ public class RecursivePriorityLookupStrategy implements LookupStrategy {
|
||||
|
||||
@Override
|
||||
public List<ThreePidMapping> find(BulkLookupRequest request) {
|
||||
if (!bulkCfg.getEnabled()) {
|
||||
if (!cfg.getBulk().getEnabled()) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user