Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
Clear All
new posts

    Howto: GWT 1.6, SmartGWT and HttpProxy for cross-domain webservice calls

    Note: This is an update for GWT 1.6.
    For GWT 1.5, see here

    SmartGWT makes it really easy to call any webservice directly from within your web browser. However, if webservice and SmartGWT app are not part of the same domain (i.e. different server and/or different port), browser security policy usually prohibits cross-domain webservice calls - cross-domain XmlHttpRequests (XHR) to be precise.

    I order to work around that restriction, SmartClient uses a server-side HttpProxy servlet (which comes with the SmartClient server) for cross-domain webservice calls.
    The client-side SmartClient code detects cross-domain webservice calls and redirects them to the HttpProxy servlet, which needs to run on the same domain where the SmartClient app is hosted. The HttpProxy servlet forwards the call to the actual webservice without being restricted by any browser security policy.

    But that only works when using the SmartClient server. However, when developing a SmartGWT app, you're usually using GWT builtin webserver (jetty as of GWT 1.6), which doesn't contain the HttpProxy servlet.

    The good news is that it's possible to integrate SmartClient's HttpProxy servlet into a GWT project and thereby enable cross-domain webservice calls during SmartGWT development.

    What you need to do is this:

    1. Download and install the SmartClient SDK

    2. Copy the following files (you'll find them in the SmartClientSDK dir) to your GWT project directory:

    * <GWT_PROJECT_DIR>\src\log4j.isc.config.xml
    * <GWT_PROJECT_DIR>\war\isomorphic\system\schema\builtinTypes.xml
    * <GWT_PROJECT_DIR>\war\WEB-INF\lib\commons-codec-1_3.jar
    * <GWT_PROJECT_DIR>\war\WEB-INF\lib\commons-httpclient-3_0_1.jar
    * <GWT_PROJECT_DIR>\war\WEB-INF\lib\commons-jxpath-1_2.jar
    * <GWT_PROJECT_DIR>\war\WEB-INF\lib\commons-pool-1_3.jar
    * <GWT_PROJECT_DIR>\war\WEB-INF\lib\isc-jakarta-oro-2_0_6.jar
    * <GWT_PROJECT_DIR>\war\WEB-INF\lib\isomorphic_core_rpc.jar
    * <GWT_PROJECT_DIR>\war\WEB-INF\lib\log4j-1_2_11.jar

    3. Add the following lines at the beginning of the file <GWT_PROJECT_DIR>\war\WEB-INF\web.xml, right after the <web-app> tag:

    Code:
    <web-app> 
      <!-- Enable HttpProxy servlet begin -->
      <servlet>
        <servlet-name>httpproxy</servlet-name>
        <servlet-class>com.isomorphic.servlet.HttpProxyServlet</servlet-class>
      </servlet>
      	
      <servlet-mapping>
        <servlet-name>httpproxy</servlet-name>
        <url-pattern>/yourprojectname/sc/HttpProxy</url-pattern>
      </servlet-mapping>
      <!-- Enable HttpProxy servlet end -->
      ...
    </web-app>
    4. In your project's run/debug configuration(s), add the following line under "VM arguments":
    Code:
    -Dorg.apache.commons.logging.Log=org.apache.commons.logging.impl.SimpleLog
    5. Tell SmartClient to use the proxy servlet by calling
    Code:
    RPCManager.setUseHttpProxy(true);
    somewhere in your application before making the actual webservice call.

    That's it. Now your SmartGWT app should be able to make cross-domain webservice calls. The console output will even show the XML that's being passed through the HttpProxy servlet.
    Last edited by HHON; 23 May 2009, 22:54.

    #2
    Will this work, only if you already have a license for the SmartClient server ?

    i. I realise that your are running a server component (war app) in Jetty,
    but I assume you need a license to do so ?

    Comment

    Working...
    X