framework icon indicating copy to clipboard operation
framework copied to clipboard

Uncaught client side exception (widgetset not working) in Liferay 7

Open ctliv opened this issue 9 years ago • 10 comments

Environment:

  • Liferay 7.0 GA5
  • Vaadin 8.1.6 (all required bundles deployed to Liferay)

After deploying this portlet:

@Widgetset(AppWidgetSet.NAME)
@Theme(AppTheme.NAME)
@Component(immediate = false, service = UI.class, property = {
        "com.liferay.portlet.display-category=Vaadin samples",
        "javax.portlet.name=com.github.ctliv.vportlet.VPortlet",
        "javax.portlet.display-name=Sample Vaadin Portlet",
        "javax.portlet.portlet-mode=text/html;view",
        "javax.portlet.security-role-ref=power-user,user",
        "com.vaadin.osgi.liferay.portlet-ui=true"},
        scope = ServiceScope.PROTOTYPE)
public class VPortletUI extends UI {

    @Override
    protected void init(VaadinRequest request) {    	
		ComboBox<String> combo = new ComboBox<>("Test combo:");
		combo.setItems("One","Two","Three");
		setContent(combo);
    }

}

with this simplest custom AppWidgetSet.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE module PUBLIC "-//Google Inc.//DTD Google Web Toolkit 2.5.1//EN" "http://google-web-toolkit.googlecode.com/svn/tags/2.5.1/distro-source/core/src/gwt-module.dtd">
<module>
    <!--
        This file is automatically updated based on new dependencies by the
        goal "vaadin:update-widgetset".
    -->

    <!-- Inherit DefaultWidgetSet -->
    <inherits name="com.vaadin.DefaultWidgetSet" /> 

</module>

the following uncaught javascript error is shown in console:

Sat Nov 25 14:01:24 GMT+100 2017 com.vaadin.client.communication.MessageHandler
SEVERE: Error performing server to client RPC calls
com.google.gwt.core.client.JavaScriptException: (TypeError) : Cannot read property 'Cd' of undefined
	at Unknown.UJb(com.ctliv.test.AppWidgetSet-0.js)
	at Unknown.WJb(com.ctliv.test.AppWidgetSet-0.js)
	at Unknown.iJb(com.ctliv.test.AppWidgetSet-0.js)
	at Unknown.hJb(com.ctliv.test.AppWidgetSet-0.js)
	at Unknown.OIb(com.ctliv.test.AppWidgetSet-0.js)
	at Unknown.dJb(com.ctliv.test.AppWidgetSet-0.js)
	at Unknown.uub(com.ctliv.test.AppWidgetSet-0.js)
	at Unknown.PIb(com.ctliv.test.AppWidgetSet-0.js)
	at Unknown.Pub(com.ctliv.test.AppWidgetSet-0.js)
	at Unknown.hub(com.ctliv.test.AppWidgetSet-0.js)
	at Unknown.yi(com.ctliv.test.AppWidgetSet-0.js)
	at Unknown.qi(com.ctliv.test.AppWidgetSet-0.js)
	at Unknown.Ci(com.ctliv.test.AppWidgetSet-0.js)
	at Unknown.wi(com.ctliv.test.AppWidgetSet-0.js)
	at Unknown.Uh(com.ctliv.test.AppWidgetSet-0.js)
	at Unknown.Xh(com.ctliv.test.AppWidgetSet-0.js)
	at Unknown.eval(com.ctliv.test.AppWidgetSet-0.js)
	at Unknown.d(com.ctliv.test.AppWidgetSet-0.js)

and the combo does not open when clicking on the dropdown button. Here's a webm video or animated gif showing the issue.

The same UI (without the OSGi annotation, of course) with the very same widgetset (binary compared) deployed to a jetty server ("mvn jetty:run") works correctly, as shown here: image

Here is code for both cases:

  • Test-App (standalone): https://github.com/ctliv/vaadin-portlet-sample/tree/test-app
  • Test-OSGi (Liferay portlet): https://github.com/ctliv/vaadin-portlet-sample/tree/test-osgi

I have no clues about what may cause the problem. Many tests, including changing widgetset name (example, from "com.ctliv.test.AppWidgetSet" to "com.ctliv.AppWidgetSet") or generating widgetset on different system (Windows, Linux) did not produce any solution. Of course, after every widgetset compilation I have cleaned up browser local cache.

The solution not to compile a custom widgetset but to use Vaadin's default widgetset ("com.vaadin.DefaultWidgetSet") unfortunately is not a solution, as I have to use some addons from Vaadin Directory, so a custom widgetset must be compiled. BTW, Vaadin default widgetset works correctly (!), so the issue is somewhat related to custom widgetset compilation and Liferay.

Thanks to anyone who can give some help or hint.

ctliv avatar Nov 25 '17 14:11 ctliv

You should try either using superdevmode or PRETTY mode when compiling the widgeset to get a better stacktrace

Artur- avatar Nov 25 '17 14:11 Artur-

Thanks, Arthur.

Here is the error after applying style PRETTY:

com.ctliv.test.AppWidgetSet-0.js:7482 Sat Nov 25 17:47:46 GMT+100 2017 com.vaadin.client.communication.MessageHandler
SEVERE: Error performing server to client RPC calls
com.google.gwt.core.client.JavaScriptException: (TypeError) : Cannot read property 'iterator' of undefined
	at Unknown.$applyInvocation(com.ctliv.test.AppWidgetSet-0.js)
	at Unknown.$parseAndApplyInvocation(com.ctliv.test.AppWidgetSet-0.js)
	at Unknown.$handleRpcInvocations(com.ctliv.test.AppWidgetSet-0.js)
           ... [omitted]

Debugging the javascript, I found where the error is generated. Here's the code (line 14431 of "com.ctliv.test.AppWidgetSet-0.js"):

	_.getRpcImplementations = function getRpcImplementations(rpcInterfaceId){
	        ... [omitted]
	  return castTo(this.rpcImplementations[rpcInterfaceId], 116);
	}

Parameter "rpcInterfaceId" value in the invocation is:

com.vaadin.shared.data.DataCommunicatorClientRpc

but array "this.rpcImplementations" has just one item whose key has a dot at the end:

com.vaadin.shared.data.DataCommunicatorClientRpc.

So, 'undefined' is returned from the function and this later creates the error.

I have double checked with the standalone version (launched via jetty). In this case the key of the array item is, as expected, without the dot (com.vaadin.shared.data.DataCommunicatorClientRpc) and everything works well.

Do you know why only in OSGi/Liferay the array item is loaded with the dot in the key? The widgetset is the same in both cases, so the wrong key with the dot should come from the server, but from where?

ctliv avatar Nov 25 '17 18:11 ctliv

The rpcImplementations object is populated using only client side data. It's a JS object, represented as a FastStringMap in GWT code, but it's accessed using the array-like object[key] syntax in JS. In the case of com.vaadin.shared.data.DataCommunicatorClientRpc, the registration is done the registerRpc call in the constructor of DataCommunicatorConnector.

registerRpc does a Class.getName() and replaces any $ with . to create the key that is used in the FastStringMap. For the observed bug to occur, I would guess that either the widgetset file itself is for some reason rewritten to so that Class.getName() returns a different value, or alternatively there's some external JS that rewrites the object after the RPC has been registered?

Legioth avatar Nov 27 '17 09:11 Legioth

Hello Legioth, thanks for helping, but apparently both answers to your questions are "no", because:

I could guess that maybe Liferay (which loads its JS in the web page) could do this rewrite, but com.vaadin.shared.data.DataCommunicatorClientRpc is a Vaadin object which Liferay should totally be unaware of.

I'm not a JS pro, but debugging the code I found that there is no rewrite after initial set of the FastStringMap. The value with the ending dot is loaded at start and never changed.

BTW, here is the running example. I'll leave the VM on for a while.

ctliv avatar Nov 27 '17 11:11 ctliv

The code in com.ctliv.test.AppWidgetSet-0.js says

  rpcInterfaceId = $replaceAll(($ensureNamesAreInitialized(rpcInterface) , rpcInterface.typeName), '\$', '.');

where the code in the file SHOULD say

  rpcInterfaceId = $replaceAll(($ensureNamesAreInitialized(rpcInterface) , rpcInterface.typeName), '\\$', '.');

While the code should replace "aa$bb" -> "aa.bb", removing one slash will cause \$ to match the end of line, adding a "." to all strings.

What does the real file contain?

Artur- avatar Nov 27 '17 13:11 Artur-

I think the real file produced by widgetset compilation is F345B2DC4FC093A3D01EA20A43E1F233.cache.js. Here i see:

function $registerRpc(this$static, rpcInterface, implementation){
  var rpcInterfaceId;
  rpcInterfaceId = $replaceAll(($ensureNamesAreInitialized(rpcInterface) , rpcInterface.typeName), '\\$', '.');
  !this$static.rpcImplementations && (this$static.rpcImplementations = {});
  null == this$static.rpcImplementations[rpcInterfaceId] && (this$static.rpcImplementations[rpcInterfaceId] = new ArrayList , undefined);
  castTo(this$static.rpcImplementations[rpcInterfaceId], 116).add_1(implementation);
}

Here there are two slashes. So in some way when the code is exported through Liferay one slash gets removed. Could it be the OSGi registration of the widgetset?

ctliv avatar Nov 27 '17 14:11 ctliv

I've found the reason of the issue and the solution.

Compiled widgetset has the double slash (so , GWT compilation is correct) but unzipping the bundle installed in Liferay (the jar in liferay_folder/osgi/modules) I found that the slash had become one only.

Tracing this back, I found who removes the slash: ...Maven!

Here was my pom.xml build section:

	<build>
		<resources>
			<resource>
				<directory>src/main/resources</directory>
				<filtering>true</filtering>
			</resource>
			<resource>
				<directory>src/main/webapp</directory>
				<filtering>true</filtering>
			</resource>
		</resources>
		...[omitted]
	</build>

Changing the second filtering attribute to "false" solved the issue:

	<build>
		<resources>
			<resource>
				<directory>src/main/resources</directory>
				<filtering>true</filtering>
			</resource>
			<resource>
				<directory>src/main/webapp</directory>
				<filtering>false</filtering>
			</resource>
		</resources>
		...[omitted]
	</build>

So, no bug in Vaadin or in Liferay. I thought that maven filtering would just substitute variables (${...}) but I was wrong...

The only doubt that still remains is that in standalone version (deploying war to Jetty) the issue did not occur even if filtering was "true". This is probably due to maven-war-plugin used in place of Apache Felix Maven Plugin, so this plugin may be involved somehow.

Thanks for giving me useful hints that pointed me toward the solution. Issue can be closed.

ctliv avatar Nov 27 '17 19:11 ctliv

Alright, good to know!

Artur- avatar Nov 28 '17 08:11 Artur-

Another potential reason for the differences between the standalone version and running through Liferay is that the standalone version might directly serve the gzipped version that the GWT compiler produces, which is probably not touched by any filtering. Liferay does instead have its own gzip handling and might therefore look at the uncompressed file which might be affected by the filtering?

Legioth avatar Nov 28 '17 08:11 Legioth

Thanks, Legioth. I also checked Liferay configuration, but if this were the cause the issue would apply to default widgetset as well. Instead, the plain com.vaadin.DefaultWidgetSet was working well.

For future reference, here is where I think the problem occurs. This is maven output for standalone (jetty) version:

[INFO] --- maven-war-plugin:3.0.0:war (default-war) @ vportlet ---
            ...
[INFO] Copying webapp resources [C:\...\workspace\vaadin-portlet-sample\src\main\webapp]
            ...
[INFO] Building war: C:\...\workspace\vaadin-portlet-sample\target\vportlet-0.0.4-SNAPSHOT.war

So, src/main/webapp folder is included in the war by maven-war-plugin.

But for an OSGi bundle it is maven-bundle-plugin that assembles the jar. In particular:

  1. It copies src/main/webapp content under target/classes (I checked that here the backslash is still double).

  2. It zips folder target/classes content in the jar (along with generated OSGI-INF). Here is where the double slash becomes single slash.

So, my conclusion is that maven-bundle-plugin alters javascript files upon zipping, but just when filtering is set to true.

ctliv avatar Nov 28 '17 14:11 ctliv