List of usage examples for org.apache.commons.httpclient.contrib.proxy PluginProxyUtil detectProxy
public static ProxyHost detectProxy(URL sampleURL) throws ProxyDetectionException
From source file:org.apache.commons.httpclient.contrib.proxy.PluginProxyTestApplet.java
private void detectProxy() { String urlString = urlTextField.getText(); if (urlString == null || "".equals(urlString)) { JOptionPane.showMessageDialog(super.getRootPane(), "URL can't be empty", "Missing URL", JOptionPane.ERROR_MESSAGE); return;/*from ww w. j a va 2 s . c o m*/ } if (!urlString.startsWith("http://")) { urlString = "http://" + urlString; } try { URL url = new URL(urlString); HttpHost hostInfo = PluginProxyUtil.detectProxy(url); if (hostInfo != null) { hostLabel.setText(hostInfo.getHostName()); portLabel.setText("" + hostInfo.getPort()); } else { hostLabel.setText("none"); portLabel.setText("none"); } grid.validate(); } catch (ProxyDetectionException e) { JOptionPane.showMessageDialog(getRootPane(), e.getMessage(), "Proxy Detection Failed", JOptionPane.ERROR_MESSAGE); e.printStackTrace(); } catch (Exception e) { JOptionPane.showMessageDialog(getRootPane(), e.getMessage(), "Unexpected Exception", JOptionPane.ERROR_MESSAGE); e.printStackTrace(); } }
From source file:org.apache.commons.httpclient.contrib.proxy.PluginProxyTestApplet.java
public String getProxyHost(String urlString) { String result = urlString;//from ww w.jav a 2s . c o m try { URL url = new URL(urlString); HttpHost hostInfo = PluginProxyUtil.detectProxy(url); if (hostInfo != null) { result = hostInfo.getHostName(); } } catch (Exception e) { e.printStackTrace(); } return result; }
From source file:org.apache.commons.httpclient.contrib.proxy.PluginProxyTestApplet.java
public int getProxyPort(String urlString) { int result = 80; try {//from w w w. j a v a 2s . c om URL url = new URL(urlString); HttpHost hostInfo = PluginProxyUtil.detectProxy(url); if (hostInfo != null) { result = hostInfo.getPort(); } } catch (Exception e) { e.printStackTrace(); } return result; }
From source file:org.jets3t.apps.uploader.Uploader.java
/** * Retrieves a signed PUT URL from the given URL address. * The URL must point at a server-side script or service that accepts POST messages. * The POST message will include parameters for all the items in uploaderProperties, * that is everything in the file uploader.properties plus all the applet's parameters. * Based on this input, the server-side script decides whether to allow access and return * a signed PUT URL.//from w ww . j ava2 s . c o m * * @param credsProviderParamName * the name of the parameter containing the server URL target for the PUT request. * @return * the AWS credentials provided by the server-side script if access was allowed, null otherwise. * * @throws HttpException * @throws Exception */ private GatekeeperMessage contactGatewayServer(S3Object[] objects) throws Exception { // Retrieve credentials from URL location value by the property 'credentialsServiceUrl'. String gatekeeperUrl = uploaderProperties.getStringProperty("gatekeeperUrl", "Missing required property gatekeeperUrl"); /* * Build Gatekeeper request. */ GatekeeperMessage gatekeeperMessage = new GatekeeperMessage(); gatekeeperMessage.addApplicationProperties(userInputProperties); // Add User inputs as application properties. gatekeeperMessage.addApplicationProperties(parametersMap); // Add any Applet/Application parameters as application properties. // Make the Uploader's identifier available to Gatekeeper for version compatibility checking (if necessary) gatekeeperMessage.addApplicationProperty(GatekeeperMessage.PROPERTY_CLIENT_VERSION_ID, UPLOADER_VERSION_ID); // If a prior failure has occurred, add information about this failure. if (priorFailureException != null) { gatekeeperMessage.addApplicationProperty(GatekeeperMessage.PROPERTY_PRIOR_FAILURE_MESSAGE, priorFailureException.getMessage()); // Now reset the prior failure variable. priorFailureException = null; } // Add all S3 objects as candiates for PUT signing. for (int i = 0; i < objects.length; i++) { SignatureRequest signatureRequest = new SignatureRequest(SignatureRequest.SIGNATURE_TYPE_PUT, objects[i].getKey()); signatureRequest.setObjectMetadata(objects[i].getMetadataMap()); gatekeeperMessage.addSignatureRequest(signatureRequest); } /* * Build HttpClient POST message. */ // Add all properties/parameters to credentials POST request. HttpPost postMethod = new HttpPost(gatekeeperUrl); Properties properties = gatekeeperMessage.encodeToProperties(); Iterator<Map.Entry<Object, Object>> propsIter = properties.entrySet().iterator(); List<NameValuePair> parameters = new ArrayList<NameValuePair>(properties.size()); while (propsIter.hasNext()) { Map.Entry<Object, Object> entry = propsIter.next(); String fieldName = (String) entry.getKey(); String fieldValue = (String) entry.getValue(); parameters.add(new BasicNameValuePair(fieldName, fieldValue)); } postMethod.setEntity(new UrlEncodedFormEntity(parameters)); // Create Http Client if necessary, and include User Agent information. if (httpClientGatekeeper == null) { httpClientGatekeeper = initHttpConnection(); } // Try to detect any necessary proxy configurations. try { HttpHost proxyHost = PluginProxyUtil.detectProxy(new URL(gatekeeperUrl)); if (proxyHost != null) { httpClientGatekeeper.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxyHost); } ((DefaultHttpClient) httpClientGatekeeper).setCredentialsProvider(this); } catch (Throwable t) { log.debug("No proxy detected"); } // Perform Gateway request. log.debug("Contacting Gatekeeper at: " + gatekeeperUrl); HttpResponse response = null; try { response = httpClientGatekeeper.execute(postMethod); int responseCode = response.getStatusLine().getStatusCode(); String contentType = response.getFirstHeader("Content-Type").getValue(); if (responseCode == 200) { InputStream responseInputStream = null; Header encodingHeader = response.getFirstHeader("Content-Encoding"); if (encodingHeader != null && "gzip".equalsIgnoreCase(encodingHeader.getValue())) { log.debug("Inflating gzip-encoded response"); responseInputStream = new GZIPInputStream(response.getEntity().getContent()); } else { responseInputStream = response.getEntity().getContent(); } if (responseInputStream == null) { throw new IOException("No response input stream available from Gatekeeper"); } Properties responseProperties = new Properties(); try { responseProperties.load(responseInputStream); } finally { responseInputStream.close(); } GatekeeperMessage gatekeeperResponseMessage = GatekeeperMessage .decodeFromProperties(responseProperties); // Check for Gatekeeper Error Code in response. String gatekeeperErrorCode = gatekeeperResponseMessage.getApplicationProperties() .getProperty(GatekeeperMessage.APP_PROPERTY_GATEKEEPER_ERROR_CODE); if (gatekeeperErrorCode != null) { log.warn("Received Gatekeeper error code: " + gatekeeperErrorCode); failWithFatalError(gatekeeperErrorCode); return null; } if (gatekeeperResponseMessage.getSignatureRequests().length != objects.length) { throw new Exception("The Gatekeeper service did not provide the necessary " + objects.length + " response items"); } return gatekeeperResponseMessage; } else { log.debug("The Gatekeeper did not permit a request. Response code: " + responseCode + ", Response content type: " + contentType); throw new Exception("The Gatekeeper did not permit your request"); } } catch (Exception e) { throw new Exception("Gatekeeper did not respond", e); } finally { EntityUtils.consume(response.getEntity()); } }
From source file:org.jets3t.service.utils.signedurl.GatekeeperClientUtils.java
/** * Request permission from the Gatekeeper for a particular operation. * * @param operationType/*www .ja va 2 s . c o m*/ * @param bucketName * @param objects * @param applicationPropertiesMap * @throws HttpException * @throws Exception */ public GatekeeperMessage requestActionThroughGatekeeper(String operationType, String bucketName, S3Object[] objects, Map applicationPropertiesMap) throws HttpException, Exception { /* * Build Gatekeeper request. */ GatekeeperMessage gatekeeperMessage = new GatekeeperMessage(); gatekeeperMessage.addApplicationProperties(applicationPropertiesMap); gatekeeperMessage.addApplicationProperty(GatekeeperMessage.PROPERTY_CLIENT_VERSION_ID, userAgentDescription); // If a prior failure has occurred, add information about this failure. if (priorFailureException != null) { gatekeeperMessage.addApplicationProperty(GatekeeperMessage.PROPERTY_PRIOR_FAILURE_MESSAGE, priorFailureException.getMessage()); // Now reset the prior failure variable. priorFailureException = null; } // Add all S3 objects as candiates for PUT signing. for (int i = 0; i < objects.length; i++) { SignatureRequest signatureRequest = new SignatureRequest(operationType, objects[i].getKey()); signatureRequest.setObjectMetadata(objects[i].getMetadataMap()); signatureRequest.setBucketName(bucketName); gatekeeperMessage.addSignatureRequest(signatureRequest); } /* * Build HttpClient POST message. */ // Add all properties/parameters to credentials POST request. HttpPost postMethod = new HttpPost(gatekeeperUrl); Properties properties = gatekeeperMessage.encodeToProperties(); Iterator<Map.Entry<Object, Object>> propsIter = properties.entrySet().iterator(); List<NameValuePair> parameters = new ArrayList<NameValuePair>(properties.size()); while (propsIter.hasNext()) { Map.Entry<Object, Object> entry = propsIter.next(); String fieldName = (String) entry.getKey(); String fieldValue = (String) entry.getValue(); parameters.add(new BasicNameValuePair(fieldName, fieldValue)); } postMethod.setEntity(new UrlEncodedFormEntity(parameters)); // Create Http Client if necessary, and include User Agent information. if (httpClientGatekeeper == null) { httpClientGatekeeper = initHttpConnection(); } // Try to detect any necessary proxy configurations. try { HttpHost proxyHost = PluginProxyUtil.detectProxy(new URL(gatekeeperUrl)); if (proxyHost != null) { httpClientGatekeeper.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxyHost); } } catch (Throwable t) { if (log.isDebugEnabled()) { log.debug("No proxy detected"); } } // Perform Gateway request. if (log.isDebugEnabled()) { log.debug("Contacting Gatekeeper at: " + gatekeeperUrl); } HttpResponse response = null; try { response = httpClientGatekeeper.execute(postMethod); int responseCode = response.getStatusLine().getStatusCode(); String contentType = response.getFirstHeader("Content-Type").getValue(); if (responseCode == 200) { InputStream responseInputStream = null; Header encodingHeader = response.getFirstHeader("Content-Encoding"); if (encodingHeader != null && "gzip".equalsIgnoreCase(encodingHeader.getValue())) { if (log.isDebugEnabled()) { log.debug("Inflating gzip-encoded response"); } responseInputStream = new GZIPInputStream(response.getEntity().getContent()); } else { responseInputStream = response.getEntity().getContent(); } if (responseInputStream == null) { throw new IOException("No response input stream available from Gatekeeper"); } Properties responseProperties = new Properties(); try { responseProperties.load(responseInputStream); } finally { responseInputStream.close(); } GatekeeperMessage gatekeeperResponseMessage = GatekeeperMessage .decodeFromProperties(responseProperties); // Check for Gatekeeper Error Code in response. String gatekeeperErrorCode = gatekeeperResponseMessage.getApplicationProperties() .getProperty(GatekeeperMessage.APP_PROPERTY_GATEKEEPER_ERROR_CODE); if (gatekeeperErrorCode != null) { if (log.isWarnEnabled()) { log.warn("Received Gatekeeper error code: " + gatekeeperErrorCode); } } return gatekeeperResponseMessage; } else { if (log.isDebugEnabled()) { log.debug("The Gatekeeper did not permit a request. Response code: " + responseCode + ", Response content type: " + contentType); } throw new IOException("The Gatekeeper did not permit your request"); } } catch (IOException e) { throw e; } catch (Exception e) { throw new Exception("Gatekeeper did not respond", e); } finally { try { EntityUtils.consume(response.getEntity()); } catch (Exception ee) { // ignore } } }