Search in sources :

Example 26 with AuthScope

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

the class HttpProxyUtil method configureProxy.

public static synchronized void configureProxy(HttpClient client) {
    try {
        String url = Provisioning.getInstance().getLocalServer().getAttr(Provisioning.A_zimbraHttpProxyURL, null);
        if (url == null)
            return;
        // need to initializae all the statics
        if (sProxyUrl == null || !sProxyUrl.equals(url)) {
            sProxyUrl = url;
            sProxyUri = new URI(url);
            sProxyAuthScope = null;
            sProxyCreds = null;
            String userInfo = sProxyUri.getUserInfo();
            if (userInfo != null) {
                int i = userInfo.indexOf(':');
                if (i != -1) {
                    sProxyAuthScope = new AuthScope(sProxyUri.getHost(), sProxyUri.getPort(), null);
                    sProxyCreds = new UsernamePasswordCredentials(userInfo.substring(0, i), userInfo.substring(i + 1));
                }
            }
        }
        if (ZimbraLog.misc.isDebugEnabled()) {
            ZimbraLog.misc.debug("setting proxy: " + url);
        }
        client.getHostConfiguration().setProxy(sProxyUri.getHost(), sProxyUri.getPort());
        if (sProxyAuthScope != null && sProxyCreds != null)
            client.getState().setProxyCredentials(sProxyAuthScope, sProxyCreds);
    } catch (ServiceException e) {
        ZimbraLog.misc.warn("Unable to configureProxy: " + e.getMessage(), e);
    } catch (URISyntaxException e) {
        ZimbraLog.misc.warn("Unable to configureProxy: " + e.getMessage(), e);
    }
}
Also used : ServiceException(com.zimbra.common.service.ServiceException) AuthScope(org.apache.commons.httpclient.auth.AuthScope) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) UsernamePasswordCredentials(org.apache.commons.httpclient.UsernamePasswordCredentials)

Example 27 with AuthScope

use of org.apache.commons.httpclient.auth.AuthScope in project sling by apache.

the class HttpTestBase method setUp.

@Override
protected void setUp() throws Exception {
    super.setUp();
    MDC.put("testclass", getClass().getName());
    MDC.put("testcase", getName());
    // assume http and webdav are on the same host + port
    URL url = null;
    try {
        url = new URL(HTTP_BASE_URL);
    } catch (MalformedURLException mfe) {
        // MalformedURLException doesn't tell us the URL by default
        throw new IOException("MalformedURLException: " + HTTP_BASE_URL);
    }
    // setup HTTP client, with authentication (using default Jackrabbit credentials)
    httpClient = new TestInfoPassingClient();
    httpClient.getParams().setAuthenticationPreemptive(true);
    Credentials defaultcreds = getDefaultCredentials();
    httpClient.getState().setCredentials(new AuthScope(url.getHost(), url.getPort(), AuthScope.ANY_REALM), defaultcreds);
    testClient = new SlingIntegrationTestClient(httpClient);
    testClient.setFolderExistsTestExtension(readinessCheckExtension);
    waitForSlingStartup();
}
Also used : MalformedURLException(java.net.MalformedURLException) AuthScope(org.apache.commons.httpclient.auth.AuthScope) IOException(java.io.IOException) URL(java.net.URL) UsernamePasswordCredentials(org.apache.commons.httpclient.UsernamePasswordCredentials) Credentials(org.apache.commons.httpclient.Credentials)

Example 28 with AuthScope

use of org.apache.commons.httpclient.auth.AuthScope in project sling by apache.

the class AuthenticatedTestUtil method assertAuthenticatedHttpStatus.

/** Verify that given URL returns expectedStatusCode
     * @throws IOException */
public void assertAuthenticatedHttpStatus(Credentials creds, String urlString, int expectedStatusCode, String assertMessage) throws IOException {
    URL baseUrl = new URL(HTTP_BASE_URL);
    AuthScope authScope = new AuthScope(baseUrl.getHost(), baseUrl.getPort(), AuthScope.ANY_REALM);
    GetMethod getMethod = new GetMethod(urlString);
    getMethod.setDoAuthentication(true);
    Credentials oldCredentials = httpClient.getState().getCredentials(authScope);
    try {
        httpClient.getState().setCredentials(authScope, creds);
        final int status = httpClient.executeMethod(getMethod);
        if (assertMessage == null) {
            assertEquals(urlString, expectedStatusCode, status);
        } else {
            assertEquals(assertMessage, expectedStatusCode, status);
        }
    } finally {
        httpClient.getState().setCredentials(authScope, oldCredentials);
    }
}
Also used : AuthScope(org.apache.commons.httpclient.auth.AuthScope) GetMethod(org.apache.commons.httpclient.methods.GetMethod) URL(java.net.URL) UsernamePasswordCredentials(org.apache.commons.httpclient.UsernamePasswordCredentials) Credentials(org.apache.commons.httpclient.Credentials)

Example 29 with AuthScope

use of org.apache.commons.httpclient.auth.AuthScope in project sling by apache.

the class AuthenticatedTestUtil method getAuthenticatedPostContent.

/** retrieve the contents of given URL and assert its content type
     * @param expectedContentType use CONTENT_TYPE_DONTCARE if must not be checked
     * @throws IOException
     * @throws HttpException */
public String getAuthenticatedPostContent(Credentials creds, String url, String expectedContentType, List<NameValuePair> postParams, int expectedStatusCode) throws IOException {
    final PostMethod post = new PostMethod(url);
    URL baseUrl = new URL(HTTP_BASE_URL);
    AuthScope authScope = new AuthScope(baseUrl.getHost(), baseUrl.getPort(), AuthScope.ANY_REALM);
    post.setDoAuthentication(true);
    Credentials oldCredentials = httpClient.getState().getCredentials(authScope);
    try {
        httpClient.getState().setCredentials(authScope, creds);
        if (postParams != null) {
            final NameValuePair[] nvp = {};
            post.setRequestBody(postParams.toArray(nvp));
        }
        final int status = httpClient.executeMethod(post);
        final InputStream is = post.getResponseBodyAsStream();
        final StringBuffer content = new StringBuffer();
        final String charset = post.getResponseCharSet();
        final byte[] buffer = new byte[16384];
        int n = 0;
        while ((n = is.read(buffer, 0, buffer.length)) > 0) {
            content.append(new String(buffer, 0, n, charset));
        }
        assertEquals("Expected status " + expectedStatusCode + " for " + url + " (content=" + content + ")", expectedStatusCode, status);
        final Header h = post.getResponseHeader("Content-Type");
        if (expectedContentType == null) {
            if (h != null) {
                fail("Expected null Content-Type, got " + h.getValue());
            }
        } else if (CONTENT_TYPE_DONTCARE.equals(expectedContentType)) {
        // no check
        } else if (h == null) {
            fail("Expected Content-Type that starts with '" + expectedContentType + " but got no Content-Type header at " + url);
        } else {
            assertTrue("Expected Content-Type that starts with '" + expectedContentType + "' for " + url + ", got '" + h.getValue() + "'", h.getValue().startsWith(expectedContentType));
        }
        return content.toString();
    } finally {
        httpClient.getState().setCredentials(authScope, oldCredentials);
    }
}
Also used : NameValuePair(org.apache.commons.httpclient.NameValuePair) Header(org.apache.commons.httpclient.Header) PostMethod(org.apache.commons.httpclient.methods.PostMethod) InputStream(java.io.InputStream) AuthScope(org.apache.commons.httpclient.auth.AuthScope) URL(java.net.URL) UsernamePasswordCredentials(org.apache.commons.httpclient.UsernamePasswordCredentials) Credentials(org.apache.commons.httpclient.Credentials)

Example 30 with AuthScope

use of org.apache.commons.httpclient.auth.AuthScope in project sling by apache.

the class AuthenticatedTestUtil method getAuthenticatedContent.

/** retrieve the contents of given URL and assert its content type
     * @param expectedContentType use CONTENT_TYPE_DONTCARE if must not be checked
     * @throws IOException
     * @throws HttpException */
public String getAuthenticatedContent(Credentials creds, String url, String expectedContentType, List<NameValuePair> params, int expectedStatusCode) throws IOException {
    final GetMethod get = new GetMethod(url);
    URL baseUrl = new URL(HTTP_BASE_URL);
    AuthScope authScope = new AuthScope(baseUrl.getHost(), baseUrl.getPort(), AuthScope.ANY_REALM);
    get.setDoAuthentication(true);
    Credentials oldCredentials = httpClient.getState().getCredentials(authScope);
    try {
        httpClient.getState().setCredentials(authScope, creds);
        if (params != null) {
            final NameValuePair[] nvp = new NameValuePair[0];
            get.setQueryString(params.toArray(nvp));
        }
        final int status = httpClient.executeMethod(get);
        final InputStream is = get.getResponseBodyAsStream();
        final StringBuffer content = new StringBuffer();
        final String charset = get.getResponseCharSet();
        final byte[] buffer = new byte[16384];
        int n = 0;
        while ((n = is.read(buffer, 0, buffer.length)) > 0) {
            content.append(new String(buffer, 0, n, charset));
        }
        assertEquals("Expected status " + expectedStatusCode + " for " + url + " (content=" + content + ")", expectedStatusCode, status);
        final Header h = get.getResponseHeader("Content-Type");
        if (expectedContentType == null) {
            if (h != null) {
                fail("Expected null Content-Type, got " + h.getValue());
            }
        } else if (CONTENT_TYPE_DONTCARE.equals(expectedContentType)) {
        // no check
        } else if (h == null) {
            fail("Expected Content-Type that starts with '" + expectedContentType + " but got no Content-Type header at " + url);
        } else {
            assertTrue("Expected Content-Type that starts with '" + expectedContentType + "' for " + url + ", got '" + h.getValue() + "'", h.getValue().startsWith(expectedContentType));
        }
        return content.toString();
    } finally {
        httpClient.getState().setCredentials(authScope, oldCredentials);
    }
}
Also used : NameValuePair(org.apache.commons.httpclient.NameValuePair) Header(org.apache.commons.httpclient.Header) InputStream(java.io.InputStream) GetMethod(org.apache.commons.httpclient.methods.GetMethod) AuthScope(org.apache.commons.httpclient.auth.AuthScope) URL(java.net.URL) UsernamePasswordCredentials(org.apache.commons.httpclient.UsernamePasswordCredentials) Credentials(org.apache.commons.httpclient.Credentials)

Aggregations

AuthScope (org.apache.commons.httpclient.auth.AuthScope)52 UsernamePasswordCredentials (org.apache.commons.httpclient.UsernamePasswordCredentials)34 Credentials (org.apache.commons.httpclient.Credentials)19 GetMethod (org.apache.commons.httpclient.methods.GetMethod)12 HttpClient (org.apache.commons.httpclient.HttpClient)11 URL (java.net.URL)10 IOException (java.io.IOException)5 NTCredentials (org.apache.commons.httpclient.NTCredentials)5 Protocol (org.apache.commons.httpclient.protocol.Protocol)5 ProtocolSocketFactory (org.apache.commons.httpclient.protocol.ProtocolSocketFactory)5 Header (org.apache.commons.httpclient.Header)4 AuthScheme (org.apache.commons.httpclient.auth.AuthScheme)4 AuthState (org.apache.commons.httpclient.auth.AuthState)4 EasySSLProtocolSocketFactory (org.apache.commons.httpclient.contrib.ssl.EasySSLProtocolSocketFactory)4 ClientConfig (com.sun.jersey.api.client.config.ClientConfig)3 DefaultClientConfig (com.sun.jersey.api.client.config.DefaultClientConfig)3 InputStream (java.io.InputStream)3 InputStreamReader (java.io.InputStreamReader)3 URISyntaxException (java.net.URISyntaxException)3 ArrayList (java.util.ArrayList)3