Example usage for org.apache.commons.httpclient HostConfiguration setHost

List of usage examples for org.apache.commons.httpclient HostConfiguration setHost

Introduction

In this page you can find the example usage for org.apache.commons.httpclient HostConfiguration setHost.

Prototype

public void setHost(String paramString, int paramInt, Protocol paramProtocol) 

Source Link

Usage

From source file:org.globus.workspace.cloud.client.util.CumulusParameterConvert.java

private S3Service getService() throws S3ServiceException {
    String host = this.args.getXferHostPort();
    int ndx = host.lastIndexOf(":");
    int port = 80;
    String portS = "80";
    String httpsPortS = "443";
    int httpsPort = 443;

    if (ndx > 0) {
        portS = host.substring(ndx + 1);
        httpsPortS = portS;/*  w  w  w  . j a  v  a2s .co  m*/
        port = new Integer(portS).intValue();
        httpsPort = new Integer(httpsPortS).intValue();
        host = host.substring(0, ndx);
    }

    Jets3tProperties j3p = new Jets3tProperties();

    j3p.setProperty("s3service.s3-endpoint-http-port", portS);
    j3p.setProperty("s3service.s3-endpoint-https-port", httpsPortS);
    j3p.setProperty("s3service.disable-dns-buckets", "true");
    j3p.setProperty("s3service.s3-endpoint", host);
    j3p.setProperty("s3service.https-only", this.useHttps);
    j3p.setProperty("storage-service.internal-error-retry-max", "0");
    j3p.setProperty("httpclient.socket-timeout-ms", "0");

    HostConfiguration hc = new HostConfiguration();
    if (allowSelfSigned && this.useHttps.equalsIgnoreCase("true")) {
        // magic needed for jets3t to work with self signed cert.
        try {
            Protocol easyhttps = new Protocol("https", new CumulusProtocolSocketFactory(), 443);
            Protocol.registerProtocol("https", easyhttps);

            hc.setHost(host, httpsPort, easyhttps);
        } catch (Exception ex) {
            throw new S3ServiceException("Could not make the self signed handler " + ex.toString(), ex);
        }
    }
    AWSCredentials awsCredentials = this.getAwsCredentail();
    S3Service s3Service = new RestS3Service(awsCredentials, "cloud-client", null, j3p, hc);

    return s3Service;
}

From source file:org.methodize.nntprss.feed.Channel.java

/**
 * Retrieves the latest RSS doc from the remote site
 *//*from ww w. j a  va  2  s  .c  om*/
public synchronized void poll() {
    // Use method-level variable
    // Guard against change in history mid-poll
    polling = true;

    //      boolean keepHistory = historical;
    long keepExpiration = expiration;

    lastPolled = new Date();

    int statusCode = -1;
    HttpMethod method = null;
    String urlString = url.toString();
    try {
        HttpClient httpClient = getHttpClient();
        channelManager.configureHttpClient(httpClient);
        HttpResult result = null;

        try {

            connected = true;
            boolean redirected = false;
            int count = 0;
            do {
                URL currentUrl = new URL(urlString);
                method = new GetMethod(urlString);
                method.setRequestHeader("User-agent", AppConstants.getUserAgent());
                method.setRequestHeader("Accept-Encoding", "gzip");
                method.setFollowRedirects(false);
                method.setDoAuthentication(true);

                // ETag
                if (lastETag != null) {
                    method.setRequestHeader("If-None-Match", lastETag);
                }

                // Last Modified
                if (lastModified != 0) {
                    final String NAME = "If-Modified-Since";
                    //defend against such fun like net.freeroller.rickard got If-Modified-Since "Thu, 24 Aug 2028 12:29:54 GMT"
                    if (lastModified < System.currentTimeMillis()) {
                        final String DATE = httpDate.format(new Date(lastModified));
                        method.setRequestHeader(NAME, DATE);
                        log.debug("channel " + this.name + " using " + NAME + " " + DATE); //ALEK
                    }
                }

                method.setFollowRedirects(false);
                method.setDoAuthentication(true);

                HostConfiguration hostConfig = new HostConfiguration();
                hostConfig.setHost(currentUrl.getHost(), currentUrl.getPort(), currentUrl.getProtocol());

                result = executeHttpRequest(httpClient, hostConfig, method);
                statusCode = result.getStatusCode();
                if (statusCode == HttpStatus.SC_MOVED_PERMANENTLY
                        || statusCode == HttpStatus.SC_MOVED_TEMPORARILY
                        || statusCode == HttpStatus.SC_SEE_OTHER
                        || statusCode == HttpStatus.SC_TEMPORARY_REDIRECT) {

                    redirected = true;
                    // Resolve against current URI - may be a relative URI
                    try {
                        urlString = new java.net.URI(urlString).resolve(result.getLocation()).toString();
                    } catch (URISyntaxException use) {
                        // Fall back to just using location from result
                        urlString = result.getLocation();
                    }
                    if (statusCode == HttpStatus.SC_MOVED_PERMANENTLY && channelManager.isObserveHttp301()) {
                        try {
                            url = new URL(urlString);
                            if (log.isInfoEnabled()) {
                                log.info("Channel = " + this.name
                                        + ", updated URL from HTTP Permanent Redirect");
                            }
                        } catch (MalformedURLException mue) {
                            // Ignore URL permanent redirect for now...                        
                        }
                    }
                } else {
                    redirected = false;
                }

                //               method.getResponseBody();
                //               method.releaseConnection();
                count++;
            } while (count < 5 && redirected);

        } catch (HttpRecoverableException hre) {
            if (log.isDebugEnabled()) {
                log.debug("Channel=" + name + " - Temporary Http Problem - " + hre.getMessage());
            }
            status = STATUS_CONNECTION_TIMEOUT;
            statusCode = HttpStatus.SC_INTERNAL_SERVER_ERROR;
        } catch (ConnectException ce) {
            // @TODO Might also be a connection refused - not only a timeout...
            if (log.isDebugEnabled()) {
                log.debug("Channel=" + name + " - Connection Timeout, skipping - " + ce.getMessage());
            }
            status = STATUS_CONNECTION_TIMEOUT;
            statusCode = HttpStatus.SC_INTERNAL_SERVER_ERROR;
        } catch (UnknownHostException ue) {
            if (log.isDebugEnabled()) {
                log.debug("Channel=" + name + " - Unknown Host Exception, skipping");
            }
            status = STATUS_UNKNOWN_HOST;
            statusCode = HttpStatus.SC_INTERNAL_SERVER_ERROR;
        } catch (NoRouteToHostException re) {
            if (log.isDebugEnabled()) {
                log.debug("Channel=" + name + " - No Route To Host Exception, skipping");
            }
            status = STATUS_NO_ROUTE_TO_HOST;
            statusCode = HttpStatus.SC_INTERNAL_SERVER_ERROR;
        } catch (SocketException se) {
            // e.g. Network is unreachable            
            if (log.isDebugEnabled()) {
                log.debug("Channel=" + name + " - Socket Exception, skipping");
            }
            status = STATUS_SOCKET_EXCEPTION;
            statusCode = HttpStatus.SC_INTERNAL_SERVER_ERROR;
        }

        // Only process if ok - if not ok (e.g. not modified), don't do anything
        if (connected && statusCode == HttpStatus.SC_OK) {

            PushbackInputStream pbis = new PushbackInputStream(new ByteArrayInputStream(result.getResponse()),
                    PUSHBACK_BUFFER_SIZE);
            skipBOM(pbis);
            BufferedInputStream bis = new BufferedInputStream(pbis);
            DocumentBuilder db = AppConstants.newDocumentBuilder();

            try {
                Document rssDoc = null;
                if (!parseAtAllCost) {
                    try {
                        rssDoc = db.parse(bis);
                    } catch (InternalError ie) {
                        // Crimson library throws InternalErrors
                        if (log.isDebugEnabled()) {
                            log.debug("InternalError thrown by Crimson", ie);
                        }
                        throw new SAXException("InternalError thrown by Crimson: " + ie.getMessage());
                    }
                } else {
                    // Parse-at-all-costs selected
                    // Read in document to local array - may need to parse twice
                    ByteArrayOutputStream bos = new ByteArrayOutputStream();
                    byte[] buf = new byte[1024];
                    int bytesRead = bis.read(buf);
                    while (bytesRead > -1) {
                        if (bytesRead > 0) {
                            bos.write(buf, 0, bytesRead);
                        }
                        bytesRead = bis.read(buf);
                    }
                    bos.flush();
                    bos.close();

                    byte[] rssDocBytes = bos.toByteArray();

                    try {
                        // Try the XML document parser first - just in case
                        // the doc is well-formed
                        rssDoc = db.parse(new ByteArrayInputStream(rssDocBytes));
                    } catch (SAXParseException spe) {
                        if (log.isDebugEnabled()) {
                            log.debug("XML parse failed, trying tidy");
                        }
                        // Fallback to parse-at-all-costs parser
                        rssDoc = LooseParser.parse(new ByteArrayInputStream(rssDocBytes));
                    }
                }

                processChannelDocument(expiration, rssDoc);

                // Update last modified / etag from headers
                //               lastETag = httpCon.getHeaderField("ETag");
                //               lastModified = httpCon.getHeaderFieldDate("Last-Modified", 0);

                Header hdrETag = method.getResponseHeader("ETag");
                lastETag = hdrETag != null ? hdrETag.getValue() : null;

                Header hdrLastModified = method.getResponseHeader("Last-Modified");
                lastModified = hdrLastModified != null ? parseHttpDate(hdrLastModified.getValue()) : 0;
                log.debug("channel " + this.name + " parsed Last-Modifed " + hdrLastModified + " to "
                        + (lastModified != 0 ? "" + (new Date(lastModified)) : "" + lastModified)); //ALEK

                status = STATUS_OK;
            } catch (SAXParseException spe) {
                if (log.isEnabledFor(Priority.WARN)) {
                    log.warn("Channel=" + name + " - Error parsing RSS document - check feed");
                }
                status = STATUS_INVALID_CONTENT;
            }

            bis.close();

            // end if response code == HTTP_OK
        } else if (connected && statusCode == HttpStatus.SC_NOT_MODIFIED) {
            if (log.isDebugEnabled()) {
                log.debug("Channel=" + name + " - HTTP_NOT_MODIFIED, skipping");
            }
            status = STATUS_OK;
        } else if (statusCode == HttpStatus.SC_PROXY_AUTHENTICATION_REQUIRED) {
            if (log.isEnabledFor(Priority.WARN)) {
                log.warn("Channel=" + name + " - Proxy authentication required");
            }
            status = STATUS_PROXY_AUTHENTICATION_REQUIRED;
        } else if (statusCode == HttpStatus.SC_UNAUTHORIZED) {
            if (log.isEnabledFor(Priority.WARN)) {
                log.warn("Channel=" + name + " - Authentication required");
            }
            status = STATUS_USER_AUTHENTICATION_REQUIRED;
        }

        // Update channel in database...
        channelDAO.updateChannel(this);

    } catch (FileNotFoundException fnfe) {
        if (log.isEnabledFor(Priority.WARN)) {
            log.warn("Channel=" + name + " - File not found returned by web server - check feed");
        }
        status = STATUS_NOT_FOUND;
    } catch (Exception e) {
        if (log.isEnabledFor(Priority.WARN)) {
            log.warn("Channel=" + name + " - Exception while polling channel", e);
        }
    } catch (NoClassDefFoundError ncdf) {
        // Throw if SSL / redirection to HTTPS
        if (log.isEnabledFor(Priority.WARN)) {
            log.warn("Channel=" + name + " - NoClassDefFound", ncdf);
        }
    } finally {
        connected = false;
        polling = false;
    }

}

From source file:org.mule.transport.http.HttpClientMessageDispatcher.java

protected HostConfiguration getHostConfig(URI uri) throws Exception {
    Protocol protocol = Protocol.getProtocol(uri.getScheme().toLowerCase());

    String host = uri.getHost();/*www  . jav  a2  s.  c  o m*/
    int port = uri.getPort();
    HostConfiguration config = new HostConfiguration();
    config.setHost(host, port, protocol);
    if (StringUtils.isNotBlank(httpConnector.getProxyHostname())) {
        // add proxy support
        config.setProxy(httpConnector.getProxyHostname(), httpConnector.getProxyPort());
    }
    return config;
}

From source file:org.mule.transport.http.HttpsClientMessageDispatcher.java

@Override
protected HostConfiguration getHostConfig(URI uri) throws Exception {
    HostConfiguration hostConfig = new MuleHostConfiguration(super.getHostConfig(uri));
    String host = uri.getHost();/*from   ww  w.ja  va 2  s .  c om*/
    int port = uri.getPort();
    Protocol protocol = getProtocol(uri.getScheme().toLowerCase());
    hostConfig.setHost(host, port, protocol);
    return hostConfig;
}

From source file:org.mule.transport.http.MuleHostConfigurationTestCase.java

@Test
public void testSetHostViaHostAndPortAndProtocolName() {
    HostConfiguration hostConfig = createHostConfiguration();

    hostConfig.setHost("www.mulesoft.org", 8080, "http");

    assertMockSocketFactory(hostConfig);
    assertEquals("www.mulesoft.org", hostConfig.getHost());
    assertEquals(8080, hostConfig.getPort());
}

From source file:org.mule.transport.http.MuleHostConfigurationTestCase.java

@Test
public void testSetHostViaHostAndPortAndProtocolNameWithDifferentProtocol() throws Exception {
    new DifferentProtocolTemplate() {
        protected void doTest() throws Exception {
            HostConfiguration hostConfig = createHostConfiguration();

            hostConfig.setHost("www.mulesoft.org", 8080, "httpx");

            assertDefaultSocketFactory(hostConfig);
            assertEquals("www.mulesoft.org", hostConfig.getHost());
            assertEquals(8080, hostConfig.getPort());
        }//from  w  w  w.ja v a 2 s .c o m
    }.test();
}

From source file:org.mulgara.resolver.http.HttpContent.java

/**
 * Obtain the approrpriate connection method
 * /*  ww w  . j a  va  2  s  . co m*/
 * @param methodType can be HEAD or GET
 * @return HttpMethodBase method
 */
private HttpMethod getConnectionMethod(int methodType) {
    if (methodType != GET && methodType != HEAD) {
        throw new IllegalArgumentException("Invalid method base supplied for connection");
    }

    HostConfiguration config = new HostConfiguration();
    config.setHost(host, port, Protocol.getProtocol(schema));
    if (connection != null) {
        connection.releaseConnection();
        connection.close();
        connection = null;
    }
    try {
        connection = connectionManager.getConnectionWithTimeout(config, 0L);
    } catch (ConnectionPoolTimeoutException te) {
        // NOOP: SimpleHttpConnectionManager does not use timeouts
    }

    String proxyHost = System.getProperty("mulgara.httpcontent.proxyHost");

    if (proxyHost != null && proxyHost.length() > 0) {
        connection.setProxyHost(proxyHost);
    }

    String proxyPort = System.getProperty("mulgara.httpcontent.proxyPort");
    if (proxyPort != null && proxyPort.length() > 0) {
        connection.setProxyPort(Integer.parseInt(proxyPort));
    }

    // default timeout to 30 seconds
    connection.getParams()
            .setConnectionTimeout(Integer.parseInt(System.getProperty("mulgara.httpcontent.timeout", "30000")));

    String proxyUserName = System.getProperty("mulgara.httpcontent.proxyUserName");
    if (proxyUserName != null) {
        state.setCredentials(
                new AuthScope(System.getProperty("mulgara.httpcontent.proxyRealmHost"), AuthScope.ANY_PORT,
                        System.getProperty("mulgara.httpcontent.proxyRealm"), AuthScope.ANY_SCHEME),
                new UsernamePasswordCredentials(proxyUserName,
                        System.getProperty("mulgara.httpcontent.proxyPassword")));
    }

    HttpMethod method = null;
    if (methodType == HEAD) {
        method = new HeadMethod(httpUri.toString());
    } else {
        method = new GetMethod(httpUri.toString());
    }

    // manually follow redirects due to the
    // strictness of http client implementation

    method.setFollowRedirects(false);

    return method;
}

From source file:org.openlaszlo.servlets.responders.ResponderCompile.java

/**
 * @return File name for temporary LZX file that is the
 *         result of this http pre-processing; null for a bad request
 * @param req request/*from  w  w  w.  j  ava 2 s. c o  m*/
 * @param fileName file name associated with request
 */
private String doPreProcessing(HttpServletRequest req, String fileName) throws IOException {
    // Do an http request for this and see what we get back.
    //
    StringBuffer s = req.getRequestURL();
    int len = s.length();
    // Remove the .lzx from the end of the URL
    if (len <= 4) {
        return null;
    }
    s.delete(len - 4, len);

    // FIXME [2002-12-15 bloch] does any/all of this need to be synchronized on session?

    // First get the temporary file name for this session
    HttpSession session = req.getSession();
    String sid = session.getId();
    String tempFileName = getTempFileName(fileName, sid);
    File tempFile = new File(tempFileName);

    tempFile.deleteOnExit();

    // Now pre-process the request and copy the data to
    // the temporary file that is unique to this session

    // FIXME: query string processing

    String surl = s.toString();

    URL url = new URL(surl);
    mLogger.debug(
            /* (non-Javadoc)
             * @i18n.test
             * @org-mes="Preprocessing request at " + p[0]
             */
            org.openlaszlo.i18n.LaszloMessages.getMessage(ResponderCompile.class.getName(), "051018-263",
                    new Object[] { surl }));
    GetMethod getRequest = new LZGetMethod();
    getRequest.setPath(url.getPath());
    //getRequest.setQueryString(url.getQuery());
    getRequest.setQueryString(req.getQueryString());

    // Copy headers to request
    LZHttpUtils.proxyRequestHeaders(req, getRequest);
    // Mention the last modified time, if the file exists
    if (tempFile.exists()) {
        long lastModified = tempFile.lastModified();
        getRequest.addRequestHeader("If-Modified-Since", LZHttpUtils.getDateString(lastModified));
    } else {
        // Otherwise, create a listener that will clean up the tempfile
        // Note: web server administrators must make sure that their servers are killed
        // gracefully or temporary files will not be handled by the LZBindingListener.
        // Add a binding listener for this session that
        // will remove our temporary files
        LZBindingListener listener = (LZBindingListener) session.getAttribute("tmpl");
        if (listener == null) {
            listener = new LZBindingListener(tempFileName);
            session.setAttribute("tmpl", listener);
        } else {
            listener.addTempFile(tempFileName);
        }
    }

    HostConfiguration hostConfig = new HostConfiguration();
    hostConfig.setHost(url.getHost(), url.getPort(), url.getProtocol());

    HttpClient htc = new HttpClient();
    htc.setHostConfiguration(hostConfig);

    int rc = htc.executeMethod(getRequest);
    mLogger.debug("Response Status: " + rc);
    if (rc >= 400) {
        // respondWithError(req, res, "HTTP Status code: " + rc + " for url " + surl, rc);
        return null;
    }
    if (rc != HttpServletResponse.SC_NOT_MODIFIED) {
        FileOutputStream output = new FileOutputStream(tempFile);
        try {
            // FIXME:[2002-12-17 bloch] verify that the response body is XML
            FileUtils.sendToStream(getRequest.getResponseBodyAsStream(), output);
            // TODO: [2002-12-15 bloch] What to do with response headers?
        } catch (FileUtils.StreamWritingException e) {
            mLogger.warn(
                    /* (non-Javadoc)
                     * @i18n.test
                     * @org-mes="StreamWritingException while sending error: " + p[0]
                     */
                    org.openlaszlo.i18n.LaszloMessages.getMessage(ResponderCompile.class.getName(),
                            "051018-313", new Object[] { e.getMessage() }));
        } finally {
            FileUtils.close(output);
        }
    }

    return tempFileName;
}

From source file:org.osaf.caldav4j.BaseTestCase.java

public HostConfiguration createHostConfiguration() {
    HostConfiguration hostConfig = new HostConfiguration();
    hostConfig.setHost(getCalDAVServerHost(), getCalDAVServerPort(), getCalDavSeverProtocol());
    return hostConfig;
}

From source file:org.osaf.caldav4j.BaseTestCase.java

public static HostConfiguration createHostConfiguration(CaldavCredential caldavCredential) {
    HostConfiguration hostConfig = new HostConfiguration();
    hostConfig.setHost(caldavCredential.host, caldavCredential.port, caldavCredential.protocol);
    return hostConfig;
}