[ALF-20856] Rhino 1.7R4 - ClassCastException: org.mozilla.javascript.ConsString cannot be cast to java.lang.String Created: 21-Feb-14  Updated: 18-Oct-15  Resolved: 19-Sep-14

Status: Closed
Project: Alfresco
Component/s: JavaScript API, Repository
Affects Version/s: 5.0
Fix Version/s: None
Security Level: external (External user)

Type: Bug Priority: Critical
Reporter: Peter Löfgren Assignee: Closed Issues
Resolution: Not a bug Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

mac os Maverick, Postgresql


Regression:
Regression
Date of First Response:
Resolution Time Custom Field: 30 weeks, 40 minutes, 33 seconds

 Description   

I tested 4.3 on HEAD. In my code I used pdftoolkit.
With the new rhino-js-1.7R4.har i get the error in my webscript using javascript:

Caused by: java.lang.ClassCastException: org.mozilla.javascript.ConsString cannot be cast to java.lang.String
	at org.alfresco.extension.pdftoolkit.repo.action.executer.PDFWatermarkActionExecuter.textAction(PDFWatermarkActionExecuter.java:434)
	at org.alfresco.extension.pdftoolkit.repo.action.executer.PDFWatermarkActionExecuter.doWatermark(PDFWatermarkActionExecuter.java:237)
	at org.alfresco.extension.pdftoolkit.repo.action.executer.PDFWatermarkActionExecuter.executeImpl(PDFWatermarkActionExecuter.java:181)

If I change back to rhino-js-1.6R7.jar the code works again.

The code is in javascript that cause the error
pdftoolkit.watermarkPDF(this.param);
where param is a javascript object

   this.param =  {
      "target" : "thenoderefgoeshere",
      "destination-folder" : null,
      "watermark-image" : null,
      "position" : "topright",
      "location-x" : "",
      "location-y" : "",
      "watermark-type" : "text",
      "watermark-pages" : "all",
      "watermark-depth" : "over",
      "watermark-text" : "Example",
      "watermark-font" : "Courier",
      "watermark-size" : "12"
   };


 Comments   
Comment by Peter Löfgren [ 21-Feb-14 ]

The pdftoolkit used is
https://code.google.com/p/alfresco-pdf-toolkit/

Comment by Peter Löfgren [ 21-Feb-14 ]

Found out what the issue was.

The call was to watermarkPDF(NativeObject obj)
The org.mozilla.javascript.NativeObject has changed substantially.

My code was using "mixed" strings (Java and javascript). I used java to format date in javascript

df = new java.text.SimpleDateFormat('yyyy-MM-dd');
theDate = df.format(new Date());
// This one doesn't work, creates ConsString https://github.com/mozilla/rhino/blob/master/src/org/mozilla/javascript/ConsString.java
// and this is not compatible with Rhino 1.7.R4 since it is a mixed string
// stamptext = 'Example text ' + theDate.toString();
stamptext = ('Example text ' + theDate).toString();
// The above toString() convert all internal representations to String
 
   this.param =  {
      "target" : null,
      "destination-folder" : null,
      "watermark-image" : null,
      "position" : "topright",
      "location-x" : "",
      "location-y" : "",
      "watermark-type" : "text",
      "watermark-pages" : "all",
      "watermark-depth" : "over",
      "watermark-text" : stamptext,
      "watermark-font" : "Courier",
      "watermark-size" : "12"
   };
   
pdftoolkit.watermarkPDF(this.param);

So the important thing here is to do
('javascript string' + aJavaStringObject).toString();
when passing strings from javascript to NativeObject.
I can see this happen since strings from node properties are lang.java.String

Comment by Peter Löfgren [ 04-Jul-14 ]

This status has been changed to need info. I'm not sure what kind of info you want, but I'll give it a try.
The class org.mozilla.javascript.NativeObject in the upgraded Rhino has changed substantially in how int handles Java to javascript object conversion (check the rhino source for this).

This means that any Java string to JavaScript string conversion and concatenating with a String (see examples in previous comment) may result in this error. Since it apparently doesn't happen in any of the Alfresco javascript webscripts code, it may only affect external developers, and this is a documentation issue, put it in the release notes.

Comment by Brian Remmington [ 16-Sep-14 ]

Tony, please would you take a look at the info that Peter has provided and see whether the docs need to mention something here?

Comment by Kevin Roast [X] (Inactive) [ 17-Sep-14 ]

>The class org.mozilla.javascript.NativeObject in the upgraded Rhino has changed substantially in how int handles Java to javascript object conversion (check the rhino source for this).

Correct Rhino now use an "optimized" string handling mechanism to improve performance - much like Google V8 does (in fact they copied the basic impl it would appear).

Developers will need to ensure that all concatenated strings are fully "toString()" before passing back to Java for template processing etc.
This is not an Alfresco issue but a subtle Rhino issue.

We need to use Rhino1.7 to provide newer JavaScript language support and particuarly for security reason to have JSON.parse() available (instead of the weaker eval() pattern). Rhino has always provided us with "entertainment" as developers as this is the latest fun. It would be great to move to Nashorn or similar for JavaScript processing in Alfresco but that is not a simple thing to do (and Java7 support...)

We will add it to the 5.0 release JS docs.

Comment by Tony Bedford [X] (Inactive) [ 19-Sep-14 ]

I've added the following text to the main web scripts page of the 5.0 docs (XML source):

"For Alfresco One 5.0 and Alfresco Community 5.0 the Rhino JavaScript engine was upgraded to version 1.7R4. This can in certain rare circumstances break existing web script code. This can happen where JavaScript-based web scripts pass a string to the model, prior to it being passed to the FreeMarker template. The string must be fully converted, using the toString() method. If you notice that previously working web scripts no longer work, then this possible reason should be investigated."

Comment by Tony Bedford [X] (Inactive) [ 19-Sep-14 ]

I would normally set to "Building" at this point to await a doc build, but I don't seem to be able to do that. So passing back to Brian...

Comment by Kevin Roast [X] (Inactive) [ 19-Sep-14 ]

Added to docs for Developer info in 5.0

FYI we found this affected approx 1% of our JS backed webscripts in production.

Comment by Richard Esplin [X] (Inactive) [ 16-Oct-15 ]

This issue has been marked as resolved since the last release of Alfresco Community Edition, so we are closing it.

Generated at Tue Jul 07 05:00:00 BST 2020 using JIRA 7.6.3#76005-sha1:8a4e38d34af948780dbf52044e7aafb13a7cae58.