List of usage examples for org.apache.http.client.methods HttpRequestBase setHeader
public void setHeader(Header header)
From source file:mx.openpay.client.core.impl.DefaultHttpServiceClient.java
protected void addHeaders(final HttpRequestBase request) { request.addHeader(new BasicHeader("User-Agent", this.userAgent)); request.addHeader(new BasicHeader("Accept", "application/json")); request.setHeader(new BasicHeader("Content-Type", "application/json")); }
From source file:pl.psnc.synat.wrdz.zmkd.invocation.RestServiceCaller.java
/** * Invoke a service according to the execution info. * //from ww w. ja v a2 s . c o m * In case of success, the entity stream is not closed. * * @param execInfo * info how to execute a REST service * @return execution outcome * @throws InvalidHttpRequestException * when constructed request is invalid * @throws InvalidHttpResponseException * when response is invalid */ public ExecutionOutcome invoke(ExecutionInfo execInfo) throws InvalidHttpRequestException, InvalidHttpResponseException { HttpClient httpClient = getHttpClient(); HttpRequestBase request = null; try { request = RestServiceCallerUtils.constructServiceRequestBase(execInfo); } catch (URISyntaxException e) { logger.error("Incorrect URI of the service.", e); throw new InvalidHttpRequestException(e); } if (request instanceof HttpEntityEnclosingRequestBase) { HttpEntity entity = RestServiceCallerUtils.constructServiceRequestEntity(execInfo); if (entity != null) { ((HttpEntityEnclosingRequestBase) request).setEntity(entity); } Header header = RestServiceCallerUtils.constructServiceRequestHeader(execInfo); if (header != null) { request.setHeader(header); } } HttpParams params = new BasicHttpParams(); params.setParameter("http.protocol.handle-redirects", false); request.setParams(params); HttpResponse response = null; try { response = httpClient.execute(request); } catch (ClientProtocolException e) { logger.error("Incorrect protocol.", e); throw new InvalidHttpResponseException(e); } catch (IOException e) { logger.error("IO error during execution.", e); throw new InvalidHttpResponseException(e); } try { return RestServiceCallerUtils.retrieveOutcome(response); } catch (IllegalStateException e) { logger.error("Cannot retrived the stream with the content.", e); EntityUtils.consumeQuietly(response.getEntity()); throw new InvalidHttpResponseException(e); } catch (IOException e) { logger.error("IO error when retrieving content.", e); EntityUtils.consumeQuietly(response.getEntity()); throw new InvalidHttpResponseException(e); } // remember to close the entity stream after read it. }
From source file:es.tid.fiware.fiwareconnectors.cygnus.backends.hdfs.HDFSBackendImpl.java
private HttpResponse doHDFSRequest(HttpClient httpClient, String method, String url, ArrayList<Header> headers, StringEntity entity) throws Exception { HttpResponse response = null;/* w w w . ja v a 2 s. c o m*/ HttpRequestBase request = null; if (method.equals("PUT")) { HttpPut req = new HttpPut(url); if (entity != null) { req.setEntity(entity); } // if request = req; } else if (method.equals("POST")) { HttpPost req = new HttpPost(url); if (entity != null) { req.setEntity(entity); } // if request = req; } else if (method.equals("GET")) { request = new HttpGet(url); } else { throw new CygnusRuntimeError("HTTP method not supported: " + method); } // if else if (headers != null) { for (Header header : headers) { request.setHeader(header); } // for } // if logger.debug("HDFS request: " + request.toString()); try { response = httpClient.execute(request); } catch (IOException e) { throw new CygnusPersistenceError(e.getMessage()); } // try catch request.releaseConnection(); logger.debug("HDFS response: " + response.getStatusLine().toString()); return response; }
From source file:org.apache.solr.client.solrj.impl.HttpSolrClient.java
private void setBasicAuthHeader(SolrRequest request, HttpRequestBase method) throws UnsupportedEncodingException { if (request.getBasicAuthUser() != null && request.getBasicAuthPassword() != null) { String userPass = request.getBasicAuthUser() + ":" + request.getBasicAuthPassword(); String encoded = Base64.byteArrayToBase64(userPass.getBytes(UTF_8)); method.setHeader(new BasicHeader("Authorization", "Basic " + encoded)); }/* w ww . ja v a 2 s .co m*/ }
From source file:com.telefonica.iot.cygnus.backends.http.HttpBackend.java
/** * Does a Http request given a method, a relative URL, a list of headers and * the payload Protected method due to it's used by the tests. * //from w ww .j a va 2 s.c o m * @param method * @param url * @param headers * @param entity * @return The result of the request * @throws CygnusRuntimeError * @throws CygnusPersistenceError */ protected JsonResponse doRequest(String method, String url, ArrayList<Header> headers, StringEntity entity) throws CygnusRuntimeError, CygnusPersistenceError { HttpResponse httpRes = null; HttpRequestBase request; switch (method) { case "PUT": HttpPut reqPut = new HttpPut(url); if (entity != null) { reqPut.setEntity(entity); } // if request = reqPut; break; case "POST": HttpPost reqPost = new HttpPost(url); if (entity != null) { reqPost.setEntity(entity); } // if request = reqPost; break; case "GET": request = new HttpGet(url); break; case "DELETE": request = new HttpDelete(url); break; default: throw new CygnusRuntimeError("Http '" + method + "' method not supported"); } // switch if (headers != null) { for (Header header : headers) { request.setHeader(header); } // for } // if LOGGER.debug("Http request: " + request.toString()); try { httpRes = httpClient.execute(request); } catch (IOException e) { request.releaseConnection(); throw new CygnusPersistenceError("Request error", "IOException", e.getMessage()); } // try catch JsonResponse response = createJsonResponse(httpRes); request.releaseConnection(); return response; }
From source file:com.mashape.unirest.android.http.HttpClientHelper.java
private static HttpRequestBase prepareRequest(HttpRequest request, boolean async) { Object defaultHeaders = Options.getOption(Option.DEFAULT_HEADERS); if (defaultHeaders != null) { @SuppressWarnings("unchecked") Set<Entry<String, String>> entrySet = ((Map<String, String>) defaultHeaders).entrySet(); for (Entry<String, String> entry : entrySet) { request.header(entry.getKey(), entry.getValue()); }//from ww w .j a v a 2 s.co m } if (!request.getHeaders().containsKey(USER_AGENT_HEADER)) { request.header(USER_AGENT_HEADER, USER_AGENT); } if (!request.getHeaders().containsKey(ACCEPT_ENCODING_HEADER)) { request.header(ACCEPT_ENCODING_HEADER, "gzip"); } HttpRequestBase reqObj = null; String urlToRequest = null; try { URL url = new URL(request.getUrl()); URI uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), URLDecoder.decode(url.getPath(), "UTF-8"), "", url.getRef()); urlToRequest = uri.toURL().toString(); if (url.getQuery() != null && !url.getQuery().trim().equals("")) { if (!urlToRequest.substring(urlToRequest.length() - 1).equals("?")) { urlToRequest += "?"; } urlToRequest += url.getQuery(); } else if (urlToRequest.substring(urlToRequest.length() - 1).equals("?")) { urlToRequest = urlToRequest.substring(0, urlToRequest.length() - 1); } } catch (Exception e) { throw new RuntimeException(e); } switch (request.getHttpMethod()) { case GET: reqObj = new HttpGet(urlToRequest); break; case POST: reqObj = new HttpPost(urlToRequest); break; case PUT: reqObj = new HttpPut(urlToRequest); break; case DELETE: //reqObj = new HttpDeleteWithBody(urlToRequest); break; case PATCH: //reqObj = new HttpPatchWithBody(urlToRequest); break; case OPTIONS: reqObj = new HttpOptions(urlToRequest); break; case HEAD: reqObj = new HttpHead(urlToRequest); break; } Set<Entry<String, List<String>>> entrySet = request.getHeaders().entrySet(); for (Entry<String, List<String>> entry : entrySet) { List<String> values = entry.getValue(); if (values != null) { for (String value : values) { reqObj.addHeader(entry.getKey(), value); } } } // Set body if (!(request.getHttpMethod() == HttpMethod.GET || request.getHttpMethod() == HttpMethod.HEAD)) { if (request.getBody() != null) { HttpEntity entity = request.getBody().getEntity(); if (async) { if (reqObj.getHeaders(CONTENT_TYPE) == null || reqObj.getHeaders(CONTENT_TYPE).length == 0) { reqObj.setHeader(entity.getContentType()); } try { ByteArrayOutputStream output = new ByteArrayOutputStream(); entity.writeTo(output); NByteArrayEntity en = new NByteArrayEntity(output.toByteArray()); ((HttpEntityEnclosingRequestBase) reqObj).setEntity(en); } catch (IOException e) { throw new RuntimeException(e); } } else { ((HttpEntityEnclosingRequestBase) reqObj).setEntity(entity); } } } return reqObj; }
From source file:net.ychron.unirestinst.http.HttpClientHelper.java
private HttpRequestBase prepareRequest(HttpRequest request, boolean async) { Object defaultHeaders = options.getOption(Option.DEFAULT_HEADERS); if (defaultHeaders != null) { @SuppressWarnings("unchecked") Set<Entry<String, String>> entrySet = ((Map<String, String>) defaultHeaders).entrySet(); for (Entry<String, String> entry : entrySet) { request.header(entry.getKey(), entry.getValue()); }/* ww w .jav a2 s . c o m*/ } if (!request.getHeaders().containsKey(USER_AGENT_HEADER)) { request.header(USER_AGENT_HEADER, USER_AGENT); } if (!request.getHeaders().containsKey(ACCEPT_ENCODING_HEADER)) { request.header(ACCEPT_ENCODING_HEADER, "gzip"); } HttpRequestBase reqObj = null; String urlToRequest = null; try { URL url = new URL(request.getUrl()); URI uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), URLDecoder.decode(url.getPath(), "UTF-8"), "", url.getRef()); urlToRequest = uri.toURL().toString(); if (url.getQuery() != null && !url.getQuery().trim().equals("")) { if (!urlToRequest.substring(urlToRequest.length() - 1).equals("?")) { urlToRequest += "?"; } urlToRequest += url.getQuery(); } else if (urlToRequest.substring(urlToRequest.length() - 1).equals("?")) { urlToRequest = urlToRequest.substring(0, urlToRequest.length() - 1); } } catch (Exception e) { throw new RuntimeException(e); } switch (request.getHttpMethod()) { case GET: reqObj = new HttpGet(urlToRequest); break; case POST: reqObj = new HttpPost(urlToRequest); break; case PUT: reqObj = new HttpPut(urlToRequest); break; case DELETE: reqObj = new HttpDeleteWithBody(urlToRequest); break; case PATCH: reqObj = new HttpPatchWithBody(urlToRequest); break; case OPTIONS: reqObj = new HttpOptions(urlToRequest); break; case HEAD: reqObj = new HttpHead(urlToRequest); break; } Set<Entry<String, List<String>>> entrySet = request.getHeaders().entrySet(); for (Entry<String, List<String>> entry : entrySet) { List<String> values = entry.getValue(); if (values != null) { for (String value : values) { reqObj.addHeader(entry.getKey(), value); } } } // Set body if (!(request.getHttpMethod() == HttpMethod.GET || request.getHttpMethod() == HttpMethod.HEAD)) { if (request.getBody() != null) { HttpEntity entity = request.getBody().getEntity(); if (async) { if (reqObj.getHeaders(CONTENT_TYPE) == null || reqObj.getHeaders(CONTENT_TYPE).length == 0) { reqObj.setHeader(entity.getContentType()); } try { ByteArrayOutputStream output = new ByteArrayOutputStream(); entity.writeTo(output); NByteArrayEntity en = new NByteArrayEntity(output.toByteArray()); ((HttpEntityEnclosingRequestBase) reqObj).setEntity(en); } catch (IOException e) { throw new RuntimeException(e); } } else { ((HttpEntityEnclosingRequestBase) reqObj).setEntity(entity); } } } return reqObj; }