Package org.apache.commons.httpclient

Examples of org.apache.commons.httpclient.HttpState


            if (proxyHost != null)
            {
                hc.setProxy(proxyHost, proxyPort);
            }
            HttpState state = new HttpState();
           
            if (proxyUser != null && proxyPass != null)
            {
                state.setProxyCredentials(null, new UsernamePasswordCredentials(proxyUser, proxyPass));
            }

            GetMethod get = new GetMethod(link);
            cl.setHostConfiguration(hc);
            cl.setState(state);
View Full Code Here


        GetMethod indexGet = new GetMethod( path );
       
        if ( httpClient.executeMethod(indexGet) == 200 )
        {
       
            HttpState state = httpClient.getState();
            Cookie[] cookies = state.getCookies();
          for (Cookie cookie : cookies)
      {
                if( cookie.getName().equalsIgnoreCase("JSESSIONID") )
                {
                    sessionId = "JSESSIONID=" + cookie.getValue();
View Full Code Here

     * @param authManager
     *            the <code>AuthManager</code> containing all the authorisations for
     *            this <code>UrlConfig</code>
     */
    private void setConnectionAuthorization(HttpClient client, URL u, AuthManager authManager) {
        HttpState state = client.getState();
        if (authManager != null) {
            HttpClientParams params = client.getParams();
            Authorization auth = authManager.getAuthForURL(u);
            if (auth != null) {
                    String username = auth.getUser();
                    String realm = auth.getRealm();
                    String domain = auth.getDomain();
                    if (log.isDebugEnabled()){
                        log.debug(username + " >  D="+ username + " D="+domain+" R="+realm);
                    }
                    state.setCredentials(
                            new AuthScope(u.getHost(),u.getPort(),
                                    realm.length()==0 ? null : realm //"" is not the same as no realm
                                    ,AuthScope.ANY_SCHEME),
                            // NT Includes other types of Credentials
                            new NTCredentials(
                                    username,
                                    auth.getPass(),
                                    localHost,
                                    domain
                            ));
                    // We have credentials - should we set pre-emptive authentication?
                    if (canSetPreEmptive){
                        log.debug("Setting Pre-emptive authentication");
                        params.setAuthenticationPreemptive(true);
                    }
            } else {
                state.clearCredentials();
                if (canSetPreEmptive){
                    params.setAuthenticationPreemptive(false);
                }
            }
        } else {
            state.clearCredentials();
        }
    }
View Full Code Here

            final HTTPRequestType requestType,
            final HTTPVersion version,
            final ProxyManager proxyManager,
            final XMLPipelineContext xmlPipelineContext) throws HTTPException {

        final HttpState state = new HttpState();
        state.setCookiePolicy(CookiePolicy.COMPATIBILITY);

        HttpClientFactory factory = HttpClientFactory.getDefaultInstance();
        HttpClientBuilder builder = factory.createClientBuilder();
        builder.setState(state);
        builder.setConnectionTimeout(timeout);
        builder.setRoundTripTimeout(timeout);

        // create the HTTPClient instance.
        final HttpClient httpClient = builder.buildHttpClient();

        // return an HTTPRequestExecutor that uses HttpClient to perform
        // the request
        return new HTTPRequestExecutor() {

            /**
             * The request method to use.
             */
            HttpMethod method = null;

            /**
             * The list of request parameters
             */
            List requestParameters = null;

            /**
             * A list containing the request headers.
             */
            List requestHeaders = null;

            // javadoc inherited
            public HTTPResponseAccessor execute() throws HTTPException {

                // create the request method this will be POST or GET depending
                // on the requestType argument. This will copy the request
                // parameters to the appropraite place.
                method = createMethod(url, requestType, requestParameters);

                transferRequestHeaders(method, requestHeaders);

                // set the http version for the request
                ((HttpMethodBase) method).setHttp11(
                        version == HTTPVersion.HTTP_1_1);

                if (proxyManager != null) {
                    try {
                        URL realUrl = new URL(url);

                        // Get the proxy config to use for the host.
                        Proxy proxy = proxyManager.getProxyForHost(
                                realUrl.getHost());
                        if (proxy != null) {
                            method.getHostConfiguration().
                                setProxy(proxy.getHost(), proxy.getPort());

                        if (proxy.useAuthorization()) {
                            method.setDoAuthentication(true);

                            // mock up a authentication challenge so we can get
                            // the response (which can be sent before the
                            // challenge if we want to save time and effort)
                            method.setRequestHeader(
                                HTTPAuthorizationScheme.PROXY_AUTHORIZATION_HEADER,
                                HTTPAuthorizationScheme.BASIC.
                                        createResponseForChallenge(HTTPAuthorizationScheme.MOCK_CHALLENGE_BASIC,
                                    proxy));
                        }
                        }
                    } catch (MalformedURLException mue) {
                        LOGGER.error(mue);
                    }
                }

                try {
                    HttpStatusCode statusCode = httpClient.executeMethod(method);
                    if (xmlPipelineContext != null) {
                        final DependencyContext dependencyContext =
                            xmlPipelineContext.getDependencyContext();
                        if (dependencyContext != null &&
                                dependencyContext.isTrackingDependencies()) {
                            dependencyContext.addDependency(
                                UncacheableDependency.getInstance());
                        }
                    }
                    return new HTTPClientResponseAccessor(method, statusCode);
                } catch (IOException e) {
                    throw new HTTPException(e);
                }
            }

            // javadoc inherited
            public void release() {
                if (method != null) {
                    method.releaseConnection();
                }
            }

            // javadoc inherited
            public void addRequestParameter(RequestParameter parameter) {

                // if no list of request parameters exists then create one and
                // add the request parameter
                if (parameter != null) {
                    if (requestParameters == null) {
                        requestParameters = new ArrayList();
                    }
                    requestParameters.add(parameter);
                }
            }

            // javadoc inherited
            public void addRequestHeader(Header header) {
                if (header != null) {
                    if (requestHeaders == null) {
                        requestHeaders = new ArrayList();
                    }
                    requestHeaders.add(header);
                }
            }

            // javadoc inherited
            public void addRequestCookie(Cookie cookie) {
                // For each cookie we need to create an equivalent HttpClient
                // cookie

                org.apache.commons.httpclient.Cookie httpClientCookie =
                        cookieToHTTPClientCookie(cookie);

                // Now add the cookie header to the HttpState. The state object
                // is needed as cookies set directly as headers on the request
                // are ignored.
                state.addCookie(httpClientCookie);
            }

            /**
             * Method that transfers the list of request headers to the
             * HttpMethod supplied.
View Full Code Here

        }
        this.manager = manager;

        // If no state is provided then create an empty one that is only
        // used by this instance.
        HttpState state = builder.getState();
        if (state == null) {
            state = new HttpState();
        }
        httpState = state;
    }
View Full Code Here

            RequestForwardingHttpMethod method =
                new RequestForwardingHttpMethod(request, destination);
            
            // Build the forwarded connection   
            HttpConnection conn = new HttpConnection(destination.getHost(), destination.getPort());           
            HttpState state = new HttpState();
            state.setCredentials(null, destination.getHost(),
                new UsernamePasswordCredentials(destination.getUser(), destination.getPassword()));
            method.setPath(path);
           
            // Execute the method
            method.execute(state, conn);
View Full Code Here

        try {
            DOMStreamer propertyStreamer = new DOMStreamer(this.xmlConsumer);
            OptionsMethod optionsMethod = new OptionsMethod(this.targetUrl);
            SearchMethod searchMethod = new SearchMethod(this.targetUrl, query);
            HttpURL url = new HttpURL(this.targetUrl);
            HttpState state = new HttpState();
            state.setCredentials(null, new UsernamePasswordCredentials(
                    url.getUser(),
                    url.getPassword()));                      
            HttpConnection conn = new HttpConnection(url.getHost(), url.getPort());
            WebdavResource resource = new WebdavResource(new HttpURL(this.targetUrl));
            if(!resource.exists()) {
View Full Code Here

            return;
        }

        /* Call up the remote HTTP server */
        HttpConnection connection = new HttpConnection(this.method.getHostConfiguration());
        HttpState state = new HttpState();
        this.method.setFollowRedirects(true);
        int status = this.method.execute(state, connection);
        if (status == 404) {
            throw new ResourceNotFoundException("Unable to access \"" + this.method.getURI()
                    + "\" (HTTP 404 Error)");
View Full Code Here

            // MCHANGES-89 Allow circular redirects
            HttpClientParams clientParams = client.getParams();
            clientParams.setBooleanParameter( HttpClientParams.ALLOW_CIRCULAR_REDIRECTS, true );

            HttpState state = new HttpState();

            HostConfiguration hc = new HostConfiguration();

            client.setHostConfiguration( hc );
View Full Code Here

     * @param authManager
     *            the <code>AuthManager</code> containing all the authorisations for
     *            this <code>UrlConfig</code>
     */
    private void setConnectionAuthorization(HttpClient client, URL u, AuthManager authManager) {
        HttpState state = client.getState();
        if (authManager != null) {
            HttpClientParams params = client.getParams();
            Authorization auth = authManager.getAuthForURL(u);
            if (auth != null) {
                    String username = auth.getUser();
                    String realm = auth.getRealm();
                    String domain = auth.getDomain();
                    if (log.isDebugEnabled()){
                        log.debug(username + " >  D="+ username + " D="+domain+" R="+realm);
                    }
                    state.setCredentials(
                            new AuthScope(u.getHost(),u.getPort(),
                                    realm.length()==0 ? null : realm //"" is not the same as no realm
                                    ,AuthScope.ANY_SCHEME),
                            // NT Includes other types of Credentials
                            new NTCredentials(
                                    username,
                                    auth.getPass(),
                                    localHost,
                                    domain
                            ));
                    // We have credentials - should we set pre-emptive authentication?
                    if (canSetPreEmptive){
                        log.debug("Setting Pre-emptive authentication");
                        params.setAuthenticationPreemptive(true);
                    }
            } else {
                state.clearCredentials();
                if (canSetPreEmptive){
                    params.setAuthenticationPreemptive(false);
                }
            }
        } else {
            state.clearCredentials();
        }
    }
View Full Code Here

TOP

Related Classes of org.apache.commons.httpclient.HttpState

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.