List of usage examples for org.apache.http.impl.client CloseableHttpClient close
public void close() throws IOException;
From source file:org.eclipse.dirigible.cli.apis.ImportProjectAPI.java
private static void executeRequest(CloseableHttpClient httpClient, HttpPost postRequest) throws IOException, ClientProtocolException { try {/*from w w w . j a v a 2 s . c o m*/ CloseableHttpResponse response = httpClient.execute(postRequest); try { logger.log(Level.INFO, "----------------------------------------"); logger.log(Level.INFO, response.getStatusLine().toString()); HttpEntity resultEntity = response.getEntity(); if (resultEntity != null) { logger.log(Level.INFO, "Response content length: " + resultEntity.getContentLength()); } EntityUtils.consume(resultEntity); } finally { response.close(); } } finally { httpClient.close(); } }
From source file:org.keycloak.testsuite.util.URLAssert.java
public static void assertGetURL(URI url, String accessToken, AssertResponseHandler handler) { CloseableHttpClient httpclient = HttpClients.createDefault(); try {//from w w w. j a va2s . c om HttpGet get = new HttpGet(url); get.setHeader("Authorization", "Bearer " + accessToken); CloseableHttpResponse response = httpclient.execute(get); if (response.getStatusLine().getStatusCode() != 200) { throw new RuntimeException( "Response status error: " + response.getStatusLine().getStatusCode() + ": " + url); } handler.assertResponse(response); } catch (Exception e) { throw new RuntimeException(e); } finally { try { httpclient.close(); } catch (IOException e) { throw new RuntimeException(e); } } }
From source file:org.keycloak.helper.TestsHelper.java
public static boolean testGetWithAuth(String endpoint, String token) throws IOException { CloseableHttpClient client = HttpClientBuilder.create().build(); try {/*from www. j a v a 2 s .c o m*/ HttpGet get = new HttpGet(baseUrl + endpoint); get.addHeader("Authorization", "Bearer " + token); HttpResponse response = client.execute(get); if (response.getStatusLine().getStatusCode() != 200) { return false; } HttpEntity entity = response.getEntity(); InputStream is = entity.getContent(); try { return true; } finally { is.close(); } } finally { client.close(); } }
From source file:org.smartloli.kafka.eagle.common.util.HttpClientUtils.java
/** * Send request by post method./*from ww w . ja va 2 s . com*/ * * @param uri: * http://ip:port/demo */ public static String doPostJson(String uri, String data) { String result = ""; try { CloseableHttpClient client = null; CloseableHttpResponse response = null; try { HttpPost httpPost = new HttpPost(uri); httpPost.setHeader(HTTP.CONTENT_TYPE, "application/json"); httpPost.setEntity(new StringEntity(data, ContentType.create("text/json", "UTF-8"))); client = HttpClients.createDefault(); response = client.execute(httpPost); HttpEntity entity = response.getEntity(); result = EntityUtils.toString(entity); } finally { if (response != null) { response.close(); } if (client != null) { client.close(); } } } catch (Exception e) { e.printStackTrace(); LOG.error("Do post json request has error, msg is " + e.getMessage()); } return result; }
From source file:org.zaproxy.zap.extension.zapwso2jiraplugin.JiraRestClient.java
public static void invokePutMethodWithFile(String auth, String url, String path) { CloseableHttpClient httpclient = HttpClients.createDefault(); HttpPost httppost = new HttpPost(url); httppost.setHeader("X-Atlassian-Token", "nocheck"); httppost.setHeader("Authorization", "Basic " + auth); File fileToUpload = new File(path); FileBody fileBody = new FileBody(fileToUpload); HttpEntity entity = MultipartEntityBuilder.create().addPart("file", fileBody).build(); httppost.setEntity(entity);/*from w w w.j av a 2s.c om*/ CloseableHttpResponse response = null; try { response = httpclient.execute(httppost); } catch (Exception e) { log.error("File upload failed when involing the update method with file "); } finally { try { httpclient.close(); } catch (IOException e) { log.error("Exception occered when closing the connection"); } } }
From source file:co.aurasphere.botmill.fb.internal.util.network.NetworkUtils.java
/** * Sends a request.//from w w w .ja v a 2 s. c o m * * @param request * the request to send * @return response the response. */ private static String send(HttpRequestBase request) { CloseableHttpClient httpClient = HttpClientBuilder.create().build(); logger.debug(request.getRequestLine().toString()); HttpResponse httpResponse = null; String response = null; try { httpResponse = httpClient.execute(request); response = logResponse(httpResponse); } catch (Exception e) { logger.error("Error during HTTP connection to Facebook: ", e); } finally { try { httpClient.close(); } catch (IOException e) { logger.error("Error while closing HTTP connection: ", e); } } return response; }
From source file:com.ibm.ra.remy.web.utils.BusinessRulesUtils.java
/** * Invoke Business Rules with JSON content * * @param content The payload of itineraries to send to Business Rules. * @return A JSON string representing the output of Business Rules. *//*from w w w . java 2 s . c om*/ public static String invokeRulesService(String json) throws Exception { PropertiesReader constants = PropertiesReader.getInstance(); String username = constants.getStringProperty(USERNAME_KEY); String password = constants.getStringProperty(PASSWORD_KEY); String endpoint = constants.getStringProperty(EXECUTION_REST_URL_KEY) + constants.getStringProperty(RULE_APP_PATH_KEY); CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password)); CloseableHttpClient httpClient = HttpClientBuilder.create() .setDefaultCredentialsProvider(credentialsProvider).build(); String responseString = ""; try { HttpPost httpPost = new HttpPost(endpoint); httpPost.setHeader(HTTP.CONTENT_TYPE, ContentType.APPLICATION_JSON.toString()); StringEntity jsonEntity = new StringEntity(json, MessageUtils.ENCODING); httpPost.setEntity(jsonEntity); CloseableHttpResponse response = httpClient.execute(httpPost); try { HttpEntity entity = response.getEntity(); responseString = EntityUtils.toString(entity, MessageUtils.ENCODING); EntityUtils.consume(entity); } finally { response.close(); } } finally { httpClient.close(); } return responseString; }
From source file:com.ibm.CloudResourceBundle.java
private static Rows getServerResponse(CloudDataConnection connect) throws Exception { Rows rows = null;/*from w w w . j av a 2 s . c o m*/ CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials(new AuthScope("provide.castiron.com", 443), new UsernamePasswordCredentials(connect.getUserid(), connect.getPassword())); CloseableHttpClient httpclient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build(); try { // Call the service and get all the strings for all the languages HttpGet httpget = new HttpGet(connect.getURL()); httpget.addHeader("API_SECRET", connect.getSecret()); CloseableHttpResponse response = httpclient.execute(httpget); try { InputStream in = response.getEntity().getContent(); ObjectMapper mapper = new ObjectMapper(); rows = mapper.readValue(new InputStreamReader(in, "UTF-8"), Rows.class); EntityUtils.consume(response.getEntity()); } finally { response.close(); } } finally { httpclient.close(); } return rows; }
From source file:org.wso2.security.tools.zap.ext.zapwso2jiraplugin.JiraRestClient.java
public static void invokePutMethodWithFile(String auth, String url, String path) { CloseableHttpClient httpclient = HttpClients.createDefault(); HttpPost httppost = new HttpPost(url); httppost.setHeader(IssueCreatorConstants.ATLASSIAN_TOKEN, "nocheck"); httppost.setHeader(IssueCreatorConstants.ATLASSIAN_AUTHORIZATION, "Basic " + auth); File fileToUpload = new File(path); FileBody fileBody = new FileBody(fileToUpload); HttpEntity entity = MultipartEntityBuilder.create().addPart("file", fileBody).build(); httppost.setEntity(entity);// ww w.j ava 2 s . co m CloseableHttpResponse response = null; try { response = httpclient.execute(httppost); } catch (Exception e) { log.error("File upload failed when involing the update method with file "); } finally { try { httpclient.close(); } catch (IOException e) { log.error("Exception occured when closing the connection"); } } }
From source file:org.springframework.xd.distributed.util.ServerProcessUtils.java
/** * Block the executing thread until the admin server is responding to * HTTP requests./*from w w w. ja v a2s. co m*/ * * @param url URL for admin server * @throws InterruptedException if the executing thread is interrupted * @throws java.lang.IllegalStateException if a successful connection to the * admin server was not established */ private static void waitForAdminServer(String url) throws InterruptedException, IllegalStateException { boolean connected = false; Exception exception = null; int httpStatus = 0; long expiry = System.currentTimeMillis() + 30000; CloseableHttpClient httpClient = HttpClientBuilder.create().build(); try { do { try { Thread.sleep(100); HttpGet httpGet = new HttpGet(url); httpStatus = httpClient.execute(httpGet).getStatusLine().getStatusCode(); if (httpStatus == HttpStatus.SC_OK) { connected = true; } } catch (IOException e) { exception = e; } } while ((!connected) && System.currentTimeMillis() < expiry); } finally { try { httpClient.close(); } catch (IOException e) { // ignore exception on close } } if (!connected) { StringBuilder builder = new StringBuilder(); builder.append("Failed to connect to '").append(url).append("'"); if (httpStatus > 0) { builder.append("; last HTTP status: ").append(httpStatus); } if (exception != null) { StringWriter writer = new StringWriter(); exception.printStackTrace(new PrintWriter(writer)); builder.append("; exception: ").append(exception.toString()).append(", ").append(writer.toString()); } throw new IllegalStateException(builder.toString()); } }