List of usage examples for org.apache.http.impl.client CloseableHttpClient execute
public <T> T execute(final HttpUriRequest request, final ResponseHandler<? extends T> responseHandler) throws IOException, ClientProtocolException
From source file:it.govpay.web.console.utils.HttpClientUtils.java
public static HttpResponse sendRichiestaPagamento(String urlToInvoke, RichiestaPagamento richiestaPagamento, Logger log) throws Exception { HttpResponse responseGET = null;//from ww w . j ava2s.co m try { log.debug("Invio del pagamento in corso..."); URL urlObj = new URL(urlToInvoke); HttpHost target = new HttpHost(urlObj.getHost(), urlObj.getPort(), urlObj.getProtocol()); CloseableHttpClient client = HttpClientBuilder.create().disableRedirectHandling().build(); HttpPost richiestaPost = new HttpPost(); richiestaPost.setURI(urlObj.toURI()); log.debug("Serializzazione pagamento in corso..."); byte[] bufferPagamento = JaxbUtils.toBytes(richiestaPagamento); log.debug("Serializzazione pagamento completata."); HttpEntity bodyEntity = new InputStreamEntity(new ByteArrayInputStream(bufferPagamento), ContentType.APPLICATION_XML); richiestaPost.setEntity(bodyEntity); richiestaPost.setHeader("Content-Type", ContentType.APPLICATION_XML.getMimeType()); log.debug("Invio tramite client Http in corso..."); responseGET = client.execute(target, richiestaPost); if (responseGET == null) throw new NullPointerException("La Response HTTP e' null"); log.debug("Invio tramite client Http completato."); return responseGET; } catch (Exception e) { log.error("Errore durante l'invio della richiesta di pagamento: " + e.getMessage(), e); throw e; } }
From source file:io.fabric8.maven.support.Apps.java
/** * Posts a file to the git repository//from www . java 2 s.com */ public static HttpResponse postFileToGit(File file, String user, String password, String consoleUrl, String branch, String path, Logger logger) throws URISyntaxException, IOException { HttpClientBuilder builder = HttpClients.custom(); if (Strings.isNotBlank(user) && Strings.isNotBlank(password)) { CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials(new AuthScope("localhost", 443), new UsernamePasswordCredentials(user, password)); builder = builder.setDefaultCredentialsProvider(credsProvider); } CloseableHttpClient client = builder.build(); try { String url = consoleUrl; if (!url.endsWith("/")) { url += "/"; } url += "git/"; url += branch; if (!path.startsWith("/")) { url += "/"; } url += path; logger.info("Posting App Zip " + file.getName() + " to " + url); URI buildUrl = new URI(url); HttpPost post = new HttpPost(buildUrl); // use multi part entity format FileBody zip = new FileBody(file); HttpEntity entity = MultipartEntityBuilder.create().addPart(file.getName(), zip).build(); post.setEntity(entity); // post.setEntity(new FileEntity(file)); HttpResponse response = client.execute(URIUtils.extractHost(buildUrl), post); logger.info("Response: " + response); if (response != null) { int statusCode = response.getStatusLine().getStatusCode(); if (statusCode < 200 || statusCode >= 300) { throw new IllegalStateException("Failed to post App Zip to: " + url + " " + response); } } return response; } finally { Closeables.closeQuietly(client); } }
From source file:com.dnastack.bob.service.processor.util.HttpUtils.java
/** * Executes GET/POST and obtain the response. * * @param request request/*ww w.j a va2 s . co m*/ * * @return response */ public static String executeRequest(HttpRequestBase request) { String response = null; CloseableHttpClient httpclient = HttpClients.createDefault(); try { ResponseHandler<String> responseHandler = new ResponseHandler<String>() { @Override public String handleResponse(final HttpResponse response) throws ClientProtocolException, IOException { int status = response.getStatusLine().getStatusCode(); if (status >= 200 && status < 300) { HttpEntity entity = response.getEntity(); return entity != null ? EntityUtils.toString(entity) : null; } else { throw new ClientProtocolException("Unexpected response status: " + status); } } }; response = httpclient.execute(request, responseHandler); } catch (IOException ex) { // ignore, response already set to null } finally { try { httpclient.close(); } catch (IOException ex) { // ignore } } return response; }
From source file:com.javaquery.aws.elasticsearch.DeleteExample.java
/** * Perform delete request.// w w w .ja v a 2 s.c om * @param httpDelete */ public static void httpDeleteRequest(HttpDelete httpDelete) { /* Create object of CloseableHttpClient */ CloseableHttpClient httpClient = HttpClients.createDefault(); /* Response handler for after request execution */ ResponseHandler<String> responseHandler = new ResponseHandler<String>() { @Override public String handleResponse(HttpResponse response) throws ClientProtocolException, IOException { /* Get status code */ int status = response.getStatusLine().getStatusCode(); if (status >= 200 && status < 300) { /* Convert response to String */ HttpEntity entity = response.getEntity(); return entity != null ? EntityUtils.toString(entity) : null; } else { throw new ClientProtocolException("Unexpected response status: " + status); } } }; try { /* Execute URL and attach after execution response handler */ String strResponse = httpClient.execute(httpDelete, responseHandler); /* Print the response */ System.out.println("Response: " + strResponse); } catch (Exception e) { e.printStackTrace(); } }
From source file:com.javaquery.aws.elasticsearch.GetExample.java
/** * Perform get request.// w w w .j a va2 s. com * @param httpGet */ public static void httpGetRequest(HttpGet httpGet) { /* Create object of CloseableHttpClient */ CloseableHttpClient httpClient = HttpClients.createDefault(); /* Response handler for after request execution */ ResponseHandler<String> responseHandler = new ResponseHandler<String>() { @Override public String handleResponse(HttpResponse response) throws ClientProtocolException, IOException { /* Get status code */ int status = response.getStatusLine().getStatusCode(); if (status >= 200 && status < 300) { /* Convert response to String */ HttpEntity entity = response.getEntity(); return entity != null ? EntityUtils.toString(entity) : null; } else { throw new ClientProtocolException("Unexpected response status: " + status); } } }; try { /* Execute URL and attach after execution response handler */ String strResponse = httpClient.execute(httpGet, responseHandler); /* Print the response */ System.out.println("Response: " + strResponse); } catch (Exception e) { e.printStackTrace(); } }
From source file:com.javaquery.aws.elasticsearch.AddUpdateExample.java
/** * Perform post request./* ww w .j a v a 2 s . c o m*/ * @param httpPost */ public static void httpPostRequest(HttpPost httpPost) { /* Create object of CloseableHttpClient */ CloseableHttpClient httpClient = HttpClients.createDefault(); /* Response handler for after request execution */ ResponseHandler<String> responseHandler = new ResponseHandler<String>() { @Override public String handleResponse(HttpResponse response) throws ClientProtocolException, IOException { /* Get status code */ int status = response.getStatusLine().getStatusCode(); if (status >= 200 && status < 300) { /* Convert response to String */ HttpEntity entity = response.getEntity(); return entity != null ? EntityUtils.toString(entity) : null; } else { throw new ClientProtocolException("Unexpected response status: " + status); } } }; try { /* Execute URL and attach after execution response handler */ String strResponse = httpClient.execute(httpPost, responseHandler); /* Print the response */ System.out.println("Response: " + strResponse); } catch (Exception e) { e.printStackTrace(); } }
From source file:com.lifetime.util.TimeOffUtil.java
private static List<RepliconTimeOff> getTimeOffInfo(int page, int pageSize) throws IOException, ParseException { String url = "https://na5.replicon.com/liferay/services/" + "TimeOffListService1.svc/GetData"; CloseableHttpClient httpClient = HttpClients.createDefault(); CredentialsProvider provider = new BasicCredentialsProvider(); UsernamePasswordCredentials credential = new UsernamePasswordCredentials(COMPANY_NAME + '\\' + USERNAME, PASSWORD);/*from w ww.j a v a 2s . c o m*/ provider.setCredentials(AuthScope.ANY, credential); HttpClientContext localContext = HttpClientContext.create(); localContext.setCredentialsProvider(provider); HttpPost httpRequest = new HttpPost(url); HttpEntity requestEntity = getTimeoffRequestEntity(page, pageSize); if (requestEntity != null) { httpRequest.setHeader("Content-type", "application/json"); httpRequest.setEntity(requestEntity); } CloseableHttpResponse response = httpClient.execute(httpRequest, localContext); HttpEntity entity = response.getEntity(); return createTimeOffList(EntityUtils.toString(entity)); }
From source file:org.pepstock.jem.commands.util.HttpUtil.java
/** * Calls a http node of JEM to get group anme of Hazelcast cluster, * necessary to client to connect to JEM. * //from w w w. j a v a 2 s . c om * @param url http URL to call * @return group name of Hazelcast cluster * @throws SubmitException if errors occur */ public static String getGroupName(String url) throws SubmitException { // creates a HTTP client CloseableHttpClient httpclient = null; try { httpclient = createHttpClient(url); // concats URL with query string String completeUrl = url + HttpUtil.NAME_QUERY_STRING; // prepares GET request and basic response handler HttpGet httpget = new HttpGet(completeUrl); ResponseHandler<String> responseHandler = new BasicResponseHandler(); // executes and no parsing // result must be only a string String responseBody = httpclient.execute(httpget, responseHandler); return responseBody.trim(); } catch (KeyManagementException e) { throw new SubmitException(SubmitMessage.JEMW002E, e); } catch (UnrecoverableKeyException e) { throw new SubmitException(SubmitMessage.JEMW002E, e); } catch (NoSuchAlgorithmException e) { throw new SubmitException(SubmitMessage.JEMW002E, e); } catch (KeyStoreException e) { throw new SubmitException(SubmitMessage.JEMW002E, e); } catch (URISyntaxException e) { throw new SubmitException(SubmitMessage.JEMW002E, e); } catch (ClientProtocolException e) { throw new SubmitException(SubmitMessage.JEMW002E, e); } catch (IOException e) { throw new SubmitException(SubmitMessage.JEMW002E, e); } finally { // close http client if (httpclient != null) { try { httpclient.close(); } catch (IOException e) { LogAppl.getInstance().ignore(e.getMessage(), e); } } } }
From source file:org.mobile.mpos.util.HttpClientHelper.java
/** * get request/*from w w w.j av a 2s . co m*/ * @param uri * @return */ public static String get(String uri) { CloseableHttpClient httpClient = HttpClients.createDefault(); try { HttpGet httpget = new HttpGet(uri); log.info("Executing request " + httpget.getRequestLine()); // Create a custom response handler ResponseHandler<String> responseHandler = new ResponseHandler<String>() { public String handleResponse(final HttpResponse response) throws ClientProtocolException, IOException { int status = response.getStatusLine().getStatusCode(); if (status >= 200 && status < 300) { HttpEntity entity = response.getEntity(); return entity != null ? EntityUtils.toString(entity) : null; } else { throw new ClientProtocolException("Unexpected response status: " + status); } } }; String responseBody = httpClient.execute(httpget, responseHandler); log.info(" response " + responseBody); return responseBody; } catch (ClientProtocolException e) { log.error("ClientProtocolException " + e.getMessage()); } catch (IOException e) { log.error("IOException " + e.getMessage()); } finally { close(httpClient); } return null; }
From source file:com.optimizely.ab.OptimizelyHttpClientTest.java
@Test public void testExecute() throws IOException { HttpUriRequest httpUriRequest = RequestBuilder.get().build(); ResponseHandler<Boolean> responseHandler = response -> null; CloseableHttpClient mockHttpClient = mock(CloseableHttpClient.class); when(mockHttpClient.execute(httpUriRequest, responseHandler)).thenReturn(true); OptimizelyHttpClient optimizelyHttpClient = new OptimizelyHttpClient(mockHttpClient); assertTrue(optimizelyHttpClient.execute(httpUriRequest, responseHandler)); }