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

后端 未结 5 1092
感动是毒
感动是毒 2020-12-24 08:56

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

5条回答
  •  萌比男神i
    2020-12-24 09:30

    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

提交回复
热议问题