/** * Stop the configured Selenium Grid hub server. * * @param localOnly 'true' to target only local Grid hub server * @return 'false' if [localOnly] and hub is remote; otherwise 'true' */ public static boolean stopGridHub(boolean localOnly) { if (localOnly && !isLocalHub()) { return false; } GridHubConfiguration hubConfig = SeleniumConfig.getConfig().getHubConfig(); if (isHubActive(hubConfig)) { HttpHost hubHost = GridUtility.getHubHost(hubConfig); try { GridUtility.getHttpResponse(hubHost, HUB_SHUTDOWN); new UrlChecker().waitUntilUnavailable(SHUTDOWN_DELAY, TimeUnit.SECONDS, URI.create(hubHost.toURI()).toURL()); } catch (IOException | TimeoutException e) { throw UncheckedThrow.throwUnchecked(e); } } setHubProcess(null); return true; }
public void authFailed( final HttpHost authhost, final AuthScheme authScheme, final HttpContext context) { if (authhost == null) { throw new IllegalArgumentException("Host may not be null"); } if (context == null) { throw new IllegalArgumentException("HTTP context may not be null"); } AuthCache authCache = (AuthCache) context.getAttribute(ClientContext.AUTH_CACHE); if (authCache != null) { if (this.log.isDebugEnabled()) { this.log.debug("Clearing cached auth scheme for " + authhost); } authCache.remove(authhost); } }
@Before @SuppressWarnings("unchecked") public void createRestClient() throws IOException { CloseableHttpAsyncClient httpClient = mock(CloseableHttpAsyncClient.class); when(httpClient.<HttpResponse>execute(any(HttpAsyncRequestProducer.class), any(HttpAsyncResponseConsumer.class), any(HttpClientContext.class), any(FutureCallback.class))).thenAnswer(new Answer<Future<HttpResponse>>() { @Override public Future<HttpResponse> answer(InvocationOnMock invocationOnMock) throws Throwable { HttpAsyncRequestProducer requestProducer = (HttpAsyncRequestProducer) invocationOnMock.getArguments()[0]; HttpUriRequest request = (HttpUriRequest)requestProducer.generateRequest(); HttpHost httpHost = requestProducer.getTarget(); HttpClientContext context = (HttpClientContext) invocationOnMock.getArguments()[2]; assertThat(context.getAuthCache().get(httpHost), instanceOf(BasicScheme.class)); FutureCallback<HttpResponse> futureCallback = (FutureCallback<HttpResponse>) invocationOnMock.getArguments()[3]; //return the desired status code or exception depending on the path if (request.getURI().getPath().equals("/soe")) { futureCallback.failed(new SocketTimeoutException(httpHost.toString())); } else if (request.getURI().getPath().equals("/coe")) { futureCallback.failed(new ConnectTimeoutException(httpHost.toString())); } else if (request.getURI().getPath().equals("/ioe")) { futureCallback.failed(new IOException(httpHost.toString())); } else { int statusCode = Integer.parseInt(request.getURI().getPath().substring(1)); StatusLine statusLine = new BasicStatusLine(new ProtocolVersion("http", 1, 1), statusCode, ""); futureCallback.completed(new BasicHttpResponse(statusLine)); } return null; } }); int numHosts = RandomNumbers.randomIntBetween(getRandom(), 2, 5); httpHosts = new HttpHost[numHosts]; for (int i = 0; i < numHosts; i++) { httpHosts[i] = new HttpHost("localhost", 9200 + i); } failureListener = new HostsTrackingFailureListener(); restClient = new RestClient(httpClient, 10000, new Header[0], httpHosts, null, failureListener); }
@WebLog @RequestMapping(value="/proxyController/doValidateProxy") @ResponseBody public Proxy doValidateProxy(String id, String proxyType, String proxyIp, Integer proxyPort) { HttpHost httpHost = new HttpHost(proxyIp, proxyPort, proxyType); Proxy proxy = new Proxy(); if(HttpManager.get().checkProxy(httpHost)) { proxy.setType(proxyType); proxy.setIp(proxyIp); proxy.setPort(proxyPort); proxyDao.updateProxyById(id); //更新最后验证时间 } else { proxyDao.deleteProxyById(id); //物理删除数据 } return proxy; }
@Override public Socket connectSocket( final int connectTimeout, final Socket socket, final HttpHost host, final InetSocketAddress remoteAddress, final InetSocketAddress localAddress, final HttpContext context) throws IOException { if (log.isDebugEnabled()) { log.debug("Connecting to {}:{}", remoteAddress.getAddress(), remoteAddress.getPort()); } Socket connectedSocket = super.connectSocket(connectTimeout, socket, host, remoteAddress, localAddress, context); if (connectedSocket instanceof SSLSocket) { return new SdkSslSocket((SSLSocket) connectedSocket); } return new SdkSocket(connectedSocket); }
private HttpContext httpContext() { BasicCredentialsProvider basicCredentialsProvider = new BasicCredentialsProvider(); if (isNotBlank(this.username) && this.password != null) { UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(this.username, this.password); HttpHost httpHost = HttpHost.create(this.rootConfluenceUrl); AuthScope authScope = new AuthScope(httpHost); basicCredentialsProvider.setCredentials(authScope, credentials); BasicAuthCache basicAuthCache = new BasicAuthCache(); basicAuthCache.put(httpHost, new BasicScheme()); HttpClientContext httpClientContext = HttpClientContext.create(); httpClientContext.setCredentialsProvider(basicCredentialsProvider); httpClientContext.setAuthCache(basicAuthCache); return httpClientContext; } else { return null; } }
@Inject public ApacheHttpRequestRecordReplayer( Config config, ApacheHttpClientFactory httpClientFactory, MetricRegistry metricRegistry, JtlPrinter jtlPrinter) { // Check arguments. checkNotNull(config, "config"); checkNotNull(httpClientFactory, "httpClientFactory"); checkNotNull(metricRegistry, "metricRegistry"); checkNotNull(jtlPrinter, "jtlPrinter"); // Set class fields. this.metricRegistry = metricRegistry; this.jtlPrinter = jtlPrinter; this.httpHost = new HttpHost(config.getTargetHost(), config.getTargetPort()); this.httpClient = httpClientFactory.create(); LOGGER.debug("instantiated"); }
public void checkProxys(){ while(true){ Proxy proxy = this.poplProxy(); HttpHost host = new HttpHost(proxy.getIp(),proxy.getPort()); RequestConfig config = RequestConfig.custom().setProxy(host).build(); HttpGet httpGet = new HttpGet(PROXY_TEST_URL); httpGet.setConfig(config); try { CloseableHttpResponse response = this.httpClient.execute(httpGet); String content = EntityUtils.toString(response.getEntity(), Charset.forName("UTF-8")); if(content!=null&&content.trim().equals(proxy.getIp())) this.pushrProxy(proxy); } catch (IOException e) { } } }
@Test public void testAllowOnlySync() throws Exception { proxy = new SimpleFixedHttpProxy(); URI robotsURL = URI.create("http://foo.bor/robots.txt"); proxy.add200(robotsURL, "", "# goodguy can do anything\n" + "User-agent: goodguy\n" + "Disallow:\n\n" + "# every other guy can do nothing\n" + "User-agent: *\n" + "Disallow: /\n" ); final URI url = URI.create("http://foo.bor/goo/zoo.html"); // Disallowed proxy.start(); HttpClient httpClient = FetchDataTest.getHttpClient(new HttpHost("localhost", proxy.port()), false); FetchData fetchData = new FetchData(Helpers.getTestConfiguration(this)); fetchData.fetch(robotsURL, httpClient, null, null, true); assertTrue(URLRespectsRobots.apply(URLRespectsRobots.parseRobotsResponse(fetchData, "goodGuy"), url)); assertTrue(URLRespectsRobots.apply(URLRespectsRobots.parseRobotsResponse(fetchData, "goodGuy foo"), url)); assertFalse(URLRespectsRobots.apply(URLRespectsRobots.parseRobotsResponse(fetchData, "badGuy"), url)); assertFalse(URLRespectsRobots.apply(URLRespectsRobots.parseRobotsResponse(fetchData, "badGuy foo"), url)); }
public void testResponseWarnings() throws Exception { HttpHost host = new HttpHost("localhost", 9200); HttpUriRequest request = randomHttpRequest(new URI("/index/type/_api")); int numWarnings = randomIntBetween(1, 5); StringBuilder expected = new StringBuilder("request [").append(request.getMethod()).append(" ").append(host) .append("/index/type/_api] returned ").append(numWarnings).append(" warnings: "); Header[] warnings = new Header[numWarnings]; for (int i = 0; i < numWarnings; i++) { String warning = "this is warning number " + i; warnings[i] = new BasicHeader("Warning", warning); if (i > 0) { expected.append(","); } expected.append("[").append(warning).append("]"); } assertEquals(expected.toString(), RequestLogger.buildWarningMessage(request, host, warnings)); }
/** * Logs a request that failed */ static void logFailedRequest(Log logger, HttpUriRequest request, HttpHost host, Exception e) { if (logger.isDebugEnabled()) { logger.debug("request [" + request.getMethod() + " " + host + getUri(request.getRequestLine()) + "] failed", e); } if (tracer.isTraceEnabled()) { String traceRequest; try { traceRequest = buildTraceRequest(request, host); } catch (IOException e1) { tracer.trace("error while reading request for trace purposes", e); traceRequest = ""; } tracer.trace(traceRequest); } }
public void builder(String twoFact){ instagram = Instagram4j.builder().username(username).password(password).build(); instagram.setup(); if(sneakUsername.equals("")){ sneakUsername = username; } if (proxyEnabled){ HttpHost proxy = new HttpHost(serverIp, portNumber, "http"); instagram.getClient().getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy); instagram.getClient().getParams().setIntParameter("http.connection.timeout", 600000); instagram.getClient().getCredentialsProvider().setCredentials( new AuthScope(serverIp, portNumber), new UsernamePasswordCredentials(netUser, netPass)); } try { if(!twoFact.equals("")) instagram.login(twoFact); else{instagram.login();} refreshResult(); } catch (IOException e) { e.printStackTrace(); } }
public void updateSecureConnection( final OperatedClientConnection conn, final HttpHost target, final HttpContext context, final HttpParams params) throws IOException { if (conn == null) { throw new IllegalArgumentException("Connection may not be null"); } if (target == null) { throw new IllegalArgumentException("Target host may not be null"); } if (params == null) { throw new IllegalArgumentException("Parameters may not be null"); } if (!conn.isOpen()) { throw new IllegalStateException("Connection must be open"); } final Scheme schm = schemeRegistry.getScheme(target.getSchemeName()); if (!(schm.getSchemeSocketFactory() instanceof SchemeLayeredSocketFactory)) { throw new IllegalArgumentException ("Target scheme (" + schm.getName() + ") must have layered socket factory."); } SchemeLayeredSocketFactory lsf = (SchemeLayeredSocketFactory) schm.getSchemeSocketFactory(); Socket sock; try { sock = lsf.createLayeredSocket( conn.getSocket(), target.getHostName(), target.getPort(), params); } catch (ConnectException ex) { throw new HttpHostConnectException(target, ex); } prepareSocket(sock, context, params); conn.update(sock, target, lsf.isSecure(sock), params); }
/** * Chooses a proxy from a list of available proxies. * The default implementation just picks the first non-SOCKS proxy * from the list. If there are only SOCKS proxies, * {@link Proxy#NO_PROXY Proxy.NO_PROXY} is returned. * Derived classes may implement more advanced strategies, * such as proxy rotation if there are multiple options. * * @param proxies the list of proxies to choose from, * never <code>null</code> or empty * @param target the planned target, never <code>null</code> * @param request the request to be sent, never <code>null</code> * @param context the context, or <code>null</code> * * @return a proxy type */ protected Proxy chooseProxy(List<Proxy> proxies, HttpHost target, HttpRequest request, HttpContext context) { if ((proxies == null) || proxies.isEmpty()) { throw new IllegalArgumentException ("Proxy list must not be empty."); } Proxy result = null; // check the list for one we can use for (int i=0; (result == null) && (i < proxies.size()); i++) { Proxy p = proxies.get(i); switch (p.type()) { case DIRECT: case HTTP: result = p; break; case SOCKS: // SOCKS hosts are not handled on the route level. // The socket may make use of the SOCKS host though. break; } } if (result == null) { //@@@ log as warning or info that only a socks proxy is available? // result can only be null if all proxies are socks proxies // socks proxies are not handled on the route planning level result = Proxy.NO_PROXY; } return result; }
public static List<HttpHost> vertxProxyHosts() { final List<String> hostStrings = Arrays.asList(proxyHostsString().split(",")); final int port = vertxProxyPort(); return hostStrings.stream() .map(String::trim) .filter(s -> !s.isEmpty()) .map(s -> new HttpHost(s, port)) .collect(Collectors.toList()); }
public RequestConfig.Builder createRequestConfigBuilder(SiteConfig siteConfig, Request request, HttpHost proxy) { RequestConfig.Builder requestConfigBuilder = RequestConfig.custom(); requestConfigBuilder.setConnectTimeout(siteConfig.getConnectTimeout()); requestConfigBuilder.setSocketTimeout(siteConfig.getSocketTimeout()); requestConfigBuilder.setRedirectsEnabled(siteConfig.isRedirectsEnabled()); requestConfigBuilder.setConnectionRequestTimeout(siteConfig.getConnectionRequestTimeout()); requestConfigBuilder.setCircularRedirectsAllowed(siteConfig.isCircularRedirectsAllowed()); requestConfigBuilder.setMaxRedirects(siteConfig.getMaxRedirects()); requestConfigBuilder.setCookieSpec(siteConfig.getCookieSpec()); requestConfigBuilder.setProxy(proxy); return requestConfigBuilder; }
/** * 获取Http客户端连接对象 * @param timeOut 超时时间 * @param proxy 代理 * @param cookie Cookie * @return Http客户端连接对象 */ public CloseableHttpClient createHttpClient(int timeOut,HttpHost proxy,BasicClientCookie cookie) { // 创建Http请求配置参数 RequestConfig.Builder builder = RequestConfig.custom() // 获取连接超时时间 .setConnectionRequestTimeout(timeOut) // 请求超时时间 .setConnectTimeout(timeOut) // 响应超时时间 .setSocketTimeout(timeOut) .setCookieSpec(CookieSpecs.STANDARD); if (proxy!=null) { builder.setProxy(proxy); } RequestConfig requestConfig = builder.build(); // 创建httpClient HttpClientBuilder httpClientBuilder = HttpClients.custom(); httpClientBuilder // 把请求相关的超时信息设置到连接客户端 .setDefaultRequestConfig(requestConfig) // 把请求重试设置到连接客户端 .setRetryHandler(new RetryHandler()) // 配置连接池管理对象 .setConnectionManager(connManager); if (cookie!=null) { CookieStore cookieStore = new BasicCookieStore(); cookieStore.addCookie(cookie); httpClientBuilder.setDefaultCookieStore(cookieStore); } return httpClientBuilder.build(); }
@Override public Socket connectSocket(final int connectTimeout, final Socket socket, final HttpHost host, final InetSocketAddress remoteAddress, final InetSocketAddress localAddress, final HttpContext context) throws IOException { try { socket.connect(new UnixSocketAddress(socketFile), connectTimeout); } catch (SocketTimeoutException e) { throw new ConnectTimeoutException(e, null, remoteAddress.getAddress()); } return socket; }
/** * 获取Http客户端连接对象 * @param timeOut 超时时间 * @param proxy 代理 * @param cookie Cookie * @return Http客户端连接对象 */ private CloseableHttpClient createHttpClient(int timeOut,HttpHost proxy,BasicClientCookie cookie) { // 创建Http请求配置参数 RequestConfig.Builder builder = RequestConfig.custom() // 获取连接超时时间 .setConnectionRequestTimeout(timeOut) // 请求超时时间 .setConnectTimeout(timeOut) // 响应超时时间 .setSocketTimeout(timeOut) .setCookieSpec(CookieSpecs.STANDARD); if (proxy!=null) { builder.setProxy(proxy); } RequestConfig requestConfig = builder.build(); // 创建httpClient HttpClientBuilder httpClientBuilder = HttpClients.custom(); httpClientBuilder // 把请求相关的超时信息设置到连接客户端 .setDefaultRequestConfig(requestConfig) // 把请求重试设置到连接客户端 .setRetryHandler(new RetryHandler()) // 配置连接池管理对象 .setConnectionManager(connManager); if (cookie!=null) { CookieStore cookieStore = new BasicCookieStore(); cookieStore.addCookie(cookie); httpClientBuilder.setDefaultCookieStore(cookieStore); } return httpClientBuilder.build(); }
public void testSetPathPrefixNull() { try { RestClient.builder(new HttpHost("localhost", 9200)).setPathPrefix(null); fail("pathPrefix set to null should fail!"); } catch (final NullPointerException e) { assertEquals("pathPrefix must not be null", e.getMessage()); } }
public <T> T execute(HttpHost target, HttpRequest request, ResponseHandler<? extends T> responseHandler, HttpContext context) throws IOException, ClientProtocolException { HttpResponse response = execute(target, request, context); try { return responseHandler.handleResponse(response); } finally { HttpEntity entity = response.getEntity(); if (entity != null) EntityUtils.consume(entity); } }
/** * Creates the CONNECT request for tunnelling. * Called by {@link #createTunnelToTarget createTunnelToTarget}. * * @param route the route to establish * @param context the context for request execution * * @return the CONNECT request for tunnelling */ protected HttpRequest createConnectRequest(HttpRoute route, HttpContext context) { // see RFC 2817, section 5.2 and // INTERNET-DRAFT: Tunneling TCP based protocols through // Web proxy servers HttpHost target = route.getTargetHost(); String host = target.getHostName(); int port = target.getPort(); if (port < 0) { Scheme scheme = connManager.getSchemeRegistry(). getScheme(target.getSchemeName()); port = scheme.getDefaultPort(); } StringBuilder buffer = new StringBuilder(host.length() + 6); buffer.append(host); buffer.append(':'); buffer.append(Integer.toString(port)); String authority = buffer.toString(); ProtocolVersion ver = HttpProtocolParams.getVersion(params); HttpRequest req = new BasicHttpRequest ("CONNECT", authority, ver); return req; }
public static CloseableHttpAsyncClient createSSLClient(HttpHost proxy) { TrustStrategy acceptingTrustStrategy = new TrustStrategy() { @Override public boolean isTrusted(X509Certificate[] arg0, String arg1) throws CertificateException { return true; } }; try { SSLContext sslContext = SSLContexts.custom() .loadTrustMaterial(null, acceptingTrustStrategy) .build(); HttpAsyncClientBuilder client = HttpAsyncClients.custom() .setDefaultCookieStore(new BasicCookieStore()) .setSSLContext(sslContext) .setSSLHostnameVerifier(SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); if (proxy !=null) { client.setProxy(proxy); } return client.build(); } catch (Exception e) { System.out.println("Could not create SSLContext"); return null; } }
private CredentialsProvider credentialsProvider() { // sets up credentials object CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials( new AuthScope(new HttpHost(HOST)), new UsernamePasswordCredentials(USER, PASS)); return credsProvider; }
/** * Internal, fully-specified constructor. * This constructor does <i>not</i> clone the proxy chain array, * nor test it for <code>null</code> elements. This conversion and * check is the responsibility of the public constructors. * The order of arguments here is different from the similar public * constructor, as required by Java. * * @param local the local address to route from, or * <code>null</code> for the default * @param target the host to which to route * @param proxies the proxy chain to use, or * <code>null</code> for a direct route * @param secure <code>true</code> if the route is (to be) secure, * <code>false</code> otherwise * @param tunnelled the tunnel type of this route, or * <code>null</code> for PLAIN * @param layered the layering type of this route, or * <code>null</code> for PLAIN */ private HttpRoute(InetAddress local, HttpHost target, HttpHost[] proxies, boolean secure, TunnelType tunnelled, LayerType layered) { if (target == null) { throw new IllegalArgumentException ("Target host may not be null."); } if (proxies == null) { throw new IllegalArgumentException ("Proxies may not be null."); } if ((tunnelled == TunnelType.TUNNELLED) && (proxies.length == 0)) { throw new IllegalArgumentException ("Proxy required if tunnelled."); } // tunnelled is already checked above, that is in line with the default if (tunnelled == null) tunnelled = TunnelType.PLAIN; if (layered == null) layered = LayerType.PLAIN; this.targetHost = target; this.localAddress = local; this.proxyChain = proxies; this.secure = secure; this.tunnelled = tunnelled; this.layered = layered; }
/** * Determine if the configured Selenium Grid hub server is the local host. * * @return 'true' if Grid hub is local host; otherwise 'false' */ public static boolean isLocalHub() { GridHubConfiguration hubConfig = SeleniumConfig.getConfig().getHubConfig(); HttpHost hubHost = GridUtility.getHubHost(hubConfig); try { InetAddress hubAddr = InetAddress.getByName(hubHost.getHostName()); return (GridUtility.isThisMyIpAddress(hubAddr)); } catch (UnknownHostException e) { LOGGER.warn("Unable to get IP address for '{}'", hubHost.getHostName(), e); return false; } }
public static void testWithProxy(HttpClient httpClient) { HttpHost proxy = new HttpHost("172.16.80.8", 8080); CredentialsProvider credsProvider = new BasicCredentialsProvider(); UsernamePasswordCredentials creds = new UsernamePasswordCredentials("yaoman", "sinochem1"); credsProvider.setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT), creds); httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy); ((DefaultHttpClient) httpClient).setCredentialsProvider(credsProvider); }
public ClientYamlTestClient(ClientYamlSuiteRestSpec restSpec, RestClient restClient, List<HttpHost> hosts, Version esVersion) throws IOException { assert hosts.size() > 0; this.restSpec = restSpec; this.restClient = restClient; this.esVersion = esVersion; }
SlackWebSocketSessionImpl(WebSocketContainerProvider webSocketContainerProvider, String authToken, Proxy.Type proxyType, String proxyAddress, int proxyPort, boolean reconnectOnDisconnection, long heartbeat, TimeUnit unit) { this.authToken = authToken; this.proxyAddress = proxyAddress; this.proxyPort = proxyPort; this.proxyHost = new HttpHost(proxyAddress, proxyPort); this.reconnectOnDisconnection = reconnectOnDisconnection; this.heartbeat = heartbeat != 0 ? unit.toMillis(heartbeat) : DEFAULT_HEARTBEAT_IN_MILLIS; this.webSocketContainerProvider = webSocketContainerProvider != null ? webSocketContainerProvider : new DefaultWebSocketContainerProvider(proxyAddress,proxyPort); addInternalListeners(); }
private HttpHost c() { String g; if (VERSION.SDK_INT >= 11) { g = g(); if (g != null && !g.contains("wap")) { return null; } URL b = b(); if (b == null) { return null; } b.a.equalsIgnoreCase(b.getProtocol()); Object property = System.getProperty("https.proxyHost"); String property2 = System.getProperty("https.proxyPort"); if (TextUtils.isEmpty(property)) { return null; } return new HttpHost(property, Integer.parseInt(property2)); } NetworkInfo f = f(); if (f == null || !f.isAvailable() || f.getType() != 0) { return null; } g = Proxy.getDefaultHost(); int defaultPort = Proxy.getDefaultPort(); if (g != null) { return new HttpHost(g, defaultPort); } return null; }
private HttpResponse a(HttpHost httpHost, HttpRequest httpRequest) throws Exception { try { return this.c.execute(httpHost, httpRequest); } catch (Throwable e) { throw new Exception(e); } }
public void authFailed( final HttpHost authhost, final AuthScheme authScheme, final HttpContext context) { AuthCache authCache = (AuthCache) context.getAttribute(ClientContext.AUTH_CACHE); if (authCache == null) { return; } if (this.log.isDebugEnabled()) { this.log.debug("Removing from cache '" + authScheme.getSchemeName() + "' auth scheme for " + authhost); } authCache.remove(authhost); }
@Override public void startComponent() { if (lowLevelClient == null) { // Create a connection for each contact point. Set<HttpHost> hosts = contactPoints.stream() .map(s -> new HttpHost(s, port)) .collect(Collectors.toSet()); // Initialize the low-level REST client managing the underlying connections to ElasticSearch. lowLevelClient = RestClient.builder(hosts.toArray(new HttpHost[contactPoints.size()])).build(); // Initialize the high-level REST client which sends the actual requests to ElasticSearch. highLevelClient = new RestHighLevelClient(lowLevelClient); LOGGER.info("Initialized connections to ElasticSearch: %s (port %d)", String.join(",", contactPoints), port); } }
@Override protected RestClient client(String benchmarkTargetHost) { return RestClient .builder(new HttpHost(benchmarkTargetHost, 9200)) .setHttpClientConfigCallback(b -> b.setDefaultHeaders( Collections.singleton(new BasicHeader(HttpHeaders.ACCEPT_ENCODING, "gzip")))) .setRequestConfigCallback(b -> b.setContentCompressionEnabled(true)) .build(); }
public void opening(Socket sock, HttpHost target) throws IOException { assertNotOpen(); this.socket = sock; this.targetHost = target; // Check for shutdown after assigning socket, so that if (this.shutdown) { sock.close(); // allow this to throw... // ...but if it doesn't, explicitly throw one ourselves. throw new InterruptedIOException("Connection already shutdown"); } }
public <T> T execute( final HttpUriRequest request, final ResponseHandler<? extends T> responseHandler, final HttpContext context) throws IOException, ClientProtocolException { HttpHost target = determineTarget(request); return execute(target, request, responseHandler, context); }
private RestClient getClient(String appid) { try { return RestClient.builder(new HttpHost(esHost, esPort, esScheme)). // We prefix path with appid in order to route request to the correct index // for a particular app. Also, append '/' to prevent other mishap. setPathPrefix(appid.concat("/")).build(); } catch (Exception e) { logger.error("Failed to build Elasticsearch client for app '{}': {}", appid, e.getMessage()); return null; } }
public void testPerformRequestOnResponseExceptionWithIgnores() throws IOException { MainRequest mainRequest = new MainRequest(); CheckedFunction<MainRequest, Request, IOException> requestConverter = request -> new Request("GET", "/", Collections.emptyMap(), null); HttpResponse httpResponse = new BasicHttpResponse(newStatusLine(RestStatus.NOT_FOUND)); Response mockResponse = new Response(REQUEST_LINE, new HttpHost("localhost", 9200), httpResponse); ResponseException responseException = new ResponseException(mockResponse); when(restClient.performRequest(anyString(), anyString(), anyMapOf(String.class, String.class), anyObject(), anyVararg())).thenThrow(responseException); //although we got an exception, we turn it into a successful response because the status code was provided among ignores assertEquals(Integer.valueOf(404), restHighLevelClient.performRequest(mainRequest, requestConverter, response -> response.getStatusLine().getStatusCode(), Collections.singleton(404))); }