Example usage for org.apache.commons.httpclient HttpMethodBase setDoAuthentication

List of usage examples for org.apache.commons.httpclient HttpMethodBase setDoAuthentication

Introduction

In this page you can find the example usage for org.apache.commons.httpclient HttpMethodBase setDoAuthentication.

Prototype

@Override
public void setDoAuthentication(boolean doAuthentication) 

Source Link

Document

Sets whether or not the HTTP method should automatically handle HTTP authentication challenges (status code 401, etc.)

Usage

From source file:com.clarkparsia.sbol.editor.sparql.StardogEndpoint.java

protected void execute(HttpMethodBase post) throws HttpException, IOException, QueryEvaluationException {
    post.setDoAuthentication(true);

    boolean completed = false;
    try {//w ww.  j  a v a 2s .c  om
        int resultCode = client.executeMethod(post);
        if (resultCode >= 400) {
            throw new HttpException("Code: " + resultCode + " " + post.getResponseBodyAsString());
        }
        completed = true;
    } finally {
        if (!completed) {
            post.abort();
        }
    }
}

From source file:jeeves.utils.XmlRequest.java

private HttpMethodBase setupHttpMethod() throws UnsupportedEncodingException {
    HttpMethodBase httpMethod;

    if (method == Method.GET) {
        httpMethod = new GetMethod();

        if (query != null && !query.equals(""))
            httpMethod.setQueryString(query);

        else if (alSimpleParams.size() != 0)
            httpMethod.setQueryString(alSimpleParams.toArray(new NameValuePair[alSimpleParams.size()]));

        httpMethod.addRequestHeader("Accept", !useSOAP ? "application/xml" : "application/soap+xml");
        httpMethod.setFollowRedirects(true);
    } else {//from   www  .  j a  va  2  s .  com
        PostMethod post = new PostMethod();

        if (!useSOAP) {
            postData = (postParams == null) ? "" : Xml.getString(new Document(postParams));
            post.setRequestEntity(new StringRequestEntity(postData, "application/xml", "UTF8"));
        } else {
            postData = Xml.getString(new Document(soapEmbed(postParams)));
            post.setRequestEntity(new StringRequestEntity(postData, "application/soap+xml", "UTF8"));
        }

        httpMethod = post;
    }

    httpMethod.setPath(address);
    httpMethod.setDoAuthentication(useAuthent());

    return httpMethod;
}

From source file:com.cyberway.issue.crawler.fetcher.FetchHTTP.java

protected void innerProcess(final CrawlURI curi) throws InterruptedException {
    if (!canFetch(curi)) {
        // Cannot fetch this, due to protocol, retries, or other problems
        return;/* w w  w .  j  a va  2s  .co m*/
    }

    this.curisHandled++;

    // Note begin time
    curi.putLong(A_FETCH_BEGAN_TIME, System.currentTimeMillis());

    // Get a reference to the HttpRecorder that is set into this ToeThread.
    HttpRecorder rec = HttpRecorder.getHttpRecorder();

    // Shall we get a digest on the content downloaded?
    boolean digestContent = ((Boolean) getUncheckedAttribute(curi, ATTR_DIGEST_CONTENT)).booleanValue();
    String algorithm = null;
    if (digestContent) {
        algorithm = ((String) getUncheckedAttribute(curi, ATTR_DIGEST_ALGORITHM));
        rec.getRecordedInput().setDigest(algorithm);
    } else {
        // clear
        rec.getRecordedInput().setDigest((MessageDigest) null);
    }

    // Below we do two inner classes that add check of midfetch
    // filters just as we're about to receive the response body.
    String curiString = curi.getUURI().toString();
    HttpMethodBase method = null;
    if (curi.isPost()) {
        method = new HttpRecorderPostMethod(curiString, rec) {
            protected void readResponseBody(HttpState state, HttpConnection conn)
                    throws IOException, HttpException {
                addResponseContent(this, curi);
                if (checkMidfetchAbort(curi, this.httpRecorderMethod, conn)) {
                    doAbort(curi, this, MIDFETCH_ABORT_LOG);
                } else {
                    super.readResponseBody(state, conn);
                }
            }
        };
    } else {
        method = new HttpRecorderGetMethod(curiString, rec) {
            protected void readResponseBody(HttpState state, HttpConnection conn)
                    throws IOException, HttpException {
                addResponseContent(this, curi);
                if (checkMidfetchAbort(curi, this.httpRecorderMethod, conn)) {
                    doAbort(curi, this, MIDFETCH_ABORT_LOG);
                } else {
                    super.readResponseBody(state, conn);
                }
            }
        };
    }

    HostConfiguration customConfigOrNull = configureMethod(curi, method);

    // Set httpRecorder into curi. Subsequent code both here and later
    // in extractors expects to find the HttpRecorder in the CrawlURI.
    curi.setHttpRecorder(rec);

    // Populate credentials. Set config so auth. is not automatic.
    boolean addedCredentials = populateCredentials(curi, method);
    method.setDoAuthentication(addedCredentials);

    // set hardMax on bytes (if set by operator)
    long hardMax = getMaxLength(curi);
    // set overall timeout (if set by operator)
    long timeoutMs = 1000 * getTimeout(curi);
    // Get max fetch rate (bytes/ms). It comes in in KB/sec
    long maxRateKBps = getMaxFetchRate(curi);
    rec.getRecordedInput().setLimits(hardMax, timeoutMs, maxRateKBps);

    try {
        this.http.executeMethod(customConfigOrNull, method);
    } catch (RecorderTooMuchHeaderException ex) {
        // when too much header material, abort like other truncations
        doAbort(curi, method, HEADER_TRUNC);
    } catch (IOException e) {
        failedExecuteCleanup(method, curi, e);
        return;
    } catch (ArrayIndexOutOfBoundsException e) {
        // For weird windows-only ArrayIndex exceptions in native
        // code... see
        // http://forum.java.sun.com/thread.jsp?forum=11&thread=378356
        // treating as if it were an IOException
        failedExecuteCleanup(method, curi, e);
        return;
    }

    // set softMax on bytes to get (if implied by content-length) 
    long softMax = method.getResponseContentLength();

    try {
        if (!method.isAborted()) {
            // Force read-to-end, so that any socket hangs occur here,
            // not in later modules.
            rec.getRecordedInput().readFullyOrUntil(softMax);
        }
    } catch (RecorderTimeoutException ex) {
        doAbort(curi, method, TIMER_TRUNC);
    } catch (RecorderLengthExceededException ex) {
        doAbort(curi, method, LENGTH_TRUNC);
    } catch (IOException e) {
        cleanup(curi, e, "readFully", S_CONNECT_LOST);
        return;
    } catch (ArrayIndexOutOfBoundsException e) {
        // For weird windows-only ArrayIndex exceptions from native code
        // see http://forum.java.sun.com/thread.jsp?forum=11&thread=378356
        // treating as if it were an IOException
        cleanup(curi, e, "readFully", S_CONNECT_LOST);
        return;
    } finally {
        // ensure recording has stopped
        rec.closeRecorders();
        if (!method.isAborted()) {
            method.releaseConnection();
        }
        // Note completion time
        curi.putLong(A_FETCH_COMPLETED_TIME, System.currentTimeMillis());
        // Set the response charset into the HttpRecord if available.
        setCharacterEncoding(rec, method);
        setSizes(curi, rec);
    }

    if (digestContent) {
        curi.setContentDigest(algorithm, rec.getRecordedInput().getDigestValue());
    }
    if (logger.isLoggable(Level.INFO)) {
        logger.info((curi.isPost() ? "POST" : "GET") + " " + curi.getUURI().toString() + " "
                + method.getStatusCode() + " " + rec.getRecordedInput().getSize() + " "
                + curi.getContentType());
    }

    if (curi.isSuccess() && addedCredentials) {
        // Promote the credentials from the CrawlURI to the CrawlServer
        // so they are available for all subsequent CrawlURIs on this
        // server.
        promoteCredentials(curi);
        if (logger.isLoggable(Level.FINE)) {
            // Print out the cookie.  Might help with the debugging.
            Header setCookie = method.getResponseHeader("set-cookie");
            if (setCookie != null) {
                logger.fine(setCookie.toString().trim());
            }
        }
    } else if (method.getStatusCode() == HttpStatus.SC_UNAUTHORIZED) {
        // 401 is not 'success'.
        handle401(method, curi);
    }

    if (rec.getRecordedInput().isOpen()) {
        logger.severe(curi.toString() + " RIS still open. Should have" + " been closed by method release: "
                + Thread.currentThread().getName());
        try {
            rec.getRecordedInput().close();
        } catch (IOException e) {
            logger.log(Level.SEVERE, "second-chance RIS close failed", e);
        }
    }
}

From source file:org.apache.axis2.transport.http.AbstractHTTPSender.java

/**
 * Method used to copy all the common properties
 *
 * @param msgContext       - The messageContext of the request message
 * @param url              - The target URL
 * @param httpMethod       - The http method used to send the request
 * @param httpClient       - The httpclient used to send the request
 * @param soapActionString - The soap action atring of the request message
 * @return MessageFormatter - The messageFormatter for the relavent request message
 * @throws AxisFault - Thrown in case an exception occurs
 *///from  ww w  .  jav a2  s .  c o m
protected MessageFormatter populateCommonProperties(MessageContext msgContext, URL url,
        HttpMethodBase httpMethod, HttpClient httpClient, String soapActionString) throws AxisFault {

    if (isAuthenticationEnabled(msgContext)) {
        httpMethod.setDoAuthentication(true);
    }

    MessageFormatter messageFormatter = MessageProcessorSelector.getMessageFormatter(msgContext);

    url = messageFormatter.getTargetAddress(msgContext, format, url);

    httpMethod.setPath(url.getPath());

    httpMethod.setQueryString(url.getQuery());

    httpMethod.setRequestHeader(HTTPConstants.HEADER_CONTENT_TYPE,
            messageFormatter.getContentType(msgContext, format, soapActionString));

    httpMethod.setRequestHeader(HTTPConstants.HEADER_HOST, url.getHost());

    if (msgContext.getOptions() != null && msgContext.getOptions().isManageSession()) {
        // setting the cookie in the out path
        Object cookieString = msgContext.getProperty(HTTPConstants.COOKIE_STRING);

        if (cookieString != null) {
            StringBuffer buffer = new StringBuffer();
            buffer.append(cookieString);
            httpMethod.setRequestHeader(HTTPConstants.HEADER_COOKIE, buffer.toString());
        }
    }

    if (httpVersion.equals(HTTPConstants.HEADER_PROTOCOL_10)) {
        httpClient.getParams().setVersion(HttpVersion.HTTP_1_0);
    }
    return messageFormatter;
}

From source file:org.apache.axis2.transport.http.impl.httpclient3.HTTPSenderImpl.java

/**
 * Method used to copy all the common properties
 * /*from  ww w . ja v a  2s. com*/
 * @param msgContext
 *            - The messageContext of the request message
 * @param url
 *            - The target URL
 * @param httpMethod
 *            - The http method used to send the request
 * @param httpClient
 *            - The httpclient used to send the request
 * @param soapActionString
 *            - The soap action atring of the request message
 * @return MessageFormatter - The messageFormatter for the relavent request
 *         message
 * @throws AxisFault
 *             - Thrown in case an exception occurs
 */
protected MessageFormatter populateCommonProperties(MessageContext msgContext, URL url,
        HttpMethodBase httpMethod, HttpClient httpClient, String soapActionString) throws AxisFault {

    if (isAuthenticationEnabled(msgContext)) {
        httpMethod.setDoAuthentication(true);
    }

    MessageFormatter messageFormatter = TransportUtils.getMessageFormatter(msgContext);

    url = messageFormatter.getTargetAddress(msgContext, format, url);

    httpMethod.setPath(url.getPath());

    httpMethod.setQueryString(url.getQuery());

    httpMethod.setRequestHeader(HTTPConstants.HEADER_CONTENT_TYPE,
            messageFormatter.getContentType(msgContext, format, soapActionString));

    httpMethod.setRequestHeader(HTTPConstants.HEADER_HOST, url.getHost());

    if (msgContext.getOptions() != null && msgContext.getOptions().isManageSession()) {
        // setting the cookie in the out path
        Object cookieString = msgContext.getProperty(HTTPConstants.COOKIE_STRING);

        if (cookieString != null) {
            StringBuffer buffer = new StringBuffer();
            buffer.append(cookieString);
            httpMethod.setRequestHeader(HTTPConstants.HEADER_COOKIE, buffer.toString());
        }
    }

    if (httpVersion.equals(HTTPConstants.HEADER_PROTOCOL_10)) {
        httpClient.getParams().setVersion(HttpVersion.HTTP_1_0);
    }
    return messageFormatter;
}

From source file:org.archive.crawler.fetcher.OptimizeFetchHTTP.java

protected void innerProcess(final CrawlURI curi) throws InterruptedException {
    if (!canFetch(curi)) {
        // Cannot fetch this, due to protocol, retries, or other problems
        return;// w  ww. j  a v a  2s  .  co  m
    }

    HttpClient http = this.getClient();
    setLocalIP(http);

    this.curisHandled++;

    // Note begin time
    curi.putLong(A_FETCH_BEGAN_TIME, System.currentTimeMillis());

    // Get a reference to the HttpRecorder that is set into this ToeThread.
    HttpRecorder rec = HttpRecorder.getHttpRecorder();

    // Shall we get a digest on the content downloaded?
    boolean digestContent = ((Boolean) getUncheckedAttribute(curi, ATTR_DIGEST_CONTENT)).booleanValue();
    String algorithm = null;
    if (digestContent) {
        algorithm = ((String) getUncheckedAttribute(curi, ATTR_DIGEST_ALGORITHM));
        rec.getRecordedInput().setDigest(algorithm);
    } else {
        // clear
        rec.getRecordedInput().setDigest((MessageDigest) null);
    }

    // Below we do two inner classes that add check of midfetch
    // filters just as we're about to receive the response body.
    String curiString = curi.getUURI().toString();
    HttpMethodBase method = null;
    if (curi.isPost()) {
        method = new HttpRecorderPostMethod(curiString, rec) {
            protected void readResponseBody(HttpState state, HttpConnection conn)
                    throws IOException, HttpException {
                addResponseContent(this, curi);
                if (checkMidfetchAbort(curi, this.httpRecorderMethod, conn)) {
                    doAbort(curi, this, MIDFETCH_ABORT_LOG);
                } else {
                    super.readResponseBody(state, conn);
                }
            }
        };
    } else {
        method = new HttpRecorderGetMethod(curiString, rec) {
            protected void readResponseBody(HttpState state, HttpConnection conn)
                    throws IOException, HttpException {
                addResponseContent(this, curi);
                if (checkMidfetchAbort(curi, this.httpRecorderMethod, conn)) {
                    doAbort(curi, this, MIDFETCH_ABORT_LOG);
                } else {
                    super.readResponseBody(state, conn);
                }
            }
        };
    }

    HostConfiguration customConfigOrNull = configureMethod(curi, method);

    // Set httpRecorder into curi. Subsequent code both here and later
    // in extractors expects to find the HttpRecorder in the CrawlURI.
    curi.setHttpRecorder(rec);

    // Populate credentials. Set config so auth. is not automatic.
    boolean addedCredentials = populateCredentials(curi, method);
    method.setDoAuthentication(addedCredentials);

    // set hardMax on bytes (if set by operator)
    long hardMax = getMaxLength(curi);
    // set overall timeout (if set by operator)
    long timeoutMs = 1000 * getTimeout(curi);
    // Get max fetch rate (bytes/ms). It comes in in KB/sec
    long maxRateKBps = getMaxFetchRate(curi);
    rec.getRecordedInput().setLimits(hardMax, timeoutMs, maxRateKBps);

    try {
        http.executeMethod(customConfigOrNull, method);
    } catch (RecorderTooMuchHeaderException ex) {
        // when too much header material, abort like other truncations
        doAbort(curi, method, HEADER_TRUNC);
    } catch (IOException e) {
        failedExecuteCleanup(method, curi, e);
        return;
    } catch (ArrayIndexOutOfBoundsException e) {
        // For weird windows-only ArrayIndex exceptions in native
        // code... see
        // http://forum.java.sun.com/thread.jsp?forum=11&thread=378356
        // treating as if it were an IOException
        failedExecuteCleanup(method, curi, e);
        return;
    }

    // set softMax on bytes to get (if implied by content-length) 
    long softMax = method.getResponseContentLength();

    try {
        if (!curi.isSeed() && curi.getFetchStatus() == HttpStatus.SC_NOT_MODIFIED) {
            logger.debug(curi.getUURI().toString() + " is not modify");
            curi.skipToProcessorChain(getController().getPostprocessorChain());
        } else if (!method.isAborted()) {
            // Force read-to-end, so that any socket hangs occur here,
            // not in later modules.
            rec.getRecordedInput().readFullyOrUntil(softMax);
        }
    } catch (RecorderTimeoutException ex) {
        doAbort(curi, method, TIMER_TRUNC);
    } catch (RecorderLengthExceededException ex) {
        doAbort(curi, method, LENGTH_TRUNC);
    } catch (IOException e) {
        cleanup(curi, e, "readFully", S_CONNECT_LOST);
        return;
    } catch (ArrayIndexOutOfBoundsException e) {
        // For weird windows-only ArrayIndex exceptions from native code
        // see http://forum.java.sun.com/thread.jsp?forum=11&thread=378356
        // treating as if it were an IOException
        cleanup(curi, e, "readFully", S_CONNECT_LOST);
        return;
    } finally {
        // ensure recording has stopped
        rec.closeRecorders();
        logger.debug("cloase backup file.&uri= " + curi.getCrawlURIString());
        if (!method.isAborted()) {
            method.releaseConnection();
        }
        // Note completion time
        curi.putLong(A_FETCH_COMPLETED_TIME, System.currentTimeMillis());
        // Set the response charset into the HttpRecord if available.
        setCharacterEncoding(rec, method);
        setSizes(curi, rec);
    }

    if (digestContent) {
        curi.setContentDigest(algorithm, rec.getRecordedInput().getDigestValue());
    }

    logger.info((curi.isPost() ? "POST" : "GET") + " " + curi.getUURI().toString() + " "
            + method.getStatusCode() + " " + rec.getRecordedInput().getSize() + " " + curi.getContentType());

    if (curi.isSuccess() && addedCredentials) {
        // Promote the credentials from the CrawlURI to the CrawlServer
        // so they are available for all subsequent CrawlURIs on this
        // server.
        promoteCredentials(curi);
        if (logger.isDebugEnabled()) {
            // Print out the cookie.  Might help with the debugging.
            Header setCookie = method.getResponseHeader("set-cookie");
            if (setCookie != null) {
                logger.debug(setCookie.toString().trim());
            }
        }
    } else if (method.getStatusCode() == HttpStatus.SC_UNAUTHORIZED) {
        // 401 is not 'success'.
        handle401(method, curi);
    }

    if (rec.getRecordedInput().isOpen()) {
        logger.error(curi.toString() + " RIS still open. Should have" + " been closed by method release: "
                + Thread.currentThread().getName());
        try {
            rec.getRecordedInput().close();
        } catch (IOException e) {
            logger.error("second-chance RIS close failed", e);
        }
    }
}

From source file:org.eclipse.smila.connectivity.framework.crawler.web.http.HttpResponse.java

/**
 * Sets the http parameters.//  w  w  w  .j  a  v  a  2  s.  com
 * 
 * @param http
 *          the http
 * @param httpMethod
 *          the http method
 */
private void setHttpParameters(HttpBase http, HttpMethodBase httpMethod) {
    httpMethod.setFollowRedirects(false);
    httpMethod.setRequestHeader("User-Agent", http.getUserAgent());
    httpMethod.setRequestHeader("Referer", http.getReferer());

    httpMethod.setDoAuthentication(true);

    for (Header header : http.getHeaders()) {
        httpMethod.addRequestHeader(header);
    }

    final HttpMethodParams params = httpMethod.getParams();
    if (http.getUseHttp11()) {
        params.setVersion(HttpVersion.HTTP_1_1);
    } else {
        params.setVersion(HttpVersion.HTTP_1_0);
    }
    params.makeLenient();
    params.setContentCharset("UTF-8");

    if (http.isCookiesEnabled()) {
        params.setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
    } else {
        params.setCookiePolicy(CookiePolicy.IGNORE_COOKIES);
    }
    params.setBooleanParameter(HttpMethodParams.SINGLE_COOKIE_HEADER, true);
    // the default is to retry 3 times; if
    // the request body was sent the method is not retried, so there is
    // little danger in retrying
    // retries are handled on the higher level
    params.setParameter(HttpMethodParams.RETRY_HANDLER, null);
}

From source file:org.fao.geonet.csw.common.requests.CatalogRequest.java

private HttpMethodBase setupHttpMethod() throws UnsupportedEncodingException {
    HttpMethodBase httpMethod;

    if (method == Method.GET) {
        alGetParams = new ArrayList<NameValuePair>();

        if (alSetupGetParams.size() != 0) {
            alGetParams.addAll(alSetupGetParams);
        }/*from  w  ww.  ja  v a 2 s .  c om*/

        setupGetParams();
        httpMethod = new GetMethod();
        httpMethod.setPath(path);
        httpMethod.setQueryString(alGetParams.toArray(new NameValuePair[1]));
        System.out.println("GET params:" + httpMethod.getQueryString());
        if (useSOAP)
            httpMethod.addRequestHeader("Accept", "application/soap+xml");
    } else {
        Element params = getPostParams();
        PostMethod post = new PostMethod();
        if (!useSOAP) {
            postData = Xml.getString(new Document(params));
            post.setRequestEntity(new StringRequestEntity(postData, "application/xml", "UTF8"));
        } else {
            postData = Xml.getString(new Document(soapEmbed(params)));
            post.setRequestEntity(new StringRequestEntity(postData, "application/soap+xml", "UTF8"));
        }
        System.out.println("POST params:" + Xml.getString(params));
        httpMethod = post;
        httpMethod.setPath(path);
    }

    //      httpMethod.setFollowRedirects(true);

    if (useAuthent) {
        Credentials cred = new UsernamePasswordCredentials(username, password);
        AuthScope scope = new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM);

        client.getState().setCredentials(scope, cred);
        httpMethod.setDoAuthentication(true);
    }

    return httpMethod;
}

From source file:org.fao.oaipmh.requests.Transport.java

private HttpMethodBase setupHttpMethod() {
    HttpMethodBase httpMethod;

    if (method == Method.GET) {
        httpMethod = new GetMethod();
        httpMethod.setQueryString(alParams.toArray(new NameValuePair[1]));
    } else {//from  w w  w.  ja  v  a  2 s.c  o  m
        PostMethod pm = new PostMethod();
        pm.setRequestBody(alParams.toArray(new NameValuePair[1]));

        httpMethod = pm;
    }

    httpMethod.setPath(address);
    httpMethod.setDoAuthentication(useAuthent());

    return httpMethod;
}

From source file:org.kuali.mobility.knowledgebase.service.KnowledgeBaseServiceImpl.java

private String callKnowledgeBase(String url, RequestEntity requestEntity, boolean isPost) throws Exception {
    String output = null;//from w  w  w.  j a  v a 2  s .  c  om
    //      Document doc = null;
    //      SAXBuilder builder = new SAXBuilder();
    //      BufferedReader in = null;

    HttpClient client = null;
    client = new HttpClient();
    Credentials defaultcreds = new UsernamePasswordCredentials(this.username, this.password);
    client.getState().setCredentials(new AuthScope("remote.kb.iu.edu", 80, AuthScope.ANY_REALM), defaultcreds);
    HttpMethodBase method = null;
    if (isPost) {
        method = preparePostMethod(url, requestEntity);
    } else {
        method = new GetMethod(url);
    }
    method.setDoAuthentication(true);

    //      int timeout = getSocketTimeout(Constants.RSS_SOCKET_TIMEOUT_SECONDS, Constants.RSS_SOCKET_DEFAULT_TIMEOUT);
    int timeout = getSocketTimeout("blah", 5000);
    client.getParams().setParameter(HttpClientParams.SO_TIMEOUT, new Integer(timeout));
    client.getParams().setParameter(HttpClientParams.CONNECTION_MANAGER_TIMEOUT, new Long(timeout));
    client.getParams().setParameter(HttpConnectionParams.CONNECTION_TIMEOUT, new Integer(timeout));
    try {
        int status = client.executeMethod(method);
        //            System.out.println(status + "\n" + get.getResponseBodyAsString());
        //            in = new BufferedReader(new InputStreamReader(method.getResponseBodyAsStream()));
        //            doc = builder.build(in);
        output = this.inputStreamToString(method.getResponseBodyAsStream());
    } finally {
        method.releaseConnection();
    }

    return output;
}