From Zimbra :: Wiki
|
Article Information
|
| This article applies to the following ZCS versions.
|
ZCS 7.0
|
ZCS 6.0
|
Description
This zimlet shows using the AjxRps.invoke() method to call a URL from your zimlet via GET and POST. Also shows how to call an external service (via the Proxy Servlet).
Definition File
<zimlet name="com_zimbra_example_httpgetpost" version="1.0" description="An example zimlet using HTTP GET and POST.">
<include>com_zimbra_example_httpgetpost.js</include>
<handlerObject>com_zimbra_example_httpgetpost_HandlerObject</handlerObject>
<zimletPanelItem label="Zimlet Example (HTTP GET and POST)" icon="zimbraIcon">
<toolTipText>Right click to display the menu</toolTipText>
<contextMenu>
<menuItem label="HTTP GET" id="CALL_VIA_HTTP_GET" />
<menuItem label="HTTP POST" id="CALL_VIA_HTTP_POST" />
<menuItem label="HTTP GET (external)" id="CALL_VIA_HTTP_EXTERNAL_GET" />
</contextMenu>
</zimletPanelItem>
</zimlet>
Handler Object
/**
* Displays the zimlet jsp page.
*
*/
com_zimbra_example_httpgetpost_HandlerObject.prototype._executeHttpGet =
function() {
var jspUrl = this.getResource("jspfile.jsp");
var response = AjxRpc.invoke(null, jspUrl, null, null, true);
if (response.success == true) {
appCtxt.getAppController().setStatusMsg(response.text);
}
};
/**
* Displays the zimlet jsp page.
*
*/
com_zimbra_example_httpgetpost_HandlerObject.prototype._executeHttpPost =
function() {
var jspUrl = this.getResource("jspfile.jsp");
var response = AjxRpc.invoke(null, jspUrl, null, null, false);
if (response.success == true) {
appCtxt.getAppController().setStatusMsg(response.text);
}
};
/**
* Performs a "GET" against an external server using the Proxy Servlet.
*
*/
com_zimbra_example_httpgetpost_HandlerObject.prototype._executeExternalHttpGet =
function() {
var extServer = "http://search.twitter.com/search.json";
// create and encode the query params of the external server url
var extServerParams = ["q", "=", AjxStringUtil.urlComponentEncode("london bridge")].join("");
// add parameters to the url
//ATTENTION: you'll find "params" instead of "extServerParams" in the source code you're
//downloading, so you'll have to change it manually...if not done already
var extServerUrl = [extServer, "?", extServerParams].join("");
// url encode the external server url
// since it will part of the query params for the proxy servet
var encodedExtServerUrl = AjxStringUtil.urlComponentEncode(extServerUrl);
// create proxy servlet URL
var proxyServletUrl = [ZmZimletBase.PROXY, encodedExtServerUrl].join("");
// submit the URL and asynchronous response (using callback)
AjxRpc.invoke(null, proxyServletUrl, null, new AjxCallback(this, this._httpExternalGetCallback), false);
};
/**
* Handles the callback from the external http GET AjxRpc.invoke().
*
*
* @see com_zimbra_example_httpgetpost_HandlerObject._executeExternalHttpGet
*/
com_zimbra_example_httpgetpost_HandlerObject.prototype._httpExternalGetCallback =
function(response) {
if (response.success == false) {
// display the error response
appCtxt.getAppController().setStatusMsg("Error: " + response.text, ZmStatusView.LEVEL_WARNING);
return;
}
// display the response
appCtxt.getAppController().setStatusMsg(response.text);
};
Download