org.apache.http.impl.client.CloseableHttpClient Java Examples

The following examples show how to use org.apache.http.impl.client.CloseableHttpClient. 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 File: NexusArtifactClient.java    From cubeai with Apache License 2.0 8 votes vote down vote up
public boolean deleteArtifact(String longUrl) {
    try {
        CloseableHttpClient httpClient;
        if (getUserName() != null && getPassword() != null) {
            URL url = new URL(getUrl());
            HttpHost httpHost = new HttpHost(url.getHost(), url.getPort());
            CredentialsProvider credsProvider = new BasicCredentialsProvider();
            credsProvider.setCredentials(new AuthScope(httpHost),
                new UsernamePasswordCredentials(getUserName(), getPassword()));
            httpClient = HttpClientBuilder.create().setDefaultCredentialsProvider(credsProvider).build();
        } else {
            httpClient = HttpClientBuilder.create().build();
        }
        RestTemplate restTemplate = new RestTemplate(new HttpComponentsClientHttpRequestFactory(httpClient));
        restTemplate.delete(new URI(longUrl));
    } catch (Exception e) {
        return false;
    }
    return true;
}
 
Example #2
Source File: HttpUtils.java    From DataLink with Apache License 2.0 7 votes vote down vote up
/**
 * 执行一个HTTP GET请求,返回请求响应的HTML
 *
 * @param url 请求的URL地址
 * @return 返回请求响应的HTML
 * @throws IOException
 */
public static String doGet(String url, Map<String, String> params) throws IOException {
    CloseableHttpClient client = HttpClientBuilder.create().build();
    if(params!=null && params.size()>0) {
        url = url + "?" + map2Str(params);
    }
    HttpGet httpGet = new HttpGet(url);
    try {
        HttpResponse httpResponse = client.execute(httpGet);
        if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
            HttpEntity entity = httpResponse.getEntity();
            return EntityUtils.toString(entity, "utf-8");
        }
    } finally {
        if (client != null) {
            client.close();
        }
    }
    return "";
}
 
Example #3
Source File: HttpClientHelper.java    From tiktok4j with MIT License 6 votes vote down vote up
/**
 * Get HTTP response.
 *
 * @param url
 * @return
 */
public static JsonNode getHttpResponse(String url) {
	try {
		LogHelper.debug("Get : " + url);
		HttpGet httpGet = new HttpGet(url);
		httpGet.setHeader("User-Agent", Configurations.HTTP_CLIENT_USER_AGENT);
		try (CloseableHttpClient client = HttpClientBuilder.create().build();
				CloseableHttpResponse response = client.execute(httpGet)) {
			LogHelper.debug(response);
			LogHelper.debug("code = " + response.getStatusLine().getStatusCode());
			String responseString = EntityUtils.toString(response.getEntity());
			LogHelper.debug("response = " + responseString);
			return JsonHelper.getObjectMapper().readTree(responseString);
		}
	} catch (Exception e) {
		throw new RuntimeException(e);
	}
}
 
Example #4
Source File: HttpUtils.java    From we-cmdb with Apache License 2.0 6 votes vote down vote up
/**
 * get请求
 *
 * @param msgs
 * @param url
 * @return
 * @throws ClientProtocolException
 * @throws UnknownHostException
 * @throws IOException
 */
public static String get(Map<String, Object> msgs, String url)
        throws ClientProtocolException, UnknownHostException, IOException {
    CloseableHttpClient httpClient = HttpClients.createDefault();
    try {
        List<NameValuePair> valuePairs = new ArrayList<NameValuePair>();
        if (null != msgs) {
            for (Entry<String, Object> entry : msgs.entrySet()) {
                if (entry.getValue() != null) {
                    valuePairs.add(new BasicNameValuePair(entry.getKey(),
                            entry.getValue().toString()));
                }
            }
        }
        // EntityUtils.toString(new UrlEncodedFormEntity(valuePairs),
        // CHARSET);
        url = url + "?" + URLEncodedUtils.format(valuePairs, CHARSET_UTF_8);
        HttpGet request = new HttpGet(url);
        CloseableHttpResponse resp = httpClient.execute(request);
        return EntityUtils.toString(resp.getEntity(), CHARSET_UTF_8);
    } finally {
        httpClient.close();
    }
}
 
Example #5
Source File: Qbittorrent.java    From BoxHelper with GNU General Public License v3.0 6 votes vote down vote up
@Override
public boolean removeTorrent(String link, String name) throws IOException {
    if (link == null || "".equals(link) || name == null || "".equals(name)) return true;
    System.out.println("\u001b[37;1m [Info]   \u001b[36m  qBittorrent:\u001b[0m Removing torrent " + name + " ...");
    String hash = null;
    Long size = 0L;
    CloseableHttpClient httpClient = HttpClients.createDefault();
    for (Object torrent: this.allTorrents){
        if (((QbittorrentTorrents) torrent).getName().contains(name) || ((QbittorrentTorrents) torrent).getName().contains(name.replaceAll("\\s", "\\."))){
            hash = ((QbittorrentTorrents) torrent).getHash();
            size = ((QbittorrentTorrents) torrent).getTotal_size();
            break;
        }
    }
    HttpGet httpget = new HttpGet("http://127.0.0.1:" + getPort() + "/api/v2/torrents/delete?hashes=" + hash + "&deleteFiles=true");
    httpget.addHeader("User-Agent", "BoxHelper");
    httpget.addHeader("Host", "127.0.0.1:" + getPort());
    httpget.addHeader("Cookie", "SID=" + this.sessionID);
    httpClient.execute(httpget);
    boolean removed = recheck(hash, name, size, TorrentState.ALL);
    if (removed) System.out.println("\u001b[33;1m [Warning]\u001b[36m  qBittorrent:\u001b[0m " + name + " did not removed. Retry later...");
    else System.out.println("\u001b[37;1m [Info]   \u001b[36m  qBittorrent:\u001b[0m " + name + " successfully removed.");
    httpClient.close();
    return removed;
}
 
Example #6
Source File: HttpClient4Utils.java    From ZTuoExchange_framework with MIT License 6 votes vote down vote up
/**
 * 实例化HttpClient
 *
 * @param maxTotal
 * @param maxPerRoute
 * @param socketTimeout
 * @param connectTimeout
 * @param connectionRequestTimeout
 * @return
 */
public static HttpClient createHttpClient(int maxTotal, int maxPerRoute, int socketTimeout, int connectTimeout,
                                          int connectionRequestTimeout) {
    RequestConfig defaultRequestConfig = RequestConfig.custom()
            .setSocketTimeout(socketTimeout)
            .setConnectTimeout(connectTimeout)
            .setConnectionRequestTimeout(connectionRequestTimeout).build();

    PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
    cm.setMaxTotal(maxTotal);
    cm.setDefaultMaxPerRoute(maxPerRoute);
    cm.setValidateAfterInactivity(200); // 一个连接idle超过200ms,再次被使用之前,需要先做validation
    CloseableHttpClient httpClient = HttpClients.custom()
            .setConnectionManager(cm)
            .setConnectionTimeToLive(30, TimeUnit.SECONDS)
            .setRetryHandler(new StandardHttpRequestRetryHandler(3, true)) // 配置出错重试
            .setDefaultRequestConfig(defaultRequestConfig).build();

    startMonitorThread(cm);

    return httpClient;
}
 
Example #7
Source File: HttpUtil.java    From pacbot with Apache License 2.0 6 votes vote down vote up
/**
 * Gets the.
 *
 * @param uri
 *            the uri
 * @return the string
 */
public static String get(String uri ,String bearerToken) throws Exception  {
    HttpGet httpGet = new HttpGet(uri);
    httpGet.addHeader("content-type", "application/json");
    httpGet.addHeader("cache-control", "no-cache");
    if(!Strings.isNullOrEmpty(bearerToken)){
        httpGet.addHeader("Authorization", "Bearer "+bearerToken);
    }
    CloseableHttpClient httpClient = getHttpClient();
    if(httpClient!=null){
        HttpResponse httpResponse;
        try {
           
            httpResponse = httpClient.execute(httpGet);
            if( httpResponse.getStatusLine().getStatusCode()==HttpStatus.SC_UNAUTHORIZED){
                throw new UnAuthorisedException();
            }
            return EntityUtils.toString(httpResponse.getEntity());
        } catch (Exception e) {
            LOGGER.error("Error getting the data " , e);
            throw e;
        }
    }
    return "{}";
}
 
Example #8
Source File: AbstractUnitTest.java    From deprecated-security-ssl with Apache License 2.0 6 votes vote down vote up
protected String executeSimpleRequest(final String request) throws Exception {

        CloseableHttpClient httpClient = null;
        CloseableHttpResponse response = null;
        try {
            httpClient = getHTTPClient();
            response = httpClient.execute(new HttpGet(getHttpServerUri() + "/" + request));

            if (response.getStatusLine().getStatusCode() >= 300) {
                throw new Exception("Statuscode " + response.getStatusLine().getStatusCode()+" - "+response.getStatusLine().getReasonPhrase()+ "-" +IOUtils.toString(response.getEntity().getContent(), StandardCharsets.UTF_8));
            }

            return IOUtils.toString(response.getEntity().getContent(), StandardCharsets.UTF_8);
        } finally {

            if (response != null) {
                response.close();
            }

            if (httpClient != null) {
                httpClient.close();
            }
        }
    }
 
Example #9
Source File: ServerLifeCycleTest.java    From JerryMouse with MIT License 6 votes vote down vote up
@Test(expected = HttpHostConnectException.class)
public void testServerStartAndStop() throws Exception {
    int availablePort = NetUtils.getAvailablePort();
    HttpServer httpSever = new HttpServer("/tmp/static");
    List<Context> contexts = httpSever.getContexts();
    Context context = contexts.get(0);
    context.setPort(availablePort);
    httpSever.start();
    CloseableHttpClient httpclient = HttpClients.createDefault();
    HttpGet httpget = new HttpGet("http://localhost:" + availablePort + "/");
    CloseableHttpResponse response = httpclient.execute(httpget);
    HttpEntity entity = response.getEntity();
    String responseStr = EntityUtils.toString(entity);
    assertEquals("/", responseStr);
    httpSever.destroy();
    httpclient.execute(httpget);
}
 
Example #10
Source File: TikTok4jTikTokImpl.java    From tiktok4j with MIT License 6 votes vote down vote up
/**
 * Get HTTP response.
 *
 * @param url
 * @return
 */
private static JsonNode getHttpResponseAsJson(String url) {
	try {
		LogHelper.debug("Get : " + url);
		HttpGet httpGet = new HttpGet(url);
		httpGet.setHeader("Host", UrlHelper.getHostname(url));
		httpGet.setHeader("X-SS-TC", "0");
		httpGet.setHeader("User-Agent", USER_AGENT);
		httpGet.setHeader("Accept-Encoding", "gzip");
		httpGet.setHeader("Connection", "keep-alive");
		httpGet.setHeader("X-Tt-Token", "");
		httpGet.setHeader("sdk-version", "1");
		httpGet.setHeader("Cookie", "");

		try (CloseableHttpClient client = HttpClientBuilder.create().build();
				CloseableHttpResponse response = client.execute(httpGet)) {
			LogHelper.debug(response);
			LogHelper.debug("code = " + response.getStatusLine().getStatusCode());
			String responseString = EntityUtils.toString(response.getEntity());
			LogHelper.debug("response = " + responseString);
			return JsonHelper.getObjectMapper().readTree(responseString);
		}
	} catch (Exception e) {
		throw new RuntimeException(e);
	}
}
 
Example #11
Source File: CloudfrontAuthorizedHTMLContentDistributionRule.java    From pacbot with Apache License 2.0 6 votes vote down vote up
public boolean isWebSiteHosted(String url) throws Exception {
	HttpGet httpGet = new HttpGet(url);
	httpGet.addHeader("content-type", "text/html");
	CloseableHttpClient httpClient = HttpClientBuilder.create().build();
	if (httpClient != null) {
		HttpResponse httpResponse;
		try {
			httpResponse = httpClient.execute(httpGet);
			if (httpResponse.getStatusLine().getStatusCode() >= 400) {
				return false;
			}
		} catch (Exception e) {
			logger.error("Exception getting from url  :[{}],[{}] ", url, e.getMessage());
			throw e;
		}
	}
	return true;
}
 
Example #12
Source File: NexusArtifactClient.java    From cubeai with Apache License 2.0 6 votes vote down vote up
public boolean deleteArtifact(String longUrl) {
    try {
        CloseableHttpClient httpClient;
        if (getUserName() != null && getPassword() != null) {
            URL url = new URL(getUrl());
            HttpHost httpHost = new HttpHost(url.getHost(), url.getPort());
            CredentialsProvider credsProvider = new BasicCredentialsProvider();
            credsProvider.setCredentials(new AuthScope(httpHost),
                new UsernamePasswordCredentials(getUserName(), getPassword()));
            httpClient = HttpClientBuilder.create().setDefaultCredentialsProvider(credsProvider).build();
        } else {
            httpClient = HttpClientBuilder.create().build();
        }
        RestTemplate restTemplate = new RestTemplate(new HttpComponentsClientHttpRequestFactory(httpClient));
        restTemplate.delete(new URI(longUrl));
    } catch (Exception e) {
        return false;
    }
    return true;
}
 
Example #13
Source File: CommonService.java    From WeEvent with Apache License 2.0 6 votes vote down vote up
public CloseableHttpResponse getCloseResponse(HttpServletRequest req, String newUrl, String jsonString) throws ServletException {
    CloseableHttpResponse closeResponse;
    try {
        log.info("url {}", newUrl);
        CloseableHttpClient client = this.generateHttpClient();
        if (req.getMethod().equals(METHOD_TYPE)) {
            HttpGet get = this.getMethod(newUrl, req);
            closeResponse = client.execute(get);
        } else {
            HttpPost postMethod = this.postMethod(newUrl, req, jsonString);
            closeResponse = client.execute(postMethod);
        }
    } catch (Exception e) {
        log.error("getCloseResponse fail,error:{}", e.getMessage());
        throw new ServletException(e.getMessage());
    }
    return closeResponse;
}
 
Example #14
Source File: PluginManagerTest.java    From plugin-installation-manager-tool with MIT License 6 votes vote down vote up
@Test
public void checkVersionSpecificUpdateCenterBadRequestTest() throws Exception {
    pm.setJenkinsVersion(new VersionNumber("2.176"));

    mockStatic(HttpClients.class);
    CloseableHttpClient httpclient = mock(CloseableHttpClient.class);

    when(HttpClients.createSystem()).thenReturn(httpclient);
    HttpHead httphead = mock(HttpHead.class);

    whenNew(HttpHead.class).withAnyArguments().thenReturn(httphead);
    CloseableHttpResponse response = mock(CloseableHttpResponse.class);
    when(httpclient.execute(httphead)).thenReturn(response);

    StatusLine statusLine = mock(StatusLine.class);
    when(response.getStatusLine()).thenReturn(statusLine);

    int statusCode = HttpStatus.SC_BAD_REQUEST;
    when(statusLine.getStatusCode()).thenReturn(statusCode);

    pm.checkAndSetLatestUpdateCenter();
    String expected = cfg.getJenkinsUc().toString();
    assertEquals(expected, pm.getJenkinsUCLatest());
}
 
Example #15
Source File: PluginManagerTest.java    From plugin-installation-manager-tool with MIT License 6 votes vote down vote up
@Test
public void checkVersionSpecificUpdateCenterTest() throws Exception {
    //Test where version specific update center exists
    pm.setJenkinsVersion(new VersionNumber("2.176"));

    mockStatic(HttpClients.class);
    CloseableHttpClient httpclient = mock(CloseableHttpClient.class);

    when(HttpClients.createSystem()).thenReturn(httpclient);
    HttpHead httphead = mock(HttpHead.class);

    whenNew(HttpHead.class).withAnyArguments().thenReturn(httphead);
    CloseableHttpResponse response = mock(CloseableHttpResponse.class);
    when(httpclient.execute(httphead)).thenReturn(response);

    StatusLine statusLine = mock(StatusLine.class);
    when(response.getStatusLine()).thenReturn(statusLine);

    int statusCode = HttpStatus.SC_OK;
    when(statusLine.getStatusCode()).thenReturn(statusCode);

    pm.checkAndSetLatestUpdateCenter();

    String expected = dirName(cfg.getJenkinsUc()) + pm.getJenkinsVersion() + Settings.DEFAULT_UPDATE_CENTER_FILENAME;
    assertEquals(expected, pm.getJenkinsUCLatest());
}
 
Example #16
Source File: JunitExistingRestTest.java    From performance-tests with MIT License 6 votes vote down vote up
/**
 * This test sometimes failed due to GitHub rate limiting settings.
 * The failures should be captured in the reports(CSV and html).
 * This is knowing kept here to test the rate limiting and show
 * how a failed test can be tracked in the log/reports
 */
@Test
public void testGitHubGetApi() throws IOException, InterruptedException {
    CloseableHttpClient httpClient = HttpClients.createDefault();
    HttpGet request = new HttpGet("https://api.github.com//users/octocat");

    // - - - - - - - - - - - - - - - - - - - - - - - - -
    // Add known delay to reflect "responseDelay" value
    // in the CSV report at least more than this number.
    // - - - - - - - - - - - - - - - - - - - - - - - - -
    Thread.sleep(1000);

    HttpResponse response = httpClient.execute(request);
    final String responseBodyActual = IOUtils.toString(response.getEntity().getContent(), "UTF-8");
    System.out.println("### response: \n" + responseBodyActual);

    assertThat(response.getStatusLine().getStatusCode(), CoreMatchers.is(200));
    assertThat(responseBodyActual, CoreMatchers.containsString("\"login\":\"octocat\""));

}
 
Example #17
Source File: HttpUtil.java    From pacbot with Apache License 2.0 6 votes vote down vote up
public static String httpGetMethodWithHeaders(String url,Map<String, Object> headers) throws Exception {
    String json = null;
    
    HttpGet get = new HttpGet(url);
    CloseableHttpClient httpClient = null;
    if (headers != null && !headers.isEmpty()) {
        for (Map.Entry<String, Object> entry : headers.entrySet()) {
            get.setHeader(entry.getKey(), entry.getValue().toString());
        }
    }
    try {
        httpClient = getHttpClient();
        CloseableHttpResponse res = httpClient.execute(get);
        if (res.getStatusLine().getStatusCode() == 200) {
            json = EntityUtils.toString(res.getEntity());
        }
    } finally {
        if (httpClient != null) {
            httpClient.close();
        }
    }
    return json;
}
 
Example #18
Source File: PipelineTest.java    From JerryMouse with MIT License 6 votes vote down vote up
@Test
public void testContentLengthOutBound() throws Exception {
    int availablePort = NetUtils.getAvailablePort();
    HttpServer httpSever = new HttpServer("/tmp/static");
    List<Context> contexts = httpSever.getContexts();
    Context context = contexts.get(0);
    context.setPort(availablePort);
    context.init();
    context.start();
    ServletContext servletContext = context.getServletContext();
    BoundPipeline rootBoundPipeline = new RootBoundPipeline(servletContext);
    BoundPipeline contentOutBoundPipeline = new TestContentOutBoundPipeline(servletContext);
    rootBoundPipeline.setNext(contentOutBoundPipeline);
    servletContext.setOutboundPipeline(rootBoundPipeline);
    CloseableHttpClient httpclient = HttpClients.createDefault();
    HttpGet httpget = new HttpGet("http://localhost:" + availablePort + "/hello");
    CloseableHttpResponse response = httpclient.execute(httpget);
    HttpEntity entity = response.getEntity();
    String responseStr = EntityUtils.toString(entity);
    assertEquals("hel", responseStr);
}
 
Example #19
Source File: HttpUtil.java    From anyline with Apache License 2.0 6 votes vote down vote up
public static HttpResult put(CloseableHttpClient client, Map<String, String> headers, String url, String encode,  List<HttpEntity> entitys) { 
	if(null == client){ 
		if(url.contains("https://")){ 
			client = defaultSSLClient(); 
		}else{ 
			client =  defaultClient(); 
		} 
	} 
	if(url.startsWith("//")){ 
		url = "http:" + url; 
	} 
	HttpResult result = new HttpResult(); 
	HttpPut method = new HttpPut(url); 
	if(null != entitys){ 
		for(HttpEntity entity:entitys){ 
			method.setEntity(entity); 
		} 
	} 
	setHeader(method, headers); 
	result = exe(client, method, encode); 
	return result; 
}
 
Example #20
Source File: HTTPClientBuilderTest.java    From AuTe-Framework with Apache License 2.0 5 votes vote down vote up
@Test
public void testWithAllBuild() {
    CloseableHttpClient closeableHttpClient = new HTTPClientBuilder().withCookiesStore()
            .withGlobalConfig()
            .withSllContext()
            .build();
    assertNotNull(closeableHttpClient);
}
 
Example #21
Source File: OpenTSDBReporterTest.java    From metrics with Apache License 2.0 5 votes vote down vote up
@Test
public void testReportingBaseUriWithPath(@Mocked CloseableHttpClient httpClient,
    @Mocked CloseableHttpResponse closeableHttpResponse, @Mocked StatusLine statusLine)
    throws InterruptedException, IOException {
  new Expectations() {{
    httpClient.execute((HttpUriRequest) any);
    result = closeableHttpResponse;
    closeableHttpResponse.getStatusLine();
    result = statusLine;
    statusLine.getStatusCode();
    result = 200;
  }};

  MetricRegistry registry = new MetricRegistry();
  OpenTSDBReporter reporter = OpenTSDBReporter.builder()
      .withBaseUri(URI.create("http://localhost:4242/some/path/"))
      .build();
  registry.addReporter(reporter);

  Counter counter = registry.counter("counter");
  counter.inc("tag", "value");

  Thread.sleep(3000);

  new Verifications() {{
    HttpUriRequest request;
    httpClient.execute(request = withCapture());
    assertEquals("/some/path/api/v1/put", request.getURI().getPath());
    times = 1;
  }};
}
 
Example #22
Source File: AlexaHttpClient.java    From arcusplatform with Apache License 2.0 5 votes vote down vote up
private ProactiveCreds executeOAuth(List<NameValuePair> args) {
   HttpPost post = createPost(config.getOauthEndpoint(), OAUTH_HEADER);
   List<NameValuePair> form = ImmutableList.<NameValuePair>builder()
      .addAll(args)
      .add(new BasicNameValuePair("client_id", config.getOauthClientId()))
      .add(new BasicNameValuePair("client_secret", config.getOauthClientSecret()))
      .build();
   post.setEntity(new UrlEncodedFormEntity(form, StandardCharsets.UTF_8));

   try(CloseableHttpClient client = httpClient()) {
      HttpEntity entity = null;
      try(CloseableHttpResponse response = client.execute(post)) {
         if(response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
            entity = response.getEntity();
            Map<String, Object> error = JSON.fromJson(EntityUtils.toString(entity, StandardCharsets.UTF_8), ERR_TYPE);
            logger.warn("oauth request failed with {}", error);
            if(StringUtils.equals("invalid_grant", (String) error.get("error"))) {
               logger.warn("disabled skill because the grant is no longer valid");
               throw new SkillDisabledException();
            }
            throw new RuntimeException("oauth http post failed: " + response.getStatusLine());
         }
         return createCreds(response);
      } finally {
         consumeQuietly(entity);
      }
   } catch(IOException e) {
      AlexaMetrics.incOauthFailure();
      throw new RuntimeException(e);
   }
}
 
Example #23
Source File: AbstractSlackProcessor.java    From SkaETL with Apache License 2.0 5 votes vote down vote up
@Override
public void process(K key, V value) {
    try {

        String v;

        if (!StringUtils.isBlank(template)) {
            v = TemplateUtils.getInstance().process(template, getMsg(value));
            v = "{\"text\":\"" + StringEscapeUtils.escapeJson(v) + "\"}";
        } else
            v = "{\"text\":\"" + StringEscapeUtils.escapeJson(buildMsg(value)) + "\"}";

        CloseableHttpClient client = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost(uri);

        StringEntity entity = new StringEntity(v);
        httpPost.setEntity(entity);
        httpPost.setHeader("Accept", "application/json");
        httpPost.setHeader("Content-type", "application/json");

        CloseableHttpResponse response = client.execute(httpPost);
        client.close();

        int code = response.getStatusLine().getStatusCode();
        String reason = response.getStatusLine().getReasonPhrase();

        if (code == 200)
            log.debug("Message sended to Slack key {} value {}", key, value);
        else
            log.error("Error during Slack calls: code {} reason {}", code, reason);

    } catch (Exception ex) {
        log.error("Exception during Slack calls {}", ex.getMessage());
    }
}
 
Example #24
Source File: GitlabServerPullRequestDecorator.java    From sonarqube-community-branch-plugin with GNU General Public License v3.0 5 votes vote down vote up
private void deleteCommitDiscussionNote(String commitDiscussionNoteURL, Map<String, String> headers) throws IOException {
    //https://docs.gitlab.com/ee/api/discussions.html#delete-a-commit-thread-note
    HttpDelete httpDelete = new HttpDelete(commitDiscussionNoteURL);
    for (Map.Entry<String, String> entry : headers.entrySet()) {
        httpDelete.addHeader(entry.getKey(), entry.getValue());
    }

    try (CloseableHttpClient httpClient = HttpClients.createSystem()) {
        LOGGER.info("Deleting {} with headers {}", commitDiscussionNoteURL, headers);

        HttpResponse httpResponse = httpClient.execute(httpDelete);
        validateGitlabResponse(httpResponse, 204, "Commit discussions note deleted");
    }
}
 
Example #25
Source File: BeanConfig.java    From api-layer with Eclipse Public License 2.0 5 votes vote down vote up
@Bean
public HttpClientChooser httpClientChooser(@Qualifier("secureHttpClientWithKeystore")
                                               CloseableHttpClient withCertificate,
                                           @Qualifier("secureHttpClientWithoutKeystore")
                                               CloseableHttpClient withoutCertificate
) {
    return new HttpClientChooser(withoutCertificate, withCertificate);
}
 
Example #26
Source File: JPGHandler.java    From arcusplatform with Apache License 2.0 5 votes vote down vote up
private byte[] postToSnapshotServer(byte[] buf) throws Exception {
   HttpPost post = new HttpPost(snaphostUrl);
   RequestConfig config = RequestConfig.custom()
         .setConnectTimeout(SNAPSHOT_TIMEOUT)
         .setSocketTimeout(SNAPSHOT_TIMEOUT)
         .build();
   post.setHeader(org.apache.http.HttpHeaders.CONTENT_TYPE, "application/octet-stream");
   ByteArrayEntity entity = new ByteArrayEntity(buf, 0, buf.length);
   post.setEntity(entity);
   post.setConfig(config);

   // TODO:  pooling
   CloseableHttpClient client = HttpClients.createDefault();

   try {
      log.debug("sending post to snapshot server to convert iframe to a jpg");
      org.apache.http.HttpResponse response = client.execute(post);
      EntityUtils.consume(entity);
      if(response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
         JPG_REQUEST_TRANSCODE_RESPONSE.inc();
         throw new Exception("Failed to transcode iframe to jpeg, transcoder returned : " + response.getStatusLine().getStatusCode());
      }

      HttpEntity resEntity = response.getEntity();
      ByteBuf resBuffer = Unpooled.wrappedBuffer(EntityUtils.toByteArray(resEntity));
      EntityUtils.consume(resEntity);
      log.debug("got response from snapshot server of length {}", resBuffer.readableBytes());

      byte[] data = new byte[resBuffer.readableBytes()];
      resBuffer.getBytes(resBuffer.readerIndex(), data);
      return data;
   } catch(Exception e) {
      JPG_REQUEST_TRANSCODE_ERROR.inc();
      log.error("failed to convert iframe to snapshot", e);
      throw e;
   } finally {
      client.close();
   }
}
 
Example #27
Source File: HttpClientUtil.java    From youkefu with Apache License 2.0 5 votes vote down vote up
/** 
 * 发送 GET 请求(HTTP),K-V形式 
 * @param url 
 * @param params 
 * @return 
 * @throws IOException 
 */  
public static String doGet(String url, Map<String, Object> params) throws IOException {  
    String apiUrl = url;  
    StringBuffer param = new StringBuffer();  
    int i = 0;  
    for (String key : params.keySet()) {  
        if (i == 0)  
            param.append("?");  
        else  
            param.append("&");  
        param.append(key).append("=").append(params.get(key));  
        i++;  
    }  
    apiUrl += param;  
    String result = null;  
    CloseableHttpClient httpclient = HttpClients.createDefault();  
    try {  
        HttpGet httpPost = new HttpGet(apiUrl);  
        HttpResponse response = httpclient.execute(httpPost);  
  
        HttpEntity entity = response.getEntity();  
        if (entity != null) {  
            InputStream instream = entity.getContent();  
            result = IOUtils.toString(instream, "UTF-8");  
        }  
    } catch (IOException e) {  
        e.printStackTrace();  
    }finally {
    	if(httpclient!=null) {
    		httpclient.close();
    	}
    }
    return result;  
}
 
Example #28
Source File: HttpClientGenerator.java    From plumemo with Apache License 2.0 5 votes vote down vote up
private CloseableHttpClient generateClient(Site site) {
    HttpClientBuilder httpClientBuilder = HttpClients.custom();

    httpClientBuilder.setConnectionManager(connectionManager);
    if (site.getUserAgent() != null) {
        httpClientBuilder.setUserAgent(site.getUserAgent());
    } else {
        httpClientBuilder.setUserAgent("");
    }
    if (site.isUseGzip()) {
        httpClientBuilder.addInterceptorFirst(new HttpRequestInterceptor() {

            @Override
            public void process(
                    HttpRequest request,
                    HttpContext context) throws HttpException, IOException {
                if (!request.containsHeader("Accept-Encoding")) {
                    request.addHeader("Accept-Encoding", "gzip");
                }
            }
        });
    }
    //解决post/redirect/post 302跳转问题
    httpClientBuilder.setRedirectStrategy(new CustomRedirectStrategy());

    SocketConfig.Builder socketConfigBuilder = SocketConfig.custom();
    socketConfigBuilder.setSoKeepAlive(true).setTcpNoDelay(true);
    socketConfigBuilder.setSoTimeout(site.getTimeOut());
    SocketConfig socketConfig = socketConfigBuilder.build();
    httpClientBuilder.setDefaultSocketConfig(socketConfig);
    connectionManager.setDefaultSocketConfig(socketConfig);
    httpClientBuilder.setRetryHandler(new DefaultHttpRequestRetryHandler(site.getRetryTimes(), true));
    generateCookie(httpClientBuilder, site);
    return httpClientBuilder.build();
}
 
Example #29
Source File: HTTPClientBuilder.java    From AuTe-Framework with Apache License 2.0 5 votes vote down vote up
public CloseableHttpClient build() {
  org.apache.http.impl.client.HttpClientBuilder clientBuilder = HttpClients.custom();
  if (cookieStore != null) {
    clientBuilder.setDefaultCookieStore(cookieStore);
  }
  if (sslContext != null) {
    clientBuilder.setSSLContext(sslContext);
  }
  if (globalConfig != null) {
    clientBuilder.setDefaultRequestConfig(globalConfig);
  }

  return clientBuilder.build();
}
 
Example #30
Source File: GatewayRibbonConfig.java    From api-layer with Eclipse Public License 2.0 5 votes vote down vote up
@Bean
@Primary
@Autowired
public RibbonLoadBalancingHttpClient ribbonLoadBalancingHttpClient(
    @Qualifier("httpClientProxy") CloseableHttpClient httpClientProxy,
    IClientConfig config,
    ServerIntrospector serverIntrospector,
    ApimlRibbonRetryFactory retryFactory,
    RibbonLoadBalancerContext ribbonContext
) {
    ApimlRetryableClient client = new ApimlRetryableClient(
        httpClientProxy, config, serverIntrospector, retryFactory);
    client.setRibbonLoadBalancerContext(ribbonContext);
    return client;
}