diff --git a/src/main/java/io/kamax/mxisd/Mxisd.java b/src/main/java/io/kamax/mxisd/Mxisd.java index 6d1c14b..dc9ed9f 100644 --- a/src/main/java/io/kamax/mxisd/Mxisd.java +++ b/src/main/java/io/kamax/mxisd/Mxisd.java @@ -40,6 +40,7 @@ import io.kamax.mxisd.lookup.provider.BridgeFetcher; import io.kamax.mxisd.lookup.provider.RemoteIdentityServerFetcher; import io.kamax.mxisd.lookup.strategy.LookupStrategy; import io.kamax.mxisd.lookup.strategy.RecursivePriorityLookupStrategy; +import io.kamax.mxisd.matrix.IdentityServerUtils; import io.kamax.mxisd.notification.NotificationHandlerSupplier; import io.kamax.mxisd.notification.NotificationHandlers; import io.kamax.mxisd.notification.NotificationManager; @@ -86,6 +87,7 @@ public class Mxisd { .setMaxConnTotal(Integer.MAX_VALUE) .build(); + IdentityServerUtils.setHttpClient(httpClient); srvFetcher = new RemoteIdentityServerFetcher(httpClient); store = new OrmLiteSqlStorage(cfg); diff --git a/src/main/java/io/kamax/mxisd/lookup/provider/RemoteIdentityServerFetcher.java b/src/main/java/io/kamax/mxisd/lookup/provider/RemoteIdentityServerFetcher.java index a288084..dbcb68b 100644 --- a/src/main/java/io/kamax/mxisd/lookup/provider/RemoteIdentityServerFetcher.java +++ b/src/main/java/io/kamax/mxisd/lookup/provider/RemoteIdentityServerFetcher.java @@ -25,6 +25,7 @@ import com.google.gson.JsonObject; import com.google.gson.JsonParseException; import io.kamax.matrix.json.GsonUtil; import io.kamax.mxisd.exception.InvalidResponseJsonException; +import io.kamax.mxisd.http.IsAPIv1; import io.kamax.mxisd.http.io.identity.ClientBulkLookupRequest; import io.kamax.mxisd.lookup.SingleLookupReply; import io.kamax.mxisd.lookup.SingleLookupRequest; @@ -73,7 +74,7 @@ public class RemoteIdentityServerFetcher implements IRemoteIdentityServerFetcher try { URIBuilder b = new URIBuilder(remote); - b.setPath("/_matrix/identity/api/v1/lookup"); + b.setPath(IsAPIv1.Base + "/lookup"); b.addParameter("medium", request.getType()); b.addParameter("address", request.getThreePid()); HttpGet req = new HttpGet(b.build()); @@ -116,7 +117,7 @@ public class RemoteIdentityServerFetcher implements IRemoteIdentityServerFetcher ClientBulkLookupRequest mappingRequest = new ClientBulkLookupRequest(); mappingRequest.setMappings(mappings); - String url = remote + "/_matrix/identity/api/v1/bulk_lookup"; + String url = remote + IsAPIv1.Base + "/bulk_lookup"; try { HttpPost request = RestClientUtils.post(url, mappingRequest); try (CloseableHttpResponse response = client.execute(request)) { diff --git a/src/main/java/io/kamax/mxisd/matrix/IdentityServerUtils.java b/src/main/java/io/kamax/mxisd/matrix/IdentityServerUtils.java index bfc7ee3..dbf3705 100644 --- a/src/main/java/io/kamax/mxisd/matrix/IdentityServerUtils.java +++ b/src/main/java/io/kamax/mxisd/matrix/IdentityServerUtils.java @@ -6,13 +6,17 @@ import com.google.gson.JsonParser; import io.kamax.mxisd.http.IsAPIv1; import org.apache.commons.io.IOUtils; import org.apache.commons.lang.StringUtils; +import org.apache.http.client.config.RequestConfig; +import org.apache.http.client.methods.CloseableHttpResponse; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.impl.client.CloseableHttpClient; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.xbill.DNS.*; import java.io.IOException; -import java.net.HttpURLConnection; import java.net.MalformedURLException; +import java.net.URI; import java.net.URL; import java.nio.charset.StandardCharsets; import java.util.ArrayList; @@ -21,31 +25,41 @@ import java.util.List; import java.util.Optional; // FIXME placeholder, this must go in matrix-java-sdk for 1.0 +// FIXME this class is just a mistake and should never have happened. Make sure to get rid of for v2.x public class IdentityServerUtils { private static Logger log = LoggerFactory.getLogger(IdentityServerUtils.class); private static JsonParser parser = new JsonParser(); + private static CloseableHttpClient client; + + public static void setHttpClient(CloseableHttpClient client) { + IdentityServerUtils.client = client; + } + public static boolean isUsable(String remote) { if (StringUtils.isBlank(remote)) { + log.info("IS URL is blank, not usable"); return false; } - try { - // FIXME use Apache HTTP client - HttpURLConnection rootSrvConn = (HttpURLConnection) new URL(remote + IsAPIv1.Base).openConnection(); - // TODO turn this into a configuration property - rootSrvConn.setConnectTimeout(2000); + HttpGet req = new HttpGet(URI.create(remote + IsAPIv1.Base)); + req.setConfig(RequestConfig.custom() + .setConnectTimeout(2000) + .setConnectionRequestTimeout(2000) + .build() + ); - int status = rootSrvConn.getResponseCode(); + try (CloseableHttpResponse res = client.execute(req)) { + int status = res.getStatusLine().getStatusCode(); if (status != 200) { log.info("Usability of {} as Identity server: answer status: {}", remote, status); return false; } - JsonElement el = parser.parse(IOUtils.toString(rootSrvConn.getInputStream(), StandardCharsets.UTF_8)); + JsonElement el = parser.parse(IOUtils.toString(res.getEntity().getContent(), StandardCharsets.UTF_8)); if (!el.isJsonObject()) { - log.debug("IS {} did not send back a JSON object for single 3PID lookup"); + log.debug("IS {} did not send back an empty JSON object as per spec, not a valid IS"); return false; }