2

I am writing an application in GWT that is supposed to get the JSON from external server. I am using JsonpRequestBuilder but I always get failures (timeouts) since this is a JSON no JSONP.

If I would have my own server then I would be able to change the response format (to JSONP).

What can I change to get it working ?

I have checked it with Google Chrome's debugger and I do! get the response but somehow code falls into the onFailure method and I can't use the response.

String url = "http://api.mobile.endomondo.com/mobile/api/workout/list?authToken=XXXX";

JsonpRequestBuilder jsonp = new JsonpRequestBuilder();
jsonp.setTimeout(100000);
jsonp.setCallbackParam("callback");
 jsonp.requestObject(url,
     new AsyncCallback<TrainingData>() { // Type-safe!

       @Override
       public void onFailure(Throwable throwable) {
         // Easy to debug! (hopefully)
           Window.alert(throwable.getMessage());
       }
       @Override
       public void onSuccess(TrainingData trd) {
         // Success!
           //Window.alert("JSON obtained");
           Window.alert(trd.getEntries().toString());
         }
       });

EDIT Now I cam getting

Cross-site hosted mode not yet implemented. See issue http://code.google.com/p/google-web-toolkit/issues/detail?id=2079

Patryk
  • 18,244
  • 37
  • 110
  • 212
  • We fixed it by running a proxy servlet to fake local locations for all of our external sites. We are running JEE apps in JBoss so we are using: https://github.com/dsmiley/HTTP-Proxy-Servlet – Shadow Man Jun 13 '13 at 00:11
  • Can you show it on code how have you done it ? ( so that I can accept your answer ? ) – Patryk Jun 13 '13 at 05:23
  • Your "Cross-site hosted mode not yet implemented" error is due to the use of ``. It's unrelated to your first issue. If you need it, then use `` instead (as said on the linked issue) – Thomas Broyer Jun 13 '13 at 09:13
  • @Patryk I added an example below. Your app would just hit `localhost/deployment_root/firstResource/path/to/resource` and that would be forwarded to `http://targethost:port/and/root/path/path/to/resource`... I hope it helps you. – Shadow Man Jun 13 '13 at 20:48

1 Answers1

2

We are using maven and JBoss 7.2.0. But you should also be able to get this working in Tomcat or other containers if you want/need to also with minimal or no changes.

We did it using dsmiley's httpproxy servlet: download that and build it, adding it to your maven repository (or forget the pom.xml file and just add the newly built jar to your libs).

In our pom.xml file we add a dependency:

   <dependency>
      <groupId>org.mitre.dsmiley.httpproxy</groupId>
      <artifactId>smiley-http-proxy-servlet</artifactId>
      <!-- 1.3-SNAPSHOT adds ability to handle spaces in paths, 1.2 works otherwise -->
      <version>1.3-SNAPSHOT</version>
   </dependency>

In your web.xml add servlet mappings:

   <servlet>
      <servlet-name>FirstResourceProxy</servlet-name>
      <servlet-class>org.mitre.dsmiley.httpproxy.ProxyServlet</servlet-class>
      <init-param>
         <param-name>targetUri</param-name>
         <param-value>http://targethost:port/and/root/path</param-value>
      </init-param>
      <init-param>
         <param-name>log</param-name>
         <param-value>true</param-value>
      </init-param>
   </servlet>
   <servlet-mapping>
      <servlet-name>FirstResourceProxy</servlet-name>
      <url-pattern>/firstResource/*</url-pattern>
   </servlet-mapping>

   <servlet>
      <servlet-name>SecondResourceProxy</servlet-name>
      <servlet-class>org.mitre.dsmiley.httpproxy.ProxyServlet</servlet-class>
      <init-param>
         <param-name>targetUri</param-name>
         <param-value>http://targethost:port/and/root/path</param-value>
      </init-param>
      <init-param>
         <param-name>log</param-name>
         <param-value>true</param-value>
      </init-param>
   </servlet>
   <servlet-mapping>
      <servlet-name>SecondResourceProxy</servlet-name>
      <url-pattern>/secondResource/*</url-pattern>
   </servlet-mapping>
Shadow Man
  • 2,855
  • 1
  • 19
  • 33
  • Could you explain please what does targetUri mean? – Ismail Sahin Apr 01 '14 at 12:46
  • The `targetUri` is the target URI (aka URL) to forward all requests from the source (`servlet-mapping>url-pattern`) to. So here, a request to `local-deployment/firstResource/foo/bar/blah` will go to `http://targethost:port/and/root/path/foo/bar/blah` and a request to `local-deployment/secondResource/foo/blah/baz.html` will go to `http://targethost:port/and/root/path/foo/blah/baz.html`. In actual use, the first and second resources will probably forward to different `targetUri`s, but this is just an example. – Shadow Man Apr 01 '14 at 20:38