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;
}
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();
}
}
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);
}
}
}
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 "";
}
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;
}
Aggregations