Search in sources :

Example 6 with UserDirectorySearchResult

use of io.kamax.mxisd.http.io.UserDirectorySearchResult in project mxisd by kamax-io.

the class ExecDirectoryStoreTest method byNameSuccessNoOutput.

@Test
public void byNameSuccessNoOutput() {
    UserDirectorySearchResult result = getStore(sno).searchByDisplayName("user");
    assertFalse(result.isLimited());
    assertTrue(result.getResults().isEmpty());
}
Also used : UserDirectorySearchResult(io.kamax.mxisd.http.io.UserDirectorySearchResult) Test(org.junit.Test)

Example 7 with UserDirectorySearchResult

use of io.kamax.mxisd.http.io.UserDirectorySearchResult in project mxisd by kamax-io.

the class RestDirectoryProviderTest method byThreepidFound.

@Test
public void byThreepidFound() {
    stubFor(post(urlEqualTo(endpoint)).willReturn(aResponse().withHeader("Content-Type", ContentType.APPLICATION_JSON.getMimeType()).withBody(new String(byThreepidResponse.getBytes(StandardCharsets.UTF_8), StandardCharsets.UTF_8))));
    UserDirectorySearchResult result = p.searchBy3pid(byThreepidSearch);
    assertTrue(!result.isLimited());
    assertEquals(1, result.getResults().size());
    UserDirectorySearchResult.Result entry = result.getResults().iterator().next();
    assertNotNull(entry);
    assertTrue(StringUtils.equals(byThreepidAvatar, entry.getAvatarUrl()));
    assertTrue(StringUtils.equals(byThreepidDisplay, entry.getDisplayName()));
    assertTrue(StringUtils.equals(MatrixID.asAcceptable(byThreepidId, domain).getId(), entry.getUserId()));
    verify(postRequestedFor(urlMatching(endpoint)).withHeader("Content-Type", containing(ContentType.APPLICATION_JSON.getMimeType())).withRequestBody(equalTo(byThreepidRequest)));
}
Also used : UserDirectorySearchResult(io.kamax.mxisd.http.io.UserDirectorySearchResult) Test(org.junit.Test)

Example 8 with UserDirectorySearchResult

use of io.kamax.mxisd.http.io.UserDirectorySearchResult in project mxisd by kamax-io.

the class UserDirectorySearchHandler method handleRequest.

@Override
public void handleRequest(HttpServerExchange exchange) {
    String accessToken = getAccessToken(exchange);
    UserDirectorySearchRequest searchQuery = parseJsonTo(exchange, UserDirectorySearchRequest.class);
    URI target = URI.create(exchange.getRequestURL());
    UserDirectorySearchResult result = mgr.search(target, accessToken, searchQuery.getSearchTerm());
    respondJson(exchange, GsonUtil.get().toJson(result));
}
Also used : UserDirectorySearchRequest(io.kamax.mxisd.http.io.UserDirectorySearchRequest) URI(java.net.URI) UserDirectorySearchResult(io.kamax.mxisd.http.io.UserDirectorySearchResult)

Example 9 with UserDirectorySearchResult

use of io.kamax.mxisd.http.io.UserDirectorySearchResult in project mxisd by kamax-io.

the class DirectoryManager method search.

public UserDirectorySearchResult search(URI target, String accessToken, String query) {
    if (StringUtils.startsWith(query, "@")) {
        query = query.substring(1);
    }
    log.info("Performing search for '{}'", query);
    log.info("Original request URL: {}", target);
    UserDirectorySearchResult result = new UserDirectorySearchResult();
    if (cfg.getExclude().getHomeserver()) {
        log.info("Skipping HS directory data, disabled in config");
    } else {
        URIBuilder builder = dns.transform(target);
        log.info("Querying HS at {}", builder);
        builder.setParameter("access_token", accessToken);
        HttpPost req = RestClientUtils.post(builder.toString(), new UserDirectorySearchRequest(query));
        try (CloseableHttpResponse res = client.execute(req)) {
            int status = res.getStatusLine().getStatusCode();
            Charset charset = ContentType.getOrDefault(res.getEntity()).getCharset();
            String body = IOUtils.toString(res.getEntity().getContent(), charset);
            if (status != 200) {
                MatrixErrorInfo info = GsonUtil.get().fromJson(body, MatrixErrorInfo.class);
                if (StringUtils.equals("M_UNRECOGNIZED", info.getErrcode())) {
                    // FIXME no hardcoding, use Enum
                    log.warn("Homeserver does not support Directory feature, skipping");
                } else {
                    log.error("Homeserver returned an error while performing directory search");
                    throw new HttpMatrixException(status, info.getErrcode(), info.getError());
                }
            }
            UserDirectorySearchResult resultHs = GsonUtil.get().fromJson(body, UserDirectorySearchResult.class);
            log.info("Found {} match(es) in HS for '{}'", resultHs.getResults().size(), query);
            result.getResults().addAll(resultHs.getResults());
            if (resultHs.isLimited()) {
                result.setLimited(true);
            }
        } catch (JsonSyntaxException e) {
            throw new InternalServerError("Invalid JSON reply from the HS: " + e.getMessage());
        } catch (IOException e) {
            throw new InternalServerError("Unable to query the HS: I/O error: " + e.getMessage());
        }
    }
    for (DirectoryProvider provider : providers) {
        log.info("Using Directory provider {}", provider.getClass().getSimpleName());
        UserDirectorySearchResult resultProvider = provider.searchByDisplayName(query);
        log.info("Display name: found {} match(es) for '{}'", resultProvider.getResults().size(), query);
        result.getResults().addAll(resultProvider.getResults());
        if (resultProvider.isLimited()) {
            result.setLimited(true);
        }
        if (cfg.getExclude().getThreepid()) {
            log.info("Skipping 3PID data, disabled in config");
        } else {
            resultProvider = provider.searchBy3pid(query);
            log.info("Threepid: found {} match(es) for '{}'", resultProvider.getResults().size(), query);
            result.getResults().addAll(resultProvider.getResults());
            if (resultProvider.isLimited()) {
                result.setLimited(true);
            }
        }
    }
    log.info("Total matches: {} - limited? {}", result.getResults().size(), result.isLimited());
    return result;
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) UserDirectorySearchRequest(io.kamax.mxisd.http.io.UserDirectorySearchRequest) HttpMatrixException(io.kamax.mxisd.exception.HttpMatrixException) Charset(java.nio.charset.Charset) IOException(java.io.IOException) InternalServerError(io.kamax.mxisd.exception.InternalServerError) URIBuilder(org.apache.http.client.utils.URIBuilder) JsonSyntaxException(com.google.gson.JsonSyntaxException) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) MatrixErrorInfo(io.kamax.matrix.MatrixErrorInfo) UserDirectorySearchResult(io.kamax.mxisd.http.io.UserDirectorySearchResult)

Example 10 with UserDirectorySearchResult

use of io.kamax.mxisd.http.io.UserDirectorySearchResult in project mxisd by kamax-io.

the class RestDirectoryProviderTest method byNameFound.

@Test
public void byNameFound() {
    stubFor(post(urlEqualTo(endpoint)).willReturn(aResponse().withHeader("Content-Type", ContentType.APPLICATION_JSON.getMimeType()).withBody(byNameResponse)));
    UserDirectorySearchResult result = p.searchByDisplayName(byNameSearch);
    assertTrue(!result.isLimited());
    assertEquals(1, result.getResults().size());
    UserDirectorySearchResult.Result entry = result.getResults().iterator().next();
    assertNotNull(entry);
    assertTrue(StringUtils.equals(byNameAvatar, entry.getAvatarUrl()));
    assertTrue(StringUtils.equals(byNameDisplay, entry.getDisplayName()));
    assertTrue(StringUtils.equals(MatrixID.asAcceptable(byNameId, domain).getId(), entry.getUserId()));
    verify(postRequestedFor(urlMatching(endpoint)).withHeader("Content-Type", containing(ContentType.APPLICATION_JSON.getMimeType())).withRequestBody(equalTo(byNameRequest)));
}
Also used : UserDirectorySearchResult(io.kamax.mxisd.http.io.UserDirectorySearchResult) Test(org.junit.Test)

Aggregations

UserDirectorySearchResult (io.kamax.mxisd.http.io.UserDirectorySearchResult)17 Test (org.junit.Test)9 InternalServerError (io.kamax.mxisd.exception.InternalServerError)5 UserDirectorySearchRequest (io.kamax.mxisd.http.io.UserDirectorySearchRequest)4 IOException (java.io.IOException)3 Connection (java.sql.Connection)2 PreparedStatement (java.sql.PreparedStatement)2 ResultSet (java.sql.ResultSet)2 SQLException (java.sql.SQLException)2 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)2 JsonSyntaxException (com.google.gson.JsonSyntaxException)1 MatrixErrorInfo (io.kamax.matrix.MatrixErrorInfo)1 ExecDirectoryStore (io.kamax.mxisd.backend.exec.ExecDirectoryStore)1 ExecConfig (io.kamax.mxisd.config.ExecConfig)1 LdapConfig (io.kamax.mxisd.config.ldap.LdapConfig)1 HttpMatrixException (io.kamax.mxisd.exception.HttpMatrixException)1 URI (java.net.URI)1 Charset (java.nio.charset.Charset)1 CursorException (org.apache.directory.api.ldap.model.cursor.CursorException)1 CursorLdapReferralException (org.apache.directory.api.ldap.model.cursor.CursorLdapReferralException)1