List of usage examples for org.apache.http.client.config RequestConfig copy
public static RequestConfig.Builder copy(final RequestConfig config)
From source file:gobblin.writer.http.AbstractHttpWriterBuilder.java
public B fromConfig(Config config) { config = config.withFallback(FALLBACK); RequestConfig requestConfig = RequestConfig.copy(RequestConfig.DEFAULT) .setSocketTimeout(config.getInt(REQUEST_TIME_OUT_MS_KEY)) .setConnectTimeout(config.getInt(CONNECTION_TIME_OUT_MS_KEY)) .setConnectionRequestTimeout(config.getInt(CONNECTION_TIME_OUT_MS_KEY)).build(); getHttpClientBuilder().setDefaultRequestConfig(requestConfig); if (config.hasPath(STATIC_SVC_ENDPOINT)) { try {// ww w . ja v a 2 s . c o m svcEndpoint = Optional.of(new URI(config.getString(AbstractHttpWriterBuilder.STATIC_SVC_ENDPOINT))); } catch (URISyntaxException e) { throw new RuntimeException(e); } } String connMgrStr = config.getString(HTTP_CONN_MANAGER); switch (ConnManager.valueOf(connMgrStr.toUpperCase())) { case BASIC: httpConnManager = new BasicHttpClientConnectionManager(); break; case POOLING: PoolingHttpClientConnectionManager poolingConnMgr = new PoolingHttpClientConnectionManager(); poolingConnMgr.setMaxTotal(config.getInt(POOLING_CONN_MANAGER_MAX_TOTAL_CONN)); poolingConnMgr.setDefaultMaxPerRoute(config.getInt(POOLING_CONN_MANAGER_MAX_PER_CONN)); httpConnManager = poolingConnMgr; break; default: throw new IllegalArgumentException(connMgrStr + " is not supported"); } LOG.info("Using " + httpConnManager.getClass().getSimpleName()); return typedSelf(); }
From source file:mx.openpay.client.core.impl.DefaultHttpServiceClient.java
@Override public void setSocketTimeout(final int timeout) { this.requestConfig = RequestConfig.copy(this.requestConfig).setSocketTimeout(timeout).build(); }
From source file:org.apache.tamaya.etcd.EtcdAccessor.java
/** * Get the etcd server version.//from w ww. j a v a 2s . co m * * @return the etcd server version, never null. */ public String getVersion() { String version = "<ERROR>"; try { final CloseableHttpClient httpclient = HttpClients.createDefault(); final HttpGet httpGet = new HttpGet(serverURL + "/version"); httpGet.setConfig(RequestConfig.copy(RequestConfig.DEFAULT).setSocketTimeout(socketTimeout) .setConnectTimeout(timeout).build()); try (CloseableHttpResponse response = httpclient.execute(httpGet)) { if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { final HttpEntity entity = response.getEntity(); // and ensure it is fully consumed version = EntityUtils.toString(entity); EntityUtils.consume(entity); } } return version; } catch (final Exception e) { LOG.log(Level.INFO, "Error getting etcd version from: " + serverURL, e); } return version; }
From source file:io.joynr.messaging.http.HttpMessageSender.java
public void sendMessage(final MessageContainer messageContainer, final FailureAction failureAction) { logger.trace("SEND messageId: {} channelId: {}", messageContainer.getMessageId(), messageContainer.getChannelId()); HttpContext context = new BasicHttpContext(); String channelId = messageContainer.getChannelId(); String messageId = messageContainer.getMessageId(); if (messageContainer.isExpired()) { logger.error("SEND executionQueue.run channelId: {}, messageId: {} TTL expired: ", messageId, messageContainer.getExpiryDate()); failureAction.execute(new JoynrTimeoutException(messageContainer.getExpiryDate())); return;/*from ww w .j av a2 s .c o m*/ } // execute http command to send CloseableHttpResponse response = null; String sendUrl = null; try { String serializedMessage = messageContainer.getSerializedMessage(); sendUrl = urlResolver.getSendUrl(messageContainer.getChannelId()); logger.debug("SENDING message channelId: {}, messageId: {} toUrl: {}", new String[] { channelId, messageId, sendUrl }); if (sendUrl == null) { logger.error("SEND executionQueue.run channelId: {}, messageId: {} No channelId found", messageId, messageContainer.getExpiryDate()); failureAction.execute(new JoynrMessageNotSentException("no channelId found")); return; } HttpPost httpPost = httpRequestFactory.createHttpPost(URI.create(sendUrl)); httpPost.addHeader(new BasicHeader(httpConstants.getHEADER_CONTENT_TYPE(), httpConstants.getAPPLICATION_JSON() + ";charset=UTF-8")); httpPost.setEntity(new StringEntity(serializedMessage, "UTF-8")); // Clone the default config Builder requestConfigBuilder = RequestConfig.copy(defaultRequestConfig); requestConfigBuilder.setConnectionRequestTimeout(httpConstants.getSEND_MESSAGE_REQUEST_TIMEOUT()); httpPost.setConfig(requestConfigBuilder.build()); response = httpclient.execute(httpPost, context); StatusLine statusLine = response.getStatusLine(); int statusCode = statusLine.getStatusCode(); String statusText = statusLine.getReasonPhrase(); switch (statusCode) { case HttpURLConnection.HTTP_OK: case HttpURLConnection.HTTP_CREATED: logger.debug("SEND to ChannelId: {} messageId: {} completed successfully", channelId, messageId); break; case HttpURLConnection.HTTP_BAD_REQUEST: HttpEntity entity = response.getEntity(); if (entity == null) { logger.error( "SEND to ChannelId: {} messageId: {} completed in error. No further reason found in message body", channelId, messageId); return; } String body = EntityUtils.toString(entity, "UTF-8"); JoynrMessagingError error = objectMapper.readValue(body, JoynrMessagingError.class); JoynrMessagingErrorCode joynrMessagingErrorCode = JoynrMessagingErrorCode .getJoynrMessagingErrorCode(error.getCode()); logger.error(error.toString()); switch (joynrMessagingErrorCode) { case JOYNRMESSAGINGERROR_CHANNELNOTFOUND: failureAction.execute(new JoynrChannelMissingException("Channel does not exist. Status: " + statusCode + " error: " + error.getCode() + "reason:" + error.getReason())); break; default: logger.error("SEND error channelId: {}, messageId: {} url: {} error: {} code: {} reason: {} ", new Object[] { channelId, messageId, sendUrl, statusText, error.getCode(), error.getReason() }); failureAction.execute(new JoynrCommunicationException("Http Error while communicating: " + statusText + body + " error: " + error.getCode() + "reason:" + error.getReason())); break; } break; default: logger.error("SEND to ChannelId: {} messageId: {} - unexpected response code: {} reason: {}", new Object[] { channelId, messageId, statusCode, statusText }); break; } } catch (Exception e) { // An exception occured - this could still be a communication error (e.g Connection refused) logger.error("SEND error channelId: {}, messageId: {} url: {} error: {}", new Object[] { channelId, messageId, sendUrl, e.getMessage() }); failureAction.execute(new JoynrCommunicationException( e.getClass().getName() + "Exception while communicating. error: " + e.getMessage())); } finally { if (response != null) { try { response.close(); } catch (IOException e) { } } } }
From source file:dal.arris.DeviceDAO.java
private void prepare() { PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(); cm.setMaxTotal(1);/*from w ww . j a v a 2 s .co m*/ cm.setDefaultMaxPerRoute(1); HttpHost ip = new HttpHost("10.200.6.150", 80); cm.setMaxPerRoute(new HttpRoute(ip), 50); // Cookies RequestConfig globalConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.DEFAULT).build(); httpclient = HttpClients.custom().setConnectionManager(cm).setDefaultRequestConfig(globalConfig).build(); String auth = end.getCred().getUser() + ":" + end.getCred().getPass(); byte[] encodedAuth = Base64.encodeBase64(auth.getBytes(Charset.forName("UTF-8"))); authHeader = "Basic " + new String(encodedAuth); localConfig = RequestConfig.copy(globalConfig).setCookieSpec(CookieSpecs.STANDARD_STRICT).build(); }
From source file:fredboat.audio.player.AbstractPlayer.java
public static AudioPlayerManager registerSourceManagers(AudioPlayerManager mng) { mng.registerSourceManager(new PlaylistImportSourceManager()); //Determine which Source managers are enabled //By default, all are enabled except HttpAudioSources if (Config.CONFIG.isYouTubeEnabled()) { YoutubeAudioSourceManager youtubeAudioSourceManager = new YoutubeAudioSourceManager(); youtubeAudioSourceManager.configureRequests( config -> RequestConfig.copy(config).setCookieSpec(CookieSpecs.IGNORE_COOKIES).build()); mng.registerSourceManager(youtubeAudioSourceManager); }/*from w w w .j a va 2 s.c o m*/ if (Config.CONFIG.isSoundCloudEnabled()) { mng.registerSourceManager(new SoundCloudAudioSourceManager()); } if (Config.CONFIG.isBandCampEnabled()) { mng.registerSourceManager(new BandcampAudioSourceManager()); } if (Config.CONFIG.isTwitchEnabled()) { mng.registerSourceManager(new TwitchStreamAudioSourceManager()); } if (Config.CONFIG.isVimeoEnabled()) { mng.registerSourceManager(new VimeoAudioSourceManager()); } if (Config.CONFIG.isMixerEnabled()) { mng.registerSourceManager(new BeamAudioSourceManager()); } if (Config.CONFIG.isSpotifyEnabled()) { mng.registerSourceManager(new SpotifyPlaylistSourceManager()); } if (Config.CONFIG.isHttpEnabled()) { //add new source managers above the HttpAudio one, because it will either eat your request or throw an exception //so you will never reach a source manager below it mng.registerSourceManager(new HttpSourceManager()); } return mng; }
From source file:no.api.meteo.client.DefaultMeteoClient.java
private RequestConfig createRequestConfig() { return RequestConfig.copy(defaultRequestConfig).setSocketTimeout(timeout).setConnectTimeout(timeout) .setConnectionRequestTimeout(timeout).build(); }
From source file:org.apache.tamaya.etcd.EtcdAccessor.java
/** * Ask etcd for a single key, value pair. Hereby the response returned from * etcd:/*from w ww.ja v a 2 s . c om*/ * * <pre> * { * "action": "get", * "node": { * "createdIndex": 2, * "key": "/message", * "modifiedIndex": 2, * "value": "Hello world" * } * } * </pre> * * is mapped to: * * <pre> * key=value * _key.source=[etcd]http://127.0.0.1:4001 * _key.createdIndex=12 * _key.modifiedIndex=34 * _key.ttl=300 * _key.expiration=... * </pre> * * @param key the requested key * @return the mapped result, including meta-entries. */ public Map<String, String> get(String key) { final Map<String, String> result = new HashMap<>(); try { final HttpGet httpGet = new HttpGet(serverURL + "/v2/keys/" + key); httpGet.setConfig(RequestConfig.copy(RequestConfig.DEFAULT).setSocketTimeout(socketTimeout) .setConnectionRequestTimeout(timeout).setConnectTimeout(connectTimeout).build()); try (CloseableHttpResponse response = httpclient.execute(httpGet)) { if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { final HttpEntity entity = response.getEntity(); final JsonReader reader = readerFactory .createReader(new StringReader(EntityUtils.toString(entity))); final JsonObject o = reader.readObject(); final JsonObject node = o.getJsonObject("node"); if (node.containsKey("value")) { result.put(key, node.getString("value")); result.put("_" + key + ".source", "[etcd]" + serverURL); } if (node.containsKey("createdIndex")) { result.put("_" + key + ".createdIndex", String.valueOf(node.getInt("createdIndex"))); } if (node.containsKey("modifiedIndex")) { result.put("_" + key + ".modifiedIndex", String.valueOf(node.getInt("modifiedIndex"))); } if (node.containsKey("expiration")) { result.put("_" + key + ".expiration", String.valueOf(node.getString("expiration"))); } if (node.containsKey("ttl")) { result.put("_" + key + ".ttl", String.valueOf(node.getInt("ttl"))); } EntityUtils.consume(entity); } else { result.put("_" + key + ".NOT_FOUND.target", "[etcd]" + serverURL); } } } catch (final Exception e) { LOG.log(Level.INFO, "Error reading key '" + key + "' from etcd: " + serverURL, e); result.put("_ERROR", "Error reading key '" + key + "' from etcd: " + serverURL + ": " + e.toString()); } return result; }
From source file:com.ritesh.idea.plugin.util.HttpRequestBuilder.java
private HttpRequestBuilder() { requestConfig = RequestConfig.copy(RequestConfig.DEFAULT).setConnectTimeout(CONNECT_TIMEOUT).build(); }
From source file:org.aliuge.crawler.fetcher.DefaultFetcher.java
public PageFetchResult fetch(WebURL webUrl, boolean proxy) { PageFetchResult fetchResult = new PageFetchResult(); String toFetchURL = webUrl.getUrl(); HttpGet get = new HttpGet(toFetchURL); get.addHeader("Accept-Encoding", "gzip"); get.addHeader("User-Agent", config.getAgent()); RequestConfig requestConfig = null;/*from www.j a va2 s .c o m*/ CloseableHttpResponse response = null; synchronized (mutex) { long now = (new Date()).getTime(); if (now - lastFetchTime < ((FetchConfig) config).getDelayBetweenRequests()) { try { Thread.sleep(((FetchConfig) config).getDelayBetweenRequests() - (now - lastFetchTime)); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } lastFetchTime = (new Date()).getTime(); } int statusCode = 0; int count = 5; while (statusCode != HttpStatus.SC_OK && count-- > 0) { HttpHost proxyHost = null; if (proxy) { proxyHost = getProxyIp(); if (proxyHost != null) requestConfig = RequestConfig.copy(defaultRequestConfig).setSocketTimeout(10000) .setConnectTimeout(10000).setProxy(proxyHost).build(); } get.setConfig(requestConfig); try { response = httpClient.execute(get); statusCode = response.getStatusLine().getStatusCode(); fetchResult.setEntity(response.getEntity()); fetchResult.setResponseHeaders(response.getAllHeaders()); } catch (IOException e) { // e.printStackTrace(); // log.info("Fatal transport error: " + e.getMessage()+ // " while fetching " + toFetchURL + " (link found in doc #"+ // webUrl.getParentDocid() + ")"); addFailedProxy(proxyHost.toHostString()); /* * if (null != get) get.abort(); * fetchResult.setStatusCode(CustomFetchStatus * .FatalTransportError); */ // return fetchResult; } } fetchResult.setStatusCode(statusCode); fetchResult.setFetchedUrl(toFetchURL); if (fetchResult.getStatusCode() == HttpStatus.SC_OK) { long size = fetchResult.getEntity().getContentLength(); if (size == -1) { Header length = response.getLastHeader("Content-Length"); if (length == null) { length = response.getLastHeader("Content-length"); } if (length != null) { size = Integer.parseInt(length.getValue()); } else { size = -1; } } if (size > ((FetchConfig) config).getMaxDownloadSizePerPage()) { fetchResult.setStatusCode(CustomFetchStatus.PageTooBig); get.abort(); return fetchResult; } // fetchResult.setStatusCode(HttpStatus.SC_OK); return fetchResult; } get.abort(); fetchResult.setStatusCode(CustomFetchStatus.UnknownError); return fetchResult; }