List of usage examples for org.apache.http.util EntityUtils consumeQuietly
public static void consumeQuietly(HttpEntity httpEntity)
From source file:ai.susi.server.ClientConnection.java
/** * get a redirect for an url: this method shall be called if it is expected that a url * is redirected to another url. This method then discovers the redirect. * @param urlstring/*from w w w . j a va 2 s .com*/ * @param useAuthentication * @return the redirect url for the given urlstring * @throws IOException if the url is not redirected */ public static String getRedirect(String urlstring, boolean useAuthentication) throws IOException { HttpGet get = new HttpGet(urlstring); get.setConfig(RequestConfig.custom().setRedirectsEnabled(false).build()); get.setHeader("User-Agent", USER_AGENT); CloseableHttpClient httpClient = HttpClients.custom() .setConnectionManager(getConnctionManager(useAuthentication)) .setDefaultRequestConfig(defaultRequestConfig).build(); HttpResponse httpResponse = httpClient.execute(get); HttpEntity httpEntity = httpResponse.getEntity(); if (httpEntity != null) { if (httpResponse.getStatusLine().getStatusCode() == 301) { for (Header header : httpResponse.getAllHeaders()) { if (header.getName().equalsIgnoreCase("location")) { EntityUtils.consumeQuietly(httpEntity); return header.getValue(); } } EntityUtils.consumeQuietly(httpEntity); throw new IOException("redirect for " + urlstring + ": no location attribute found"); } else { EntityUtils.consumeQuietly(httpEntity); throw new IOException( "no redirect for " + urlstring + " fail: " + httpResponse.getStatusLine().getStatusCode() + ": " + httpResponse.getStatusLine().getReasonPhrase()); } } else { throw new IOException("client connection to " + urlstring + " fail: no connection"); } }
From source file:io.openvidu.java.client.Session.java
/** * Updates every property of the Session with the current status it has in * OpenVidu Server. This is especially useful for getting the list of active * connections to the Session// w w w .j a v a 2s . c om * ({@link io.openvidu.java.client.Session#getActiveConnections()}) and use * those values to call * {@link io.openvidu.java.client.Session#forceDisconnect(Connection)} or * {@link io.openvidu.java.client.Session#forceUnpublish(Publisher)}. <br> * * To update every Session object owned by OpenVidu object, call * {@link io.openvidu.java.client.OpenVidu#fetch()} * * @return true if the Session status has changed with respect to the server, * false if not. This applies to any property or sub-property of the * object * * @throws OpenViduHttpException * @throws OpenViduJavaClientException */ public boolean fetch() throws OpenViduJavaClientException, OpenViduHttpException { String beforeJSON = this.toJson(); HttpGet request = new HttpGet(OpenVidu.urlOpenViduServer + OpenVidu.API_SESSIONS + "/" + this.sessionId); request.setHeader(HttpHeaders.CONTENT_TYPE, "application/x-www-form-urlencoded"); HttpResponse response; try { response = OpenVidu.httpClient.execute(request); } catch (IOException e) { throw new OpenViduJavaClientException(e.getMessage(), e.getCause()); } try { int statusCode = response.getStatusLine().getStatusCode(); if ((statusCode == org.apache.http.HttpStatus.SC_OK)) { this.resetSessionWithJson(httpResponseToJson(response)); String afterJSON = this.toJson(); boolean hasChanged = !beforeJSON.equals(afterJSON); log.info("Session info fetched for session '{}'. Any change: {}", this.sessionId, hasChanged); return hasChanged; } else { throw new OpenViduHttpException(statusCode); } } finally { EntityUtils.consumeQuietly(response.getEntity()); } }
From source file:com.globalsight.everest.workflowmanager.WfStatePostThread.java
private void doPost(WorkflowStatePosts wfStatePost, JSONObject message) { int num = wfStatePost.getRetryNumber(); CloseableHttpClient httpClient = getHttpClient(); for (int i = 0; i < num; i++) { try {//from w w w . ja v a 2 s .co m String listenerUrl = wfStatePost.getListenerURL(); String secretKey = wfStatePost.getSecretKey(); HttpPost httpPost = new HttpPost(listenerUrl); httpPost.setHeader(HttpHeaders.AUTHORIZATION, secretKey); RequestConfig config = RequestConfig.custom() .setConnectionRequestTimeout(wfStatePost.getTimeoutPeriod() * 1000) .setConnectTimeout(wfStatePost.getTimeoutPeriod() * 1000) .setSocketTimeout(wfStatePost.getTimeoutPeriod() * 1000).build(); httpPost.setConfig(config); StringEntity reqEntity = new StringEntity(message.toString()); reqEntity.setContentEncoding("UTF-8"); reqEntity.setContentType("application/json"); httpPost.setEntity(reqEntity); HttpResponse response = null; try { response = httpClient.execute(httpPost); } catch (Exception e) { s_logger.error("Post workflow transition info error:", e); } finally { if (response != null) { EntityUtils.consumeQuietly(response.getEntity()); } } if (response != null) { int statusCode = response.getStatusLine().getStatusCode(); if (statusCode == 204) { break; } else { logPostFailureInfo(statusCode); if (StringUtils.isNotEmpty(wfStatePost.getNotifyEmail()) && (i == num)) { String recipient = wfStatePost.getNotifyEmail(); long companyId = wfStatePost.getCompanyId(); String[] messageArguments = { wfStatePost.getName(), wfStatePost.getListenerURL(), message.toString() }; ServerProxy.getMailer().sendMailFromAdmin(recipient, messageArguments, MailerConstants.WORKFLOW_STATE_POST_FAILURE_SUBJECT, MailerConstants.WORKFLOW_STATE_POST_FAILURE_MESSAGE, String.valueOf(companyId)); } } } } catch (Exception e) { s_logger.error(e); } } }
From source file:com.cloudbees.tomcat.valves.PrivateAppValveIntegratedTest.java
@Test public void pre_emptive_basic_authentication_scenario() throws IOException { System.out.println("pre_emptive_basic_authentication_scenario"); privateAppValve.setAuthenticationEntryPoint(PrivateAppValve.AuthenticationEntryPoint.BASIC_AUTH); httpClient.getCredentialsProvider().setCredentials( new AuthScope(httpHost.getHostName(), httpHost.getPort()), new UsernamePasswordCredentials(accessKey, secretKey)); // Create AuthCache instance AuthCache authCache = new BasicAuthCache(); // Generate BASIC scheme object and add it to the local auth cache BasicScheme basicAuth = new BasicScheme(); authCache.put(httpHost, basicAuth);/* w w w .j a v a 2 s . c om*/ // Add AuthCache to the execution context BasicHttpContext localcontext = new BasicHttpContext(); localcontext.setAttribute(ClientContext.AUTH_CACHE, authCache); HttpGet httpget = new HttpGet("/"); for (int i = 0; i < 3; i++) { HttpResponse response = httpClient.execute(httpHost, httpget, localcontext); assertThat(response.getStatusLine().getStatusCode(), equalTo(HttpServletResponse.SC_OK)); assertThat(response.containsHeader("x-response"), is(true)); dumpHttpResponse(response); EntityUtils.consumeQuietly(response.getEntity()); } }
From source file:fi.okm.mpass.shibboleth.attribute.resolver.dc.impl.RestDataConnector.java
/** {@inheritDoc} */ @Nullable//from w w w .jav a2 s. c o m @Override protected Map<String, IdPAttribute> doDataConnectorResolve( @Nonnull final AttributeResolutionContext attributeResolutionContext, @Nonnull final AttributeResolverWorkContext attributeResolverWorkContext) throws ResolutionException { final Map<String, IdPAttribute> attributes = new HashMap<>(); log.debug("Calling {} for resolving attributes", endpointUrl); String authnIdValue = collectSingleAttributeValue( attributeResolverWorkContext.getResolvedIdPAttributeDefinitions(), hookAttribute); log.debug("AuthnID before URL encoding = {}", authnIdValue); if (authnIdValue == null) { log.error("Could not resolve hookAttribute value"); throw new ResolutionException("Could not resolve hookAttribute value"); } try { authnIdValue = URLEncoder.encode(collectSingleAttributeValue( attributeResolverWorkContext.getResolvedIdPAttributeDefinitions(), hookAttribute), "UTF-8"); } catch (UnsupportedEncodingException e) { log.error("Could not use UTF-8 for encoding authnID"); throw new ResolutionException("Could not use UTF-8 for encoding authnID", e); } log.debug("AuthnID after URL encoding = {}", authnIdValue); final String idpIdValue = collectSingleAttributeValue( attributeResolverWorkContext.getResolvedIdPAttributeDefinitions(), idpId); if (StringSupport.trimOrNull(idpIdValue) == null) { log.error("Could not resolve idpId value"); throw new ResolutionException("Could not resolve idpId value"); } final String attributeCallUrl = endpointUrl + "?" + idpIdValue + "=" + authnIdValue; final HttpClient httpClient; try { httpClient = buildClient(); } catch (Exception e) { log.error("Could not build HTTP client, skipping attribute resolution", e); return attributes; } log.debug("Calling URL {}", attributeCallUrl); final HttpContext context = HttpClientContext.create(); final HttpUriRequest getMethod = RequestBuilder.get().setUri(attributeCallUrl) .setHeader("Authorization", "Token " + token).build(); final HttpResponse restResponse; final long timestamp = System.currentTimeMillis(); try { restResponse = httpClient.execute(getMethod, context); } catch (Exception e) { log.error("Could not open connection to REST API, skipping attribute resolution", e); return attributes; } final int status = restResponse.getStatusLine().getStatusCode(); log.info("API call took {} ms, response code {}", System.currentTimeMillis() - timestamp, status); if (log.isTraceEnabled()) { if (restResponse.getAllHeaders() != null) { for (Header header : restResponse.getAllHeaders()) { log.trace("Header {}: {}", header.getName(), header.getValue()); } } } try { final String restResponseStr = EntityUtils.toString(restResponse.getEntity(), "UTF-8"); log.trace("Response {}", restResponseStr); if (status == HttpStatus.SC_OK) { final Gson gson = new Gson(); final UserDTO ecaUser = gson.fromJson(restResponseStr, UserDTO.class); populateAttributes(attributes, ecaUser); log.debug("{} attributes are now populated", attributes.size()); } else { log.warn("No attributes found for session with idpId {}, http status {}", idpIdValue, status); } } catch (Exception e) { log.error("Error in connection to Data API", e); } finally { EntityUtils.consumeQuietly(restResponse.getEntity()); } return attributes; }
From source file:sachin.spider.SpiderConfig.java
private String handleRedirect(String url) { try {//from w w w . j a va 2 s .c om HttpGet httpget = new HttpGet(url); RequestConfig requestConfig = RequestConfig.custom().setRedirectsEnabled(true) .setCircularRedirectsAllowed(true).setRelativeRedirectsAllowed(true) .setConnectionRequestTimeout(getConnectionRequestTimeout()).setSocketTimeout(getSocketTimeout()) .setConnectTimeout(getConnectionTimeout()).build(); httpget.setConfig(requestConfig); HttpClientBuilder builder = HttpClientBuilder.create(); builder.setUserAgent(getUserAgentString()); if (isAuthenticate()) { CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(getUsername(), getPassword())); builder.setDefaultCredentialsProvider(credentialsProvider); } CloseableHttpClient httpclient = builder.build(); HttpClientContext context = HttpClientContext.create(); CloseableHttpResponse response = httpclient.execute(httpget, context); HttpHost target = context.getTargetHost(); List<URI> redirectLocations = context.getRedirectLocations(); URI location = URIUtils.resolve(httpget.getURI(), target, redirectLocations); url = location.toString(); EntityUtils.consumeQuietly(response.getEntity()); HttpClientUtils.closeQuietly(response); } catch (IOException | URISyntaxException ex) { Logger.getLogger(SpiderConfig.class.getName()).log(Level.SEVERE, null, ex); System.err.println(url); } return url; }
From source file:eu.europa.esig.dss.client.http.commons.FileCacheDataLoader.java
@Override public byte[] post(final String urlString, final byte[] content) throws DSSException { final String fileName = ResourceLoader.getNormalizedFileName(urlString); // The length for the InputStreamEntity is needed, because some receivers (on the other side) need this // information. // To determine the length, we cannot read the content-stream up to the end and re-use it afterwards. // This is because, it may not be possible to reset the stream (= go to position 0). // So, the solution is to cache temporarily the complete content data (as we do not expect much here) in a // byte-array. final byte[] digest = DSSUtils.digest(DigestAlgorithm.MD5, content); final String digestHexEncoded = DSSUtils.toHex(digest); final String cacheFileName = fileName + "." + digestHexEncoded; final File file = getCacheFile(cacheFileName); if (file.exists()) { LOG.debug("Cached file was used"); final byte[] byteArray = DSSUtils.toByteArray(file); return byteArray; } else {/*from w w w. j a v a 2 s.c o m*/ LOG.debug("There is no cached file!"); } final byte[] returnedBytes; if (!isNetworkProtocol(urlString)) { final String resourcePath = resourceLoader.getAbsoluteResourceFolder(urlString.trim()); final File fileResource = new File(resourcePath); returnedBytes = DSSUtils.toByteArray(fileResource); return returnedBytes; } HttpPost httpRequest = null; HttpResponse httpResponse = null; try { final URI uri = URI.create(urlString.trim()); httpRequest = new HttpPost(uri); final ByteArrayInputStream bis = new ByteArrayInputStream(content); final HttpEntity httpEntity = new InputStreamEntity(bis, content.length); final HttpEntity requestEntity = new BufferedHttpEntity(httpEntity); httpRequest.setEntity(requestEntity); if (contentType != null) { httpRequest.setHeader(CONTENT_TYPE, contentType); } httpResponse = super.getHttpResponse(httpRequest, urlString); returnedBytes = readHttpResponse(urlString, httpResponse); if (returnedBytes.length != 0) { final File cacheFile = getCacheFile(cacheFileName); DSSUtils.saveToFile(returnedBytes, cacheFile); } } catch (IOException e) { throw new DSSException(e); } finally { if (httpRequest != null) { httpRequest.releaseConnection(); } if (httpResponse != null) { EntityUtils.consumeQuietly(httpResponse.getEntity()); } } return returnedBytes; }
From source file:sachin.spider.WebSpider.java
private void processURL(WebURL curUrl) { curUrl.setProccessed(true);//www .ja va 2s . co m String url = curUrl.getUrl(); HttpGet httpget = new HttpGet(url); RequestConfig requestConfig = getRequestConfigWithRedirectDisabled(); httpget.setConfig(requestConfig); try { HttpClientContext context = HttpClientContext.create(); long startingTime = System.currentTimeMillis(); try (CloseableHttpResponse response = httpclient.execute(httpget, context)) { long endingTime = System.currentTimeMillis(); StatusLine statusLine = response.getStatusLine(); int statusCode = statusLine.getStatusCode(); curUrl.setStatusCode(statusCode); curUrl.setStatusMessage(EnglishReasonPhraseCatalog.INSTANCE.getReason(statusCode, Locale.ENGLISH)); curUrl.setResposneTime(((int) (endingTime - startingTime)) / 1000); curUrl.setHeaders(response.getAllHeaders()); curUrl.setBaseHref(context.getTargetHost().toString()); if (curUrl.getStatusCode() >= 300 && curUrl.getStatusCode() < 400) { handleRedirectedLink(curUrl); } else if (statusCode == 200) { try { processPage(response, curUrl); } catch (Exception ex) { Logger.getLogger(WebSpider.class.getName()).log(Level.SEVERE, null, ex); } } handleLink(curUrl, response, statusCode, EnglishReasonPhraseCatalog.INSTANCE.getReason(statusCode, Locale.ENGLISH)); EntityUtils.consumeQuietly(response.getEntity()); HttpClientUtils.closeQuietly(response); } } catch (Exception ex) { System.out.println(curUrl.getUrl()); curUrl.setErrorMsg(ex.toString()); Logger.getLogger(WebSpider.class.getName()).log(Level.SEVERE, null, ex); } finally { httpget.releaseConnection(); } }
From source file:com.meltmedia.cadmium.cli.DeployCommand.java
public boolean checkWarDeployment(String warName, String url, HttpClient client) throws Exception { HttpGet get = new HttpGet(url + "/" + warName); try {/*from www. j a va2 s . c o m*/ HttpResponse response = client.execute(get); if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { Map<String, Object> responseObj = new Gson().fromJson(EntityUtils.toString(response.getEntity()), new TypeToken<Map<String, Object>>() { }.getType()); if (responseObj.get("errors") != null && responseObj.get("errors") instanceof List) { List<String> errors = (List<String>) responseObj.get("errors"); System.err.println("The following nodes have failed:"); for (String node : errors) { System.err.println(" " + node); } throw new Exception(); } if (!started && (Boolean) responseObj.get("started")) { started = true; System.out.println("The server has began to deploy."); } else if (started && !((Boolean) responseObj.get("started"))) { System.err.println("The server has stopped deploying the new war."); throw new Exception(); } if ((Boolean) responseObj.get("finished")) { return true; } } else { EntityUtils.consumeQuietly(response.getEntity()); System.err.println("Failed to wait for war to deploy."); throw new Exception(); } } finally { get.releaseConnection(); } return false; }
From source file:com.cloudbees.servlet.filters.PrivateAppFilterIntegratedTest.java
@Test public void pre_emptive_basic_authentication_scenario() throws IOException { System.out.println("pre_emptive_basic_authentication_scenario"); privateAppFilter.setAuthenticationEntryPoint(PrivateAppFilter.AuthenticationEntryPoint.BASIC_AUTH); httpClient.getCredentialsProvider().setCredentials( new AuthScope(httpHost.getHostName(), httpHost.getPort()), new UsernamePasswordCredentials(accessKey, secretKey)); // Create AuthCache instance AuthCache authCache = new BasicAuthCache(); // Generate BASIC scheme object and add it to the local auth cache BasicScheme basicAuth = new BasicScheme(); authCache.put(httpHost, basicAuth);//from w w w. ja va 2s . c o m // Add AuthCache to the execution context BasicHttpContext localcontext = new BasicHttpContext(); localcontext.setAttribute(ClientContext.AUTH_CACHE, authCache); HttpGet httpget = new HttpGet("/"); for (int i = 0; i < 3; i++) { HttpResponse response = httpClient.execute(httpHost, httpget, localcontext); assertThat(response.getStatusLine().getStatusCode(), equalTo(HttpServletResponse.SC_OK)); assertThat(response.containsHeader("x-response"), is(true)); dumpHttpResponse(response); EntityUtils.consumeQuietly(response.getEntity()); } }