Monday, November 17, 2008

OpenLayers ProxyHost

One of the most difficult problems for me to solve with OpenLayers was how to set up a ProxyHost. The FAQ was helpful to a certain point but it assumes that you will be using a php or python scipt like the example they provide. There is no information on how to do it within the Java code of the project.

It took a long time for me to figure out that the best way to do it was to create a servlet inside the NetBeans portlet project. Then what I struggled with was the path to the servlet not being known by the JavaScript code that was on the jsp page with the OpenLayers map because the servlet was not in the same context as the portlet. For a number of weeks I was able to get it working by hardcoding the URL string into the OpenLayers.Layer.WFS call. But I knew that wouldn't work once I moved from the portlet I developed locally to a remote host.

After a full day's struggle I finally got the key piece of code into the view.jsp. First a bit of servlet code to create the URL:

<% String proxy = "http://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath() + "/ionic_proxy"; %><% String proxy = "http://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath() + "/ionic_proxy"; %>< % String proxy = "http://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath() + "/ionic_proxy"; % >

Then, within the OpenLayer.Layer.WMS the call becomes:

var wfs = new OpenLayers.Layer.WFS( "NID",
"< %=proxy% ><%=proxy%>",
{typename: "iwfs:NID_GEOM", maxfeatures:30},
{featureClass: OpenLayers.Feature.WFS} );

Whew, what a bother.

No comments: