Start structural port from Spring Boot to Undertow

This commit is contained in:
Max Dor
2018-12-25 05:32:00 +01:00
parent df44428a85
commit 05493da27c
195 changed files with 3313 additions and 2079 deletions

View File

@@ -20,36 +20,43 @@
package io.kamax.mxisd.profile;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import io.kamax.matrix._MatrixID;
import io.kamax.matrix._ThreePid;
import io.kamax.matrix.json.GsonUtil;
import io.kamax.mxisd.dns.ClientDnsOverwrite;
import io.kamax.mxisd.exception.InternalServerError;
import io.kamax.mxisd.proxy.Response;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpRequestBase;
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.stereotype.Component;
import javax.annotation.PostConstruct;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.Collection;
import java.util.List;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;
@Component
public class ProfileManager {
private final Logger log = LoggerFactory.getLogger(ProfileManager.class);
private transient final Logger log = LoggerFactory.getLogger(ProfileManager.class);
private List<ProfileProvider> providers;
private ClientDnsOverwrite dns;
private CloseableHttpClient client;
@Autowired
public ProfileManager(List<ProfileProvider> providers) {
this.providers = providers;
}
public ProfileManager(List<? extends ProfileProvider> providers, ClientDnsOverwrite dns, CloseableHttpClient client) {
this.dns = dns;
this.client = client;
@PostConstruct
public void build() {
log.info("--- Profile providers ---");
providers = providers.stream()
this.providers = providers.stream()
.filter(pp -> {
log.info("\t- {} - Is enabled? {}", pp.getClass().getSimpleName(), pp.isEnabled());
return pp.isEnabled();
@@ -84,4 +91,29 @@ public class ProfileManager {
return getList(p -> p.getRoles(user));
}
public Response enhance(_MatrixID userId, HttpRequestBase request) {
try {
request.setURI(dns.transform(request.getURI()).build());
Response res = new Response();
try (CloseableHttpResponse hsResponse = client.execute(request)) {
res.setStatus(hsResponse.getStatusLine().getStatusCode());
JsonElement el = GsonUtil.parse(EntityUtils.toString(hsResponse.getEntity()));
if (el.isJsonObject()) {
JsonObject obj = el.getAsJsonObject();
List<_ThreePid> list = getThreepids(userId);
obj.add("threepids", GsonUtil.get().toJsonTree(list));
}
res.setBody(GsonUtil.get().toJson(el));
return res;
} catch (IOException e) {
throw new RuntimeException(e);
}
} catch (URISyntaxException e) {
log.error("Unable to build target URL for profile proxy enhancement", e);
throw new InternalServerError(e);
}
}
}

View File

@@ -0,0 +1,40 @@
/*
* 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.profile;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Supplier;
import java.util.stream.Collectors;
public class ProfileProviders {
private static final List<Supplier<? extends ProfileProvider>> providers = new ArrayList<>();
public static void register(Supplier<? extends ProfileProvider> supplier) {
providers.add(supplier);
}
public static List<? extends ProfileProvider> get() {
return providers.stream().map(Supplier::get).collect(Collectors.toList());
}
}