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

一笑奈何 提交于 2019-12-18 10:45:08

问题


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.

Yes, I have seen Invoke a GWT RPC service from Java directly, but this discussion goes into a different direction.


回答1:


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.




回答2:


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




回答3:


You can find what you search in this article about GwtRpcCommLayer : http://googlewebtoolkit.blogspot.com/2010/07/gwtrpccommlayer-extending-gwt-rpc-to-do.html




回答4:


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.




回答5:


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



来源:https://stackoverflow.com/questions/2116870/how-can-i-call-a-gwt-rpc-method-on-a-server-from-a-non-gwt-but-java-gapplicati

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!