How to tackle changing properties on the clientside in GWT applications? - gwt

while developing GWT apps we ran into lots of problems with project configuration. Let me explain... As usual in development, we have few environments for our application: local, demo, preview and live. Of course they are running on different machines, some are using SSL while others don't. But most importantly - all of them have different URLs.
Now, in few places in our application we need specify some URLs. Usually we would use *.properties files stored on server, and tools like Spring taglibs and it's <spring:message /> tag. But since GWT does not have such tools, we ended up in leaving hard coded URLs and performing code replacement on different SVN branches. As you can imagine - this is the worst possible scenario, causing us much problems.
So, my question is:
how one could build proper, flexible mechanism of storing config properties shared for both client and server side in GWT application. This properties have to be available for server-side handlers, client app (compiled JavaScript), UiBinder, other code running on server (workers, Spring, etc.).
The preferred way would be to avoid gwtc build if we change value of some property, but I guess it will be hard to achieve. So I will accept any reasonable alternative.

How about using relative URI references (e.g. absolute paths, without scheme or authority; i.e. /path/to/foo instead of http://example.com/path/to/foo)?
And in the few places where you absolutely need an URI (with scheme and authority), then use another property to store the "prefix" (e.g. http://example.com), and then concatenate with the above path.
Those places where you need a full URI should all be on the server, which means you don't have to recompile your GWT project when you change the "prefix", so everything is only runtime configuration and you can deploy the same artifacts in all environments.
That being said, if you ever need something configurable at runtime in GWT, then use a dynamic host page and JSNI (or a com.google.gwt.i18n.client.Dictionary); see http://code.google.com/webtoolkit/articles/dynamic_host_page.html

Related

GWT Server to server communication

I have 2 different gwt applications, both on different machines, but they need to communicate.
Is their a simple way, using the gwt mechanisem to do it and access an existing servlet on another gwt application, or do i need to open another http connection between them some other way?
The most standardized way to do that is to use JMX: it is future prone and a lot of work was already done for you.
http://en.wikipedia.org/wiki/Java_Management_Extensions
Your issue cannot be considered as a GWT problem since GWT is limited to java script, and you need real java methods to communicate together, doing it through GWT is at best unperformant and at worse the source of many problems (since you are not inside a browser, and using a "virtual browser" to execute your javascript might lead to subtile very hard to understand differences)

A good way to structure AngularJS client code with Play 2.1 as backend

I own a Play 2.1 application.
Initially, I used the default template mechanisms from Play 2.1 until I .. learned AngularJS.
Now, I clearly want my client side to be an AngularJS app.
However, while surfing the net, I find there are no clear way to achieve it:
Letting Play behave as a simple RESTful application (deleting the view folder) and making a totally distinct project to build the view (AngularJS app initialized by grunt.js).
Advantage: Likely to be less messy, and front and backend teams would work easily separately.
Drawback: Need another HTTP server for the AngularJS app.
Try to totally integrate AngularJS app with the traditional Play's workflow.
Drawback: With a pretty complex framework like AngularJS, it would lead to a confusion of templates managementfor instance : scala.html (for Play) / tpl.html (for Angular) ... => messy.
Making a custom folder within the play project but distinct from the initial folders created by the Play scaffolding. Let's call it myangularview instead of traditional view for instance. Then, publish static contents generated by grunt.js into the Play's public folder, in order to be reachable from browser through Play's routing.
Advantage: SRP between components is still fairly respected and no need to use another light HTTP server for the client-side like in 1.
I pointed out my own views of advantage and drawbacks.
What would be a great way to achieve the combination of Play with Angular?
Yes, I'm answering to my own question :)
I came across this way of doing:
http://jeff.konowit.ch/posts/yeoman-rails-angular/
Rails?? No matter the framework is, the need remains exactly same.
It advocates a real separation between APIs (backend side), and front-end side (in this case making AJAX calls to backend server).
Thus, what I've learned is:
During development phase, a developer would use two servers: localhost on two distinct ports.
During production phase, the front-end elements would be encompassed into the whole backend side (the article would deal with a kind public folder, aiming to serve static contents: HTML, angular templates (for instance), CSS etc... Advantage? => dealing with a one and unique serving server's APIs exposition as well as static assets for the UI.
With this organization, some tools like Yeoman would be able to bring some really awesome handy things to developers like for instance: the livereload feature. :):)
Of course, during development phase, we end up with two different domains, (localhost:3000 and localhost:9000 for instance) causing issues for traditional ajax requests. Then, as the article points out, a proxy may be really useful.
I really find this whole practice very elegant and pleasant to work with.
There was an interesting discussion on the play mailinglist a couple of days ago about frontend-stack/solution, could be something in it for you, quite some people using angular it seems: https://groups.google.com/forum/#!searchin/play-framework/frontend/play-framework/IKdOowvRH0s/tQsD9zp--5oJ

File handling in GWT(at server side)

I have some doubt regarding gwt file handling.
How to do file handling in gwt(at server side). How to create simple file. I mean which class is particularly used for CREATING and HANDLING it ??
Where is the file created. Is it under server package or somewhere else ??
How to resolve serialization and serialization problem. ??
GWT is a client-side technology. It's server-agnostic (and can also be used client-only, e.g. mobile apps, Chrome apps, browser extensions). GWT-RPC and RequestFactory use Java servlets by default (nothing precludes implementing RequestFactory in other languages/technologies) but have otherwise no requirement or limitations.
So, server-side, it's all just Java in most of the cases, and depends on your deployment target (e.g. AppEngine restricts what you can do, servlet containers can also have security policies in place, and you're of course dependent on authorizations at the filesystem level)
Since GWT is a pure client-side technology you have all possible options like plain Servlets for example. You are not limited in any way in picking the upload approach.
However, there is a number of open source projects, which make possible to use nice features like progress bars and multiple file uploads. And those come integrated with some GWT widgets as well. Check this project for example http://code.google.com/p/gwtupload/

Portal URL from Portlet

Is it possible to get the Portal base URL (like http://www.thisismyportal.com) from a Portlet using Portlet 2.0 API?
Right now I'm planning to manually build it concatenating PorletRequest.getServerName(), PortletRequest.getServerPort() and PortletRequest.getContextPath(); but it seems kind of clumsy (and there's no PortletRequest.getProtocol())
While it is clumsy, it is the safest way to construct the URL; and while there is no PortletRequest.getProtocol() method, you can conclude the protocol using the PortletRequest.isSecure() method.
I would advise against using an external configuration for the base URL, for a couple of reasons.
First, it would be yet another configuration item for you to maintain across environments (test, integration, production and so forth). There's very little justification to hold, in configuration, something that is fully reproducible using the current request.
Second, under certain circumstances, it might be impossible to designate a particular URL as a "base URL" for the portal. An example would be the case in which the portal server is associated with multiple hosts, or multiple host aliases.
We had those configuration properties in Resource Environment Provider for the purpose of generating external URLs for sending them in emails. It was specific solution and it wasn't a problem for us as we had other properties stored there as well so we knew it will be available at runtime. I don't know if that suits your needs. It depends on your scenario.
Also, we used https only during login, so we always generated http URLs.
Hope this helps.

GWT client and server implementations of the same class

Is there any way to have the same class implemented differently on the client vs the server?
To avoid the "Why do you want to do that?" question.. I will elaborate
I am converting a very large Java client/server application. Currently it uses a Swing GUI client and talks to the server via Spring remoting (RPC). Using GWT RPC with Spring services is not a problem, there are several excellent examples available and the all seem to work well.
Several classes that are common to both the client and the server contain data that is passed back and forth. These classes also contain some behavior that is implemented by using the standard JRE classes. For example, one class contains, parses and formats date and time, including time zone, DST, etc. in a locale specific way. I could rewrite/refactor it but the application is over 10 million SLOC, resulting in literally millions of references to this class alone, so a major rewrite is not cost effective.
To use this as an example, GWT provides excellent i18n support for parsing and formatting dates. But the implementation is different to the way the JRE does it.
So I'm looking for a cleaver way where by I can inject an implementation into the shell of my DateTime class, depending on whether it is in the client (using GWT and native JS) or in the server (using the JRE). Is there a cunning way to do this? Perhaps using the module file XXXXX.gwt.xml. I'm looking for a generic solution.
You'd want to use the <super-source> for overriding one package implementation with another.
This is what GWT uses to emulate the Java Runtime classes, and (among others) provide different implementations for client and server of the com.google.gwt.regexp.shared.* classes.
I think what You are looking for, is this: <source path="client" /> in your project gwt.xml file. It tells the GWT generator where to look for client side code to convert to JS. In my project I have it set up this way:
<source path="client" />
<source path="shared" />
Basically client code is in the client directory, and in shared we keep beans and some data wrappers for client and server side.
What you could do, is to add the packages you want to convert to client with the source path like above. But you must remember, that the classes you are going to convert, can be composed only of objects and properties which GWT generator can convert into client-side java script. I'm not sure also if more accurate path can be put in the source path, like:
<source path="shared/beans/whatever" />
Another drawback is, that if you use GWT i18n support, it handles different locale in time of compilation by its own - which is good. If you decide to use your own mechanism, your classes must contain some logic to be aware of locale being used currently, which must be compatible with GWT.