Example usage for org.apache.commons.httpclient HttpState setAuthenticationPreemptive

List of usage examples for org.apache.commons.httpclient HttpState setAuthenticationPreemptive

Introduction

In this page you can find the example usage for org.apache.commons.httpclient HttpState setAuthenticationPreemptive.

Prototype

public void setAuthenticationPreemptive(boolean paramBoolean) 

Source Link

Usage

From source file:com.idega.slide.business.IWSlideServiceBean.java

@SuppressWarnings("deprecation")
private HttpClient getHttpClient(HttpURL url, UsernamePasswordCredentials credentials) throws Exception {
    HttpSession currentSession = getCurrentSession();

    HttpState state = new WebdavState();
    AuthScope authScope = new AuthScope(url.getHost(), url.getPort());
    state.setCredentials(authScope, credentials);
    if (currentSession != null) {
        IWTimestamp iwExpires = new IWTimestamp(System.currentTimeMillis());
        iwExpires.setMinute(iwExpires.getMinute() + 30);
        Date expires = new Date(iwExpires.getTimestamp().getTime());

        boolean secure = url instanceof HttpsURL;

        Cookie cookie = new Cookie(url.getHost(), CoreConstants.PARAMETER_SESSION_ID, currentSession.getId(),
                CoreConstants.SLASH, expires, secure);
        state.addCookie(cookie);// w  w  w .j av  a 2  s  .  c o m
    }

    HttpClient client = new HttpClient(new MultiThreadedHttpConnectionManager());
    client.setState(state);

    HostConfiguration hostConfig = client.getHostConfiguration();
    hostConfig.setHost(url);

    Credentials hostCredentials = null;

    if (credentials == null) {
        String userName = url.getUser();
        if (userName != null && userName.length() > 0) {
            hostCredentials = new UsernamePasswordCredentials(userName, url.getPassword());
        }
    }

    if (hostCredentials != null) {
        HttpState clientState = client.getState();
        clientState.setCredentials(null, url.getHost(), hostCredentials);
        clientState.setAuthenticationPreemptive(true);
    }

    return client;
}

From source file:org.apache.cactus.client.authentication.BasicAuthentication.java

/**
 * {@inheritDoc}/* ww w  .ja va2  s  .  c o  m*/
 * @see Authentication#configure
 */
public void configure(HttpState theState, HttpMethod theMethod, WebRequest theRequest,
        Configuration theConfiguration) {
    theState.setAuthenticationPreemptive(true);
    theState.setCredentials(null, null, new UsernamePasswordCredentials(getName(), getPassword()));
    theMethod.setDoAuthentication(true);
}

From source file:org.apache.webdav.lib.WebdavSession.java

/**
 * Get a <code>HttpClient</code> instance.
 * This method returns a new client instance, when reset is true.
 *
 * @param httpURL The http URL to connect.  only used the authority part.
 * @param reset The reset flag to represent whether the saved information
 *              is used or not./*from ww  w  .ja va 2  s. com*/
 * @return An instance of <code>HttpClient</code>.
 * @exception IOException
 */
public HttpClient getSessionInstance(HttpURL httpURL, boolean reset) throws IOException {

    if (reset || client == null) {
        client = new HttpClient();
        // Set a state which allows lock tracking
        client.setState(new WebdavState());
        HostConfiguration hostConfig = client.getHostConfiguration();
        hostConfig.setHost(httpURL);
        if (proxyHost != null && proxyPort > 0)
            hostConfig.setProxy(proxyHost, proxyPort);

        if (hostCredentials == null) {
            String userName = httpURL.getUser();
            if (userName != null && userName.length() > 0) {
                hostCredentials = new UsernamePasswordCredentials(userName, httpURL.getPassword());
            }
        }

        if (hostCredentials != null) {
            HttpState clientState = client.getState();
            clientState.setCredentials(null, httpURL.getHost(), hostCredentials);
            clientState.setAuthenticationPreemptive(true);
        }

        if (proxyCredentials != null) {
            client.getState().setProxyCredentials(null, proxyHost, proxyCredentials);
        }
    }

    return client;
}

From source file:org.jboss.test.security.test.SubjectContextUnitTestCase.java

public void testUserMethodViaServlet() throws Exception {
    log.debug("+++ testUserMethodViaServlet()");
    SecurityAssociation.clear();/*from  w  w w.  ja v  a  2s.c  o m*/

    Principal callerIdentity = new SimplePrincipal("jduke");
    Principal runAsIdentity = new SimplePrincipal("runAsUser");
    HashSet expectedCallerRoles = new HashSet();
    expectedCallerRoles.add("groupMemberCaller");
    expectedCallerRoles.add("userCaller");
    expectedCallerRoles.add("allAuthCaller");
    expectedCallerRoles.add("webUser");
    HashSet expectedRunAsRoles = new HashSet();
    expectedRunAsRoles.add("identitySubstitutionCaller");
    expectedRunAsRoles.add("extraRunAsRole");
    CallerInfo info = new CallerInfo(callerIdentity, runAsIdentity, expectedCallerRoles, expectedRunAsRoles);

    String baseURL = HttpUtils.getBaseURL("jduke", "theduke");
    PostMethod formPost = new PostMethod(baseURL + "subject-context/restricted/RunAsServlet");
    formPost.setDoAuthentication(true);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream oos = new ObjectOutputStream(baos);
    oos.writeObject("userMethod");
    oos.writeObject(info);
    oos.close();
    log.info("post content length: " + baos.toByteArray().length);
    ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
    formPost.setRequestBody(bais);
    String host = formPost.getHostConfiguration().getHost();
    HttpClient httpConn = new HttpClient();
    HttpState state = httpConn.getState();
    state.setAuthenticationPreemptive(true);
    UsernamePasswordCredentials upc = new UsernamePasswordCredentials("jduke", "theduke");
    state.setCredentials("JBossTest Servlets", host, upc);

    int responseCode = httpConn.executeMethod(formPost);
    assertTrue("POST OK(" + responseCode + ")", responseCode == HttpURLConnection.HTTP_OK);
}

From source file:org.pengyou.client.lib.DavSession.java

/**
 * Get a <code>HttpClient</code> instance.
 * This method returns a new client instance, when reset is true.
 *
 * @param reset The reset flag to represent whether the saved information
 *              is used or not./*from   w  ww  .j  a va 2  s.c o m*/
 * @return An instance of <code>HttpClient</code>.
 * @exception IOException
 */
public HttpClient getSessionInstance(boolean reset) throws IOException {

    if (reset || client == null) {
        HttpURL httpURL = context.getHttpURL();
        client = new HttpClient();
        // Set a state which allows lock tracking
        client.setState(new WebdavState());
        HostConfiguration hostConfig = client.getHostConfiguration();
        hostConfig.setHost(httpURL);
        if (context.getProxyHost() != null && context.getProxyPort() > 0)
            hostConfig.setProxy(context.getProxyHost(), context.getProxyPort());

        if (hostCredentials == null) {
            String userName = httpURL.getUser();
            if (userName != null && userName.length() > 0) {
                hostCredentials = new UsernamePasswordCredentials(userName, httpURL.getPassword());
            }
        }

        if (hostCredentials != null) {
            HttpState clientState = client.getState();
            clientState.setCredentials(null, httpURL.getHost(), hostCredentials);
            clientState.setAuthenticationPreemptive(true);
        }

        if (proxyCredentials != null) {
            client.getState().setProxyCredentials(null, context.getProxyHost(), proxyCredentials);
        }
    }

    return client;
}