Search in sources :

Example 36 with HttpMethod

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

the class RemoteFreeBusyProvider method getResults.

@Override
public List<FreeBusy> getResults() {
    ArrayList<FreeBusy> fbList = new ArrayList<FreeBusy>();
    for (Request req : mRequestList) {
        HttpMethod method = null;
        Account acct = (Account) req.data;
        try {
            StringBuilder targetUrl = new StringBuilder();
            targetUrl.append(UserServlet.getRestUrl(acct));
            targetUrl.append("/Calendar?fmt=ifb");
            targetUrl.append("&start=").append(mStart);
            targetUrl.append("&end=").append(mEnd);
            if (req.folder != FreeBusyQuery.CALENDAR_FOLDER_ALL)
                targetUrl.append("&").append(UserServlet.QP_FREEBUSY_CALENDAR).append("=").append(req.folder);
            try {
                if (mExApptUid != null)
                    targetUrl.append("&").append(UserServlet.QP_EXUID).append("=").append(URLEncoder.encode(mExApptUid, "UTF-8"));
            } catch (UnsupportedEncodingException e) {
            }
            String authToken = null;
            try {
                if (mSoapCtxt != null)
                    authToken = mSoapCtxt.getAuthToken().getEncoded();
            } catch (AuthTokenException e) {
            }
            if (authToken != null) {
                targetUrl.append("&").append(ZimbraServlet.QP_ZAUTHTOKEN).append("=");
                try {
                    targetUrl.append(URLEncoder.encode(authToken, "UTF-8"));
                } catch (UnsupportedEncodingException e) {
                }
            }
            HttpClient client = ZimbraHttpConnectionManager.getInternalHttpConnMgr().newHttpClient();
            HttpProxyUtil.configureProxy(client);
            method = new GetMethod(targetUrl.toString());
            String fbMsg;
            try {
                HttpClientUtil.executeMethod(client, method);
                byte[] buf = ByteUtil.getContent(method.getResponseBodyAsStream(), 0);
                fbMsg = new String(buf, "UTF-8");
            } catch (IOException ex) {
                // ignore this recipient and go on
                fbMsg = null;
            }
            if (fbMsg != null) {
                ZVCalendar cal = ZCalendarBuilder.build(fbMsg);
                for (Iterator<ZComponent> compIter = cal.getComponentIterator(); compIter.hasNext(); ) {
                    ZComponent comp = compIter.next();
                    if (ICalTok.VFREEBUSY.equals(comp.getTok())) {
                        FreeBusy fb = FreeBusy.parse(comp);
                        fbList.add(fb);
                    }
                }
            }
        } catch (ServiceException e) {
            ZimbraLog.fb.warn("can't get free/busy information for " + req.email, e);
        } finally {
            if (method != null)
                method.releaseConnection();
        }
    }
    return fbList;
}
Also used : Account(com.zimbra.cs.account.Account) ArrayList(java.util.ArrayList) HttpServletRequest(javax.servlet.http.HttpServletRequest) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) ZComponent(com.zimbra.common.calendar.ZCalendar.ZComponent) ZVCalendar(com.zimbra.common.calendar.ZCalendar.ZVCalendar) ServiceException(com.zimbra.common.service.ServiceException) AuthTokenException(com.zimbra.cs.account.AuthTokenException) HttpClient(org.apache.commons.httpclient.HttpClient) GetMethod(org.apache.commons.httpclient.methods.GetMethod) HttpMethod(org.apache.commons.httpclient.HttpMethod)

Example 37 with HttpMethod

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

the class TestProvIDN method testBasicAuth.

@Test
public void testBasicAuth() throws Exception {
    Names.IDNName domainName = new Names.IDNName(makeTestDomainName("basicAuthTest."));
    Domain domain = createDomain(domainName.uName(), domainName.uName());
    Names.IDNName acctName = new Names.IDNName("acct", domainName.uName());
    Account acct = (Account) createTest(EntryType.ACCOUNT, NameType.UNAME, acctName);
    HttpState initialState = new HttpState();
    /*
        Cookie authCookie = new Cookie(restURL.getURL().getHost(), "ZM_AUTH_TOKEN", mAuthToken, "/", null, false);
        Cookie sessionCookie = new Cookie(restURL.getURL().getHost(), "JSESSIONID", mSessionId, "/zimbra", null, false);
        initialState.addCookie(authCookie);
        initialState.addCookie(sessionCookie);
        */
    String guestName = acct.getUnicodeName();
    String guestPassword = "test123";
    Credentials loginCredentials = new UsernamePasswordCredentials(guestName, guestPassword);
    initialState.setCredentials(AuthScope.ANY, loginCredentials);
    HttpClient client = new HttpClient();
    client.setState(initialState);
    String url = UserServlet.getRestUrl(acct) + "/Calendar";
    System.out.println("REST URL: " + url);
    HttpMethod method = new GetMethod(url);
    HttpMethodParams methodParams = method.getParams();
    methodParams.setCredentialCharset("UTF-8");
    try {
        int respCode = HttpClientUtil.executeMethod(client, method);
        if (respCode != HttpStatus.SC_OK) {
            System.out.println("failed, respCode=" + respCode);
        } else {
            boolean chunked = false;
            boolean textContent = false;
        /*
                 System.out.println("Headers:");
                 System.out.println("--------");
                 for (Header header : method.getRequestHeaders()) {
                     System.out.print("    " + header.toString());
                 }
                 System.out.println();
                 
                 System.out.println("Body:");
                 System.out.println("-----");
                 String respBody = method.getResponseBodyAsString();
                 System.out.println(respBody);
                 */
        }
    } finally {
        // Release the connection.
        method.releaseConnection();
    }
}
Also used : Account(com.zimbra.cs.account.Account) HttpState(org.apache.commons.httpclient.HttpState) HttpMethodParams(org.apache.commons.httpclient.params.HttpMethodParams) UsernamePasswordCredentials(org.apache.commons.httpclient.UsernamePasswordCredentials) Names(com.zimbra.qa.unittest.prov.Names) HttpClient(org.apache.commons.httpclient.HttpClient) GetMethod(org.apache.commons.httpclient.methods.GetMethod) Domain(com.zimbra.cs.account.Domain) UsernamePasswordCredentials(org.apache.commons.httpclient.UsernamePasswordCredentials) Credentials(org.apache.commons.httpclient.Credentials) HttpMethod(org.apache.commons.httpclient.HttpMethod)

Example 38 with HttpMethod

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

the class WebClientServiceUtil method sendServiceRequestToEveryUiNode.

/**
     * send service request to every ui node
     * @param serviceUrl the url that should be matched and handled by ServiceServlet in ZimbraWebClient
     * @throws ServiceException
     */
public static void sendServiceRequestToEveryUiNode(String serviceUrl) throws ServiceException {
    List<Server> servers = Provisioning.getInstance().getAllServers(Provisioning.SERVICE_WEBCLIENT);
    if (servers == null || servers.isEmpty()) {
        servers.add(Provisioning.getInstance().getLocalServer());
    }
    AuthToken authToken = AuthProvider.getAdminAuthToken();
    ZimbraLog.misc.debug("got admin auth token");
    //sequentially flush each node
    HttpClient client = ZimbraHttpConnectionManager.getExternalHttpConnMgr().newHttpClient();
    HttpProxyUtil.configureProxy(client);
    for (Server server : servers) {
        if (isServerAtLeast8dot5(server)) {
            HttpMethod method = null;
            try {
                method = new GetMethod(URLUtil.getServiceURL(server, serviceUrl, false));
                ZimbraLog.misc.debug("connecting to ui node %s", server.getName());
                try {
                    method.addRequestHeader(PARAM_AUTHTOKEN, authToken.getEncoded());
                } catch (AuthTokenException e) {
                    ZimbraLog.misc.warn(e);
                }
                int respCode = HttpClientUtil.executeMethod(client, method);
                if (respCode != 200) {
                    ZimbraLog.misc.warn("service failed, return code: %d", respCode);
                }
            } catch (Exception e) {
                ZimbraLog.misc.warn("service failed for node %s", server.getName(), e);
            } finally {
                if (method != null) {
                    method.releaseConnection();
                }
            }
        }
    }
    if (authToken != null && authToken.isRegistered()) {
        try {
            authToken.deRegister();
            ZimbraLog.misc.debug("de-registered auth token, isRegistered?%s", authToken.isRegistered());
        } catch (AuthTokenException e) {
            ZimbraLog.misc.warn("failed to de-register auth token", e);
        }
    }
}
Also used : Server(com.zimbra.cs.account.Server) HttpClient(org.apache.commons.httpclient.HttpClient) AuthTokenException(com.zimbra.cs.account.AuthTokenException) GetMethod(org.apache.commons.httpclient.methods.GetMethod) AuthToken(com.zimbra.cs.account.AuthToken) HttpMethod(org.apache.commons.httpclient.HttpMethod) IOException(java.io.IOException) ServiceException(com.zimbra.common.service.ServiceException) AuthTokenException(com.zimbra.cs.account.AuthTokenException)

Example 39 with HttpMethod

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

the class WebClientServiceUtil method sendServiceRequestToUiNode.

public static String sendServiceRequestToUiNode(Server server, String serviceUrl) throws ServiceException {
    if (isServerAtLeast8dot5(server)) {
        HttpClient client = ZimbraHttpConnectionManager.getExternalHttpConnMgr().newHttpClient();
        HttpProxyUtil.configureProxy(client);
        AuthToken authToken = AuthProvider.getAdminAuthToken();
        ZimbraLog.misc.debug("got admin auth token");
        String resp = "";
        HttpMethod method = null;
        try {
            method = new GetMethod(URLUtil.getServiceURL(server, serviceUrl, false));
            ZimbraLog.misc.debug("connecting to ui node %s", server.getName());
            method.addRequestHeader(PARAM_AUTHTOKEN, authToken.getEncoded());
            int result = HttpClientUtil.executeMethod(client, method);
            ZimbraLog.misc.debug("resp: %d", result);
            resp = method.getResponseBodyAsString();
            ZimbraLog.misc.debug("got response from ui node: %s", resp);
        } catch (IOException e) {
            ZimbraLog.misc.warn("failed to get response from ui node", e);
        } catch (AuthTokenException e) {
            ZimbraLog.misc.warn("failed to get authToken", e);
        } finally {
            if (method != null) {
                method.releaseConnection();
            }
        }
        if (authToken != null && authToken.isRegistered()) {
            try {
                authToken.deRegister();
                ZimbraLog.misc.debug("de-registered auth token, isRegistered?%s", authToken.isRegistered());
            } catch (AuthTokenException e) {
                ZimbraLog.misc.warn("failed to de-register authToken", e);
            }
        }
        return resp;
    }
    return "";
}
Also used : HttpClient(org.apache.commons.httpclient.HttpClient) AuthTokenException(com.zimbra.cs.account.AuthTokenException) GetMethod(org.apache.commons.httpclient.methods.GetMethod) AuthToken(com.zimbra.cs.account.AuthToken) IOException(java.io.IOException) HttpMethod(org.apache.commons.httpclient.HttpMethod)

Example 40 with HttpMethod

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

the class WebClientServiceUtil method sendServiceRequestToOneRandomUiNode.

/**
     * send service request to one random ui node, keep trying until succeeds.
     * @param serviceUrl the url that should be matched and handled by ServiceServlet in ZimbraWebClient
     *        reqHeaders the map of req/rsp attributes that need to be set by the UI node
     * @return response from ui node in String
     * @throws ServiceException
     */
public static String sendServiceRequestToOneRandomUiNode(String serviceUrl, Map<String, String> reqHeaders) throws ServiceException {
    List<Server> servers = Provisioning.getInstance().getAllServers(Provisioning.SERVICE_WEBCLIENT);
    if (servers == null || servers.isEmpty()) {
        servers.add(Provisioning.getInstance().getLocalServer());
    }
    HttpClient client = ZimbraHttpConnectionManager.getExternalHttpConnMgr().newHttpClient();
    HttpProxyUtil.configureProxy(client);
    AuthToken authToken = AuthProvider.getAdminAuthToken();
    ZimbraLog.misc.debug("got admin auth token");
    String resp = "";
    for (Server server : servers) {
        if (isServerAtLeast8dot5(server)) {
            HttpMethod method = null;
            try {
                method = new GetMethod(URLUtil.getServiceURL(server, serviceUrl, false));
                ZimbraLog.misc.debug("connecting to ui node %s", server.getName());
                method.addRequestHeader(PARAM_AUTHTOKEN, authToken.getEncoded());
                // Add all the req headers passed to this function as well
                for (Map.Entry<String, String> entry : reqHeaders.entrySet()) {
                    method.addRequestHeader(entry.getKey(), entry.getValue());
                    ZimbraLog.misc.debug("adding request header %s=%s", entry.getKey(), entry.getValue());
                }
                int result = HttpClientUtil.executeMethod(client, method);
                ZimbraLog.misc.debug("resp: %d", result);
                resp = method.getResponseBodyAsString();
                ZimbraLog.misc.debug("got response from ui node: %s", resp);
                //try ui nodes one by one until one succeeds.
                break;
            } catch (IOException e) {
                ZimbraLog.misc.warn("failed to get response from ui node", e);
            } catch (AuthTokenException e) {
                ZimbraLog.misc.warn("failed to get authToken", e);
            } finally {
                if (method != null) {
                    method.releaseConnection();
                }
            }
        }
    }
    if (authToken != null && authToken.isRegistered()) {
        try {
            authToken.deRegister();
            ZimbraLog.misc.debug("de-registered auth token, isRegistered?%s", authToken.isRegistered());
        } catch (AuthTokenException e) {
            ZimbraLog.misc.warn("failed to de-register authToken", e);
        }
    }
    return resp;
}
Also used : Server(com.zimbra.cs.account.Server) HttpClient(org.apache.commons.httpclient.HttpClient) AuthTokenException(com.zimbra.cs.account.AuthTokenException) GetMethod(org.apache.commons.httpclient.methods.GetMethod) AuthToken(com.zimbra.cs.account.AuthToken) IOException(java.io.IOException) Map(java.util.Map) HttpMethod(org.apache.commons.httpclient.HttpMethod)

Aggregations

HttpMethod (org.apache.commons.httpclient.HttpMethod)151 HttpClient (org.apache.commons.httpclient.HttpClient)99 GetMethod (org.apache.commons.httpclient.methods.GetMethod)95 InputStream (java.io.InputStream)61 IOException (java.io.IOException)43 ArrayList (java.util.ArrayList)30 HttpException (org.apache.commons.httpclient.HttpException)28 Map (java.util.Map)24 Test (org.junit.Test)23 Element (org.w3c.dom.Element)22 HashMap (java.util.HashMap)20 PostMethod (org.apache.commons.httpclient.methods.PostMethod)19 Header (org.apache.commons.httpclient.Header)17 List (java.util.List)14 NameValuePair (org.apache.commons.httpclient.NameValuePair)13 NodeList (org.w3c.dom.NodeList)12 FileInputStream (java.io.FileInputStream)10 HttpTest (org.apache.sling.commons.testing.integration.HttpTest)10 SAXBuilder (org.jdom.input.SAXBuilder)10 UsernamePasswordCredentials (org.apache.commons.httpclient.UsernamePasswordCredentials)9