Search in sources :

Example 31 with HttpState

use of org.apache.commons.httpclient.HttpState in project zm-mailbox by Zimbra.

the class TestCookieReuse method testReuseAdminCookieWithCsrf.

/**
     * Verify that we CAN make an admin GET request by reusing a valid csrf-enabled cookie
     */
@Test
public void testReuseAdminCookieWithCsrf() throws Exception {
    AuthToken at = AuthProvider.getAdminAuthToken();
    at.setCsrfTokenEnabled(true);
    int port = 7071;
    try {
        port = Provisioning.getInstance().getLocalServer().getIntAttr(Provisioning.A_zimbraAdminPort, 0);
    } catch (ServiceException e) {
        ZimbraLog.test.error("Unable to get admin SOAP port", e);
    }
    String host = Provisioning.getInstance().getLocalServer().getName();
    String getServerConfigURL = "https://localhost:" + port + "/service/collectconfig/?host=" + host;
    HttpClient eve = ZimbraHttpConnectionManager.getInternalHttpConnMgr().newHttpClient();
    HttpState state = new HttpState();
    at.encode(state, true, "localhost");
    eve.setState(state);
    GetMethod get = new GetMethod(getServerConfigURL);
    int statusCode = HttpClientUtil.executeMethod(eve, get);
    Assert.assertEquals("This request should succeed. Getting status code " + statusCode, HttpStatus.SC_OK, statusCode);
}
Also used : ServiceException(com.zimbra.common.service.ServiceException) HttpClient(org.apache.commons.httpclient.HttpClient) HttpState(org.apache.commons.httpclient.HttpState) GetMethod(org.apache.commons.httpclient.methods.GetMethod) AuthToken(com.zimbra.cs.account.AuthToken) ZAuthToken(com.zimbra.common.auth.ZAuthToken) ZimbraAuthToken(com.zimbra.cs.account.ZimbraAuthToken) Test(org.junit.Test)

Example 32 with HttpState

use of org.apache.commons.httpclient.HttpState in project zm-mailbox by Zimbra.

the class ZMailbox method getHttpClient.

public HttpClient getHttpClient(URI uri) {
    boolean isAdmin = uri.getPort() == LC.zimbra_admin_service_port.intValue();
    HttpState initialState = HttpClientUtil.newHttpState(getAuthToken(), uri.getHost(), isAdmin);
    HttpClient client = ZimbraHttpConnectionManager.getInternalHttpConnMgr().newHttpClient();
    client.setState(initialState);
    client.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
    return client;
}
Also used : HttpClient(org.apache.commons.httpclient.HttpClient) HttpState(org.apache.commons.httpclient.HttpState)

Example 33 with HttpState

use of org.apache.commons.httpclient.HttpState in project maven-plugins by apache.

the class ClassicJiraDownloader method doExecute.

/**
     * Execute the query on the JIRA server.
     *
     * @throws Exception on error
     */
public void doExecute() throws Exception {
    try {
        HttpClient client = new HttpClient();
        // MCHANGES-89 Allow circular redirects
        HttpClientParams clientParams = client.getParams();
        clientParams.setBooleanParameter(HttpClientParams.ALLOW_CIRCULAR_REDIRECTS, true);
        // MCHANGES-237
        clientParams.setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
        HttpState state = new HttpState();
        HostConfiguration hc = new HostConfiguration();
        client.setHostConfiguration(hc);
        client.setState(state);
        String baseUrl = JiraHelper.getBaseUrl(project.getIssueManagement().getUrl());
        getLog().debug("JIRA lives at: " + baseUrl);
        // Here we only need the host part of the URL
        determineProxy(baseUrl, client);
        prepareBasicAuthentication(client);
        boolean jiraAuthenticationSuccessful = false;
        if (isJiraAuthenticationConfigured()) {
            // Here we only need the parts up to and including the host part of the URL
            jiraAuthenticationSuccessful = doJiraAuthentication(client, baseUrl);
        }
        if ((isJiraAuthenticationConfigured() && jiraAuthenticationSuccessful) || !isJiraAuthenticationConfigured()) {
            String fullUrl;
            if (useJql) {
                fullUrl = getJqlQueryURL();
            } else {
                fullUrl = getParameterBasedQueryURL(client);
            }
            if (log.isDebugEnabled()) {
                log.debug("download jira issues from url " + fullUrl);
            }
            // execute the GET
            download(client, fullUrl);
        }
    } catch (Exception e) {
        if (project.getIssueManagement() != null) {
            getLog().error("Error accessing " + project.getIssueManagement().getUrl(), e);
        } else {
            getLog().error("Error accessing mock project issues", e);
        }
    }
}
Also used : HostConfiguration(org.apache.commons.httpclient.HostConfiguration) HttpClient(org.apache.commons.httpclient.HttpClient) HttpClientParams(org.apache.commons.httpclient.params.HttpClientParams) HttpState(org.apache.commons.httpclient.HttpState) HttpException(org.apache.commons.httpclient.HttpException) IOException(java.io.IOException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException)

Aggregations

HttpState (org.apache.commons.httpclient.HttpState)33 HttpClient (org.apache.commons.httpclient.HttpClient)25 GetMethod (org.apache.commons.httpclient.methods.GetMethod)18 Test (org.junit.Test)13 ServiceException (com.zimbra.common.service.ServiceException)9 ZAuthToken (com.zimbra.common.auth.ZAuthToken)8 URI (java.net.URI)8 Cookie (org.apache.commons.httpclient.Cookie)8 PostMethod (org.apache.commons.httpclient.methods.PostMethod)8 ZMailbox (com.zimbra.client.ZMailbox)7 AuthToken (com.zimbra.cs.account.AuthToken)7 UsernamePasswordCredentials (org.apache.commons.httpclient.UsernamePasswordCredentials)6 Account (com.zimbra.cs.account.Account)5 IOException (java.io.IOException)5 Map (java.util.Map)5 HttpMethod (org.apache.commons.httpclient.HttpMethod)5 Element (com.zimbra.common.soap.Element)4 SoapHttpTransport (com.zimbra.common.soap.SoapHttpTransport)4 ZimbraAuthToken (com.zimbra.cs.account.ZimbraAuthToken)4 ArrayList (java.util.ArrayList)4