private HttpClient createHttpClient() { HttpClient client = new HttpClient(); // added by Unicon to handle proxy hosts String proxyHost = System.getProperty( "http.proxyHost" ); if ( proxyHost != null ) { int proxyPort = -1; String proxyPortStr = System.getProperty( "http.proxyPort" ); if (proxyPortStr != null) { try { proxyPort = Integer.parseInt( proxyPortStr ); } catch (NumberFormatException e) { logger.warn("Invalid number for system property http.proxyPort ("+proxyPortStr+"), using default port instead"); } } ProxyHost proxy = new ProxyHost( proxyHost, proxyPort ); client.getHostConfiguration().setProxyHost( proxy ); } // added by Unicon to force encoding to UTF-8 client.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, UTF8_CHARSET); client.getParams().setParameter(HttpMethodParams.HTTP_ELEMENT_CHARSET, UTF8_CHARSET); client.getParams().setParameter(HttpMethodParams.HTTP_URI_CHARSET, UTF8_CHARSET); HttpConnectionManagerParams connParams = client.getHttpConnectionManager().getParams(); if(this.kalturaConfiguration.getTimeout() != 0) { connParams.setSoTimeout(this.kalturaConfiguration.getTimeout()); connParams.setConnectionTimeout(this.kalturaConfiguration.getTimeout()); } client.getHttpConnectionManager().setParams(connParams); return client; }
public String getProxyHost(String urlString) { String result = urlString; try { URL url = new URL(urlString); ProxyHost hostInfo = PluginProxyUtil.detectProxy(url); if (hostInfo != null) { result = hostInfo.getHostName(); } } catch (Exception e) { e.printStackTrace(); } return result; }
public int getProxyPort(String urlString) { int result = 80; try { URL url = new URL(urlString); ProxyHost hostInfo = PluginProxyUtil.detectProxy(url); if (hostInfo != null) { result = hostInfo.getPort(); } } catch (Exception e) { e.printStackTrace(); } return result; }
public SlackInfo tokenAuthen(String token, String proxyUrl, int proxyPort) { HttpClient client = new HttpClient(); if (proxyUrl != null) { HostConfiguration hostConfiguration = new HostConfiguration(); hostConfiguration.setProxyHost(new ProxyHost(proxyUrl, proxyPort)); client.setHostConfiguration(hostConfiguration); } GetMethod getMethod = new GetMethod(SLACK_RTM_AUTHEN_URL + token); SlackInfo slackInfo = new SlackInfo(); try { int httpStatus = client.executeMethod(getMethod); if (httpStatus == HttpStatus.SC_OK) { ObjectMapper mapper = new ObjectMapper().setVisibility(JsonMethod.FIELD, JsonAutoDetect.Visibility.ANY); mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false); return mapper.readValue(getMethod.getResponseBodyAsStream(), SlackInfo.class); } else { slackInfo.setError("http_status_" + httpStatus); return slackInfo; } } catch (IOException ex) { slackInfo.setError("exception " + ex.getMessage()); Logger.getLogger(SlackAuthen.class.getName()).log(Level.SEVERE, null, ex); } finally { getMethod.releaseConnection(); } return slackInfo; }
/** * Returns the Proxy Host information using settings from the java plugin. * * @param sampleURL the url target for which proxy host information is * required * @return the proxy host info (name and port) or null if a direct * connection is allowed to the target url. * @throws ProxyDetectionException if detection failed */ public static ProxyHost detectProxy(URL sampleURL) throws ProxyDetectionException { ProxyHost result = null; String javaVers = System.getProperty("java.runtime.version"); if (LOG.isDebugEnabled()) { LOG.debug("About to attempt auto proxy detection under Java " + "version:"+javaVers); } // If specific, known detection methods fail may try fallback // detection method boolean invokeFailover = false; if (javaVers.startsWith("1.3")) { result = detectProxySettingsJDK13(sampleURL); if (result == null) { invokeFailover = true; } } else if (javaVers.startsWith("1.4") || (javaVers.startsWith("1.5") || javaVers.startsWith("1.6"))) { result = detectProxySettingsJDK14_JDK15_JDK16(sampleURL); if (result == null) { invokeFailover = true; } } else { if (LOG.isDebugEnabled()) { LOG.debug("Sun Plugin reported java version not 1.3.X, " + "1.4.X, 1.5.X or 1.6.X - trying failover detection..."); } invokeFailover = true; } if (invokeFailover) { if (LOG.isDebugEnabled()) { LOG.debug("Using failover proxy detection..."); } result = getPluginProxyConfigSettings(); } if (NO_PROXY_HOST.equals(result)) { result = null; } return result; }
/** * This method performs the proxying of the request to the target address. * * @param target The target address. Has to be a fully qualified address. The request is send as-is to this address. * @param hsRequest The request data which should be send to the * @param hsResponse The response data which will contain the data returned by the proxied request to target. * @throws java.io.IOException Passed on from the connection logic. */ public static void execute(final String target, final HttpServletRequest hsRequest, final HttpServletResponse hsResponse) throws IOException { if ( log.isInfoEnabled() ) { log.info("execute, target is " + target); log.info("response commit state: " + hsResponse.isCommitted()); } if (StringUtils.isBlank(target)) { log.error("The target address is not given. Please provide a target address."); return; } log.info("checking url"); final URL url; try { url = new URL(target); } catch (MalformedURLException e) { log.error("The provided target url is not valid.", e); return; } log.info("seting up the host configuration"); final HostConfiguration config = new HostConfiguration(); ProxyHost proxyHost = getUseProxyServer((String) hsRequest.getAttribute("use-proxy")); if (proxyHost != null) config.setProxyHost(proxyHost); final int port = url.getPort() != -1 ? url.getPort() : url.getDefaultPort(); config.setHost(url.getHost(), port, url.getProtocol()); if ( log.isInfoEnabled() ) log.info("config is " + config.toString()); final HttpMethod targetRequest = setupProxyRequest(hsRequest, url); if (targetRequest == null) { log.error("Unsupported request method found: " + hsRequest.getMethod()); return; } //perform the reqeust to the target server final HttpClient client = new HttpClient(new SimpleHttpConnectionManager()); if (log.isInfoEnabled()) { log.info("client state" + client.getState()); log.info("client params" + client.getParams().toString()); log.info("executeMethod / fetching data ..."); } final int result; if (targetRequest instanceof EntityEnclosingMethod) { final RequestProxyCustomRequestEntity requestEntity = new RequestProxyCustomRequestEntity( hsRequest.getInputStream(), hsRequest.getContentLength(), hsRequest.getContentType()); final EntityEnclosingMethod entityEnclosingMethod = (EntityEnclosingMethod) targetRequest; entityEnclosingMethod.setRequestEntity(requestEntity); result = client.executeMethod(config, entityEnclosingMethod); } else { result = client.executeMethod(config, targetRequest); } //copy the target response headers to our response setupResponseHeaders(targetRequest, hsResponse); InputStream originalResponseStream = targetRequest.getResponseBodyAsStream(); //the body might be null, i.e. for responses with cache-headers which leave out the body if (originalResponseStream != null) { OutputStream responseStream = hsResponse.getOutputStream(); copyStream(originalResponseStream, responseStream); } log.info("set up response, result code was " + result); }
/** * Returns the Proxy Host information using settings from the java plugin. * * @param sampleURL the url target for which proxy host information is * required * @return the proxy host info (name and port) or null if a direct * connection is allowed to the target url. * @throws ProxyDetectionException if detection failed */ public static ProxyHost detectProxy(URL sampleURL) throws ProxyDetectionException { ProxyHost result = null; String javaVers = System.getProperty("java.runtime.version"); if (LOG.isDebugEnabled()) { LOG.debug("About to attempt auto proxy detection under Java " + "version:"+javaVers); } // If specific, known detection methods fail may try fallback // detection method boolean invokeFailover = false; if (javaVers.startsWith("1.3")) { result = detectProxySettingsJDK13(sampleURL); if (result == null) { invokeFailover = true; } } else if (javaVers.startsWith("1.4")) { result = detectProxySettingsJDK14(sampleURL); if (result == null) { invokeFailover = true; } } else if (javaVers.startsWith("1.5")) { invokeFailover = true; } else { if (LOG.isDebugEnabled()) { LOG.debug("Sun Plugin reported java version not 1.3.X, " + "1.4.X or 1.5.X - trying failover detection..."); } invokeFailover = true; } if (invokeFailover) { if (LOG.isDebugEnabled()) { LOG.debug("Using failover proxy detection..."); } result = getPluginProxyConfigSettings(); } if (NO_PROXY_HOST.equals(result)) { result = null; } return result; }