1

I am building a multilingual application and I am storing the value of the latest language used in a cookie.

When the user opens up the application, the sessionScope variable is not set and the code will look for the cookie value and reload the page if not in the proper Locale.

I am getting the "com.ibm.xsp.acl.RedirectSignal" warning each time the page is reloaded in the proper locale, and I'd lilke to avoid it.

My code is located in the beforeRenderResponse event of the ApplicationLayout control I am using in the application, and looks like this:

if(!sessionScope.lang) {  //this only happens when the page is opened in browser for the first time
    var lang = context.getUrlParameter("lang").toLowerCase();
    if(!!lang) {
            sessionScope.lang = lang.toUpperCase();
            //set cookie for next time the site is opened by user
            setCookie("lang", lang.toUpperCase(), 30);
            context.setLocale(new Locale(lang.toLowerCase()));
    } else {
        //set language from cookie
        var lang = getLangFromCookie();
        if(!!lang) {
            sessionScope.lang = lang;
            context.setLocale(new Locale(lang.toLowerCase()));
        } else {
            sessionScope.lang = Appconfig.defaultLang;
            context.setLocale(new Locale(Appconfig.defaultLang.toLowerCase()));
        }
    }

    //need to trpa the redirect error thrown here, as it is just a warning - avoid filling log with this
    //importPackage(com.ibm.xsp.acl.RedirectSignal);
    importPackage(com.ibm.xsp.acl.RedirectSignal);

    try {
        //reload the page so Locale value kicks in
        context.reloadPage();
    } catch (RedirectSignal rs) { 
        //just ignore
    }
}

Even though I added the importPackage line, I am still getting an error when saving the code (it is in a script Library):

Encountered " "rs"" at line...

How can I make this work?

Thanks :D

Per Henrik Lausten
  • 21,331
  • 3
  • 29
  • 76
Ben Dubuc
  • 523
  • 3
  • 14

2 Answers2

2

The catch is a Throwable, not a RedirectSignal. Here's the code I use in my handleException function

try {
    try {
        if (t instanceof RedirectSignal) {
            return;
        }
        if ("javax.faces.el.EvaluationException".equals(t.getClass().getName())) {
            if (t.getCause() instanceof RedirectSignal) {
                return;
            }
        }
    } catch (Throwable e) {
        // Error checking cause, skip
    }
    // Now log the error
} catch (Throwable e) {
    e.printStackTrace();
}
Paul Stephen Withers
  • 15,699
  • 1
  • 15
  • 33
  • How can I do that in SSJS? I can't seem to use the Throwable t as the catch parameter. – Ben Dubuc May 28 '15 at 13:18
  • 1
    The code I gave was Java - sorry, I don't write much SSJS these days. Using it from SSJS is problematic. SSJS doesn't throw a Java Exception or Throwable. It's a variety of Java classes, as you'll see if you look at the processUncaughtException method in XPages OpenLog Logger's PhaseListener. The catch block won't need to specify Throwable, but I'm not sure if printStackTrace() and "instanceof" are available or relevant. getClass().getName() would work. Tommy's code will work for a specific call to context.reloadPage(). – Paul Stephen Withers May 29 '15 at 09:19
  • Alternatively, you may want to crerate a generic SSJS method where you pass the page to redirect to. If no page is passed, then call context.reloadPage(), otherwise context.redirectToPage(passedVal) – Paul Stephen Withers May 29 '15 at 09:20
1

In SSJS you don't define the type/class of the exception in the catch block. Since you're not doing anything with the "exception", there's also no need to import the RedirectSignal class.

try {      
    context.reloadPage();
} catch ( redirectSignal ) { 
    // Ignore redirect signal "exception"
}
Tommy Valand
  • 868
  • 6
  • 10
  • Works like a charm, I don't see the redirect messages in the log anymore. Not sure why I got this message, but from what I have read, it seems it might be when a page redirects to itself. If that's the case then this was an expected behavior as I am indeed reloading the same page, after changing the Locale, so the page reloads in the proper language. Can't live with the browser's Locale only, I need the user (or a URL param or even a cookie) to have the choice of his Locale, independant of the browser. – Ben Dubuc May 29 '15 at 18:48