List of usage examples for org.apache.http.impl.client HttpClientBuilder setDefaultRequestConfig
public final HttpClientBuilder setDefaultRequestConfig(final RequestConfig config)
From source file:org.apache.zeppelin.livy.BaseLivyInterpreter.java
private RestTemplate createRestTemplate() { String keytabLocation = getProperty("zeppelin.livy.keytab"); String principal = getProperty("zeppelin.livy.principal"); boolean isSpnegoEnabled = StringUtils.isNotEmpty(keytabLocation) && StringUtils.isNotEmpty(principal); HttpClient httpClient = null;//from ww w. j a va 2 s .com if (livyURL.startsWith("https:")) { String keystoreFile = getProperty("zeppelin.livy.ssl.trustStore"); String password = getProperty("zeppelin.livy.ssl.trustStorePassword"); if (StringUtils.isBlank(keystoreFile)) { throw new RuntimeException("No zeppelin.livy.ssl.trustStore specified for livy ssl"); } if (StringUtils.isBlank(password)) { throw new RuntimeException("No zeppelin.livy.ssl.trustStorePassword specified " + "for livy ssl"); } FileInputStream inputStream = null; try { inputStream = new FileInputStream(keystoreFile); KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType()); trustStore.load(new FileInputStream(keystoreFile), password.toCharArray()); SSLContext sslContext = SSLContexts.custom().loadTrustMaterial(trustStore).build(); SSLConnectionSocketFactory csf = new SSLConnectionSocketFactory(sslContext); HttpClientBuilder httpClientBuilder = HttpClients.custom().setSSLSocketFactory(csf); RequestConfig reqConfig = new RequestConfig() { @Override public boolean isAuthenticationEnabled() { return true; } }; httpClientBuilder.setDefaultRequestConfig(reqConfig); Credentials credentials = new Credentials() { @Override public String getPassword() { return null; } @Override public Principal getUserPrincipal() { return null; } }; CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials(AuthScope.ANY, credentials); httpClientBuilder.setDefaultCredentialsProvider(credsProvider); if (isSpnegoEnabled) { Registry<AuthSchemeProvider> authSchemeProviderRegistry = RegistryBuilder .<AuthSchemeProvider>create().register(AuthSchemes.SPNEGO, new SPNegoSchemeFactory()) .build(); httpClientBuilder.setDefaultAuthSchemeRegistry(authSchemeProviderRegistry); } httpClient = httpClientBuilder.build(); } catch (Exception e) { throw new RuntimeException("Failed to create SSL HttpClient", e); } finally { if (inputStream != null) { try { inputStream.close(); } catch (IOException e) { LOGGER.error("Failed to close keystore file", e); } } } } RestTemplate restTemplate = null; if (isSpnegoEnabled) { if (httpClient == null) { restTemplate = new KerberosRestTemplate(keytabLocation, principal); } else { restTemplate = new KerberosRestTemplate(keytabLocation, principal, httpClient); } } else { if (httpClient == null) { restTemplate = new RestTemplate(); } else { restTemplate = new RestTemplate(new HttpComponentsClientHttpRequestFactory(httpClient)); } } restTemplate.getMessageConverters().add(0, new StringHttpMessageConverter(Charset.forName("UTF-8"))); return restTemplate; }
From source file:com.netflix.spinnaker.orca.pipeline.util.HttpClientUtils.java
private static CloseableHttpClient httpClientWithServiceUnavailableRetryStrategy() { HttpClientBuilder httpClientBuilder = HttpClients.custom() .setServiceUnavailableRetryStrategy(new ServiceUnavailableRetryStrategy() { @Override//from w w w . j a v a2s .co m public boolean retryRequest(HttpResponse response, int executionCount, HttpContext context) { int statusCode = response.getStatusLine().getStatusCode(); HttpUriRequest currentReq = (HttpUriRequest) context .getAttribute(HttpCoreContext.HTTP_REQUEST); LOGGER.info("Response code {} for {}", statusCode, currentReq.getURI()); if (statusCode >= HttpStatus.SC_OK && statusCode <= 299) { return false; } boolean shouldRetry = (statusCode == 429 || RETRYABLE_500_HTTP_STATUS_CODES.contains(statusCode)) && executionCount <= MAX_RETRIES; if (!shouldRetry) { throw new RetryRequestException(String.format("Not retrying %s. Count %d, Max %d", currentReq.getURI(), executionCount, MAX_RETRIES)); } LOGGER.error("Retrying request on response status {}. Count {} Max is {}", statusCode, executionCount, MAX_RETRIES); return true; } @Override public long getRetryInterval() { return RETRY_INTERVAL; } }); httpClientBuilder.setRetryHandler((exception, executionCount, context) -> { HttpUriRequest currentReq = (HttpUriRequest) context.getAttribute(HttpCoreContext.HTTP_REQUEST); Uninterruptibles.sleepUninterruptibly(RETRY_INTERVAL, TimeUnit.MILLISECONDS); LOGGER.info("Encountered network error. Retrying request {}, Count {} Max is {}", currentReq.getURI(), executionCount, MAX_RETRIES); return executionCount <= MAX_RETRIES; }); httpClientBuilder.setDefaultRequestConfig(RequestConfig.custom().setConnectionRequestTimeout(TIMEOUT_MILLIS) .setConnectTimeout(TIMEOUT_MILLIS).setSocketTimeout(TIMEOUT_MILLIS).build()); return httpClientBuilder.build(); }
From source file:org.ow2.proactive.http.CommonHttpClientBuilder.java
public CloseableHttpClient build() { org.apache.http.impl.client.HttpClientBuilder internalHttpClientBuilder = createInternalHttpClientBuilder(); if (useSystemProperties) { internalHttpClientBuilder.useSystemProperties(); }/* w ww .j av a2s. c o m*/ if (overrideAllowAnyHostname != null) { if (overrideAllowAnyHostname) { acceptAnyHostname = true; } else { acceptAnyHostname = false; } } if (overrideAllowAnyCertificate != null) { if (overrideAllowAnyCertificate) { acceptAnyCertificate = true; } else { acceptAnyCertificate = false; } } if (acceptAnyCertificate) { internalHttpClientBuilder.setSslcontext(createSslContext()); } if (acceptAnyHostname) { internalHttpClientBuilder.setHostnameVerifier(SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); } if (maxConnections > 0) { internalHttpClientBuilder.setMaxConnPerRoute(maxConnections); internalHttpClientBuilder.setMaxConnTotal(maxConnections); } if (requestConfig != null) { internalHttpClientBuilder.setDefaultRequestConfig(requestConfig); } if (!useContentCompression) { internalHttpClientBuilder.disableContentCompression(); } return internalHttpClientBuilder.build(); }
From source file:com.hp.mqm.clt.RestClient.java
public RestClient(Settings settings) { this.settings = settings; HttpClientBuilder httpClientBuilder = HttpClients.custom(); cookieStore = new BasicCookieStore(); httpClientBuilder.setDefaultCookieStore(cookieStore); RequestConfig.Builder requestConfigBuilder = RequestConfig.custom().setCookieSpec(CookieSpecs.STANDARD) .setSocketTimeout(DEFAULT_SO_TIMEOUT).setConnectTimeout(DEFAULT_CONNECTION_TIMEOUT); // proxy setting if (StringUtils.isNotEmpty(settings.getProxyHost())) { HttpHost proxy = new HttpHost(settings.getProxyHost(), settings.getProxyPort()); requestConfigBuilder.setProxy(proxy); if (settings.getProxyUser() != null) { CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); credentialsProvider.setCredentials(new AuthScope(settings.getProxyHost(), settings.getProxyPort()), new UsernamePasswordCredentials(settings.getProxyUser(), settings.getProxyPassword())); httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider); }// www .j a v a 2 s .c om } httpClient = httpClientBuilder.setDefaultRequestConfig(requestConfigBuilder.build()).build(); }
From source file:org.commonjava.util.jhttpc.HttpFactory.java
public CloseableHttpClient createClient(final SiteConfig location) throws JHttpCException { CloseableHttpClient client;/*from w ww . jav a 2 s . c o m*/ if (location != null) { HttpClientBuilder builder = HttpClients.custom(); if (authenticator != null) { builder = authenticator.decorateClientBuilder(builder); } logger.debug("Using site config: {} for advanced client options", location); SiteConnectionConfig connConfig = new SiteConnectionConfig(location); final SSLConnectionSocketFactory sslFac = createSSLSocketFactory(location); if (sslFac != null) { // HostnameVerifier verifier = new SSLHostnameVerifierImpl( ); // builder.setSSLHostnameVerifier( verifier ); builder.setSSLSocketFactory(sslFac); connConfig.withSSLConnectionSocketFactory(sslFac); } ConnectionManagerTracker managerWrapper = connectionCache.getTrackerFor(connConfig); builder.setConnectionManager(managerWrapper.acquire()); if (location.getProxyHost() != null) { final HttpRoutePlanner planner = new DefaultProxyRoutePlanner( new HttpHost(location.getProxyHost(), getProxyPort(location))); builder.setRoutePlanner(planner); } final int timeout = 1000 * location.getRequestTimeoutSeconds(); builder.setDefaultRequestConfig(RequestConfig.custom().setConnectionRequestTimeout(timeout) .setSocketTimeout(timeout).setConnectTimeout(timeout).build()); client = new TrackedHttpClient(builder.build(), managerWrapper); // client = builder.build(); } else { client = HttpClients.createDefault(); } return client; }
From source file:com.clustercontrol.hub.session.FluentdTransferFactory.java
/** * ?????????/*from w ww. ja va 2 s .c o m*/ * */ @Override public Transfer createTansfer(final TransferInfo info, final PropertyBinder binder) throws TransferException { // HTTP ????? TransferDestProp urlProp = null; TransferDestProp connectTimeoutProp = null; TransferDestProp requestTimeoutProp = null; for (TransferDestProp prop : info.getDestProps()) { switch (prop.getName()) { case prop_url: urlProp = prop; break; case prop_connect_timeout: connectTimeoutProp = prop; break; case prop_request_timeout: requestTimeoutProp = prop; break; default: logger.warn(String.format("createTansfer() : unknown property(%s)", prop.getValue())); break; } } if (urlProp == null || urlProp.getValue() == null) throw new TransferException(String.format("createTansfer() : Value of \"%s\" must be set.", prop_url)); if (!urlPattern.matcher(urlProp.getValue()).matches()) throw new TransferException( String.format("createTansfer() : invalid url format. url=%s", urlProp.getValue())); final String urlStr = urlProp.getValue(); Integer timeout; try { timeout = Integer.valueOf(connectTimeoutProp.getValue()); } catch (NumberFormatException e) { timeout = DEFAULT_CONNECT_TIMEOUT; logger.warn(String.format("createTansfer() : can't regognize connectTimeout(%s) as number.", connectTimeoutProp.getValue())); } catch (NullPointerException e) { timeout = DEFAULT_CONNECT_TIMEOUT; logger.warn(String.format( "createTansfer() : connectTimeout is null, then use default value as connectTimeout(%s).", timeout)); } final Integer connectTimeout = timeout; try { timeout = Integer.valueOf(requestTimeoutProp.getValue()); } catch (NumberFormatException e) { timeout = DEFAULT_REQUEST_TIMEOUT; logger.warn(String.format("createTansfer() : can't regognize requestTimeout(%s) as number.", requestTimeoutProp.getValue())); } catch (NullPointerException e) { timeout = DEFAULT_CONNECT_TIMEOUT; logger.warn(String.format( "createTansfer() : requestTimeout is null, then use default value as requestTimeout(%s).", timeout)); } final Integer requestTimeout = timeout; // ?? return new Transfer() { private static final int BUFF_SIZE = 1024 * 1024; private static final int BODY_MAX_SIZE = 5 * BUFF_SIZE; private CloseableHttpClient client = null; /* * ?? * */ @Override public TransferNumericData transferNumerics(Iterable<TransferNumericData> numerics, TrasferCallback<TransferNumericData> callback) throws TransferException { TransferNumericData lastPosition = null; for (TransferNumericData numeric : numerics) { ObjectNode root = JsonNodeFactory.instance.objectNode(); root.put("item_name", numeric.key.getItemName()); root.put("display_name", numeric.key.getDisplayName()); root.put("monitor_id", numeric.key.getMonitorId()); root.put("facility_id", numeric.key.getFacilityid()); root.put("time", numeric.data.getTime()); root.put("value", numeric.data.getValue()); root.put("position", numeric.data.getPosition()); String url = binder.bind(numeric.key, numeric.data, urlStr); String data = root.toString(); try { send(url, data); lastPosition = numeric; if (callback != null) callback.onTransferred(lastPosition); } catch (Exception e) { logger.warn(e.getMessage(), e); internalError_monitor(numeric.key.getMonitorId(), data, e, url); break; } } return lastPosition; } /* * ?? * */ @Override public TransferStringData transferStrings(Iterable<TransferStringData> strings, TrasferCallback<TransferStringData> callback) throws TransferException { TransferStringData lastPosition = null; for (TransferStringData string : strings) { ObjectNode root = JsonNodeFactory.instance.objectNode(); root.put("target_name", string.key.getTargetName()); root.put("monitor_id", string.key.getMonitorId()); root.put("facility_id", string.key.getFacilityId()); root.put("log_format_id", string.data.getLogformatId()); root.put("time", string.data.getTime()); root.put("source", string.data.getValue()); for (CollectDataTag t : string.data.getTagList()) { root.put(t.getKey(), t.getValue()); } root.put("position", string.data.getDataId()); String url = binder.bind(string.key, string.data, urlStr); String data = root.toString(); try { send(url, data); lastPosition = string; if (callback != null) callback.onTransferred(lastPosition); } catch (Exception e) { logger.warn(e.getMessage(), e); internalError_monitor(string.key.getMonitorId(), data, e, url); break; } } return lastPosition; } /* * ?? * */ @Override public JobSessionEntity transferJobs(Iterable<JobSessionEntity> sessions, TrasferCallback<JobSessionEntity> callback) throws TransferException { JobSessionEntity lastPosition = null; for (JobSessionEntity session : sessions) { ObjectNode sessionNode = JsonNodeFactory.instance.objectNode(); sessionNode.put("ssession_id", session.getSessionId()); sessionNode.put("job_id", session.getJobId()); sessionNode.put("jobunit_id", session.getJobunitId()); sessionNode.put("schedule_date", session.getScheduleDate()); sessionNode.put("position", session.getPosition()); ArrayNode jobArray = sessionNode.putArray("jobs"); for (JobSessionJobEntity job : session.getJobSessionJobEntities()) { ObjectNode jobNode = jobArray.addObject(); jobNode.put("job_id", job.getId().getJobId()); jobNode.put("jobunit_id", job.getId().getJobunitId()); if (job.getScopeText() != null) jobNode.put("scope_text", job.getScopeText()); if (job.getStatus() != null) jobNode.put("status", job.getStatus()); if (job.getStartDate() != null) jobNode.put("start_date", job.getStartDate()); if (job.getEndDate() != null) jobNode.put("end_date", job.getEndDate()); if (job.getEndValue() != null) jobNode.put("end_value", job.getEndValue()); if (job.getEndStatus() != null) jobNode.put("end_status", job.getEndStatus()); if (job.getResult() != null) jobNode.put("result", job.getResult()); if (job.getJobInfoEntity() != null) jobNode.put("job_type", job.getJobInfoEntity().getJobType()); if (!job.getJobSessionNodeEntities().isEmpty()) { ArrayNode nodeArray = jobNode.putArray("nodes"); for (JobSessionNodeEntity node : job.getJobSessionNodeEntities()) { ObjectNode nodeNode = nodeArray.addObject(); nodeNode.put("facility_id", node.getId().getFacilityId()); nodeNode.put("node_name", node.getNodeName()); nodeNode.put("status", node.getStatus()); nodeNode.put("start_date", node.getStartDate()); nodeNode.put("end_date", node.getEndDate()); nodeNode.put("end_value", node.getEndValue()); nodeNode.put("message", node.getMessage()); nodeNode.put("result", node.getResult()); nodeNode.put("start_date", node.getStartDate()); nodeNode.put("startup_time", node.getStartupTime()); nodeNode.put("instance_id", node.getInstanceId()); } } } String url = binder.bind(session, urlStr); String data = sessionNode.toString(); try { send(url, data); lastPosition = session; if (callback != null) callback.onTransferred(lastPosition); } catch (Exception e) { logger.warn(e.getMessage(), e); internalError_session(session.getSessionId(), data, e, url); break; } } return lastPosition; } /* * ?? * */ @Override public EventLogEntity transferEvents(Iterable<EventLogEntity> events, TrasferCallback<EventLogEntity> callback) throws TransferException { EventLogEntity lastPosition = null; for (EventLogEntity event : events) { ObjectNode eventNode = JsonNodeFactory.instance.objectNode(); eventNode.put("monitor_id", event.getId().getMonitorId()); eventNode.put("monitor_detail_id", event.getId().getMonitorDetailId()); eventNode.put("plugin_id", event.getId().getPluginId()); eventNode.put("generation_date", event.getGenerationDate()); eventNode.put("facility_id", event.getId().getFacilityId()); eventNode.put("scope_text", event.getScopeText()); eventNode.put("application", event.getApplication()); eventNode.put("message", event.getMessage()); eventNode.put("message_org", event.getMessageOrg()); eventNode.put("priority", event.getPriority()); eventNode.put("confirm_flg", event.getConfirmFlg()); eventNode.put("confirm_date", event.getCommentDate()); eventNode.put("confirm_user", event.getCommentUser()); eventNode.put("duplication_count", event.getDuplicationCount()); eventNode.put("output_date", event.getId().getOutputDate()); eventNode.put("inhibited_flg", event.getInhibitedFlg()); eventNode.put("comment_date", event.getCommentDate()); eventNode.put("comment_user", event.getCommentUser()); eventNode.put("comment", event.getComment()); eventNode.put("position", event.getPosition()); String url = binder.bind(event, urlStr); String data = eventNode.toString(); try { send(url, data); lastPosition = event; if (callback != null) callback.onTransferred(lastPosition); } catch (Exception e) { logger.warn(e.getMessage(), e); internalError_monitor(event.getId().getMonitorId(), data, e, url); break; } } return lastPosition; } /* * ID ?? * */ @Override public String getDestTypeId() { return transfer_id; } /* * Fluentd ?????? HttpClient ?? * ????????? * */ private CloseableHttpClient getHttpClient() { if (client == null) { client = createHttpClient(); } return client; } /* * Fluentd ?????? HttpClient ?? * */ private CloseableHttpClient createHttpClient() { String proxyHost = null; Integer proxyPort = null; CredentialsProvider cledentialProvider = null; List<String> ignoreHostList = new ArrayList<>(); try { // Hinemos ??? proxyHost = HinemosPropertyUtil.getHinemosPropertyStr("hub.fluentd.proxy.host", null); Long proxyPortLong = HinemosPropertyUtil.getHinemosPropertyNum("hub.fluentd.proxy.port", null); if (proxyPortLong != null) proxyPort = proxyPortLong.intValue(); if (proxyPort == null) proxyPort = 80; String proxyUser = HinemosPropertyUtil.getHinemosPropertyStr("hub.fluentd.proxy.user", null); String proxyPassword = HinemosPropertyUtil.getHinemosPropertyStr("hub.fluentd.proxy.password", null); if (proxyHost != null && proxyPort != null) { logger.debug( "initializing fluentd proxy : proxyHost = " + proxyHost + ", port = " + proxyPort); String ignoreHostStr = HinemosPropertyUtil .getHinemosPropertyStr("hub.fluentd.proxy.ignorehosts", null); if (ignoreHostStr != null) { ignoreHostList = Arrays.asList(ignoreHostStr.split(",")); } if (proxyUser != null && proxyPassword != null) { cledentialProvider = new BasicCredentialsProvider(); cledentialProvider.setCredentials(new AuthScope(proxyHost, proxyPort), new UsernamePasswordCredentials(proxyUser, proxyPassword)); } } } catch (Throwable t) { logger.warn("invalid proxy configuration.", t); proxyHost = null; proxyPort = null; cledentialProvider = null; ignoreHostList = Collections.emptyList(); } List<Header> headers = new ArrayList<>(); HttpClientBuilder builder = HttpClients.custom().setDefaultCredentialsProvider(cledentialProvider) .setDefaultHeaders(headers); Builder requestBuilder = RequestConfig.custom().setCookieSpec(CookieSpecs.DEFAULT); if (connectTimeout != null) requestBuilder.setConnectTimeout(connectTimeout); if (connectTimeout != null) requestBuilder.setSocketTimeout(requestTimeout); builder.setDefaultRequestConfig(requestBuilder.build()); if (proxyHost != null) { Matcher m = urlPattern.matcher(urlStr); if (!m.matches()) throw new InternalError(String.format("invalid url(%s)", urlStr)); m.toMatchResult(); boolean ignore = false; String host = m.group("host"); for (String ignoreHost : ignoreHostList) { if (ignoreHost.equals(host)) { ignore = true; break; } } if (!ignore) { HttpHost proxy = new HttpHost(proxyHost, proxyPort, "http"); builder.setProxy(proxy); } } if (keepAlive) { headers.add(new BasicHeader(HTTP.CONN_DIRECTIVE, HTTP.CONN_KEEP_ALIVE)); } else { headers.add(new BasicHeader(HTTP.CONN_DIRECTIVE, HTTP.CONN_CLOSE)); } return builder.build(); } /* * ?? URL ??? * */ private void send(String url, String data) throws Exception { // URL ? Matcher m = urlPattern.matcher(url); if (!m.matches()) throw new InternalError(String.format("invalid url(%s)", urlStr)); m.toMatchResult(); String path = m.group("path"); if (path != null && !path.isEmpty()) { String host = m.group("host"); String port = m.group("port"); String[] paths = path.split("/"); for (int i = 1; i < paths.length; ++i) { paths[i] = URLEncoder.encode(paths[i], "utf-8"); } url = "http://" + host + (port == null || port.isEmpty() ? "" : (":" + port)); for (int i = 1; i < paths.length; ++i) { url += "/" + paths[i]; } } HttpPost requestPost = new HttpPost(url); requestPost.addHeader("content-type", "application/json"); requestPost.setEntity(new StringEntity(data, StandardCharsets.UTF_8)); logger.debug(String.format("send() : request start. url=%s", url)); int count = 0; int maxTryCount = PropertyConstants.hub_transfer_max_try_count.number(); while (count < maxTryCount) { try { long start = HinemosTime.currentTimeMillis(); try (CloseableHttpResponse response = getHttpClient().execute(requestPost)) { long responseTime = HinemosTime.currentTimeMillis() - start; logger.debug(String.format("send() : url=%s, responseTime=%d", url, responseTime)); int statusCode = response.getStatusLine().getStatusCode(); logger.debug(String.format("send() : url=%s, code=%d", url, statusCode)); if (statusCode == HttpStatus.SC_OK) { logger.debug(String.format("send() : url=%s, success=%s", url, response.getStatusLine().toString())); if (logger.isDebugEnabled()) { ByteArrayOutputStream out = new ByteArrayOutputStream(); try (InputStream in = response.getEntity().getContent()) { byte[] buffer = new byte[BUFF_SIZE]; while (out.size() < BODY_MAX_SIZE) { int len = in.read(buffer); if (len < 0) { break; } out.write(buffer, 0, len); } } String res = new String(out.toByteArray(), "UTF-8"); if (!res.isEmpty()) logger.debug(String.format("send() : url=%s, response=%s", url, res)); } } else { throw new RuntimeException( String.format("http status code isn't 200. code=%d, message=%s", statusCode, response.getStatusLine().toString())); } } logger.debug(String.format("send() : success. url=%s, count=%d", url, count)); break; } catch (RuntimeException e) { ++count; if (count < maxTryCount) { logger.debug(e.getMessage(), e); logger.debug(String.format( "send() : fail to send, and then wait to retry. url=%s, count=%d", url, count)); try { Thread.sleep(1000); } catch (InterruptedException e1) { logger.debug(e.getMessage()); } } else { throw new TransferException( String.format("send() : fail to send. url=%s, retry count=%d, error=\"%s\"", url, count, e.getMessage())); } } } } @Override public void close() throws Exception { if (client != null) client.close(); } private void internalError_session(String sessionId, String data, Exception error, String url) { internalError_session(sessionId, data, error.getMessage() == null || error.getMessage().isEmpty() ? error.getClass().getSimpleName() : error.getMessage(), url); } private void internalError_session(String sessionId, String data, String error, String url) { AplLogger.put(PriorityConstant.TYPE_WARNING, HinemosModuleConstant.HUB_TRANSFER, MessageConstant.MESSAGE_HUB_DATA_TRANSFER_FAILED, new String[] { info.getTransferId() }, String.format("error=%s%ntransferId=%s%ndestTypeId=%s%nsessionId=%s%ndata=%s%nurl=%s", error, info.getTransferId(), info.getDestTypeId(), sessionId, data, url)); } private void internalError_monitor(String sessionId, String data, Exception error, String url) { internalError_monitor(sessionId, data, error.getMessage() == null || error.getMessage().isEmpty() ? error.getClass().getSimpleName() : error.getMessage(), url); } private void internalError_monitor(String monitorId, String data, String error, String url) { AplLogger.put(PriorityConstant.TYPE_WARNING, HinemosModuleConstant.HUB_TRANSFER, MessageConstant.MESSAGE_HUB_DATA_TRANSFER_FAILED, new String[] { info.getTransferId() }, String.format("error=%s%ntransferId=%s%ndestTypeId=%s%nmonitorId=%s%ndata=%s%nurl=%s", error, info.getTransferId(), info.getDestTypeId(), monitorId, data, url)); } }; }
From source file:org.georchestra.security.Proxy.java
private void handleRequest(HttpServletRequest request, HttpServletResponse finalResponse, RequestType requestType, String sURL, boolean localProxy) { HttpClientBuilder htb = HttpClients.custom().disableRedirectHandling(); RequestConfig config = RequestConfig.custom().setSocketTimeout(this.httpClientTimeout).build(); htb.setDefaultRequestConfig(config); ///* ww w . j a v a2 s. c o m*/ // Handle http proxy for external request. // Proxy must be configured by system variables (e.g.: -Dhttp.proxyHost=proxy -Dhttp.proxyPort=3128) htb.setRoutePlanner(new SystemDefaultRoutePlanner(ProxySelector.getDefault())); HttpClient httpclient = htb.build(); HttpResponse proxiedResponse = null; int statusCode = 500; try { URL url = null; try { url = new URL(sURL); } catch (MalformedURLException e) { // not an url finalResponse.sendError(HttpServletResponse.SC_BAD_REQUEST, e.getMessage()); return; } // HTTP protocol is required if (!"http".equalsIgnoreCase(url.getProtocol()) && !"https".equalsIgnoreCase(url.getProtocol())) { finalResponse.sendError(HttpServletResponse.SC_BAD_REQUEST, "HTTP protocol expected. \"" + url.getProtocol() + "\" used."); return; } // check if proxy must filter on final host if (!strategyForFilteringRequests.allowRequest(url)) { finalResponse.sendError(HttpServletResponse.SC_BAD_REQUEST, "Host \"" + url.getHost() + "\" is not allowed to be requested"); return; } logger.debug("Final request -- " + sURL); HttpRequestBase proxyingRequest = makeRequest(request, requestType, sURL); headerManagement.configureRequestHeaders(request, proxyingRequest); try { Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); Header[] originalHeaders = proxyingRequest.getHeaders("sec-org"); String org = ""; for (Header originalHeader : originalHeaders) { org = originalHeader.getValue(); } // no OGC SERVICE log if request going through /proxy/?url= if (!request.getRequestURI().startsWith("/sec/proxy/")) { String[] roles = new String[] { "" }; try { Header[] rolesHeaders = proxyingRequest.getHeaders("sec-roles"); if (rolesHeaders.length > 0) { roles = rolesHeaders[0].getValue().split(";"); } } catch (Exception e) { logger.error("Unable to compute roles"); } statsLogger.info(OGCServiceMessageFormatter.format(authentication.getName(), sURL, org, roles)); } } catch (Exception e) { logger.error("Unable to log the request into the statistics logger", e); } if (localProxy) { // // Hack for geoserver // Should not be here. We must use a ProxyTarget class and // define // if Host header should be forwarded or not. // request.getHeader("Host"); proxyingRequest.setHeader("Host", request.getHeader("Host")); if (logger.isDebugEnabled()) { logger.debug("Host header set to: " + proxyingRequest.getFirstHeader("Host").getValue() + " for proxy request."); } } proxiedResponse = executeHttpRequest(httpclient, proxyingRequest); StatusLine statusLine = proxiedResponse.getStatusLine(); statusCode = statusLine.getStatusCode(); String reasonPhrase = statusLine.getReasonPhrase(); if (reasonPhrase != null && statusCode > 399) { if (logger.isWarnEnabled()) { logger.warn("Error occurred. statuscode: " + statusCode + ", reason: " + reasonPhrase); } if (statusCode == 401) { // // Handle case of basic authentication. // Header authHeader = proxiedResponse.getFirstHeader("WWW-Authenticate"); finalResponse.setHeader("WWW-Authenticate", (authHeader == null) ? "Basic realm=\"Authentication required\"" : authHeader.getValue()); } // 403 and 404 are handled by specific JSP files provided by the // security-proxy webapp if ((statusCode == 404) || (statusCode == 403)) { finalResponse.sendError(statusCode); return; } } headerManagement.copyResponseHeaders(request, request.getRequestURI(), proxiedResponse, finalResponse, this.targets); if (statusCode == 302 || statusCode == 301) { adjustLocation(request, proxiedResponse, finalResponse); } // get content type String contentType = null; if (proxiedResponse.getEntity() != null && proxiedResponse.getEntity().getContentType() != null) { contentType = proxiedResponse.getEntity().getContentType().getValue(); logger.debug("content-type detected: " + contentType); } // content type has to be valid if (isCharsetRequiredForContentType(contentType)) { doHandleRequestCharsetRequired(request, finalResponse, requestType, proxiedResponse, contentType); } else { logger.debug("charset not required for contentType: " + contentType); doHandleRequest(request, finalResponse, requestType, proxiedResponse); } } catch (IOException e) { // connection problem with the host logger.error("Exception occured when trying to connect to the remote host: ", e); try { finalResponse.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE); } catch (IOException e2) { // error occured while trying to return the // "service unavailable status" finalResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); } } finally { httpclient.getConnectionManager().shutdown(); } }
From source file:com.clustercontrol.http.util.GetHttpResponse.java
private CloseableHttpClient getHttpClient() throws KeyManagementException, NoSuchAlgorithmException, KeyStoreException { if (m_client == null) { List<Header> headers = new ArrayList<>(); HttpClientBuilder builder = HttpClients.custom().setDefaultCredentialsProvider(m_cledentialProvider) .setDefaultHeaders(headers); if (!this.m_needAuthSSLCert) { // SSL ?? TrustStrategy trustStrategy = new TrustStrategy() { @Override/* ww w. j a va2 s . c o m*/ public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException { return true; } }; builder.setSSLSocketFactory(new SSLConnectionSocketFactory( new SSLContextBuilder().loadTrustMaterial(null, trustStrategy).build(), new NoopHostnameVerifier())); } RequestConfig requestConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.DEFAULT) .setConnectTimeout(m_connectTimeout).setSocketTimeout(m_requestTimeout).build(); builder.setDefaultRequestConfig(requestConfig); if (m_proxyHost != null) { HttpHost proxy = new HttpHost(m_proxyHost, m_proxyPort, m_proxyScheme == null ? "https" : m_proxyScheme); if (m_proxyUser != null && m_proxyPassword != null) { m_cledentialProvider.setCredentials(new AuthScope(proxy.getHostName(), proxy.getPort()), new UsernamePasswordCredentials(m_proxyUser, m_proxyPassword)); } builder.setProxy(proxy); } if (m_userAgent != null) { headers.add(new BasicHeader(HTTP.USER_AGENT, m_userAgent)); } if (m_cancelProxyCache) { // ? // https://www.ipa.go.jp/security/awareness/vendor/programmingv2/contents/405.html headers.add(new BasicHeader("Cache-Control", "no-cache")); headers.add(new BasicHeader("Pragma", "no-cache")); } if (keepAlive) { headers.add(new BasicHeader(HTTP.CONN_DIRECTIVE, HTTP.CONN_KEEP_ALIVE)); } else { headers.add(new BasicHeader(HTTP.CONN_DIRECTIVE, HTTP.CONN_CLOSE)); } m_client = builder.build(); } return m_client; }
From source file:org.syncany.operations.init.ApplicationLink.java
private CloseableHttpClient createHttpClient() { RequestConfig.Builder requestConfigBuilder = RequestConfig.custom().setSocketTimeout(2000) .setConnectTimeout(2000).setRedirectsEnabled(false); HttpClientBuilder httpClientBuilder = HttpClientBuilder.create(); // do we use a https proxy? String proxyHost = System.getProperty("https.proxyHost"); String proxyPortStr = System.getProperty("https.proxyPort"); String proxyUser = System.getProperty("https.proxyUser"); String proxyPassword = System.getProperty("https.proxyPassword"); if (proxyHost != null && proxyPortStr != null) { try {/*from w ww . ja va 2 s .co m*/ Integer proxyPort = Integer.parseInt(proxyPortStr); requestConfigBuilder.setProxy(new HttpHost(proxyHost, proxyPort)); logger.log(Level.INFO, "Using proxy: " + proxyHost + ":" + proxyPort); if (proxyUser != null && proxyPassword != null) { logger.log(Level.INFO, "Proxy required credentials; using '" + proxyUser + "' (username) and *** (hidden password)"); CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials(new AuthScope(proxyHost, proxyPort), new UsernamePasswordCredentials(proxyUser, proxyPassword)); httpClientBuilder.setDefaultCredentialsProvider(credsProvider); } } catch (NumberFormatException e) { logger.log(Level.WARNING, "Invalid proxy settings found. Not using proxy.", e); } } httpClientBuilder.setDefaultRequestConfig(requestConfigBuilder.build()); return httpClientBuilder.build(); }