List of usage examples for org.apache.http.client.methods HttpRequestBase getURI
public URI getURI()
From source file:com.smartsheet.api.internal.http.DefaultHttpClient.java
/** * Log to the SLF4J logger (level based upon response status code). Override this function to add logging * or capture performance metrics.//ww w . java 2 s . co m * * @param request request * @param requestEntity request body * @param response response * @param responseEntity response body * @param durationMillis response time in ms * @throws IOException */ public void logRequest(HttpRequestBase request, HttpEntitySnapshot requestEntity, HttpResponse response, HttpEntitySnapshot responseEntity, long durationMillis) throws IOException { logger.info("{} {}, Response Code:{}, Request completed in {} ms", request.getMethod(), request.getURI(), response.getStatusCode(), durationMillis); if (response.getStatusCode() != 200) { // log the request and response on error logger.warn("{}", RequestAndResponseData.of(request, requestEntity, response, responseEntity, REQUEST_RESPONSE)); } else { // log the summary request and response on success logger.debug("{}", RequestAndResponseData.of(request, requestEntity, response, responseEntity, REQUEST_RESPONSE_SUMMARY)); } }
From source file:org.wso2.carbon.identity.cloud.web.jaggery.clients.MutualSSLHttpClient.java
private String doHttpMethod(HttpRequestBase httpMethod, HttpHeaders headers) { String responseString = null; try {/*from w w w . j a va2 s .c om*/ for (Map.Entry<String, String> entry : headers.getHeaderMap().entrySet()) { httpMethod.addHeader(entry.getKey(), entry.getValue()); } HttpResponse closeableHttpResponse = httpClient.execute(httpMethod); HttpEntity entity = closeableHttpResponse.getEntity(); responseString = EntityUtils.toString(entity, "UTF-8"); } catch (IOException e) { log.error("Error while executing the http method " + httpMethod.getMethod() + ". Endpoint : " + httpMethod.getURI(), e); } return responseString; }
From source file:net.netheos.pcsapi.providers.hubic.Swift.java
private void configureSession(HttpRequestBase request, String format) { request.addHeader("X-Auth-token", authToken); if (format != null) { try {/* w w w .jav a2s.c om*/ URI uri = request.getURI(); if (uri.getRawQuery() != null) { request.setURI(URI.create(uri + "&format=" + URLEncoder.encode(format, "UTF-8"))); } else { request.setURI(URI.create(uri + "?format=" + URLEncoder.encode(format, "UTF-8"))); } } catch (UnsupportedEncodingException ex) { throw new UnsupportedOperationException("Error setting the request format", ex); } } }
From source file:eionet.webq.xforms.XFormsHTTPRequestAuthHandlerImplTest.java
@Test public void addBasicAuthIfSameHostAsInstance() { HttpRequestBase httpRequest = new HttpGet(BASIC_AUTH_KNOWN_HOST_URL + "/resource.xml"); Map<Object, Object> context = createBfContextMap(); context.put("instance", BASIC_AUTH_KNOWN_HOST_URL + "/instance.xml"); UserFile userFile = createUserFileWithAuth(); when(userFileService.getById(anyInt())).thenReturn(userFile); when(knownHostsService.getKnownHost(anyString())).thenReturn(createBasicAuthKnownHost()); requestAuthHandler.addAuthToHttpRequest(httpRequest, context); assertThat(httpRequest.getURI().toString(), equalTo(BASIC_AUTH_KNOWN_HOST_URL + "/resource.xml")); assertThat(httpRequest.getHeaders("Authorization")[0].getValue().trim(), equalTo(userFile.getAuthorization())); }
From source file:eionet.webq.xforms.XFormsHTTPRequestAuthHandlerImplTest.java
@Test public void addRequestParamAuthIfSameHostAsInstance() { HttpRequestBase httpRequest = new HttpGet(REQUEST_PARAM_KNOWN_HOST_URL + "/resource.xml"); Map<Object, Object> context = createBfContextMap(); context.put("instance", REQUEST_PARAM_KNOWN_HOST_URL + "/instance.xml"); UserFile userFile = createUserFileWithAuth(); when(userFileService.getById(anyInt())).thenReturn(userFile); when(knownHostsService.getKnownHost(anyString())).thenReturn(createRequestParamKnownHost()); requestAuthHandler.addAuthToHttpRequest(httpRequest, context); assertThat(httpRequest.getURI().toString(), equalTo(REQUEST_PARAM_KNOWN_HOST_URL + "/resource.xml?key=ticket")); }
From source file:org.opennms.smoketest.OpenNMSSeleniumTestCase.java
private Integer doRequest(final HttpRequestBase request) throws ClientProtocolException, IOException, InterruptedException { final CountDownLatch waitForCompletion = new CountDownLatch(1); final URI uri = request.getURI(); final HttpHost targetHost = new HttpHost(uri.getHost(), uri.getPort(), uri.getScheme()); CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials(new AuthScope(targetHost.getHostName(), targetHost.getPort()), new UsernamePasswordCredentials("admin", "admin")); AuthCache authCache = new BasicAuthCache(); // Generate BASIC scheme object and add it to the local auth cache BasicScheme basicAuth = new BasicScheme(); authCache.put(targetHost, basicAuth); // Add AuthCache to the execution context HttpClientContext context = HttpClientContext.create(); context.setCredentialsProvider(credsProvider); context.setAuthCache(authCache);/*from ww w .j a v a 2 s .c o m*/ final CloseableHttpClient client = HttpClients.createDefault(); final ResponseHandler<Integer> responseHandler = new ResponseHandler<Integer>() { @Override public Integer handleResponse(final HttpResponse response) throws ClientProtocolException, IOException { try { final int status = response.getStatusLine().getStatusCode(); // 400 because we return that if you try to delete something that is already deleted // 404 because it's OK if it's already not there if (status >= 200 && status < 300 || status == 400 || status == 404) { EntityUtils.consume(response.getEntity()); return status; } else { throw new ClientProtocolException("Unexpected response status: " + status); } } finally { waitForCompletion.countDown(); } } }; final Integer status = client.execute(targetHost, request, responseHandler, context); waitForCompletion.await(); client.close(); return status; }
From source file:com.jaeksoft.searchlib.crawler.web.spider.HttpDownloader.java
private final DownloadItem request(final HttpRequestBase httpRequestBase, final CredentialItem credentialItem, final List<Header> additionalHeaders, final List<CookieItem> cookies, final HttpEntity entity) throws ClientProtocolException, IOException, URISyntaxException, IllegalStateException, SearchLibException {/*from www . ja va 2 s. c o m*/ synchronized (this) { reset(); if (entity != null) ((HttpEntityEnclosingRequest) httpRequestBase).setEntity(entity); addHeader(httpRequestBase, additionalHeaders); execute(httpRequestBase, credentialItem, cookies); return getDownloadItem(httpRequestBase.getURI()); } }
From source file:me.buom.shiro.test.AppTest.java
private Header[] buildHeader(ApiKey apiKey, HttpRequestBase httpRequest, String contentType, String entity) throws Exception { String accessKey = apiKey.getAccessKey(); String secretKey = apiKey.getSecretKey(); String contentMd5 = entity != null ? DigestUtils.md5Hex(entity) : ""; String dateString = dateFormatter.get().format(new Date()); String stringToSign = String.format(Locale.US, "%s\n%s\n%s\n%s\n%s", httpRequest.getMethod(), contentMd5, contentType, dateString, httpRequest.getURI().getPath()); byte[] hexHmac = HmacSha1.hash(secretKey, stringToSign); String base64Hmac = Base64.encodeToString(hexHmac); Header[] headers = new Header[] { new BasicHeader("Content-Type", contentType), new BasicHeader("Content-MD5", contentMd5), new BasicHeader("Date", dateString), new BasicHeader("Authorization", String.format(Locale.US, "AWS %s:%s", accessKey, base64Hmac)), }; return headers; }