Re: GWT Serialization

861 views
Skip to first unread message

Manuel Carrasco Moñino

unread,
Oct 15, 2012, 3:05:20 AM10/15/12
to google-we...@googlegroups.com, Manikanda raj S
What you want is easy to do using requestbuilder + autobeans. 
Autobeans are thought to handle json string representations and convert them to javaobjects, they work in server and client sides and are full testable.

Another option I use very often in client side because of its simplicity and performance is gwtquery ajax + json-databinding 

In both cases you can use any json backend.

- Manolo

On Sat, Oct 13, 2012 at 12:35 PM, Manikanda raj S <sls...@gmail.com> wrote:
I'm using GWT RPC Calls for Server Side Request so far and it's pretty good. I'm planning on separating my Code into Servlets and GWT Client Side. Since i'm using RPC calls, it seems impossible. The Reason i want to do like this is , i'm planning to provide white labeling option for my App. So if i could separate the code to client code and servlets, i can simply provide the White Labeled client code to my Partners to host on their server. I have checked with GWT RequestBuilder and 'Access-Control Allow-Origin : Origin from Client Header' and it works fine.

However i need to implement gwt-serialization over RequestBuilder request and Servlet Responses. How can i do this ..?

Things I like to make:
  • RequestBuilder sending Serializable String(Which is a IsSerialiazible object) to Servlet.
  • Servlet deserializes the String to Java Object,Processes and Returns the String Response of a 'IsSerialiazable' (Another) Object.
  • The Response String received in GWT RequestBuilder deseriailizes it back to a Java Object(JS after Compiling).

--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To view this discussion on the web visit https://groups.google.com/d/msg/google-web-toolkit/-/ZNwK3SwtKOUJ.
To post to this group, send email to google-we...@googlegroups.com.
To unsubscribe from this group, send email to google-web-tool...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.

Manikanda raj S

unread,
Oct 15, 2012, 2:35:58 PM10/15/12
to google-we...@googlegroups.com, Manikanda raj S, Dhamodharan
Hi Manolo,
Thanks for the links. But i have been using GWT RPC in my Application for quite some time and it would be a pretty big work to migrate all those from GWT-RPC Serialization to Autobeans or json databinding. That's why i specifically asked for GWT Serialization externally. Any ideas on Externalized GWT Serialization ..?
To unsubscribe from this group, send email to google-web-toolkit+unsub...@googlegroups.com.

Manuel Carrasco Moñino

unread,
Oct 16, 2012, 1:58:24 AM10/16/12
to google-we...@googlegroups.com
So, I dont follow you. If you want to generate serialized strings in server side, you have to maintain the dependence with gwt rpc there, so why dont continuing using rpc-servlets, actually they serialize/deserialize to strings, and the same in the client side. As you say CORS works well with RPC. What is the goal of replacing gwt-rpc servlets by your own ones, or to use directly requestbuilder ?



To view this discussion on the web visit https://groups.google.com/d/msg/google-web-toolkit/-/Pfw2FLUTfxoJ.

To post to this group, send email to google-we...@googlegroups.com.
To unsubscribe from this group, send email to google-web-tool...@googlegroups.com.

Manikanda raj S

unread,
Oct 16, 2012, 2:58:49 AM10/16/12
to google-we...@googlegroups.com
CORS don't work with GWT Servlets, only with RequestBuilder. As i said before i want to be able to separate the client and server code to two different web servers. And i don't want to be redoing my code all over again. Besides GWT's Serialization seems to be pretty Good. So Why replace it , just i'm trying to use it in a different way, that's it.

To unsubscribe from this group, send email to google-web-toolkit+unsubscribe@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.

Thomas Broyer

unread,
Oct 16, 2012, 3:01:43 AM10/16/12
to google-we...@googlegroups.com


On Tuesday, October 16, 2012 8:58:49 AM UTC+2, Manikanda raj S wrote:
CORS don't work with GWT Servlets, only with RequestBuilder.


There's no reason it wouldn't work. What did you try? 

Manuel Carrasco Moñino

unread,
Oct 16, 2012, 8:31:05 AM10/16/12
to google-we...@googlegroups.com
Actually gwt RPC uses requestbuilder as its low-level transport, so cors works with rpc without any problem.

Here you have an example of cors with gwt rpc.

- In the client side you have to change the RPC service url
 GreetingServiceAsync greetingService = GWT.create(GreetingService.class);
 ((ServiceDefTarget)greetingService).setServiceEntryPoint("http://localhost:8888/mymodule/greet");

- In the server side configure a filter in your web.xml
   <filter>
      <filter-name>corsFilter</filter-name>
      <filter-class>com.example.server.CORSFilter</filter-class>
   </filter>
   <filter-mapping>
      <filter-name>corsFilter</filter-name>
      <url-pattern>/*</url-pattern>
   </filter-mapping>
- And this is an example of filter, maybe you should set any kind of security based on the Origin header

package com.example.server;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class CORSFilter implements Filter { 

  public void doFilter(ServletRequest servletRequest, 
      ServletResponse servletResponse, FilterChain filterChain) 
      throws IOException, ServletException { 

    HttpServletRequest req = (HttpServletRequest) servletRequest; 
    HttpServletResponse resp = (HttpServletResponse) servletResponse; 

    String o = req.getHeader("Origin"); 
    if ("options".equalsIgnoreCase(req.getMethod())) { 
      resp.setHeader("Allow", "GET, HEAD, POST, PUT, DELETE, TRACE, OPTIONS"); 
      if (o != null) { 
        resp.addHeader("Access-Control-Allow-Origin", o); 
        resp.addHeader("Access-Control-Allow-Methods", 
            "POST, GET, OPTIONS"); 
        resp.addHeader("Access-Control-Allow-Headers",
            "content-type,pageurl,x-gwt-permutation,x-gwt-module-base"); 
        resp.setContentType("text/plain"); 
      } 
      resp.getWriter().flush(); 
      return; 
    } 

    if (o != null) { 
      resp.addHeader("Access-Control-Allow-Origin", o); 
    } 

    if (filterChain != null) { 
      filterChain.doFilter(req, resp); 
    } 
  } 

  @Override 
  public void destroy() { 
  } 

  @Override 
  public void init(FilterConfig arg0) throws ServletException { 
  } 


The server part works with RPC, RF, RequestBuilder, gwtquery-ajax and any other js approach.

- Manolo
> --
> You received this message because you are subscribed to the Google Groups
> "Google Web Toolkit" group.
> To view this discussion on the web visit

>
> To post to this group, send email to google-we...@googlegroups.com.
> To unsubscribe from this group, send email to

Manikanda raj S

unread,
Oct 16, 2012, 9:36:44 AM10/16/12
to google-we...@googlegroups.com
Thanks for the Code Manolo. But i would also like to separate the code as of now into GWT Client part as one and Servlet Part as other. One more thing i want GWT Serialization externalized is, if we can have it externalized, then we can probably use the same for Android Development too.If we can convert the same to Objective C,we can use it for IPhone too. I'm just trying to use GWT Serialization as a API data type with my Present Code.

Thomas Broyer

unread,
Oct 16, 2012, 10:24:27 AM10/16/12
to google-we...@googlegroups.com


On Tuesday, October 16, 2012 11:16:35 AM UTC+2, Manikanda raj S wrote:
How can i actually set Response Headers in GWT Servlets ..? If i can have normal Servlets, i can have a Filter doing that.

GWT *are* "normal servlets"; you can very well use a Filter to add the appropriate Allow-Origin response header.

Manuel Carrasco Moñino

unread,
Oct 16, 2012, 10:24:46 AM10/16/12
to google-we...@googlegroups.com
Why do you say RPC servlets are not normal servlets? they are.
You can control several things in your service servlet using the methods thought for that, for instance to add customized headers you can do:

  @Override
  protected void onAfterResponseSerialized(String serializedResponse) {
    super.onAfterResponseSerialized(serializedResponse);
    HttpServletResponse r = perThreadResponse.get();
    r.setHeader("Foo", "Bar");
  }

GWT client code is static so you can host it in any webserver, just take the html and js generated by the compiler and deploy it in your preferred web server. In fact GWT is deseigned for client code, rpc or rf are provided to make live easier to those developers who want a backend in java.

On Tue, Oct 16, 2012 at 11:16 AM, Manikanda raj S <ma...@vembu.com> wrote:
How can i actually set Response Headers in GWT Servlets ..? If i can have normal Servlets, i can have a Filter doing that. Besides i want to run client and server code in different webservers, client code(Java Script,HTML) in a apache servers in my customer's location and Servlets in my own Server. How can i do that with gwt-rpc servlets ...?
--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To view this discussion on the web visit https://groups.google.com/d/msg/google-web-toolkit/-/5d0gx8dvzyUJ.

To post to this group, send email to google-we...@googlegroups.com.
To unsubscribe from this group, send email to google-web-tool...@googlegroups.com.

Manuel Carrasco Moñino

unread,
Oct 16, 2012, 10:33:35 AM10/16/12
to google-we...@googlegroups.com
RPC client part is not thought for java, although there are some libraries to handle it like gwt-syncproxy.
Otherwise, RF can be used in any JVM like android, in fact request factory stuff is out of the gwt namespace.

I dont think trying to handle gwt-rpc with xcode is a good idea.

What I do with my mobile apps is to use gwtphonegap, so as I can use the same code base for desktop and all devices, I have not to modify any line of code in my ajax code, I use RF or gquery-ajax, although rpc works fine as well.

- Manolo

To view this discussion on the web visit https://groups.google.com/d/msg/google-web-toolkit/-/NrwwZIdQSVQJ.

To post to this group, send email to google-we...@googlegroups.com.
To unsubscribe from this group, send email to google-web-tool...@googlegroups.com.

Thomas Broyer

unread,
Oct 16, 2012, 10:37:40 AM10/16/12
to google-we...@googlegroups.com


On Tuesday, October 16, 2012 3:36:44 PM UTC+2, Manikanda raj S wrote:
Thanks for the Code Manolo. But i would also like to separate the code as of now into GWT Client part as one and Servlet Part as other.

This is not a problem, provided the shared classes (RPC interfaces and the objects sent over the wire) are available to both projects (either one depends on the other, or you create a third project for shared code; see https://github.com/tbroyer/gwt-maven-archetypes for examples)
 
One more thing i want GWT Serialization externalized is, if we can have it externalized, then we can probably use the same for Android Development too.

This is a Bad Idea™.
GWT-RPC is meant to be consumed by clients that are built from the exact same source as the server. That works well for online webapps, where deploying a new version is a no-brainer. For "installed" clients and offline webapps, it doesn't work well, as you cannot be sure when your clients will be updated to the latest version; and running several serialization policies in the server is not always possible.

This is one of the reasons RequestFactory was created. Your interfaces (RequestContexts and proxies) define a contract between the clients and the server (add to that the serialization format and you have a protocol). The contract can change slightly on the server side without necessarily breaking the clients (new service methods or new properties simply won't be used by older clients).
 
If we can convert the same to Objective C,we can use it for IPhone too.

But I'm afraid it won't work for RequestFactory either (on the other hand, you have a stable protocol, so you can implement it in ObjC).
If you run on AppEngine, you might want to have a look at Google Cloud Endpoints.

Jens

unread,
Oct 16, 2012, 10:50:02 AM10/16/12
to google-we...@googlegroups.com
Correct me if I am wrong.

You have a GWT application up and running on your application server (jetty, tomcat, or similar) and now you want to move your compiled app (HTML/Javascript) to an external web server on a different domain/host. To do so you have two options:

1.) Enable CORS on your server using a servlet filter that sits in front of your GWT-RPC server services. Only downside is that CORS does not work with IE6 and IE7 at all and it only works on IE8 / IE9 if you use the XDomainRequest object instead of XMLHttpRequest. Not sure if GWT's RequestBuilder (that is used by GWT-RPC internally) already handles this (http://caniuse.com/#search=CORS)

2.) Most likely you can configure the external web server to proxy specific URL requests to your server. That means when your GWT app makes a GWT-RPC request to http://customerdomain.com/app/service , the web server can internally redirect the request to http://yourserver.com/app/service.  In that case you don't need CORS because your GWT client side app doesn't even know that the data comes from your server. 


If you want 3rd party developers, Android/iPhone apps accessing your server, you should really think about adding a new API to your server that does not depend on GWT. Your GWT app can still use GWT-RPC so you dont have to rewrite it. A common way to do so ist to provide an additional REST API using JSON/XML that can easily be used by different non-GWT clients. That way you don't have to rewrite all the GWT serialization code for your non-GWT clients (Android/iPhone) and you don't have any trouble if you want to upgrade your GWT app to a newer GWT version (this could break GWT serialization of non-GWT clients if GWT internals have changed and your GWT serialization library, in lets say Objective-C, does not reflect these changes yet. So you need to update these non-GWT apps as well. You dont want that.).

Personally I found flatpack-java promising (https://github.com/perka/flatpack-java).

Hope this helps and I have understood you correctly.

-- J.

Manikanda raj S

unread,
Oct 16, 2012, 10:56:30 AM10/16/12
to google-we...@googlegroups.com
Thanks Guys, it's been really a lot of help here. I will definitely try these things. 
Reply all
Reply to author
Forward
0 new messages