开发者

How can I call a GWT RPC method on a server from a non GWT (but Java) gapplication?

I have a regular Java application and want to access an GWT RPC endpoint. Any idea how to make this happen? My GWT application is on a GAE/J and I could use REST for example but I already have the GWT RPC endpoints and don't want to build another façade.

Y开发者_运维知识库es, I have seen Invoke a GWT RPC service from Java directly, but this discussion goes into a different direction.


GWT SyncProxy allows you to access GWT RPC services (e.g methods) from pure Java (not JSNI) code.

See http://www.gdevelop.com/w/blog/2010/01/10/testing-gwt-rpc-services/ for details.


The Java implementation in GWT of the RPC protocol in the packages com.google.gwt.user.server.rpc and com.google.gwt.user.server.rpc.impl unfortunately only covers deserialization of requests and serialization of responses. The real work is done in the classes ServerSerializationStreamReader and ServerSerializationStreamWriter (each appr. 750 lines of code).

To implement a client, you obviously need to serialze the request and deserialize the response, but since there's no documentation available for the protocol and AFAIK no Java client implementations available, you probably would have to reverse-engineer the (de)serialization classes and write your own code to do everything "the other way around".

You can find some high-level info about the protocol here


Unfortunately, I think jarnbjo is right about having to reimplement the browser's half of the RPC mechanism.

Alternately, if you end up having to write a REST interface for remote clients, you can switch your GWT app away from the RPCs and use the REST interface there too, and share your client library between the external clients and the GWT's client-side interface.


I have explored all the answer and today I succeed to working as a pure java client.

the SyncProxy needs you have the entire code of the GWT project(server side). And to do so, you just create one additional class which fire the SyncProxy into it. In this class you should import all needed classes and functions, that why you need server code.

and you should check following file could be downloaded from server:

compilation-mappings.txt
*.nocache.js
*.cache.html
*.gwt.rpc

I add the code before cookiemanager, because my server side uri is HTTPS. And my class include a login action then fire the GWT request. This is my code(I have upgraded SyncProxy a bit, because it doesn't support cookie/session auth check.):

package com.xxx.xxx.x.xx;

import java.io.IOException;
import java.net.CookieManager;
import java.net.URISyntaxException;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.X509Certificate;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

import net.customware.gwt.dispatch.client.standard.StandardDispatchService;
import net.customware.gwt.dispatch.shared.DispatchException;

import com.gdevelop.gwt.syncrpc.LoginUtils;
import com.gdevelop.gwt.syncrpc.ProxySettings;
import com.gdevelop.gwt.syncrpc.SyncProxy;

public class TestRemoteExecuteAction {

            static Logger logger = Logger.getLogger(TestRemoteExecuteAction.class.getName());
              public static void main(String[] arg) {

                  SyncProxy.setLoggingLevel(Level.ALL);

                try {

                      // Create a trust manager that does not validate certificate chains
                    TrustManager[] trustAllCerts = new TrustManager[] {new X509TrustManager() {
                            public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                                return null;
                            }
                            public void checkClientTrusted(X509Certificate[] certs, String authType) {
                            }
                            public void checkServerTrusted(X509Certificate[] certs, String authType) {
                            }
                        }
                    };

                    // Install the all-trusting trust manager
                    SSLContext sc = SSLContext.getInstance("SSL");
                    sc.init(null, trustAllCerts, new java.security.SecureRandom());
                    HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());

                    // Create all-trusting host name verifier
                    HostnameVerifier allHostsValid = new HostnameVerifier() {
                        public boolean verify(String hostname, SSLSession session) {
                            return true;
                        }
                    };

                    // Install the all-trusting host verifier
                    HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);

                    CookieManager cookiemanager = LoginUtils.loginFormBasedJ2EE("https:XXXXX", "XXXX", "XXXX");

                    SyncProxy.setBaseURL("https://XXXXXX");

                    StandardDispatchService rpcService =  SyncProxy.createProxy(StandardDispatchService.class,
                            new ProxySettings().setCookieManager(cookiemanager));

                    System.out.println(cookiemanager.getCookieStore().getCookies().get(0));
                    String JSESSIONID = cookiemanager.getCookieStore().getCookies().get(0).getValue();

                    rpcService.execute(new XXXXXAction("XXX"));



                } catch (NoSuchAlgorithmException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (KeyManagementException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (URISyntaxException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (DispatchException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } 

              }
}

Some outside link you may want:

https://code.google.com/p/gwt-syncproxy/wiki/QuickStart http://cancerberonia.blogspot.de/2012/10/testing-gwt-service-classes.html

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜