View Single Post
  #14 (permalink)  
Old 01-28-2009, 12:30 AM
Lapeth Lapeth is offline
Junior Member
 
Posts: 7
Default

This may not be a "perfect" solution. It is possible to GET data with AjxRpc.invoke, but since a regular AJAX request cannot go to other webservers (it's a browser restriction), I prefer to use a proxy jsp that receives the data from the client, passes it on to the external service, gets a result back and sends it to the client.

Something like this:

Code:
proxy.jsp:

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="java.io.*"%>
<%@page import="java.net.*"%>
<%@page import="java.util.*"%>

<%
List<String> allowedDomains=new ArrayList<String>();
allowedDomains.add("www.zimbra.com"); // external domain you want to call, without prefix, and you may add several domains by duplicating the line

    try {
    URL url = new URL(URLDecoder.decode(request.getParameter("url")));

    if (!allowedDomains.contains(url.getHost()))
        throw new Exception("Invalid hostname");

    InputStream is = url.openStream();
    BufferedReader r = new BufferedReader(new InputStreamReader(is));
    StringBuffer buf = new StringBuffer();
    String line;
    while ((line = r.readLine()) != null) {
         buf.append(line);
    }
    r.close();
    is.close();

    out.print(buf.toString());

} catch (Exception ex) {
}
%>
Then call this in javascript:
Code:
	var url=this.getResource('proxy.jsp'); // "this" being the handler object of the zimlet
	var str=escape("http://zimbra.com"+"?"+"param1=value1&param2=value2");// Create the request string
	var reqHeader = {"Content-Type":"application/x-www-form-urlencoded"};
	AjxRpc.invoke("url="+str, url, reqHeader, new AjxCallback(this, this.callbackfunction));
This code is for GET requests, but a little tweaking might just make it POST as well. The org.apache.commons.httpclient.methods.PostMethod class is a good place to start if you must POST the data. That'll of course be used in the proxy jsp.
Reply With Quote