Search in sources :

Example 31 with ThreadContext

use of org.elasticsearch.common.util.concurrent.ThreadContext in project elasticsearch by elastic.

the class DeprecationLoggerTests method testAddsHeaderWithThreadContext.

public void testAddsHeaderWithThreadContext() throws IOException {
    try (ThreadContext threadContext = new ThreadContext(Settings.EMPTY)) {
        final Set<ThreadContext> threadContexts = Collections.singleton(threadContext);
        final String param = randomAsciiOfLengthBetween(1, 5);
        logger.deprecated(threadContexts, "A simple message [{}]", param);
        final Map<String, List<String>> responseHeaders = threadContext.getResponseHeaders();
        assertThat(responseHeaders.size(), equalTo(1));
        final List<String> responses = responseHeaders.get("Warning");
        assertThat(responses, hasSize(1));
        assertThat(responses.get(0), warningValueMatcher);
        assertThat(responses.get(0), containsString("\"A simple message [" + param + "]\""));
    }
}
Also used : ThreadContext(org.elasticsearch.common.util.concurrent.ThreadContext) List(java.util.List) Matchers.containsString(org.hamcrest.Matchers.containsString)

Example 32 with ThreadContext

use of org.elasticsearch.common.util.concurrent.ThreadContext in project elasticsearch by elastic.

the class ESTestCase method before.

@Before
public final void before() {
    logger.info("[{}]: before test", getTestName());
    assertNull("Thread context initialized twice", threadContext);
    if (enableWarningsCheck()) {
        this.threadContext = new ThreadContext(Settings.EMPTY);
        DeprecationLogger.setThreadContext(threadContext);
    }
}
Also used : ThreadContext(org.elasticsearch.common.util.concurrent.ThreadContext) Before(org.junit.Before)

Example 33 with ThreadContext

use of org.elasticsearch.common.util.concurrent.ThreadContext in project elasticsearch by elastic.

the class ESRestTestCase method buildClient.

protected RestClient buildClient(Settings settings, HttpHost[] hosts) throws IOException {
    RestClientBuilder builder = RestClient.builder(hosts);
    String keystorePath = settings.get(TRUSTSTORE_PATH);
    if (keystorePath != null) {
        final String keystorePass = settings.get(TRUSTSTORE_PASSWORD);
        if (keystorePass == null) {
            throw new IllegalStateException(TRUSTSTORE_PATH + " is provided but not " + TRUSTSTORE_PASSWORD);
        }
        Path path = PathUtils.get(keystorePath);
        if (!Files.exists(path)) {
            throw new IllegalStateException(TRUSTSTORE_PATH + " is set but points to a non-existing file");
        }
        try {
            KeyStore keyStore = KeyStore.getInstance("jks");
            try (InputStream is = Files.newInputStream(path)) {
                keyStore.load(is, keystorePass.toCharArray());
            }
            SSLContext sslcontext = SSLContexts.custom().loadTrustMaterial(keyStore, null).build();
            SSLIOSessionStrategy sessionStrategy = new SSLIOSessionStrategy(sslcontext);
            builder.setHttpClientConfigCallback(httpClientBuilder -> httpClientBuilder.setSSLStrategy(sessionStrategy));
        } catch (KeyStoreException | NoSuchAlgorithmException | KeyManagementException | CertificateException e) {
            throw new RuntimeException("Error setting up ssl", e);
        }
    }
    try (ThreadContext threadContext = new ThreadContext(settings)) {
        Header[] defaultHeaders = new Header[threadContext.getHeaders().size()];
        int i = 0;
        for (Map.Entry<String, String> entry : threadContext.getHeaders().entrySet()) {
            defaultHeaders[i++] = new BasicHeader(entry.getKey(), entry.getValue());
        }
        builder.setDefaultHeaders(defaultHeaders);
    }
    return builder.build();
}
Also used : Path(java.nio.file.Path) InputStream(java.io.InputStream) ThreadContext(org.elasticsearch.common.util.concurrent.ThreadContext) RestClientBuilder(org.elasticsearch.client.RestClientBuilder) SSLIOSessionStrategy(org.apache.http.nio.conn.ssl.SSLIOSessionStrategy) CertificateException(java.security.cert.CertificateException) SSLContext(javax.net.ssl.SSLContext) KeyStoreException(java.security.KeyStoreException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) KeyStore(java.security.KeyStore) KeyManagementException(java.security.KeyManagementException) Header(org.apache.http.Header) BasicHeader(org.apache.http.message.BasicHeader) Map(java.util.Map) Collections.singletonMap(java.util.Collections.singletonMap) BasicHeader(org.apache.http.message.BasicHeader)

Aggregations

ThreadContext (org.elasticsearch.common.util.concurrent.ThreadContext)33 Matchers.containsString (org.hamcrest.Matchers.containsString)15 IOException (java.io.IOException)12 FakeRestRequest (org.elasticsearch.test.rest.FakeRestRequest)12 NodeClient (org.elasticsearch.client.node.NodeClient)8 BytesArray (org.elasticsearch.common.bytes.BytesArray)8 List (java.util.List)7 ActionListener (org.elasticsearch.action.ActionListener)4 Before (org.junit.Before)4 HashSet (java.util.HashSet)3 Map (java.util.Map)3 Settings (org.elasticsearch.common.settings.Settings)3 TransportAddress (org.elasticsearch.common.transport.TransportAddress)3 HttpServerTransport (org.elasticsearch.http.HttpServerTransport)3 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)2 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)2 FullHttpResponse (io.netty.handler.codec.http.FullHttpResponse)2 HashMap (java.util.HashMap)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 Strings.collectionToDelimitedString (org.elasticsearch.common.Strings.collectionToDelimitedString)2