Search in sources :

Example 11 with HttpState

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

the class ExchangeFreeBusyProvider method basicAuth.

private boolean basicAuth(HttpClient client, ServerInfo info) {
    HttpState state = new HttpState();
    Credentials cred = new UsernamePasswordCredentials(info.authUsername, info.authPassword);
    state.setCredentials(AuthScope.ANY, cred);
    client.setState(state);
    ArrayList<String> authPrefs = new ArrayList<String>();
    authPrefs.add(AuthPolicy.BASIC);
    client.getParams().setParameter(AuthPolicy.AUTH_SCHEME_PRIORITY, authPrefs);
    return true;
}
Also used : HttpState(org.apache.commons.httpclient.HttpState) ArrayList(java.util.ArrayList) UsernamePasswordCredentials(org.apache.commons.httpclient.UsernamePasswordCredentials) Credentials(org.apache.commons.httpclient.Credentials) UsernamePasswordCredentials(org.apache.commons.httpclient.UsernamePasswordCredentials)

Example 12 with HttpState

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

the class ExchangeFreeBusyProvider method formAuth.

private boolean formAuth(HttpClient client, ServerInfo info) throws IOException {
    StringBuilder buf = new StringBuilder();
    buf.append("destination=");
    buf.append(URLEncoder.encode(info.url, "UTF-8"));
    buf.append("&username=");
    buf.append(info.authUsername);
    buf.append("&password=");
    buf.append(URLEncoder.encode(info.authPassword, "UTF-8"));
    buf.append("&flags=0");
    buf.append("&SubmitCreds=Log On");
    buf.append("&trusted=0");
    String url = info.url + LC.calendar_exchange_form_auth_url.value();
    PostMethod method = new PostMethod(url);
    ByteArrayRequestEntity re = new ByteArrayRequestEntity(buf.toString().getBytes(), "x-www-form-urlencoded");
    method.setRequestEntity(re);
    HttpState state = new HttpState();
    client.setState(state);
    try {
        int status = HttpClientUtil.executeMethod(client, method);
        if (status >= 400) {
            ZimbraLog.fb.error("form auth to Exchange returned an error: " + status);
            return false;
        }
    } finally {
        method.releaseConnection();
    }
    return true;
}
Also used : PostMethod(org.apache.commons.httpclient.methods.PostMethod) HttpState(org.apache.commons.httpclient.HttpState) ByteArrayRequestEntity(org.apache.commons.httpclient.methods.ByteArrayRequestEntity)

Example 13 with HttpState

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

the class TestCollectConfigServletsAccess method testLDAPConfigDelegatedAdmin.

/**
     * Verify that global admin can access servlet at /service/collectldapconfig/
     * @throws Exception
     */
/*  @Test
    public void testLDAPConfigGlobalAdmin() throws Exception {
        ZAuthToken at = TestUtil.getAdminSoapTransport().getAuthToken();
        URI servletURI = new URI(getLDAPConfigServletUrl());
        HttpState initialState = HttpClientUtil.newHttpState(at, servletURI.getHost(), true);
        HttpClient restClient = ZimbraHttpConnectionManager.getInternalHttpConnMgr().newHttpClient();
        restClient.setState(initialState);
        restClient.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
        GetMethod get = new GetMethod(servletURI.toString());
        int statusCode = HttpClientUtil.executeMethod(restClient, get);
        if(statusCode == HttpStatus.SC_INTERNAL_SERVER_ERROR) {
            fail("collectldapconfig servlet is failing. Likely Zimbra SSH access is not properly configured. " + get.getResponseHeader("X-Zimbra-Fault-Message").getValue());
        } else {
            assertEquals("This request should succeed. Getting status code " + statusCode, HttpStatus.SC_OK,statusCode);
        }
    }*/
/**
     * Verify that delegated admin canNOT access servlet at /service/collectldapconfig/
     * @throws Exception
     */
@Test
public void testLDAPConfigDelegatedAdmin() throws Exception {
    ZAuthToken at = TestUtil.getAdminSoapTransport(TEST_ADMIN_NAME, PASSWORD).getAuthToken();
    URI servletURI = new URI(getLDAPConfigServletUrl());
    HttpState initialState = HttpClientUtil.newHttpState(at, servletURI.getHost(), true);
    HttpClient restClient = ZimbraHttpConnectionManager.getInternalHttpConnMgr().newHttpClient();
    restClient.setState(initialState);
    restClient.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
    GetMethod get = new GetMethod(servletURI.toString());
    int statusCode = HttpClientUtil.executeMethod(restClient, get);
    assertEquals("This request should NOT succeed. Getting status code " + statusCode, HttpStatus.SC_UNAUTHORIZED, statusCode);
}
Also used : HttpClient(org.apache.commons.httpclient.HttpClient) HttpState(org.apache.commons.httpclient.HttpState) GetMethod(org.apache.commons.httpclient.methods.GetMethod) ZAuthToken(com.zimbra.common.auth.ZAuthToken) URI(java.net.URI) Test(org.junit.Test)

Example 14 with HttpState

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

the class TestCollectConfigServletsAccess method testConfigDelegatedAdmin.

/**
     * Verify that global admin can access servlet at /service/collectconfig/
     * @throws Exception
     */
/*  @Test
    public void testConfigGlobalAdmin() throws Exception {
        ZAuthToken at = TestUtil.getAdminSoapTransport().getAuthToken();
        URI servletURI = new URI(getConfigServletUrl());
        HttpState initialState = HttpClientUtil.newHttpState(at, servletURI.getHost(), true);
        HttpClient restClient = ZimbraHttpConnectionManager.getInternalHttpConnMgr().newHttpClient();
        restClient.setState(initialState);
        restClient.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
        GetMethod get = new GetMethod(servletURI.toString());
        int statusCode = HttpClientUtil.executeMethod(restClient, get);
        if(statusCode == HttpStatus.SC_INTERNAL_SERVER_ERROR) {
            fail("collectconfig servlet is failing. Likely Zimbra SSH access is not properly configured. " + get.getResponseHeader("X-Zimbra-Fault-Message").getValue());
        } else {
            assertEquals("This request should succeed. Getting status code " + statusCode, HttpStatus.SC_OK,statusCode);
        }
    }*/
/**
     * Verify that delegated admin canNOT access servlet at /service/collectconfig/
     * @throws Exception
     */
@Test
public void testConfigDelegatedAdmin() throws Exception {
    ZAuthToken at = TestUtil.getAdminSoapTransport(TEST_ADMIN_NAME, PASSWORD).getAuthToken();
    URI servletURI = new URI(getConfigServletUrl());
    HttpState initialState = HttpClientUtil.newHttpState(at, servletURI.getHost(), true);
    HttpClient restClient = ZimbraHttpConnectionManager.getInternalHttpConnMgr().newHttpClient();
    restClient.setState(initialState);
    restClient.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
    GetMethod get = new GetMethod(servletURI.toString());
    int statusCode = HttpClientUtil.executeMethod(restClient, get);
    assertEquals("This request should NOT succeed. Getting status code " + statusCode, HttpStatus.SC_UNAUTHORIZED, statusCode);
}
Also used : HttpClient(org.apache.commons.httpclient.HttpClient) HttpState(org.apache.commons.httpclient.HttpState) GetMethod(org.apache.commons.httpclient.methods.GetMethod) ZAuthToken(com.zimbra.common.auth.ZAuthToken) URI(java.net.URI) Test(org.junit.Test)

Example 15 with HttpState

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

the class StatsImageServlet method doGet.

public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    AuthToken authToken = getAdminAuthTokenFromCookie(req, resp);
    if (authToken == null)
        return;
    String imgName = null;
    InputStream is = null;
    boolean imgAvailable = true;
    boolean localServer = false;
    boolean systemWide = false;
    String serverAddr = "";
    String noDefaultImg = req.getParameter("nodef");
    boolean noDefault = false;
    if (noDefaultImg != null && !noDefaultImg.equals("") && noDefaultImg.equals("1")) {
        noDefault = true;
    }
    String reqPath = req.getRequestURI();
    try {
        //check if this is the logger host, otherwise proxy the request to the logger host 
        String serviceHostname = Provisioning.getInstance().getLocalServer().getAttr(Provisioning.A_zimbraServiceHostname);
        String logHost = Provisioning.getInstance().getConfig().getAttr(Provisioning.A_zimbraLogHostname);
        if (!serviceHostname.equalsIgnoreCase(logHost)) {
            StringBuffer url = new StringBuffer("https");
            url.append("://").append(logHost).append(':').append(LC.zimbra_admin_service_port.value());
            url.append(reqPath);
            String queryStr = req.getQueryString();
            if (queryStr != null)
                url.append('?').append(queryStr);
            // create an HTTP client with the same cookies
            HttpState state = new HttpState();
            try {
                state.addCookie(new org.apache.commons.httpclient.Cookie(logHost, ZimbraCookie.COOKIE_ZM_ADMIN_AUTH_TOKEN, authToken.getEncoded(), "/", null, false));
            } catch (AuthTokenException ate) {
                throw ServiceException.PROXY_ERROR(ate, url.toString());
            }
            HttpClient client = ZimbraHttpConnectionManager.getInternalHttpConnMgr().newHttpClient();
            client.setState(state);
            GetMethod get = new GetMethod(url.toString());
            try {
                int statusCode = HttpClientUtil.executeMethod(client, get);
                if (statusCode != HttpStatus.SC_OK)
                    throw ServiceException.RESOURCE_UNREACHABLE(get.getStatusText(), null);
                resp.setContentType("image/gif");
                ByteUtil.copy(get.getResponseBodyAsStream(), true, resp.getOutputStream(), false);
                return;
            } catch (HttpException e) {
                throw ServiceException.RESOURCE_UNREACHABLE(get.getStatusText(), e);
            } catch (IOException e) {
                throw ServiceException.RESOURCE_UNREACHABLE(get.getStatusText(), e);
            } finally {
                get.releaseConnection();
            }
        }
    } catch (Exception ex) {
        resp.sendError(HttpServletResponse.SC_NOT_FOUND, "Image not found");
        return;
    }
    try {
        if (reqPath == null || reqPath.length() == 0) {
            resp.sendError(HttpServletResponse.SC_BAD_REQUEST);
            return;
        }
        if (mLog.isDebugEnabled())
            mLog.debug("received request to:(" + reqPath + ")");
        String[] reqParts = reqPath.split("/");
        String reqFilename = reqParts[3];
        imgName = LC.stats_img_folder.value() + File.separator + reqFilename;
        try {
            is = new FileInputStream(imgName);
        } catch (FileNotFoundException ex) {
            //unlikely case - only if the server's files are broken
            if (is != null)
                is.close();
            if (!noDefault) {
                imgName = LC.stats_img_folder.value() + File.separator + IMG_NOT_AVAIL;
                is = new FileInputStream(imgName);
            } else {
                resp.sendError(HttpServletResponse.SC_NOT_FOUND, "Image not found");
                return;
            }
        }
    } catch (Exception ex) {
        if (is != null)
            is.close();
        resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "FNF image File not found");
        return;
    }
    resp.setContentType("image/gif");
    ByteUtil.copy(is, true, resp.getOutputStream(), false);
}
Also used : HttpState(org.apache.commons.httpclient.HttpState) ServletException(javax.servlet.ServletException) ServiceException(com.zimbra.common.service.ServiceException) AuthTokenException(com.zimbra.cs.account.AuthTokenException) HttpException(org.apache.commons.httpclient.HttpException) AuthTokenException(com.zimbra.cs.account.AuthTokenException) HttpClient(org.apache.commons.httpclient.HttpClient) GetMethod(org.apache.commons.httpclient.methods.GetMethod) AuthToken(com.zimbra.cs.account.AuthToken) HttpException(org.apache.commons.httpclient.HttpException)

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