0

I have integrated oAuth2 (Facebook, LinkedIn, etc) with my XPages app to allow for authentication to easily add comments (response docs). When a user authenticates, it has to redirect to the facebook/linkedin page, then return to complete the document creation. I use the state variable to do this, and pass it in the querystring of the url. When the page reloads and sees the state variable, it calls a "beforePageLoad" event and creates the response document if the user authenticated and has the correct state document.

My problem is when there is already a state parameter in the querystring. I want to replace the value, not add it to the end. I use a solution here from stackOverflow by ellemayo called updateQueryStringParameter. When I call it from my beforePageLoads it runs, but never replaces the parameter, it only appends it to the end. I end up with ...&state=E5A&state=E5F

I have a feeling that it is in the line,

    return uri.replace(re, '$1' + key + "=" + value + '$2');

I can write the code using @ReplaceSubstring(), etc, but want to know if there are problems running regex in XPages SSJS. I read on Lotus.com that

A Regular Expression can be specified as Server-side, which uses the Java (java.util.regex) API or Client-side, which uses the browser JavaScript Regular Expression Engine. Client-side and Server-side Regular Expression syntax is similar, but there are differences that a user must be aware of.

Should I avoid regex in XPages SSJS ? I have it working extensively in client and in some field validations on the XPage itself.

Here is the call to the function:

  if(@Contains( qString,"state=")){
      qString=updateQueryStringParameter(qString, "state", linkDoc.getNoteID() );
}else{
      qString="?"+qString+"&state=" + linkDoc.getNoteID()
    }

the function:

  function updateQueryStringParameter(uri, key, value) {

  var re = new RegExp("([?&])" + key + "=.*?(&|$)", "i");
  var separator = uri.indexOf('?') !== -1 ? "&" : "?";
  if (uri.match(re)) {
  // I also tried --> if (re.test(uri)) {
    return uri.replace(re, '$1' + key + "=" + value + '$2');
  }
  else {
    return uri + separator + key + "=" + value;
  }
}
Community
  • 1
  • 1
carlo
  • 189
  • 1
  • 10
  • It is ok to use simple regex since Java and JS regex engines do have some common regex features (not all of them, the difference is really big). So, you prepend the query string param if it does not exist or update if it exists. You say, the `updateQueryStringParameter` does not work? – Wiktor Stribiżew Jan 13 '16 at 22:51
  • updateQueryStringParameter appends instead of replaces, and there are no error messages on the console. – carlo Jan 13 '16 at 22:55
  • Could you clarify if it works as expected if you hard-code the new URI in the beforePageLoad event? If yes, the problem is regex. If no, there's a different explanation, which I suspect could be the cause. – Paul Stephen Withers Jan 13 '16 at 23:00
  • Yes, I have tried to hard code it. But, I want to be able to pass the querystring with all values, which is why I need a replace solution. I expect I will just write an @function solution. – carlo Jan 13 '16 at 23:05
  • The [function seems to work](https://jsfiddle.net/1xjynu5g/), so it is not regex fault. – Wiktor Stribiżew Jan 13 '16 at 23:19
  • Thanks, stribizhev. Did you run it on Domino in an XPage Server Side Javascript? I can get it to work on a client, just not in XPage SSJS. Nevermind, I just saw that you ran it on jsFiddle. – carlo Jan 13 '16 at 23:34
  • Stribizhev, if I read the regex closer I would have seen that it must start with & or ?. My querystring didn't, thus it failed. – carlo Jan 14 '16 at 00:59

1 Answers1

1

It was not an XPage or Regex problem. I was using the querystring provided by Domino the excludes the "?" as part of the querystring. when I send "?" + qString to the function, it works. Regex needed to know where to start looking, thus it never found the start of the query string.

carlo
  • 189
  • 1
  • 10