Announcement

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

    Howto: SmartGWT and HttpProxy for cross-domain webservice calls

    [Note from Isomorphic: this approach should no longer be used. If you want to use the SC server functionality (including the HttpProxy servlet) you should download SmartGWT Pro/EE, in which case use of the HttpProxy is automatic ].

    Note: This applies to GWT 1.5
    For GWT 1.6, see here

    I thought I'd share the following information - maybe somebody will find it useful:

    I recently tried to write a SmartGWT application that needed to call a webservice.
    When finished, my SmartGWT app would be running on the same server that also hosted the webservice. But during development, my SmartGWT app would mostly run inside the GWT hosted mode browser (which uses GWT's own tomcat instance running on localhost:8888), while the webservice would run on a different domain (different server and/or port).

    You probably know that browser security policies prohibit cross-domain webservice calls - cross-domain XmlHttpRequests (XHR) to be excact. In my case, those policies prevented my SmartGWT app from calling the webservice while running inside the GWT hosted mode browser, which was quite annoying.

    I order to work around that restriction, the SmartClient library uses a server-side HttpProxy servlet (which comes with the SmartClient server) when making cross-domain webservice calls. The client-side SmartClient code detects cross-domain webservice calls and redirects them back to server where the SmartClient app is hosted. The server then routes the call through a special HttpProxy servlet, which forwards the call to the actual webservice without being restricted by any browser security policy.

    But that only works when using the SmartClient server - which is part of the SmartClient SDK. But when developing SmartGWT apps, you're usually using the GWT server, which doesn't contain the HttpProxy.

    The good news is that since both GWT's and the SmartClient's server are Tomcat servers, it's possible to integrate the HttpProxy servlet that comes with the SmartClient server into a GWT project and thereby enable cross-domain webservice calls during SmartGWT development.

    All you need to do is the following:

    1. Download and install the SmartClient SDK

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

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

    3. Add the following lines at the beginning of the file <GWT_PROJECT_DIR>\tomcat\webapps\ROOT\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>/com.yourcompany.yourproject/sc/HttpProxy</url-pattern>
      </servlet-mapping>
      <!-- Enable HttpProxy servlet end -->
      ...
    </web-app>
    4. 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 project should be able to make cross-domain webservice calls at development time. The console output will even show the XML that's being passed through the HttpProxy servlet.
    Last edited by Isomorphic; 1 Oct 2009, 08:20.

    #2
    Thanks for posting this!

    Just a note that when SmartGWT EE is available this is something that will "just work" when using that package, similar to how it behaves in the EE version of SmartClient.

    Comment


      #3
      I did as explained but still get:
      "com.google.gwt.core.client.JavaScriptException: (Error): Permission denied" in DataSource.fetchData().

      In the code I do:
      Code:
      dataSource.setDataURL("http://localhost:8080/stream/com.skullestad.SmartGwtTest/data/contactsData.js");
      (I have verified that this URL does return the desired content when used directly from a browser.)

      I have tried several versions of url-pattern, but not sure what would be the correct one. (I also tried "*.js" but same result).

      Any ideas?

      Comment


        #4
        Are you seeing your application attempt to contact the HTTPProxy? If not, you can force it to do so by calling setUseHTTPProxy(true).

        Comment


          #5
          RPCManager.setUseHttpProxy(true) solved it. Thanks!

          Comment


            #6
            I do all the things you describe in the post, but when i try to put
            Code:
             RPCManager.setUseHttpProxy(true);
            I have one error: RPCManager don´t have this function.

            Comment


              #7
              What SmartGWT release are you using? The latest SVN revision of RPCManager.java contains setUseHttpProxy(). Try using the latest SmartGWT release 1.1, or try pulling the latest sources from SVN - then it should work.

              Comment


                #8
                Sorry but does this work with plain SmartGWT (neither EE nor Pro)? I run into

                '$wnd.isc.RPCManager' is Null or not an Object. Some RPC initialization I'm missing? THX ia

                Ekki

                SmartGWT 1.3, GWT 1.7, App Engine 1.2.5, Eclipse 3.5, JRE 1.6.0_16

                CI-CUBE.BIZ feat. CubeBrowser.AppSpot.com

                Comment


                  #9
                  The only way that could happen is if you were not loading ISC_DataBinding.js, which should be automatic unless you have been messing around with module includes.

                  Comment


                    #10
                    Yes, that one was missing. To be honest, initially, I minimized the isc js loadings for the purpose of optimization and getting into.

                    Thx so much! (Sub) Thread over. Ekki

                    Comment


                      #11
                      Touble-Shooting Servlet Activation

                      Hi...

                      'though I did everything (at least I believe so) as described in the Guidelines and triple-checked it, the HttpProxy servlet is simply not activated or found. What is the best way to trouble-shoot the servlet activation. Need to get it running...

                      TIA,

                      Ekki

                      SmartGWT 1.3, GWT 1.7, App Engine 1.2.5, Eclipse 3.5, JRE 1.6.0_16

                      CI-CUBE.BIZ feat. CubeBrowser.AppSpot.com

                      Comment


                        #12
                        got a little bit deeper into, here's what seems to create the issue:

                        java.lang.NoClassDefFoundError: org/apache/commons/collections/ProxyMap

                        any comments, please?

                        Ekki

                        Comment


                          #13
                          Missing .jars, install the whole set that comes with SGWT EE.

                          If we may comment, you're creating a lot of troubles for yourself messing around with the project config (eg which .js files are loaded, which .jars are included). Best not to play with this stuff until you have the basics figured out.

                          Comment


                            #14
                            this is my way to figure the basics out!! sorry...

                            All I did is trying to do something initially posted as proven practice! Sounded more familiar to me than messing around with EE distrib

                            Comment


                              #15
                              Can you clarify if you're using SmartGWT EE, and if so which version?

                              Comment

                              Working...
                              X