Sunday, August 18, 2013

Using OpenLayers to write OGC clients

One of my recent projects involved implementing several OGC clients: WPS client, WFS client, KML reader and GML reader. WPS client and WFS client required accessing URLs of servers where particular services (WFS, WPS) were hosted.

OpenLayers is a good library which supports implementing these OGC clients. But as I was trying out these clients using Apache tomcat in localhost, they did not work properly. The requests to the servers were not sent as expected. I did a bit of Googling and found out the issue: same-origin policy. You can read more about it here:  http://docs.openlayers.org/library/request.html .

It took me some effort to find a complete solution to my issue. Parts of the solution were scattered around the web and few contained the complete solution. Many had the solution for Apache HTTP server and not for tomcat. I thought it would be useful to put a post with the solution for anyone who needs it and also for my future reference.

Solution for default Apache configuration can be found here:  http://trac.osgeo.org/openlayers/wiki/FrequentlyAskedQuestions#ProxyHost

The solution for Apache tomcat has two parts.

1. From OpenLayers side: a proxy script should be used due to the restrictions in JavaScript on the use of XMLHTTPRequest making requests to remote servers. 

  • Download the proxy.cgi from  http://trac.osgeo.org/openlayers/browser/trunk/openlayers/examples/proxy.cgi
  • Create a directory named 'cgi' in the WEB-INF directory of your web application in which you are using OpenLayers. Place the downloaded file named 'proxy.cgi' in it.
  • To tell OpenLayers what script to use as its proxy host, make a declaration like: 
    OpenLayers.ProxyHost = "/cgi-bin/proxy.cgi?url=";
    
2. From tomcat: CGI has to be enabled.
  • Open web.xml in your tomcat installation. (/tomcat/conf/)
  • Locate the following commented code in web.xml. Then uncomment that section. 
    <servlet>
        <servlet-name>cgi</servlet-name>
        <servlet-class>org.apache.catalina.servlets.CGIServlet</servlet-class>
        <init-param>
          <param-name>debug</param-name>
          <param-value>0</param-value>
        </init-param>
        <init-param>
          <param-name>cgiPathPrefix</param-name>
          <param-value>WEB-INF/cgi</param-value>
        </init-param>
         <load-on-startup>5</load-on-startup>
    </servlet>
  • Add the following following section within the <servlet> tag.
    <init-param>
         <param-name>executable</param-name>
         <param-value>/usr/bin/python</param-value> 
    </init-param>
    <init-param>
         <param-name>passShellEnvironment</param-name>
         <param-value>true</param-value>
    </init-param>

Finally it should look like follows.

    <servlet>
        <servlet-name>cgi</servlet-name>
        <servlet-class>org.apache.catalina.servlets.CGIServlet</servlet-class>
        <init-param>
          <param-name>debug</param-name>
          <param-value>0</param-value>
        </init-param>
        <init-param>
          <param-name>cgiPathPrefix</param-name>
          <param-value>WEB-INF/cgi</param-value>
        </init-param>
         <load-on-startup>5</load-on-startup>
    <init-param>
         <param-name>executable</param-name>
         <param-value>/usr/bin/python</param-value> 
    </init-param>
    <init-param>
         <param-name>passShellEnvironment</param-name>
         <param-value>true</param-value>
    </init-param>
    </servlet>
  • Scroll down from that point and you will locate <servlet-mapping> section relevant to CGI similar to following. Uncomment that section as well. Then save the file.
   <servlet-mapping>
        <servlet-name>cgi</servlet-name>
        <url-pattern>/cgi-bin/*</url-pattern>
    </servlet-mapping>
  • Finally open the context.xml file (tomcat/conf/). Edit it as follows.
<Context privileged="true" antiResourceLocking="false" antiJARLocking="false">

Now you should be able to use OpenLayers without an issue. 

No comments:

Post a Comment