It is pretty challenge to display Google Maps in Https (SSL), as so
far Google only support to display map in Http (no secure site)
The issue: The IE will popup a warning message when accessing Google
map in SSL mode to indicate that the page contains both secure and
nonsecure items.
The solution: use proxy. It applies for both Enterprise user and non-
Enterprise user
Algorithm:
1. Call http://maps.google.com/maps?file=api&v=2&key= <your
key>" in your backend server. Make sure you pass in User Agent.
2. Get content type from the content you get with above URL.
3. if content type is text then
append all google links with yourProxyPath
send text content back to user.
else
send binary content back to user.
end
Sample code: written in JSP/Java/Servlet
1. In JSP where you have Google Map.
Create two keys: one for secure and one for non secure. For
enterprise user, both would be the same.
<%
String requestHttpsUrl, requestHttpUrl;
requestHttpsUrl = "http://maps.google.com/maps?
file=api&v=2&key=<secure key>";
requestHttpUrl="http://maps.google.com/maps?
file=api&v=2&key=<non secure key> ";
String scheme = request.getScheme();
%>
<% if ("https".equalsIgnoreCase(scheme)) { %>
<script src="/<yourProxyPath>?<%=requestHttpsUrl%>" type="text/
javascript"></script>
<% } else { %>
<script src="<%=requestHttpUrl%>" type="text/javascript"></
script>
<%}%>
Where yourProxyPath is the path for your Sevlet or other path where
you put proxy logic in. For me, it is /location/googleproxy
2. create Servlet as proxy, I used Httpclient as remote connector.
Note: for binary data, normally it is image, there are two ways to
deal with it.
-- redirect : response.sendRedirect(requestUrl);
-- construct ServletOutputStream
public void service_httpclient_connection(HttpServletRequest request,
HttpServletResponse response) throws ServletException,
java.io.IOException {
String requestUrl = request.getQueryString();
String user_agent = request.getHeader("User-Agent");
HttpClient client = new HttpClient();
HttpMethod method = new GetMethod(requestUrl);
method.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
method.addRequestHeader("User-Agent", user_agent);
try {
// Execute the method.
client.getParams().setBooleanParameter(
HttpClientParams.ALLOW_CIRCULAR_REDIRECTS, false);
int statusCode = client.executeMethod(method);
if (statusCode == HttpStatus.SC_OK) {
String contentType =
method.getResponseHeader("Content-type").getValue();
if (isTextContentType(contentType)) {
// google content is text
InputStream mapScript = method.getResponseBodyAsStream();
String resultScript = convertInputStreamToString(mapScript);
mapScript.close();
// replace http: with proxy
if (resultScript.indexOf("<yourProxyPath>") == -1) {
resultScript =
resultScript.replaceAll("http:","/
<yourProxyPath>?http:");
}
PrintWriter out = response.getWriter();
out.println(resultScript);
out.flush();
} else {
//google content is binary
// that is the easy way
response.sendRedirect(requestUrl);
}
}
} finally {
// Release the connection.
method.releaseConnection();
}
}
private boolean isTextContentType(String contentType) {
return (contentType.indexOf("text") != -1);
}
Also, you can replace response.sendRedirect(requestUrl); with
following code
byte[] retBytes = method.getResponseBody();
if (retBytes != null) {
response.setContentType(contentType);
response.setContentLength(retBytes.length);
ServletOutputStream output = response.getOutputStream();
output.write(retBytes);
output.flush();
output.close();
You can view example
http://www.allhotelmotel.com/location/httpsproxy.htm
Post in Google Map Group:
http://groups.google.com/group/Google-Maps-API/browse_thread/thread/992743110e3f811f/d8e33c1292655469?lnk=gst&q=michael+w#d8e33c1292655469
No comments:
Post a Comment