Adobe says:
Adobe® Flash® Player does not allow an application to receive data from a domain other than the domain from which it was loaded, unless it has been given explicit permission.
(For more information click here)
Wave2GoogleCalendar gadget uses HTTP/XML services of Google to create events in Google Calendar. Google has already a crossdomain.xml, but it has some restrictions. For example authorization headers are not allowed and this makes impossible to use services mentioned above. A possible solution is to create your own proxy to handle your requests targeted to HTTP services. I've some Java experience and implemented therefore a proxy servlet by extending HttpServlet. I've deployed it in an application hosted in Google App Engine. Below you can see the source code. Servlet programming and generally HTTP handling in Java are not my strongest areas, so feel free to leave your comments and suggestions.
The proxy class cannot definitely support every possible POST and GET calls you would use in your Flex applications. But I am pretty sure, that it will give a good head start if you want to (or have to) use a Java proxy for your needs.
package test;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Enumeration;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Proxy extends HttpServlet
{
private static final long serialVersionUID = 1L;
public static final String CONTENT_KEY = "contentUrl";
public static final String METHOD = "realMethod";
public static final String AUTHORIZATION = "Authorization";
public static final String GDATA = "GData-Version";
private static final Logger log = Logger.getLogger(Proxy.class.getName());
private boolean atomPost = false;
@SuppressWarnings("unchecked")
public void service (HttpServletRequest httpRequest,
HttpServletResponse httpResponse)
throws ServletException
{
String contentUrl = httpRequest.getHeader(CONTENT_KEY);
String method = httpRequest.getHeader(METHOD);
atomPost = false;
if (contentUrl == null)
{
throw new ServletException("The init parameter [" + CONTENT_KEY +
"] must be specified as an init " +
"parameter.");
}
log.warning(contentUrl + " as contenturl...");
URL content = null;
try
{
content = new URL(contentUrl);
}
catch (MalformedURLException exception)
{
throw new ServletException(contentUrl + " is a malformed url.");
}
String atomString = getAtomString(httpRequest);
HttpURLConnection contentCon = null;
try
{
log.warning(contentUrl + " open connection");
contentCon = (HttpURLConnection) content.openConnection();
addHeaders(httpRequest, contentCon);
}
catch (IOException exception)
{
throw new ServletException("Problem opening " + contentUrl +
": " + exception.toString());
}
InputStream in = null;
try
{
if(method.equals("GET"))
{
log.warning(contentUrl + " do GET");
// Get the content type from the URLConnection and set it on the response.
String contentType = contentCon.getContentType();
httpResponse.setContentType(contentType);
in = contentCon.getInputStream();
}
else if(method.equals("POST"))
{
contentCon.setDoInput(true);
contentCon.setDoOutput(true);
contentCon.setUseCaches(false);
if(atomPost)
{
log.warning(contentUrl + " do ATOM POST");
OutputStream outputStream = contentCon.getOutputStream();
outputStream.write(atomString.getBytes());
outputStream.close();
}
else
{
log.warning(contentUrl + " do NORMAL POST");
// Form the POST parameters
StringBuilder postPar = new StringBuilder();
Enumeration e = httpRequest.getParameterNames();
int i=0;
while (e.hasMoreElements())
{
String name = (String)e.nextElement();
String value = httpRequest.getParameter(name);
if(!name.equals(CONTENT_KEY))
{
String _char = (i == 0) ? "" : "&";
postPar.append(_char+name+"=").append(URLEncoder.encode(value, "UTF-8"));
}
i++;
}
OutputStream outputStream = contentCon.getOutputStream();
outputStream.write(postPar.toString().getBytes("UTF-8"));
outputStream.close();
}
// Retrieve the output
log.warning(contentUrl + " retrieve output");
int responseCode = contentCon.getResponseCode();
InputStream inputStream;
if (responseCode == HttpURLConnection.HTTP_OK
|| responseCode == HttpURLConnection.HTTP_CREATED) {
inputStream = contentCon.getInputStream();
} else {
inputStream = contentCon.getErrorStream();
}
in = inputStream;
}
byte[] buffer = new byte[1024];
ByteArrayOutputStream contentStream = new ByteArrayOutputStream();
for (int cnt = in.read(buffer);
cnt != -1;
cnt = in.read(buffer))
{
contentStream.write(buffer, 0, cnt);
if (in.available() == 0)
{
break;
}
}
// This is where you have the content from the request.
String contentString = contentStream.toString();
log.warning(contentUrl + " write to client");
// Now write the bytes out to the client.
byte[] contentBytes = contentString.getBytes();
OutputStream out = httpResponse.getOutputStream();
out.write(contentBytes, 0, contentBytes.length);
out.flush();
out.close();
}
catch (Exception exception)
{
throw new ServletException("Unable to proxy request: " + exception);
}
}
private void addHeaders(HttpServletRequest httpRequest, HttpURLConnection contentCon)
{
if(atomPost)
contentCon.setRequestProperty("Content-Type", "application/atom+xml");
if(httpRequest.getHeader(AUTHORIZATION) != null)
contentCon.setRequestProperty(AUTHORIZATION, httpRequest.getHeader(AUTHORIZATION));
if(httpRequest.getHeader(GDATA) != null)
contentCon.setRequestProperty(GDATA, httpRequest.getHeader(GDATA));
}
private String getAtomString(HttpServletRequest httpRequest)
{
if(!httpRequest.getContentType().equals("application/atom+xml"))
return null;
atomPost = true;
StringBuilder sb = null;
try
{
BufferedReader rr = httpRequest.getReader();
if(rr != null)
{
sb = new StringBuilder();
String line = null;
while ((line = rr.readLine()) != null)
sb.append(line);
rr.close();
}
}
catch(Exception e)
{
return null;
}
return sb.toString();
}
}
Read more...




