Search in sources :

Example 1 with HttpOAuthConsumer

use of com.twinsoft.convertigo.engine.oauth.HttpOAuthConsumer in project convertigo by convertigo.

the class HttpConnector method doExecuteMethod.

protected int doExecuteMethod(final HttpMethod method, Context context) throws ConnectionException, URIException, MalformedURLException {
    int statuscode = -1;
    // Tells the method to automatically handle authentication.
    method.setDoAuthentication(true);
    // Tells the method to automatically handle redirection.
    method.setFollowRedirects(false);
    HttpPool httpPool = ((AbstractHttpTransaction) context.transaction).getHttpPool();
    HttpClient httpClient = context.getHttpClient3(httpPool);
    try {
        // Display the cookies
        if (handleCookie) {
            Cookie[] cookies = httpState.getCookies();
            if (Engine.logBeans.isTraceEnabled())
                Engine.logBeans.trace("(HttpConnector) HttpClient request cookies:" + Arrays.asList(cookies).toString());
        }
        forwardHeader(new HeaderForwarder() {

            public void add(String name, String value, String forwardPolicy) {
                if (HttpConnector.HTTP_HEADER_FORWARD_POLICY_IGNORE.equals(forwardPolicy)) {
                    Header exHeader = method.getRequestHeader(name);
                    if (exHeader != null) {
                        // Nothing to do
                        Engine.logEngine.debug("(WebViewer) Forwarding header '" + name + "' has been ignored due to forward policy");
                    } else {
                        method.setRequestHeader(name, value);
                        Engine.logEngine.debug("(WebViewer) Header forwarded and added: " + name + "=" + value);
                    }
                } else if (HttpConnector.HTTP_HEADER_FORWARD_POLICY_REPLACE.equals(forwardPolicy)) {
                    method.setRequestHeader(name, value);
                    Engine.logEngine.debug("(WebViewer) Header forwarded and replaced: " + name + "=" + value);
                } else if (HttpConnector.HTTP_HEADER_FORWARD_POLICY_MERGE.equals(forwardPolicy)) {
                    Header exHeader = method.getRequestHeader(name);
                    if (exHeader != null)
                        value = exHeader.getValue() + ", " + value;
                    method.setRequestHeader(name, value);
                    Engine.logEngine.debug("(WebViewer) Header forwarded and merged: " + name + "=" + value);
                }
            }
        });
        // handle oAuthSignatures if any
        if (oAuthKey != null && oAuthSecret != null && oAuthToken != null && oAuthTokenSecret != null) {
            oAuthConsumer = new HttpOAuthConsumer(oAuthKey, oAuthSecret, hostConfiguration);
            oAuthConsumer.setTokenWithSecret(oAuthToken, oAuthTokenSecret);
            oAuthConsumer.sign(method);
            oAuthConsumer = null;
        }
        HttpUtils.logCurrentHttpConnection(httpClient, hostConfiguration, httpPool);
        hostConfiguration.getParams().setIntParameter(HttpConnectionParams.SO_TIMEOUT, (int) context.requestedObject.getResponseTimeout() * 1000);
        hostConfiguration.getParams().setIntParameter(HttpConnectionParams.CONNECTION_TIMEOUT, (int) context.requestedObject.getResponseTimeout() * 1000);
        Engine.logBeans.debug("(HttpConnector) HttpClient: executing method...");
        statuscode = httpClient.executeMethod(hostConfiguration, method, httpState);
        Engine.logBeans.debug("(HttpConnector) HttpClient: end of method successfull");
        // Display the cookies
        if (handleCookie) {
            Cookie[] cookies = httpState.getCookies();
            if (Engine.logBeans.isTraceEnabled())
                Engine.logBeans.trace("(HttpConnector) HttpClient response cookies:" + Arrays.asList(cookies).toString());
        }
    } catch (SocketTimeoutException e) {
        throw new ConnectionException("Timeout reached (" + context.requestedObject.getResponseTimeout() + " sec)");
    } catch (IOException e) {
        if (!context.requestedObject.runningThread.bContinue) {
            return statuscode;
        }
        HttpUtils.logCurrentHttpConnection(httpClient, hostConfiguration, httpPool);
        Engine.logBeans.warn("(HttpConnector) HttpClient: connection error to " + sUrl + ": " + e.getMessage());
    } catch (OAuthException eee) {
        throw new ConnectionException("OAuth Connection error to " + sUrl, eee);
    }
    return statuscode;
}
Also used : Cookie(org.apache.commons.httpclient.Cookie) OAuthException(oauth.signpost.exception.OAuthException) IOException(java.io.IOException) HttpPool(com.twinsoft.convertigo.engine.enums.HttpPool) AbstractHttpTransaction(com.twinsoft.convertigo.beans.transactions.AbstractHttpTransaction) SocketTimeoutException(java.net.SocketTimeoutException) Header(org.apache.commons.httpclient.Header) HttpClient(org.apache.commons.httpclient.HttpClient) HttpOAuthConsumer(com.twinsoft.convertigo.engine.oauth.HttpOAuthConsumer)

Aggregations

AbstractHttpTransaction (com.twinsoft.convertigo.beans.transactions.AbstractHttpTransaction)1 HttpPool (com.twinsoft.convertigo.engine.enums.HttpPool)1 HttpOAuthConsumer (com.twinsoft.convertigo.engine.oauth.HttpOAuthConsumer)1 IOException (java.io.IOException)1 SocketTimeoutException (java.net.SocketTimeoutException)1 OAuthException (oauth.signpost.exception.OAuthException)1 Cookie (org.apache.commons.httpclient.Cookie)1 Header (org.apache.commons.httpclient.Header)1 HttpClient (org.apache.commons.httpclient.HttpClient)1