Java Code Examples for org.apache.http.HttpHost

The following examples show how to use org.apache.http.HttpHost. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source Project: jframe   Author: dzh   File: JfDemoESPlugin.java    License: Apache License 2.0 8 votes vote down vote up
private void startRestClient() {
    try {
        HttpHost[] hosts = new HttpHost[] {
                // new HttpHost("10.132.161.173", 30002, "http")
                new HttpHost("127.0.0.1", 9200, "http") };
        client = RestClient.builder(hosts).setRequestConfigCallback(new RestClientBuilder.RequestConfigCallback() {
            @Override
            public RequestConfig.Builder customizeRequestConfig(RequestConfig.Builder requestConfigBuilder) {
                return requestConfigBuilder.setConnectTimeout(2000).setSocketTimeout(10000);
            }
        }).setMaxRetryTimeoutMillis(10000).setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
            @Override
            public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
                return httpClientBuilder.setMaxConnPerRoute(100).setMaxConnTotal(200);
                // return httpClientBuilder
                // .setDefaultIOReactorConfig(IOReactorConfig.custom().setIoThreadCount(1).build());
            }
        }).build();
    } catch (Exception e) {
        LOG.error(e.getMessage(), e.fillInStackTrace());
    }
}
 
Example #2
Source Project: verigreen   Author: Verigreen   File: PreemptiveAuth.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void process(HttpRequest request, HttpContext context) throws HttpException, IOException {
    AuthState authState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE);
    Credentials creds;
    
    if (authState.getAuthScheme() == null) {
        AuthScheme authScheme = (AuthScheme) context.getAttribute("preemptive-auth");
        CredentialsProvider credsProvider =
                (CredentialsProvider) context.getAttribute(ClientContext.CREDS_PROVIDER);
        HttpHost targetHost =
                (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
        if (authScheme != null) {
            creds =
                    credsProvider.getCredentials(new AuthScope(
                            targetHost.getHostName(),
                            targetHost.getPort()));
            if (creds == null) {
                throw new HttpException("No credentials for preemptive authentication");
            }
            authState.update(authScheme, creds);
        }
    }
}
 
Example #3
Source Project: sana.mobile   Author: SanaMobile   File: HttpDispatcher.java    License: BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public static HttpDispatcher getInstance(String uri, Credentials credentials) {

        HttpDispatcher dispatcher = new HttpDispatcher(new HttpHost(uri),
                new BasicHttpContext());

        CredentialsProvider credsProvider = new BasicCredentialsProvider();

        AuthScope authScope = new AuthScope(
                dispatcher.host.getHostName(),
                dispatcher.host.getPort());

        credsProvider.setCredentials(authScope, credentials);

        ((DefaultHttpClient) dispatcher.client).getCredentialsProvider().setCredentials(
                authScope, credentials);
        return dispatcher;
    }
 
Example #4
Source Project: Quicksql   Author: Qihoo360   File: ElasticsearchCollector.java    License: MIT License 6 votes vote down vote up
private static RestClient connect(Map<String, Integer> coordinates,
    Map<String, String> userConfig) {
    Objects.requireNonNull(coordinates, "coordinates");
    Preconditions.checkArgument(! coordinates.isEmpty(), "no ES coordinates specified");
    final Set<HttpHost> set = new LinkedHashSet<>();
    for (Map.Entry<String, Integer> entry : coordinates.entrySet()) {
        set.add(new HttpHost(entry.getKey(), entry.getValue()));
    }

    final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
    credentialsProvider.setCredentials(AuthScope.ANY,
        new UsernamePasswordCredentials(userConfig.getOrDefault("esUser", "none"),
            userConfig.getOrDefault("esPass", "none")));

    return RestClient.builder(set.toArray(new HttpHost[0]))
        .setHttpClientConfigCallback(httpClientBuilder ->
            httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider))
        .setMaxRetryTimeoutMillis(300000).build();
}
 
Example #5
Source Project: rdf4j   Author: eclipse   File: SPARQLProtocolSession.java    License: BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
protected void setUsernameAndPasswordForUrl(String username, String password, String url) {

		if (username != null && password != null) {
			logger.debug("Setting username '{}' and password for server at {}.", username, url);
			java.net.URI requestURI = java.net.URI.create(url);
			String host = requestURI.getHost();
			int port = requestURI.getPort();
			AuthScope scope = new AuthScope(host, port);
			UsernamePasswordCredentials cred = new UsernamePasswordCredentials(username, password);
			CredentialsProvider credsProvider = new BasicCredentialsProvider();
			credsProvider.setCredentials(scope, cred);
			httpContext.setCredentialsProvider(credsProvider);
			AuthCache authCache = new BasicAuthCache();
			BasicScheme basicAuth = new BasicScheme();
			HttpHost httpHost = new HttpHost(requestURI.getHost(), requestURI.getPort(), requestURI.getScheme());
			authCache.put(httpHost, basicAuth);
			httpContext.setAuthCache(authCache);
		} else {
			httpContext.removeAttribute(HttpClientContext.AUTH_CACHE);
			httpContext.removeAttribute(HttpClientContext.CREDS_PROVIDER);
		}
	}
 
Example #6
Source Project: BUbiNG   Author: LAW-Unimi   File: URLRespectsRobotsTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void test4xxSync() throws Exception {
	proxy = new SimpleFixedHttpProxy();
	URI robotsURL = URI.create("http://foo.bor/robots.txt");
	proxy.addNon200(robotsURL, "HTTP/1.1 404 Not Found\n",
			"# goodguy can do anything\n" +
			"User-agent: goodguy\n" +
			"Disallow:\n\n" +
			"# every other guy can do nothing\n" +
			"User-agent: *\n" +
			"Disallow: /\n"
	);
	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);
	assertEquals(0, URLRespectsRobots.parseRobotsResponse(fetchData, "goodGuy").length);
	assertEquals(0, URLRespectsRobots.parseRobotsResponse(fetchData, "goodGuy foo").length);
}
 
Example #7
Source Project: aliyun-tablestore-java-sdk   Author: aliyun   File: AsyncServiceClient.java    License: Apache License 2.0 6 votes vote down vote up
public <Res> void asyncSendRequest(
    RequestMessage request,
    ExecutionContext context,
    ResponseConsumer<Res> consumer,
    FutureCallback<Res> callback,
    TraceLogger traceLogger)
{
    Preconditions.checkNotNull(request);
    Preconditions.checkNotNull(context);

    addExtraHeaders(request);

    context.getSigner().sign(request);
    handleRequest(request, context.getResquestHandlers());
    consumer.setContext(context);
    final HttpHost target = request.getActionUri().getHost();
    if (LOG.isDebugEnabled()) {
        LOG.debug(TRACE_ID_WITH_COLON + traceLogger.getTraceId() + DELIMITER + INTO_HTTP_ASYNC_CLIENT);
    }
    traceLogger.addEventTime(INTO_HTTP_ASYNC_CLIENT, System.currentTimeMillis());
    httpClient.execute(
        new OTSRequestProducer(target, request.getRequest(), traceLogger),
        consumer, callback);
}
 
Example #8
Source Project: mitmproxy-java   Author: appium   File: MitmproxyJavaTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void NullInterceptorReturnTest() throws InterruptedException, TimeoutException, IOException, UnirestException {
    List<InterceptedMessage> messages = new ArrayList<>();

    MitmproxyJava proxy = new MitmproxyJava(MITMDUMP_PATH, (InterceptedMessage m) -> {
        messages.add(m);
        return null;
    }, 8087, null);
    proxy.start();

    Unirest.setProxy(new HttpHost("localhost", 8087));
    Unirest.get("http://appium.io").header("myTestHeader", "myTestValue").asString();

    proxy.stop();

    assertThat(messages).isNotEmpty();

    final InterceptedMessage firstMessage = messages.get(0);

    assertThat(firstMessage.getRequest().getUrl()).startsWith("http://appium.io");
    assertThat(firstMessage.getRequest().getHeaders()).containsOnlyOnce(new String[]{"myTestHeader", "myTestValue"});
    assertThat(firstMessage.getResponse().getStatusCode()).isEqualTo(200);
}
 
Example #9
Source Project: YiBo   Author: cattong   File: LibRequestDirector.java    License: Apache License 2.0 6 votes vote down vote up
protected void rewriteRequestURI(
        final RequestWrapper request,
        final HttpRoute route) throws ProtocolException {
    try {

        URI uri = request.getURI();
        if (route.getProxyHost() != null && !route.isTunnelled()) {
            // Make sure the request URI is absolute
            if (!uri.isAbsolute()) {
                HttpHost target = route.getTargetHost();
                uri = URIUtils.rewriteURI(uri, target);
                request.setURI(uri);
            }
        } else {
            // Make sure the request URI is relative
            if (uri.isAbsolute()) {
                uri = URIUtils.rewriteURI(uri, null);
                request.setURI(uri);
            }
        }

    } catch (URISyntaxException ex) {
        throw new ProtocolException("Invalid URI: "
        		+ request.getRequestLine().getUri(), ex);
    }
}
 
Example #10
Source Project: docker-client   Author: spotify   File: UnixConnectionSocketFactory.java    License: Apache License 2.0 6 votes vote down vote up
@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 (!(socket instanceof UnixSocket)) {
    throw new AssertionError("Unexpected socket: " + socket);
  }

  socket.setSoTimeout(connectTimeout);
  try {
    socket.getChannel().connect(new UnixSocketAddress(socketFile));
  } catch (SocketTimeoutException e) {
    throw new ConnectTimeoutException(e, null, remoteAddress.getAddress());
  }
  return socket;
}
 
Example #11
Source Project: gerbil   Author: dice-group   File: HttpManagement.java    License: GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * Creates a HttpClientBuilder with the default settings of GERBIL.
 * 
 * @return a HttpClientBuilder with the default settings of GERBIL.
 */
public HttpClientBuilder generateHttpClientBuilder() {
    HttpClientBuilder builder = HttpClientBuilder.create();
    builder.setUserAgent(userAgent);

    String proxyHost = GerbilConfiguration.getInstance().getString(PROXY_HOST_KEY);
    int proxyPort = GerbilConfiguration.getInstance().getInt(PROXY_PORT_KEY, DEFAULT_PROXY_PORT);

    if (proxyHost != null) {
        HttpHost proxy = new HttpHost(proxyHost, proxyPort);
        DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxy);
        builder.setRoutePlanner(routePlanner);
    }
    // Use a redirect strategy that allows the "direct redirect" of POST requests
    // without creating a completely new request
    builder.setRedirectStrategy(new SimpleRedirectStrategy()).build();

    return builder;
}
 
Example #12
Source Project: cosmic   Author: MissionCriticalCloud   File: NiciraNvpApiTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testCreateSecurityProfile() throws Exception {
    final CloseableHttpResponse response = mock(CloseableHttpResponse.class);
    when(response.getStatusLine()).thenReturn(HTTP_201_REPSONSE);
    when(response.getEntity()).thenReturn(new StringEntity(SEC_PROFILE_JSON_RESPONSE));
    final CloseableHttpClient httpClient = spy(HttpClientHelper.createHttpClient(2));
    doReturn(response).when(httpClient).execute(any(HttpHost.class), any(HttpRequest.class), any(HttpClientContext.class));
    final NiciraNvpApi api = buildApi(httpClient);

    final SecurityProfile actualSecProfile = api.createSecurityProfile(new SecurityProfile());

    assertThat("Wrong Uuid in the newly created SecurityProfile", actualSecProfile, hasProperty("uuid", equalTo(UUID)));
    assertThat("Wrong Href in the newly created SecurityProfile", actualSecProfile, hasProperty("href", equalTo(HREF)));
    assertThat("Wrong Schema in the newly created SecurityProfile", actualSecProfile, hasProperty("schema", equalTo(SCHEMA)));
    verify(response, times(1)).close();
    verify(httpClient).execute(any(HttpHost.class), HttpUriRequestMethodMatcher.aMethod("POST"), any(HttpClientContext.class));
    verify(httpClient).execute(any(HttpHost.class), HttpUriRequestPathMatcher.aPath(NiciraConstants.SEC_PROFILE_URI_PREFIX), any(HttpClientContext.class));
}
 
Example #13
Source Project: apigee-android-sdk   Author: apigee   File: CachingHttpClient.java    License: Apache License 2.0 6 votes vote down vote up
private boolean alreadyHaveNewerCacheEntry(HttpHost target,
		HttpRequest request, HttpResponse backendResponse)
		throws IOException {
	HttpCacheEntry existing = null;
	try {
		existing = responseCache.getCacheEntry(target, request);
	} catch (IOException ioe) {
		// nop
	}
	if (existing == null)
		return false;
	Header entryDateHeader = existing.getFirstHeader("Date");
	if (entryDateHeader == null)
		return false;
	Header responseDateHeader = backendResponse.getFirstHeader("Date");
	if (responseDateHeader == null)
		return false;
	try {
		Date entryDate = DateUtils.parseDate(entryDateHeader.getValue());
		Date responseDate = DateUtils.parseDate(responseDateHeader
				.getValue());
		return responseDate.before(entryDate);
	} catch (DateParseException e) {
	}
	return false;
}
 
Example #14
Source Project: elasticsearch   Author: mesos   File: SearchProxyController.java    License: Apache License 2.0 6 votes vote down vote up
@RequestMapping("/_search")
public ResponseEntity<InputStreamResource> search(@RequestParam("q") String query, @RequestHeader(value = "X-ElasticSearch-Host", required = false) String elasticSearchHost) throws IOException {
    HttpHost httpHost = null;
    Collection<Task> tasks = scheduler.getTasks().values();
    Stream<HttpHost> httpHostStream = tasks.stream().map(task -> new HttpHost(task.getHostname(), task.getClientAddress().getPort()));

    if (elasticSearchHost != null) {
        httpHost = httpHostStream.filter(host -> host.toHostString().equalsIgnoreCase(elasticSearchHost)).findAny().get();
    } else {
        httpHost = httpHostStream.skip(RandomUtils.nextInt(0, tasks.size())).findAny().get();
    }

    HttpResponse esSearchResponse = httpClient.execute(httpHost, new HttpGet("/_search?q=" + URLEncoder.encode(query, "UTF-8")));

    InputStreamResource inputStreamResource = new InputStreamResource(esSearchResponse.getEntity().getContent());

    return ResponseEntity.ok()
            .contentLength(esSearchResponse.getEntity().getContentLength())
            .contentType(MediaType.APPLICATION_JSON)
            .header("X-ElasticSearch-host", httpHost.toHostString())
            .body(inputStreamResource);
}
 
Example #15
Source Project: GeoIP2-java   Author: maxmind   File: WebServiceClient.java    License: Apache License 2.0 5 votes vote down vote up
private WebServiceClient(Builder builder) {
    this.host = builder.host;
    this.port = builder.port;
    this.useHttps = builder.useHttps;
    this.locales = builder.locales;
    this.licenseKey = builder.licenseKey;
    this.accountId = builder.accountId;

    mapper = new ObjectMapper();
    mapper.disable(MapperFeature.CAN_OVERRIDE_ACCESS_MODIFIERS);
    mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);

    RequestConfig.Builder configBuilder = RequestConfig.custom()
            .setConnectTimeout(builder.connectTimeout)
            .setSocketTimeout(builder.readTimeout);

    if (builder.proxy != null && builder.proxy != Proxy.NO_PROXY) {
        InetSocketAddress address = (InetSocketAddress) builder.proxy.address();
        HttpHost proxyHost = new HttpHost(address.getHostName(), address.getPort());
        configBuilder.setProxy(proxyHost);
    }

    RequestConfig config = configBuilder.build();
    httpClient =
            HttpClientBuilder.create()
                    .setUserAgent(userAgent())
                    .setDefaultRequestConfig(config).build();
}
 
Example #16
Source Project: JuniperBot   Author: JuniperBot   File: ExYandexMusicAudioSourceManager.java    License: GNU General Public License v3.0 5 votes vote down vote up
@PostConstruct
public void reload() {
    var configuration = workerProperties.getAudio().getYandexProxy();
    if (StringUtils.isNotEmpty(configuration.getHost())) {
        configureApiBuilder(builder -> {
            builder.setProxy(new HttpHost(configuration.getHost(), configuration.getPort()));
        });
    }
}
 
Example #17
Source Project: ibm-cos-sdk-java   Author: IBM   File: SdkProxyRoutePlanner.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected HttpHost determineProxy(
    final HttpHost target,
    final HttpRequest request,
    final HttpContext context) throws HttpException {

    return doesTargetMatchNonProxyHosts(target) ? null : proxy;
}
 
Example #18
Source Project: karate   Author: intuit   File: ProxyServerTest.java    License: MIT License 5 votes vote down vote up
private static int http(HttpUriRequest request) throws Exception {
    CloseableHttpClient client = HttpClients.custom()
            .setProxy(new HttpHost("localhost", proxy.getPort()))
            .build();
    HttpResponse response = client.execute(request);
    InputStream is = response.getEntity().getContent();
    String responseString = FileUtils.toString(is);
    logger.debug("response: {}", responseString);
    return response.getStatusLine().getStatusCode();
}
 
Example #19
Source Project: skywalking   Author: apache   File: ElasticSearchClient.java    License: Apache License 2.0 5 votes vote down vote up
public static List<HttpHost> parseClusterNodes(String protocol, String nodes) {
    List<HttpHost> httpHosts = new LinkedList<>();
    log.info("elasticsearch cluster nodes: {}", nodes);
    List<String> nodesSplit = Splitter.on(",").omitEmptyStrings().splitToList(nodes);

    for (String node : nodesSplit) {
        String host = node.split(":")[0];
        String port = node.split(":")[1];
        httpHosts.add(new HttpHost(host, Integer.parseInt(port), protocol));
    }

    return httpHosts;
}
 
Example #20
Source Project: prerender-java   Author: greengerong   File: PrerenderConfig.java    License: MIT License 5 votes vote down vote up
private HttpClientBuilder configureProxy(HttpClientBuilder builder) {
    final String proxy = config.get("proxy");
    if (isNotBlank(proxy)) {
        final int proxyPort = Integer.parseInt(config.get("proxyPort"));
        DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner(new HttpHost(proxy, proxyPort));
        builder.setRoutePlanner(routePlanner);
    }
    return builder;
}
 
Example #21
Source Project: docker-client   Author: spotify   File: NpipeConnectionSocketFactoryTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testConnectSocket() throws Exception {
  final NamedPipeSocket npipeSocket = mock(NamedPipeSocket.class);
  when(npipeSocket.getChannel()).thenReturn(mock(SocketChannel.class));
  final Socket socket = sut.connectSocket(10, npipeSocket, HttpHost.create("http://foo.com"),
      mock(InetSocketAddress.class), mock(InetSocketAddress.class), mock(HttpContext.class));
  assertThat(socket, IsInstanceOf.instanceOf(NamedPipeSocket.class));
  assertThat((NamedPipeSocket) socket, equalTo(npipeSocket));
}
 
Example #22
Source Project: samza   Author: apache   File: TestApplicationMasterRestClient.java    License: Apache License 2.0 5 votes vote down vote up
private void setupMockClientResponse(int statusCode, String statusReason, String responseBody) throws IOException {
  StatusLine statusLine = mock(StatusLine.class);
  when(statusLine.getStatusCode()).thenReturn(statusCode);
  when(statusLine.getReasonPhrase()).thenReturn(statusReason);

  HttpEntity entity = mock(HttpEntity.class);
  when(entity.getContent()).thenReturn(new ReaderInputStream(new StringReader(responseBody)));

  CloseableHttpResponse response = mock(CloseableHttpResponse.class);
  when(response.getStatusLine()).thenReturn(statusLine);
  when(response.getEntity()).thenReturn(entity);

  when(mockClient.execute(any(HttpHost.class), any(HttpGet.class))).thenReturn(response);
}
 
Example #23
Source Project: ns4_gear_watchdog   Author: newsettle   File: ElasticSearchHighSink.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public synchronized void start() {
    logger.info("start elasticsearch sink......");
    HttpHost[] httpHosts = new HttpHost[serverAddresses.length];
    for (int i = 0; i < serverAddresses.length; i++) {
        String[] hostPort = serverAddresses[i].trim().split(":");
        String host = hostPort[0].trim();
        int port = hostPort.length == 2 ? Integer.parseInt(hostPort[1].trim())
                : DEFAULT_PORT;
        logger.info("elasticsearch host:{},port:{}", host, port);
        httpHosts[i] = new HttpHost(host, port, "http");
    }

    RestClientBuilder builder = RestClient.builder(httpHosts);
    if (StringUtils.isNotBlank(serverUser) || StringUtils.isNotBlank(serverPassword)) {
        final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
        credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(serverUser, serverPassword));
        builder.setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
            @Override
            public HttpAsyncClientBuilder customizeHttpClient(
                    HttpAsyncClientBuilder httpClientBuilder) {
                return httpClientBuilder
                        .setDefaultCredentialsProvider(credentialsProvider);
            }
        });
    }
    client = new RestHighLevelClient(builder);
    sinkCounter.start();
    super.start();
}
 
Example #24
Source Project: cs-actions   Author: CloudSlang   File: ContextBuilder.java    License: Apache License 2.0 5 votes vote down vote up
public HttpClientContext build() {
    if (StringUtils.isEmpty(preemptiveAuth)) {
        preemptiveAuth = "true";
    }
    HttpClientContext context = HttpClientContext.create();
    if (authTypes.size() == 1 && Boolean.parseBoolean(preemptiveAuth) && !authTypes.contains(AuthTypes.ANONYMOUS)) {
        AuthCache authCache = new BasicAuthCache();
        authCache.put(new HttpHost(uri.getHost(), uri.getPort(), uri.getScheme()),
                authSchemeLookup.lookup(authTypes.iterator().next()).create(context));
        context.setCredentialsProvider(credentialsProvider);
        context.setAuthCache(authCache);
    }
    return context;
}
 
Example #25
Source Project: Bastion   Author: bastion-dev   File: TestWithProxiedEmbeddedServer.java    License: GNU General Public License v3.0 5 votes vote down vote up
private static DefaultSchemePortResolver prepareSchemePortResolver() {
    return new DefaultSchemePortResolver() {
        @Override
        public int resolve(HttpHost host) throws UnsupportedSchemeException {
            if (host.getHostName().equalsIgnoreCase("sushi-shop.test")) {
                return 9876;
            } else {
                return super.resolve(host);
            }
        }
    };
}
 
Example #26
Source Project: code   Author: lastwhispers   File: Select.java    License: Apache License 2.0 5 votes vote down vote up
@Before
public void init() {
    //1.连接rest接口
    HttpHost http = new HttpHost("127.0.0.1", 9200, "http");
    RestClientBuilder builder = RestClient.builder(http);//rest构建器
    restHighLevelClient = new
            RestHighLevelClient(builder);//高级客户端对象
    //2.封装查询请求
    searchRequest = new SearchRequest("sku");
    searchRequest.types("doc"); //设置查询的类型
}
 
Example #27
Source Project: BigData-In-Practice   Author: whirlys   File: StatefulHttpClient.java    License: Apache License 2.0 5 votes vote down vote up
public StatefulHttpClient(int sessionTimeOut, int requestTimeOut, HttpHost proxy) {
    initCookieStore();
    this.sessionTimeOut = sessionTimeOut;
    this.requestTimeOut = requestTimeOut;
    RequestConfig.Builder requestConfigBuilder = RequestConfig.custom()
            .setConnectTimeout(this.requestTimeOut * 1000).setSocketTimeout(this.requestTimeOut * 1000);
    if (proxy != null) {
        requestConfigBuilder.setProxy(proxy);
    }
    httpclient = HttpClientBuilder.create()
            .setDefaultRequestConfig(requestConfigBuilder.build()).build();
}
 
Example #28
Source Project: lavaplayer   Author: sedmelluq   File: ExtendedConnectionOperator.java    License: Apache License 2.0 5 votes vote down vote up
private void complementException(
    Throwable exception,
    HttpHost host,
    InetSocketAddress localAddress,
    InetSocketAddress remoteAddress,
    int connectTimeout,
    InetAddress[] addresses,
    int currentIndex
) {
  StringBuilder builder = new StringBuilder();
  builder.append("Encountered when opening a connection with the following details:");

  appendField(builder, "host", host);
  appendField(builder, "localAddress", localAddress);
  appendField(builder, "remoteAddress", remoteAddress);

  builder.append("\n  connectTimeout: ").append(connectTimeout);

  appendAddresses(builder, "triedAddresses", addresses, index ->
      index <= currentIndex && addressTypesMatch(localAddress, addresses[index])
  );

  appendAddresses(builder, "untriedAddresses", addresses, index ->
      index > currentIndex && addressTypesMatch(localAddress, addresses[index])
  );

  appendAddresses(builder, "unsuitableAddresses", addresses, index ->
      !addressTypesMatch(localAddress, addresses[index])
  );

  exception.addSuppressed(new AdditionalDetails(builder.toString()));
}
 
Example #29
Source Project: core-ng-project   Author: neowu   File: ElasticSearchHost.java    License: Apache License 2.0 5 votes vote down vote up
public static HttpHost[] parse(String host) {
    String[] values = Strings.split(host, ',');
    HttpHost[] hosts = new HttpHost[values.length];
    for (int i = 0; i < values.length; i++) {
        String value = values[i].strip();
        hosts[i] = new HttpHost(value, 9200);
    }
    return hosts;
}
 
Example #30
Source Project: jframe   Author: dzh   File: TestHttpClientService.java    License: Apache License 2.0 5 votes vote down vote up
public void testJson() {
    try {
        CloseableHttpClient httpClient = HttpClients.createDefault();

        HttpHost target = new HttpHost("120.27.182.142", 80, HttpHost.DEFAULT_SCHEME_NAME);
        HttpRequestBase request = new HttpPost(target.toURI() + "/mry/usr/qryusr");
        request.addHeader("Api-Token", "76067");

        String data = "";
        // ((HttpPost) request).setEntity(new StringEntity(data,
        // ContentType.create("text/plain", "utf-8")));

        CloseableHttpResponse resp = httpClient.execute(request);
        HttpEntity entity = resp.getEntity();

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        InputStream is = entity.getContent();
        byte[] buf = new byte[32];
        int len = 0;
        while ((len = is.read(buf)) != -1) {
            baos.write(buf, 0, len);
        }
        String str = new String(baos.toByteArray(), "utf-8");
        for (byte b : baos.toByteArray()) {
            System.out.print(b);
            System.out.print(' ');
        }

        Reader reader = new InputStreamReader(entity.getContent(), ContentType.getOrDefault(entity).getCharset());
        // TODO decode by mime-type

        Map<String, String> rspMap = GSON.fromJson(reader, HashMap.class);
        String usrJson = rspMap.get("usr");
        Map<String, Object> usr = GSON.fromJson(usrJson, HashMap.class);
        System.out.println(usr.get("id").toString() + usr.get("name"));
    } catch (Exception e) {
        e.printStackTrace();
    }
}