List of usage examples for org.apache.http.client.methods HttpRequestBase setConfig
public void setConfig(final RequestConfig config)
From source file:de.stklcode.jvault.connector.HTTPVaultConnector.java
/** * Execute prepared HTTP request and return result. * * @param base Prepares Request/* w w w.j a v a 2 s.c o m*/ * @param retries number of retries * @return HTTP response * @throws VaultConnectorException on connection error */ private String request(final HttpRequestBase base, final int retries) throws VaultConnectorException { /* Set JSON Header */ base.addHeader("accept", "application/json"); CloseableHttpResponse response = null; try (CloseableHttpClient httpClient = HttpClientBuilder.create() .setSSLSocketFactory(createSSLSocketFactory()).build()) { /* Set custom timeout, if defined */ if (this.timeout != null) base.setConfig(RequestConfig.copy(RequestConfig.DEFAULT).setConnectTimeout(timeout).build()); /* Execute request */ response = httpClient.execute(base); /* Check if response is valid */ if (response == null) throw new InvalidResponseException("Response unavailable"); switch (response.getStatusLine().getStatusCode()) { case 200: return handleResult(response); case 204: return ""; case 403: throw new PermissionDeniedException(); default: if (response.getStatusLine().getStatusCode() >= 500 && response.getStatusLine().getStatusCode() < 600 && retries > 0) { /* Retry on 5xx errors */ return request(base, retries - 1); } else { /* Fail on different error code and/or no retries left */ handleError(response); /* Throw exception withoud details, if response entity is empty. */ throw new InvalidResponseException(Error.RESPONSE_CODE, response.getStatusLine().getStatusCode()); } } } catch (IOException e) { throw new InvalidResponseException(Error.READ_RESPONSE, e); } finally { if (response != null && response.getEntity() != null) try { EntityUtils.consume(response.getEntity()); } catch (IOException ignored) { // Exception ignored. } } }
From source file:eu.vital.TrustManager.connectors.ppi.PPIManager.java
private String query(String ppi_endpoint, String body, String method) throws NoSuchAlgorithmException, KeyStoreException, KeyManagementException { Cookie ck;//from w w w. java 2s . c o m //String internalToken; CloseableHttpClient httpclient; HttpRequestBase httpaction; //boolean wasEmpty; //int code; SSLContextBuilder builder = new SSLContextBuilder(); builder.loadTrustMaterial(null, new TrustSelfSignedStrategy()); SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(builder.build()); httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).build(); //httpclient = HttpClients.createDefault(); URI uri = null; try { // Prepare to forward the request to the proxy uri = new URI(ppi_endpoint); } catch (URISyntaxException e1) { //log } if (method.equals("GET")) { httpaction = new HttpGet(uri); } else { httpaction = new HttpPost(uri); } // Get token or authenticate if null or invalid //internalToken = client.getToken(); ck = new Cookie("vitalAccessToken", cookie.substring(17)); httpaction.setHeader("Cookie", ck.toString()); httpaction.setConfig(RequestConfig.custom().setConnectionRequestTimeout(3000).setConnectTimeout(3000) .setSocketTimeout(3000).build()); httpaction.setHeader("Content-Type", javax.ws.rs.core.MediaType.APPLICATION_JSON); StringEntity strEntity = new StringEntity(body, StandardCharsets.UTF_8); if (method.equals("POST")) { ((HttpPost) httpaction).setEntity(strEntity); } // Execute and get the response. CloseableHttpResponse response = null; try { response = httpclient.execute(httpaction); } catch (ClientProtocolException e) { java.util.logging.Logger.getLogger(PPIManager.class.getName()).log(Level.SEVERE, null, e); } catch (IOException e) { try { // Try again with a higher timeout try { Thread.sleep(1000); // do not retry immediately } catch (InterruptedException e1) { // e1.printStackTrace(); } httpaction.setConfig(RequestConfig.custom().setConnectionRequestTimeout(7000) .setConnectTimeout(7000).setSocketTimeout(7000).build()); response = httpclient.execute(httpaction); } catch (ClientProtocolException ea) { java.util.logging.Logger.getLogger(PPIManager.class.getName()).log(Level.SEVERE, null, ea); return ""; } catch (IOException ea) { try { // Try again with a higher timeout try { Thread.sleep(1000); // do not retry immediately } catch (InterruptedException e1) { java.util.logging.Logger.getLogger(PPIManager.class.getName()).log(Level.SEVERE, null, e1); return ""; } httpaction.setConfig(RequestConfig.custom().setConnectionRequestTimeout(12000) .setConnectTimeout(12000).setSocketTimeout(12000).build()); response = httpclient.execute(httpaction); } catch (ClientProtocolException eaa) { java.util.logging.Logger.getLogger(PPIManager.class.getName()).log(Level.SEVERE, null, eaa); return ""; } catch (Exception eaa) { java.util.logging.Logger.getLogger(PPIManager.class.getName()).log(Level.SEVERE, null, eaa); return ""; //return eaa.getMessage(); } } } int statusCode; try { statusCode = response.getStatusLine().getStatusCode(); } catch (Exception eaa) { java.util.logging.Logger.getLogger(PPIManager.class.getName()).log(Level.SEVERE, null, eaa); return ""; } if (statusCode != HttpStatus.SC_OK && statusCode != HttpStatus.SC_ACCEPTED) { if (statusCode == 503) { java.util.logging.Logger.getLogger(PPIManager.class.getName()).log(Level.SEVERE, null, "PPI 503"); return ""; } else if (statusCode == 502) { java.util.logging.Logger.getLogger(PPIManager.class.getName()).log(Level.SEVERE, null, "PPI 502"); return ""; } else if (statusCode == 401) { java.util.logging.Logger.getLogger(PPIManager.class.getName()).log(Level.SEVERE, null, "PPI 401"); return ""; } else { java.util.logging.Logger.getLogger(PPIManager.class.getName()).log(Level.SEVERE, null, "PPI 500"); return ""; //throw new ServiceUnavailableException(); } } HttpEntity entity; entity = response.getEntity(); String respString = ""; if (entity != null) { try { respString = EntityUtils.toString(entity); response.close(); return respString; } catch (ParseException | IOException e) { java.util.logging.Logger.getLogger(PPIManager.class.getName()).log(Level.SEVERE, null, "PPI 401"); return ""; } } return respString; }
From source file:de.jetwick.snacktory.HtmlFetcher.java
protected CloseableHttpResponse createUrlConnection(String urlAsStr, int timeout, boolean includeSomeGooseOptions, boolean isHead) throws MalformedURLException, IOException { CloseableHttpClient httpclient = HttpClients.createDefault(); HttpRequestBase request = null; if (isHead) { request = new HttpHead(urlAsStr); } else {// ww w . jav a 2s . c o m request = new HttpGet(urlAsStr); } RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(timeout) .setConnectTimeout(timeout).setSocketTimeout(timeout).setCookieSpec(CookieSpecs.STANDARD).build(); request.setHeader("User-Agent", userAgent); request.setHeader("Accept", accept); if (includeSomeGooseOptions) { request.setHeader("Accept-Language", language); request.setHeader("content-charset", charset); request.setHeader("Referer", referrer); // avoid the cache for testing purposes only? request.setHeader("Cache-Control", cacheControl); } // suggest respond to be gzipped or deflated (which is just another compression) // http://stackoverflow.com/q/3932117 request.setHeader("Accept-Encoding", "gzip, deflate"); request.setConfig(requestConfig); return httpclient.execute(request); }
From source file:org.opendaylight.infrautils.diagstatus.shell.HttpClient.java
public HttpResponse sendRequest(HttpRequest request) throws Exception { CloseableHttpClient httpclient = HttpClients.createDefault(); if (httpclient == null) { throw new ClientProtocolException("Couldn't create an HTTP client"); }/*from w w w . ja v a 2 s . c o m*/ RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(request.getTimeout()) .setConnectTimeout(request.getTimeout()).build(); HttpRequestBase httprequest; String method = request.getMethod(); if (method.equalsIgnoreCase("GET")) { httprequest = new HttpGet(request.getUri()); } else if (method.equalsIgnoreCase("POST")) { httprequest = new HttpPost(request.getUri()); if (request.getEntity() != null) { StringEntity sentEntity = new StringEntity(request.getEntity()); sentEntity.setContentType(request.getContentType()); ((HttpEntityEnclosingRequestBase) httprequest).setEntity(sentEntity); } } else if (method.equalsIgnoreCase("PUT")) { httprequest = new HttpPut(request.getUri()); if (request.getEntity() != null) { StringEntity sentEntity = new StringEntity(request.getEntity()); sentEntity.setContentType(request.getContentType()); ((HttpEntityEnclosingRequestBase) httprequest).setEntity(sentEntity); } } else if (method.equalsIgnoreCase("DELETE")) { httprequest = new HttpDelete(request.getUri()); } else { httpclient.close(); throw new IllegalArgumentException( "This profile class only supports GET, POST, PUT, and DELETE methods"); } httprequest.setConfig(requestConfig); // add request headers Iterator<String> headerIterator = request.getHeaders().keySet().iterator(); while (headerIterator.hasNext()) { String header = headerIterator.next(); Iterator<String> valueIterator = request.getHeaders().get(header).iterator(); while (valueIterator.hasNext()) { httprequest.addHeader(header, valueIterator.next()); } } CloseableHttpResponse response = httpclient.execute(httprequest); try { int httpResponseCode = response.getStatusLine().getStatusCode(); HashMap<String, List<String>> headerMap = new HashMap<>(); // copy response headers HeaderIterator it = response.headerIterator(); while (it.hasNext()) { Header nextHeader = it.nextHeader(); String name = nextHeader.getName(); String value = nextHeader.getValue(); if (headerMap.containsKey(name)) { headerMap.get(name).add(value); } else { List<String> list = new ArrayList<>(); list.add(value); headerMap.put(name, list); } } if (httpResponseCode > 299) { return new HttpResponse(httpResponseCode, response.getStatusLine().getReasonPhrase(), headerMap); } Optional<HttpEntity> receivedEntity = Optional.ofNullable(response.getEntity()); String httpBody = receivedEntity.isPresent() ? EntityUtils.toString(receivedEntity.get()) : null; return new HttpResponse(response.getStatusLine().getStatusCode(), httpBody, headerMap); } finally { response.close(); } }
From source file:com.gargoylesoftware.htmlunit.HttpWebConnection.java
private void setProxy(final HttpRequestBase httpRequest, final WebRequest webRequest) { final RequestConfig.Builder requestBuilder = createRequestConfigBuilder(getTimeout()); if (webRequest.getProxyHost() != null) { final HttpHost proxy = new HttpHost(webRequest.getProxyHost(), webRequest.getProxyPort()); if (webRequest.isSocksProxy()) { SocksConnectionSocketFactory.setSocksProxy(httpContext_, proxy); } else {/* w w w . j a va 2 s . co m*/ requestBuilder.setProxy(proxy); httpRequest.setConfig(requestBuilder.build()); } } else { requestBuilder.setProxy(null); httpRequest.setConfig(requestBuilder.build()); } }
From source file:eu.vital.TrustManager.connectors.dms.DMSManager.java
private String queryWithExceptions(String dms_endpoint, String body, String method) throws SocketTimeoutException, ConnectException, IOException, InterruptedException { Cookie ck;/*from w w w . j a va2s .c o m*/ //String internalToken; CloseableHttpClient httpclient; HttpRequestBase httpaction; //boolean wasEmpty; //int code; httpclient = HttpClients.createDefault(); URI uri = null; try { // Prepare to forward the request to the proxy uri = new URI(dms_URL + "/" + dms_endpoint); } catch (URISyntaxException e1) { java.util.logging.Logger.getLogger(DMSManager.class.getName()).log(Level.SEVERE, null, e1); } if (method.equals("GET")) { httpaction = new HttpGet(uri); } else { httpaction = new HttpPost(uri); } // Get token or authenticate if null or invalid //internalToken = client.getToken(); ck = new Cookie("vitalAccessToken", cookie.substring(17)); httpaction.setHeader("Cookie", ck.toString()); httpaction.setConfig(RequestConfig.custom().setConnectionRequestTimeout(5000).setConnectTimeout(5000) .setSocketTimeout(5000).build()); httpaction.setHeader("Content-Type", javax.ws.rs.core.MediaType.APPLICATION_JSON); StringEntity strEntity = new StringEntity(body, StandardCharsets.UTF_8); if (method.equals("POST")) { ((HttpPost) httpaction).setEntity(strEntity); } // Execute and get the response. CloseableHttpResponse response = null; try { response = httpclient.execute(httpaction); } catch (ClientProtocolException e) { throw new ClientProtocolException(); } catch (IOException e) { try { // Try again with a higher timeout try { Thread.sleep(1000); // do not retry immediately } catch (InterruptedException e1) { throw new InterruptedException(); // e1.printStackTrace(); } httpaction.setConfig(RequestConfig.custom().setConnectionRequestTimeout(7000) .setConnectTimeout(7000).setSocketTimeout(7000).build()); response = httpclient.execute(httpaction); } catch (ClientProtocolException ea) { java.util.logging.Logger.getLogger(DMSManager.class.getName()).log(Level.SEVERE, null, ea); throw new ClientProtocolException(); } catch (IOException ea) { try { // Try again with a higher timeout try { Thread.sleep(1000); // do not retry immediately } catch (InterruptedException e1) { java.util.logging.Logger.getLogger(DMSManager.class.getName()).log(Level.SEVERE, null, e1); throw new InterruptedException(); } httpaction.setConfig(RequestConfig.custom().setConnectionRequestTimeout(12000) .setConnectTimeout(12000).setSocketTimeout(12000).build()); response = httpclient.execute(httpaction); } catch (ClientProtocolException eaa) { java.util.logging.Logger.getLogger(DMSManager.class.getName()).log(Level.SEVERE, null, eaa); throw new ClientProtocolException(); } catch (SocketTimeoutException eaa) { java.util.logging.Logger.getLogger(DMSManager.class.getName()).log(Level.SEVERE, null, eaa); throw new SocketTimeoutException(); } catch (ConnectException eaa) { java.util.logging.Logger.getLogger(DMSManager.class.getName()).log(Level.SEVERE, null, eaa); throw new ConnectException(); } catch (ConnectTimeoutException eaa) { java.util.logging.Logger.getLogger(DMSManager.class.getName()).log(Level.SEVERE, null, eaa); throw new ConnectTimeoutException(); } } } int statusCode = response.getStatusLine().getStatusCode(); if (statusCode != HttpStatus.SC_OK && statusCode != HttpStatus.SC_ACCEPTED) { if (statusCode == 503) { java.util.logging.Logger.getLogger(DMSManager.class.getName()).log(Level.SEVERE, null, "httpStatusCode 503"); throw new ServiceUnavailableException(); } else if (statusCode == 502) { java.util.logging.Logger.getLogger(DMSManager.class.getName()).log(Level.SEVERE, null, "httpStatusCode 502"); throw new ServerErrorException(502); } else if (statusCode == 401) { java.util.logging.Logger.getLogger(DMSManager.class.getName()).log(Level.SEVERE, null, "could't Athorize the DMS"); throw new NotAuthorizedException("could't Athorize the DMS"); } else { java.util.logging.Logger.getLogger(DMSManager.class.getName()).log(Level.SEVERE, null, "httpStatusCode 500"); throw new ServiceUnavailableException(); } } HttpEntity entity; entity = response.getEntity(); String respString = ""; if (entity != null) { try { respString = EntityUtils.toString(entity); response.close(); } catch (ParseException | IOException e) { java.util.logging.Logger.getLogger(DMSManager.class.getName()).log(Level.SEVERE, null, e); } } return respString; }
From source file:com.smartsheet.api.internal.http.DefaultHttpClient.java
/** * Create the Apache HTTP request. Override this function to inject additional * haaders in the request or use a proxy. * * @param smartsheetRequest (request method and base URI come from here) * @return the Apache HTTP request/* www . jav a 2 s .co m*/ */ public HttpRequestBase createApacheRequest(HttpRequest smartsheetRequest) { HttpRequestBase apacheHttpRequest; // Create Apache HTTP request based on the smartsheetRequest request type switch (smartsheetRequest.getMethod()) { case GET: apacheHttpRequest = new HttpGet(smartsheetRequest.getUri()); break; case POST: apacheHttpRequest = new HttpPost(smartsheetRequest.getUri()); break; case PUT: apacheHttpRequest = new HttpPut(smartsheetRequest.getUri()); break; case DELETE: apacheHttpRequest = new HttpDelete(smartsheetRequest.getUri()); break; default: throw new UnsupportedOperationException( "Request method " + smartsheetRequest.getMethod() + " is not supported!"); } RequestConfig.Builder builder = RequestConfig.custom(); if (apacheHttpRequest.getConfig() != null) { builder = RequestConfig.copy(apacheHttpRequest.getConfig()); } builder.setRedirectsEnabled(true); RequestConfig config = builder.build(); apacheHttpRequest.setConfig(config); return apacheHttpRequest; }
From source file:org.apache.solr.client.solrj.impl.HttpSolrClient.java
protected NamedList<Object> executeMethod(HttpRequestBase method, final ResponseParser processor) throws SolrServerException { method.addHeader("User-Agent", AGENT); org.apache.http.client.config.RequestConfig.Builder requestConfigBuilder = HttpClientUtil .createDefaultRequestConfigBuilder(); if (soTimeout != null) { requestConfigBuilder.setSocketTimeout(soTimeout); }/* w ww.j a v a 2 s .c o m*/ if (connectionTimeout != null) { requestConfigBuilder.setConnectTimeout(connectionTimeout); } if (followRedirects != null) { requestConfigBuilder.setRedirectsEnabled(followRedirects); } method.setConfig(requestConfigBuilder.build()); HttpEntity entity = null; InputStream respBody = null; boolean shouldClose = true; try { // Execute the method. HttpClientContext httpClientRequestContext = HttpClientUtil.createNewHttpClientRequestContext(); final HttpResponse response = httpClient.execute(method, httpClientRequestContext); int httpStatus = response.getStatusLine().getStatusCode(); // Read the contents entity = response.getEntity(); respBody = entity.getContent(); Header ctHeader = response.getLastHeader("content-type"); String contentType; if (ctHeader != null) { contentType = ctHeader.getValue(); } else { contentType = ""; } // handle some http level checks before trying to parse the response switch (httpStatus) { case HttpStatus.SC_OK: case HttpStatus.SC_BAD_REQUEST: case HttpStatus.SC_CONFLICT: // 409 break; case HttpStatus.SC_MOVED_PERMANENTLY: case HttpStatus.SC_MOVED_TEMPORARILY: if (!followRedirects) { throw new SolrServerException( "Server at " + getBaseURL() + " sent back a redirect (" + httpStatus + ")."); } break; default: if (processor == null || "".equals(contentType)) { throw new RemoteSolrException(baseUrl, httpStatus, "non ok status: " + httpStatus + ", message:" + response.getStatusLine().getReasonPhrase(), null); } } if (processor == null || processor instanceof InputStreamResponseParser) { // no processor specified, return raw stream NamedList<Object> rsp = new NamedList<>(); rsp.add("stream", respBody); // Only case where stream should not be closed shouldClose = false; return rsp; } String procCt = processor.getContentType(); if (procCt != null) { String procMimeType = ContentType.parse(procCt).getMimeType().trim().toLowerCase(Locale.ROOT); String mimeType = ContentType.parse(contentType).getMimeType().trim().toLowerCase(Locale.ROOT); if (!procMimeType.equals(mimeType)) { // unexpected mime type String msg = "Expected mime type " + procMimeType + " but got " + mimeType + "."; Header encodingHeader = response.getEntity().getContentEncoding(); String encoding; if (encodingHeader != null) { encoding = encodingHeader.getValue(); } else { encoding = "UTF-8"; // try UTF-8 } try { msg = msg + " " + IOUtils.toString(respBody, encoding); } catch (IOException e) { throw new RemoteSolrException(baseUrl, httpStatus, "Could not parse response with encoding " + encoding, e); } throw new RemoteSolrException(baseUrl, httpStatus, msg, null); } } NamedList<Object> rsp = null; String charset = EntityUtils.getContentCharSet(response.getEntity()); try { rsp = processor.processResponse(respBody, charset); } catch (Exception e) { throw new RemoteSolrException(baseUrl, httpStatus, e.getMessage(), e); } if (httpStatus != HttpStatus.SC_OK) { NamedList<String> metadata = null; String reason = null; try { NamedList err = (NamedList) rsp.get("error"); if (err != null) { reason = (String) err.get("msg"); if (reason == null) { reason = (String) err.get("trace"); } metadata = (NamedList<String>) err.get("metadata"); } } catch (Exception ex) { } if (reason == null) { StringBuilder msg = new StringBuilder(); msg.append(response.getStatusLine().getReasonPhrase()).append("\n\n").append("request: ") .append(method.getURI()); reason = java.net.URLDecoder.decode(msg.toString(), UTF_8); } RemoteSolrException rss = new RemoteSolrException(baseUrl, httpStatus, reason, null); if (metadata != null) rss.setMetadata(metadata); throw rss; } return rsp; } catch (ConnectException e) { throw new SolrServerException("Server refused connection at: " + getBaseURL(), e); } catch (SocketTimeoutException e) { throw new SolrServerException("Timeout occured while waiting response from server at: " + getBaseURL(), e); } catch (IOException e) { throw new SolrServerException("IOException occured when talking to server at: " + getBaseURL(), e); } finally { if (shouldClose) { Utils.consumeFully(entity); } } }
From source file:com.serphacker.serposcope.scraper.http.ScrapClient.java
protected void initializeRequest(HttpRequestBase request, HttpClientContext context) { if (request.getFirstHeader("user-agent") == null) { request.setHeader("User-Agent", useragent); }/*from w w w . j av a2 s. c o m*/ for (Header requestHeader : requestHeaders) { request.setHeader(requestHeader); } RequestConfig.Builder configBuilder = RequestConfig .copy(request.getConfig() == null ? RequestConfig.DEFAULT : request.getConfig()); if (timeoutMS != null) { configBuilder.setConnectTimeout(timeoutMS); configBuilder.setConnectionRequestTimeout(timeoutMS); configBuilder.setSocketTimeout(timeoutMS); } if (maxRedirect == 0) { configBuilder.setRedirectsEnabled(false); } else { configBuilder.setMaxRedirects(maxRedirect); } RequestConfig config = configBuilder.build(); context.setAttribute(HttpClientContext.REQUEST_CONFIG, config); request.setConfig(config); }