public PingInfo ping(String host, int restPort, boolean noProxy) throws Exception { PingInfo myPingInfo = pingInfoProvider.createPingInfo(); RequestConfig.Builder requestConfigBuilder = httpClientProvider.createRequestConfigBuilder(host, noProxy); String url = String.format(URL_PATTERN, host, restPort) + PingHandler.URL; HttpUriRequest request = RequestBuilder.post(url) .setConfig(requestConfigBuilder.build()) .addParameter(PingHandler.PING_INFO_INPUT_NAME, gson.toJson(myPingInfo)) .build(); CloseableHttpResponse response = httpClientProvider.executeRequest(request); try { StatusLine statusLine = response.getStatusLine(); if (statusLine.getStatusCode() != HttpStatus.SC_OK) { EntityUtils.consumeQuietly(response.getEntity()); throw new Exception(statusLine.getStatusCode() + " " + statusLine.getReasonPhrase()); } HttpEntity entity = response.getEntity(); String content = EntityUtils.toString(entity); EntityUtils.consumeQuietly(entity); PingInfo receivedPingInfo = gson.fromJson(content, PingInfo.class); receivedPingInfo.getAgentId().setHost(request.getURI().getHost()); return receivedPingInfo; } finally { response.close(); } }
/** * This method retrieves data using HTTP or HTTPS protocol and 'get' method. * * @param url * @return */ protected byte[] httpGet(String url) { HttpGet httpRequest = null; HttpResponse httpResponse = null; try { final URI uri = URI.create(url.trim()); httpRequest = new HttpGet(uri); if (contentType != null) { httpRequest.setHeader(CONTENT_TYPE, contentType); } httpResponse = getHttpResponse(httpRequest, url); final byte[] returnedBytes = readHttpResponse(url, httpResponse); return returnedBytes; } finally { if (httpRequest != null) { httpRequest.releaseConnection(); } if (httpResponse != null) { EntityUtils.consumeQuietly(httpResponse.getEntity()); } } }
@Override public final HttpResponse invoke(Q query, String endpoint) throws InvocationException { Preconditions.checkNotNull(query); Preconditions.checkNotNull(endpoint); HttpRequestBase method = null; HttpEntity response = null; try { method = getHttpMethod(query, endpoint); method.setParams(getHttpClientParams(query)); org.apache.http.HttpResponse httpResponse = httpClient.execute(method); response = httpResponse.getEntity(); return HttpResponse.create( httpResponse.getStatusLine().getStatusCode(), EntityUtils.toString(response)); } catch (Exception e) { if (method != null) { log.debug( "Error during invocation with URL: " + method.getURI() + ", endpoint: " + endpoint + ", query: " + query, e); } else { log.debug("Error during invocation with: endpoint: " + endpoint + ", query: " + query, e); } throw new InvocationException("InvocationException : ", e); } finally { EntityUtils.consumeQuietly(response); } }
@Override public boolean isValidEndPoint(final URL url) { Assert.notNull(this.httpClient); HttpEntity entity = null; try (CloseableHttpResponse response = this.httpClient.execute(new HttpGet(url.toURI()))) { final int responseCode = response.getStatusLine().getStatusCode(); final int idx = Collections.binarySearch(this.acceptableCodes, responseCode); if (idx >= 0) { LOGGER.debug("Response code from server matched {}.", responseCode); return true; } LOGGER.debug( "Response code did not match any of the acceptable response codes. Code returned was {}", responseCode); if (responseCode == HttpStatus.SC_INTERNAL_SERVER_ERROR) { final String value = response.getStatusLine().getReasonPhrase(); LOGGER.error( "There was an error contacting the endpoint: {}; The error was:\n{}", url.toExternalForm(), value); } entity = response.getEntity(); } catch (final Exception e) { LOGGER.error(e.getMessage(), e); } finally { EntityUtils.consumeQuietly(entity); } return false; }
@Test public void testWaitForQuiescenceQuietPeriodAlreadySatisfied() throws IOException, InterruptedException { mockServer .when(request().withMethod("GET").withPath("/quiescencesatisfied"), Times.exactly(1)) .respond(response().withStatusCode(200)); try (CloseableHttpClient client = NewProxyServerTestUtil.getNewHttpClient(proxy.getPort())) { HttpResponse response = client.execute( new HttpGet("http://127.0.0.1:" + mockServerPort + "/quiescencesatisfied")); EntityUtils.consumeQuietly(response.getEntity()); assertEquals( "Expected successful response from server", 200, response.getStatusLine().getStatusCode()); } // wait for 2s, then wait for 1s of quiescence, which should already be satisfied Thread.sleep(2000); long start = System.nanoTime(); boolean waitSuccessful = proxy.waitForQuiescence(1, 5, TimeUnit.SECONDS); long finish = System.nanoTime(); assertTrue("Expected to successfully wait for quiescence", waitSuccessful); assertTrue( "Expected wait for quiescence to return immediately. Actual wait time was: " + TimeUnit.MILLISECONDS.convert(finish - start, TimeUnit.NANOSECONDS) + "ms", TimeUnit.MILLISECONDS.convert(finish - start, TimeUnit.NANOSECONDS) <= 1); }
@Test public void testWaitForQuiescenceAfterRequestCompleted() throws IOException { mockServer .when(request().withMethod("GET").withPath("/quiescencecompleted"), Times.exactly(1)) .respond(response().withStatusCode(200)); try (CloseableHttpClient client = NewProxyServerTestUtil.getNewHttpClient(proxy.getPort())) { HttpResponse response = client.execute( new HttpGet("http://127.0.0.1:" + mockServerPort + "/quiescencecompleted")); EntityUtils.consumeQuietly(response.getEntity()); assertEquals( "Expected successful response from server", 200, response.getStatusLine().getStatusCode()); } // wait for 2s of quiescence, now that the call has already completed long start = System.nanoTime(); boolean waitSuccessful = proxy.waitForQuiescence(2, 5, TimeUnit.SECONDS); long finish = System.nanoTime(); assertTrue("Expected to successfully wait for quiescence", waitSuccessful); assertTrue( "Expected to wait for quiescence for approximately 2s. Actual wait time was: " + TimeUnit.MILLISECONDS.convert(finish - start, TimeUnit.NANOSECONDS) + "ms", TimeUnit.MILLISECONDS.convert(finish - start, TimeUnit.NANOSECONDS) >= 1500 && TimeUnit.MILLISECONDS.convert(finish - start, TimeUnit.NANOSECONDS) <= 2500); }
/** * @param httppost A well-formed {@link HttpUriRequest} * @param owlsim An initialized {@link OWLSim} instance. It is expected that the {@link * OWLGraphWrapper} is already loaded with an ontology. * @throws Exception */ protected void runServerCommunication(HttpUriRequest httppost, OwlSim owlsim) throws Exception { // create server Server server = new Server(9031); server.setHandler(new OWLServer(g, owlsim)); try { server.start(); // create a client HttpClient httpclient = new DefaultHttpClient(); // run request LOG.info("Executing=" + httppost); HttpResponse response = httpclient.execute(httppost); LOG.info("Executed=" + httpclient); // check response HttpEntity entity = response.getEntity(); StatusLine statusLine = response.getStatusLine(); LOG.info("Status=" + statusLine.getStatusCode()); if (statusLine.getStatusCode() == 200) { String responseContent = EntityUtils.toString(entity); handleResponse(responseContent); } else { EntityUtils.consumeQuietly(entity); } } finally { // clean up server.stop(); } }
public static void close(HttpResponse response) { if (response == null) { return; } HttpEntity entity = response.getEntity(); if (entity != null) { EntityUtils.consumeQuietly(entity); } }
private void httpDelete(String url, String warningMessage) { HttpDelete delete = new HttpDelete(url); try { CloseableHttpResponse httpResponse = httpClient.execute(delete); EntityUtils.consumeQuietly(httpResponse.getEntity()); } catch (IOException e) { LOGGER.warn(warningMessage, e); throw new ActivityRequestFailed(warningMessage, e); } }
/** * Quietly closes any {@link HttpEntity} in the provided {@link HttpResponse}, suppressing any * exceptions. Ensures that the entity content is fully consumed and the content stream, if * exists, is closed. Also attempts to close the underlying {@link HttpResponse} entity as well, * if supported. */ public static final void consumeResponseQuietly(final HttpResponse response) { if (response != null) { // Consume the response entity, closing any associated streams. consumeQuietly(response.getEntity()); // If the response itself is an instance of Closable, cannot hurt // to also close the higher order response too in the event that // request pools are waiting for a free connection. if (response instanceof Closeable) { closeQuietly(((Closeable) response)); } } }
private String retrieveUrlContent(final String jenkinsJobsUrl) throws IOException { final HttpUriRequest httpUriRequest = new HttpGet(jenkinsJobsUrl); final HttpResponse httpResponse = httpClient.execute(httpUriRequest, httpContext); final HttpEntity entity = httpResponse.getEntity(); if (entity != null) { final InputStream inputStream = new PushbackInputStream(entity.getContent()); try { final StringWriter writer = new StringWriter(); IOUtils.copy(inputStream, writer, CHARSET); return writer.toString(); } finally { EntityUtils.consumeQuietly(entity); } } return ""; }
public RestTestResponse(Response response) throws IOException { this.response = response; if (response.getEntity() != null) { try { this.body = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8); } catch (IOException e) { EntityUtils.consumeQuietly(response.getEntity()); throw new RuntimeException(e); } finally { IOUtils.closeWhileHandlingException(response); } } else { this.body = null; } parseResponseBody(); }
/** * 执行请求,返回字节 * * @param charset * @param httpUriRequest * @return */ public byte[] execute_byte(HttpUriRequest httpUriRequest, Map<String, String> header) { byte[] data = null; HttpEntity entity = null; try { CloseableHttpResponse httpResponse = httpclient.execute(httpUriRequest); int statusCode = httpResponse.getStatusLine().getStatusCode(); entity = httpResponse.getEntity(); Header heade = entity.getContentType(); if (heade != null) { log.info("statusCode : " + statusCode + " ContentType : " + heade.getValue()); setContent_type(heade.getValue()); } else { log.info("statusCode : " + statusCode + " ContentType : unknown ."); } setStatusCode(statusCode); if (statusCode == 200) { data = EntityUtils.toByteArray(entity); } else if (statusCode == 302 || statusCode == 300 || statusCode == 301) { URL referer = httpUriRequest.getURI().toURL(); httpUriRequest.abort(); Header location = httpResponse.getFirstHeader("Location"); String locationurl = location.getValue(); if (!locationurl.startsWith("http")) { URL u = new URL(referer, locationurl); locationurl = u.toExternalForm(); } data = GetImg(locationurl, header); } else { data = EntityUtils.toByteArray(entity); } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (httpUriRequest != null) { httpUriRequest.abort(); } if (entity != null) { EntityUtils.consumeQuietly(entity); } } return data; }
public HttpRsp _send(String request, String jsonData) { HttpRsp httpRsp = new HttpRsp(); if (null == serverHost || null == client) { httpRsp.status = -1; return httpRsp; } if (StringUtils.isBlank(request)) { return httpRsp; } HttpRequestBase httpRequest; if (StringUtils.isNotBlank(jsonData)) { httpRequest = new HttpPost(request); ((HttpPost) httpRequest).setEntity(new StringEntity(jsonData, Charset.forName("utf-8"))); } else { httpRequest = new HttpGet(request); } try { HttpResponse response = client.execute(serverHost, httpRequest); httpRsp.status = response.getStatusLine().getStatusCode(); if (200 == httpRsp.status) { httpRsp.content = EntityUtils.toString(response.getEntity(), "utf-8"); if (logger.isDebugEnabled()) { logger.debug("Http请求成功 {} {}", request, jsonData); } else { logger.info("Http请求成功 {} {}", request, StringUtils.length(jsonData)); } } EntityUtils.consumeQuietly(response.getEntity()); } catch (Exception e) { logger.error("Http执行异常:" + request, e); httpRsp.status = -1; } finally { httpRequest.releaseConnection(); } return httpRsp; }
public static String getResponseString(HttpResponse httpResponse) throws IOException { BufferedReader br = null; try { br = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent())); String readLine; String response = ""; while (((readLine = br.readLine()) != null)) { response += readLine; } return response; } finally { EntityUtils.consumeQuietly(httpResponse.getEntity()); if (br != null) { try { br.close(); } catch (IOException e) { log.warn("Error while closing the connection! " + e.getMessage()); } } } }
@Override public byte[] post(final String url, final byte[] content) throws DSSException { LOG.debug("Fetching data via POST from url " + url); HttpPost httpRequest = null; HttpResponse httpResponse = null; try { final URI uri = URI.create(url.trim()); httpRequest = new HttpPost(uri); // 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 ByteArrayInputStream bis = new ByteArrayInputStream(content); final HttpEntity requestEntity = new InputStreamEntity(bis, content.length); httpRequest.setEntity(requestEntity); if (contentType != null) { httpRequest.setHeader(CONTENT_TYPE, contentType); } httpResponse = getHttpResponse(httpRequest, url); final byte[] returnedBytes = readHttpResponse(url, httpResponse); return returnedBytes; } finally { if (httpRequest != null) { httpRequest.releaseConnection(); } if (httpResponse != null) { EntityUtils.consumeQuietly(httpResponse.getEntity()); } } }
@Test public void testWaitForQuiescenceTimeoutLessThanQuietPeriodUnuccessful() throws IOException, InterruptedException { mockServer .when( request().withMethod("GET").withPath("/quiescencesmalltimeoutunsuccessful"), Times.exactly(1)) .respond(response().withStatusCode(200)); try (CloseableHttpClient client = NewProxyServerTestUtil.getNewHttpClient(proxy.getPort())) { HttpResponse response = client.execute( new HttpGet( "http://127.0.0.1:" + mockServerPort + "/quiescencesmalltimeoutunsuccessful")); EntityUtils.consumeQuietly(response.getEntity()); assertEquals( "Expected successful response from server", 200, response.getStatusLine().getStatusCode()); } Thread.sleep(1000); // wait for 3s of quiescence within 1s, which should not be possible since the last request just // finished. waitForQuiescence should // be able to detect that and return immediately. long start = System.nanoTime(); boolean waitSuccessful = proxy.waitForQuiescence(3, 1, TimeUnit.SECONDS); long finish = System.nanoTime(); assertFalse("Expected to unsuccessfully wait for quiescence", waitSuccessful); assertTrue( "Expected wait for quiescence to return immediately. Actual wait time was: " + TimeUnit.MILLISECONDS.convert(finish - start, TimeUnit.NANOSECONDS) + "ms", TimeUnit.MILLISECONDS.convert(finish - start, TimeUnit.NANOSECONDS) >= 0 && TimeUnit.MILLISECONDS.convert(finish - start, TimeUnit.NANOSECONDS) <= 10); }
@Test public void testWaitForQuiescenceTimeoutLessThanQuietPeriodSuccessful() throws IOException, InterruptedException { mockServer .when( request().withMethod("GET").withPath("/quiescencesmalltimeoutsuccess"), Times.exactly(1)) .respond(response().withStatusCode(200)); try (CloseableHttpClient client = NewProxyServerTestUtil.getNewHttpClient(proxy.getPort())) { HttpResponse response = client.execute( new HttpGet("http://127.0.0.1:" + mockServerPort + "/quiescencesmalltimeoutsuccess")); EntityUtils.consumeQuietly(response.getEntity()); assertEquals( "Expected successful response from server", 200, response.getStatusLine().getStatusCode()); } Thread.sleep(2500); // wait for 3s of quiescence, which should wait no more than 500ms long start = System.nanoTime(); boolean waitSuccessful = proxy.waitForQuiescence(3, 1, TimeUnit.SECONDS); long finish = System.nanoTime(); assertTrue("Expected to successfully wait for quiescence", waitSuccessful); assertTrue( "Expected to wait for quiescence for approximately 500ms. Actual wait time was: " + TimeUnit.MILLISECONDS.convert(finish - start, TimeUnit.NANOSECONDS) + "ms", TimeUnit.MILLISECONDS.convert(finish - start, TimeUnit.NANOSECONDS) >= 300 && TimeUnit.MILLISECONDS.convert(finish - start, TimeUnit.NANOSECONDS) <= 700); }
@Override public HttpMessage sendMessageToEndPoint(final URL url) { Assert.notNull(this.httpClient); HttpEntity entity = null; try (CloseableHttpResponse response = this.httpClient.execute(new HttpGet(url.toURI()))) { final int responseCode = response.getStatusLine().getStatusCode(); for (final int acceptableCode : this.acceptableCodes) { if (responseCode == acceptableCode) { LOGGER.debug("Response code received from server matched {}.", responseCode); entity = response.getEntity(); final HttpMessage msg = new HttpMessage(url, IOUtils.toString(entity.getContent(), StandardCharsets.UTF_8)); msg.setContentType(entity.getContentType().getValue()); msg.setResponseCode(responseCode); return msg; } } LOGGER.debug( "Response code did not match any of the acceptable response codes. Code returned {}", responseCode); if (responseCode == HttpStatus.SC_INTERNAL_SERVER_ERROR) { final String value = response.getStatusLine().getReasonPhrase(); LOGGER.error( "There was an error contacting the endpoint: {}; The error:\n{}", url.toExternalForm(), value); } } catch (final Exception e) { LOGGER.error(e.getMessage(), e); } finally { EntityUtils.consumeQuietly(entity); } return null; }
@Override protected void init() throws OAuth2Exception { final URI authURI = OAuthClientUtils.getAuthorizationURI( oauth2GrantServiceURI.toASCIIString(), OAuth2Provider.CLIENT_ID, OAuth2Provider.REDIRECT_URI, null, null); // Disable automatic redirects handling final HttpParams params = new BasicHttpParams(); params.setParameter(ClientPNames.HANDLE_REDIRECTS, false); final DefaultHttpClient httpClient = new DefaultHttpClient(params); JsonNode oAuthAuthorizationData = null; String authenticityCookie = null; try { // 1. Need to (basic) authenticate against the OAuth2 service final HttpGet method = new HttpGet(authURI); method.addHeader( "Authorization", "Basic " + Base64.encodeBase64String("odatajclient:odatajclient".getBytes())); final HttpResponse response = httpClient.execute(method); // 2. Pull out OAuth2 authorization data and "authenticity" cookie (CXF specific) oAuthAuthorizationData = new XmlMapper().readTree(EntityUtils.toString(response.getEntity())); final Header setCookieHeader = response.getFirstHeader("Set-Cookie"); if (setCookieHeader == null) { throw new IllegalStateException("OAuth flow is broken"); } authenticityCookie = setCookieHeader.getValue(); } catch (Exception e) { throw new OAuth2Exception(e); } String code = null; try { // 3. Submit the HTTP form for allowing access to the application final URI location = new URIBuilder(oAuthAuthorizationData.get("replyTo").asText()) .addParameter( "session_authenticity_token", oAuthAuthorizationData.get("authenticityToken").asText()) .addParameter("client_id", oAuthAuthorizationData.get("clientId").asText()) .addParameter("redirect_uri", oAuthAuthorizationData.get("redirectUri").asText()) .addParameter("oauthDecision", "allow") .build(); final HttpGet method = new HttpGet(location); method.addHeader( "Authorization", "Basic " + Base64.encodeBase64String("odatajclient:odatajclient".getBytes())); method.addHeader("Cookie", authenticityCookie); final HttpResponse response = httpClient.execute(method); final Header locationHeader = response.getFirstHeader("Location"); if (response.getStatusLine().getStatusCode() != 303 || locationHeader == null) { throw new IllegalStateException("OAuth flow is broken"); } // 4. Get the authorization code value out of this last redirect code = StringUtils.substringAfterLast(locationHeader.getValue(), "="); EntityUtils.consumeQuietly(response.getEntity()); } catch (Exception e) { throw new OAuth2Exception(e); } // 5. Obtain the access token try { accessToken = OAuthClientUtils.getAccessToken( getAccessTokenService(), OAUTH2_CONSUMER, new AuthorizationCodeGrant(code)); } catch (OAuthServiceException e) { throw new OAuth2Exception(e); } if (accessToken == null) { throw new OAuth2Exception("No OAuth2 access token"); } }