List of usage examples for org.apache.http.client.methods HttpRequestBase getURI
public URI getURI()
From source file:com.tremolosecurity.proxy.util.LastMileUtil.java
public static void addLastMile(ConfigManager cfg, String username, String userNameAttr, HttpRequestBase req, String keyAlias, boolean addHeader) throws Exception { if (!addHeader) { return;/* w ww.ja va2 s .co m*/ } String uri = req.getURI().getPath(); DateTime now = new DateTime(); DateTime notBefore = now.minus(5 * 60 * 1000); DateTime notAfter = now.plus(5 * 60 * 1000); LastMile lm = new LastMile(uri, notBefore, notAfter, 0, "nochain"); lm.getAttributes().add(new Attribute(userNameAttr, username)); SecretKey sk = cfg.getSecretKey(keyAlias); String header = lm.generateLastMileToken(sk); req.addHeader("tremoloHeader", header); }
From source file:io.adventurous.android.api.ApiHttpClient.java
public static HttpResponse execute(HttpRequestBase request, ApiCredentials credentials, String deviceId) throws IOException { MyLog.d(TAG, request.getMethod() + ": " + request.getURI()); setApiCredentials(credentials, request); setDeviceIdHeader(request, deviceId); long start = System.currentTimeMillis(); HttpResponse response = mClient.execute(request); long end = System.currentTimeMillis(); MyLog.d(TAG, request.getMethod() + ": " + request.getURI() + " - took " + (end - start) + "ms"); return response; }
From source file:io.adventurous.android.api.ApiHttpClient.java
private static void setApiCredentials(ApiCredentials credentials, HttpRequestBase request) { if (credentials == null || credentials instanceof NullApiCredentials) { mClient.getCredentialsProvider().clear(); } else {//from w w w . j a va 2 s. c om String host = request.getURI().getHost(); int authPort = request.getURI().getPort(); mClient.getCredentialsProvider().setCredentials(new AuthScope(host, authPort), new UsernamePasswordCredentials(credentials.getUsername(), credentials.getPassword())); } }
From source file:com.ibm.sbt.services.client.ClientServicesException.java
static private String createMessage(HttpRequestBase request, HttpResponse response) { String msg = null;/*from w w w .j ava 2 s .c o m*/ int statusCode = response.getStatusLine().getStatusCode(); String reasonPhrase = response.getStatusLine().getReasonPhrase(); URI requestUri = request.getURI(); try { HandlerRaw handler = new HandlerRaw(); Object data = handler.parseContent(request, response, response.getEntity()); msg = "Request to url {0} returned an error response {1}:{2} {3}"; msg = StringUtil.format(msg, requestUri, statusCode, reasonPhrase, data); } catch (Exception e) { msg = "Request to url {0} returned an error response {1}:{2}"; msg = StringUtil.format(msg, requestUri, statusCode, reasonPhrase); } return msg; }
From source file:com.bluexml.side.deployer.alfresco.directcopy.AlfrescoHotDeployerHelper.java
public static String executeRequest(HttpClient httpclient, HttpRequestBase post) throws Exception { String responseS = ""; // Execute the request HttpResponse response;/*from ww w .ja v a2s.c om*/ System.out.println("AlfrescoHotDeployerHelper.executeRequest() request:" + post); System.out.println("AlfrescoHotDeployerHelper.executeRequest() URI :" + post.getURI()); response = httpclient.execute(post); // Examine the response status StatusLine statusLine = response.getStatusLine(); int statusCode = statusLine.getStatusCode(); System.out.println(response.getStatusLine()); // Get hold of the response entity HttpEntity entity = response.getEntity(); // If the response does not enclose an entity, there is no need // to worry about connection release if (entity != null) { InputStream instream = entity.getContent(); try { BufferedReader reader = new BufferedReader(new InputStreamReader(instream)); // do something useful with the response responseS = readBuffer(responseS, reader); } catch (IOException ex) { // In case of an IOException the connection will be released // back to the connection manager automatically throw ex; } catch (RuntimeException ex) { // In case of an unexpected exception you may want to abort // the HTTP request in order to shut down the underlying // connection and release it back to the connection manager. post.abort(); throw ex; } finally { // Closing the input stream will trigger connection release instream.close(); } // When HttpClient instance is no longer needed, // shut down the connection manager to ensure // immediate deallocation of all system resources httpclient.getConnectionManager().shutdown(); } if (statusCode != 200) { throw new Exception("Request Fail HTTP response :" + statusLine); } return responseS; }
From source file:org.cloudifysource.rest.util.RestUtils.java
/** * /*ww w.j av a 2 s. c om*/ * @param response * . * @param httpMethod * . * @return response's body. * @throws IOException . * @throws RestErrorException . */ public static String getResponseBody(final HttpResponse response, final HttpRequestBase httpMethod) throws IOException, RestErrorException { InputStream instream = null; try { final HttpEntity entity = response.getEntity(); if (entity == null) { final RestErrorException e = new RestErrorException("comm_error", httpMethod.getURI().toString(), " response entity is null"); throw e; } instream = entity.getContent(); return getStringFromStream(instream); } finally { if (instream != null) { try { instream.close(); } catch (final IOException e) { e.printStackTrace(); } } } }
From source file:jfabrix101.lib.helper.NetworkHelper.java
/** * Last part of execution an http request. * Execute a request and return the body. * @param httpClient//from ww w . j a v a 2 s.c o m * @param httpRequest * @return * @throws Exception */ private static String execHttpRequest(HttpClient httpClient, HttpRequestBase httpRequest) throws Exception { HttpResponse response = httpClient.execute(httpRequest); int returnCode = response.getStatusLine().getStatusCode(); String htmlBody = EntityUtils.toString(response.getEntity()); if (returnCode != HttpStatus.SC_OK) { mLogger.error("- Network error reading URL: " + httpRequest.getURI().toString()); mLogger.error("- Network error: HttpStatusCode : " + response.getStatusLine().getStatusCode()); return null; } return htmlBody; }
From source file:org.eclipse.mylyn.commons.http.HttpUtil.java
public static HttpHost createHost(HttpRequestBase method) { URI uri = method.getURI(); return new HttpHost(uri.getHost(), uri.getPort(), uri.getScheme()); }
From source file:com.smartsheet.api.internal.http.RequestAndResponseData.java
/** * factory method for creating a RequestAndResponseData object from request and response data with the specifid trace fields *//*ww w. ja v a2 s . c o m*/ public static RequestAndResponseData of(HttpRequestBase request, HttpEntitySnapshot requestEntity, HttpResponse response, HttpEntitySnapshot responseEntity, Set<Trace> traces) throws IOException { RequestData.Builder requestBuilder = new RequestData.Builder(); ResponseData.Builder responseBuilder = new ResponseData.Builder(); if (request != null) { requestBuilder.withCommand(request.getMethod() + " " + request.getURI()); boolean binaryBody = false; if (traces.contains(Trace.RequestHeaders) && request.getAllHeaders() != null) { requestBuilder.withHeaders(); for (Header header : request.getAllHeaders()) { String headerName = header.getName(); String headerValue = header.getValue(); if ("Authorization".equals(headerName) && headerValue.length() > 0) { headerValue = "Bearer ****" + headerValue.substring(Math.max(0, headerValue.length() - 4)); } else if ("Content-Disposition".equals(headerName)) { binaryBody = true; } requestBuilder.addHeader(headerName, headerValue); } } if (requestEntity != null) { if (traces.contains(Trace.RequestBody)) { requestBuilder .setBody(binaryBody ? binaryBody(requestEntity) : getContentAsText(requestEntity)); } else if (traces.contains(Trace.RequestBodySummary)) { requestBuilder.setBody(binaryBody ? binaryBody(requestEntity) : truncateAsNeeded(getContentAsText(requestEntity), TRUNCATE_LENGTH)); } } } if (response != null) { boolean binaryBody = false; responseBuilder.withStatus(response.getStatusText()); if (traces.contains(Trace.ResponseHeaders) && response.getHeaders() != null) { responseBuilder.withHeaders(); for (Map.Entry<String, String> header : response.getHeaders().entrySet()) { String headerName = header.getKey(); String headerValue = header.getValue(); if ("Content-Disposition".equals(headerName)) { binaryBody = true; } responseBuilder.addHeader(headerName, headerValue); } } if (responseEntity != null) { if (traces.contains(Trace.ResponseBody)) { responseBuilder .setBody(binaryBody ? binaryBody(responseEntity) : getContentAsText(responseEntity)); } else if (traces.contains(Trace.ResponseBodySummary)) { responseBuilder.setBody(binaryBody ? binaryBody(responseEntity) : truncateAsNeeded(getContentAsText(responseEntity), TRUNCATE_LENGTH)); } } } return new RequestAndResponseData(requestBuilder.build(), responseBuilder.build()); }
From source file:com.kolich.havalo.client.service.HavaloClientSigner.java
private static final String getStringToSign(final HttpRequestBase request) { final StringBuilder sb = new StringBuilder(); // HTTP-Verb (GET, PUT, POST, or DELETE) + "\n" sb.append(request.getMethod().toUpperCase()).append(LINE_SEPARATOR_UNIX); // RFC822 formatted Date (from 'Date' header on request) + "\n" sb.append(request.getFirstHeader(DATE).getValue()).append(LINE_SEPARATOR_UNIX); // Content-Type (from 'Content-Type' request header, optional) + "\n" final Header contentType; if ((contentType = request.getFirstHeader(CONTENT_TYPE)) != null) { sb.append(contentType.getValue()); }/*from w w w . j a va 2s . c om*/ sb.append(LINE_SEPARATOR_UNIX); // CanonicalizedResource sb.append(request.getURI().getRawPath()); return sb.toString(); }