5

I Would like to parse the static URL in Apex.

I am looking for a method that can take the URL string as a parameter and Mask the "Parameter values" of the given URL.

Example URL Format

URL:`https://goggle.ca?firstName=papa&lastName=john&birthDt=1945-06-01'`

Sample code :

static string sanitizeUrl(String url) {
        String returnStr ;
        Set<String> strSet = new Set<String>{'firstName', 'lastName'};
        for(String key : strSet) {
            system.debug(url.param(key));
        if(!String.isBlank(url.param(key))) 
         returnStr = returnStr.replaceAll(url.param(key),'Strip it');
        }
        return returnStr;
    }

I have gone through the Salesforce Documentation : Salesforce Documentation :URL Class

But I couldn't use the methods on the static URL.

2 Answers 2

6

In addition to battery.cord's excellent answer, I suspect you're actually looking to sanitize all parameters, so you might want a method like this:

static String sanitizeUrl(String urlString, String mask) {
  ApexPages.PageReference ref = new ApexPages.PageReference(urlString);
  Map<String, String> params = ref.getParameters();
  for(String key: params.keySet()) {
    params.put(key, mask);
  }
  return ref.getUrl();
}

If it's a list of parameters to redact:

static String sanitizeUrl(String urlString, String mask, Set<String> maskedParams) {
  ApexPages.PageReference ref = new ApexPages.PageReference(urlString);
  Map<String, String> params = ref.getParameters();
  for(String key: params.keySet()) {
    if(maskedParams.contains(key)) {
      params.put(key, mask);
    }
  }
  return ref.getUrl();
}

Other variations are also possible, depending on your intent; the main point is to use PageReference to get the values from the query string, manipulate them, and then return the modified URL.

0
5

You can't use param or other methods from PageReference or Url on this variable, because its actually a String. You can only use String methods on it.

You'll need to create a PageReference from the String passed to your function. Its really simple, you just pass the string over to the constructor.

You'll need to use getParameters().get() instead of param() (since thats not a method). You've got a few other issues (such as never defining a value for returnStr, but you try to set it to its own value, after calling a replace on it - this'll fail with a null ref), but I see what you're trying to do. I don't think replacing the key names will work - Maps have to have unique names, and it looks like you're just replacing the key, but leaving the value - creating an illegal url/illegal map in code.

My version below just removes the key and the value from the parameter map, if the key is in the url.

Heres a rewrite of your code, without the issues above. You can run this in the developer console to verify the result is as expected (including before/after formatting examples is really helpful for answerers - im guessing this is the format you're looking for, but Im unsure):

static string sanitizeUrl(String input) {
    PageReference url = new PageReference(input);

    Set<String> keysToRemove = new Set<String>{'firstName', 'lastName'};

        // See @sfdcfox's comment 
        /**for(String key:keysToRemove) {
            // Remove key & value 
            if (url.getParameters().containsKey(key)) {
                url.getParameters().remove(key);
            }
        }**/

    url.getParameters().keySet().removeAll(keysToRemove);

    return url.getUrl(); 
}

String result = sanitizeUrl('https://goggle.ca?firstName=papa&lastName=john&birthDt=1945-06-01');
String expectedResult = 'https://goggle.ca?birthDt=1945-06-01';

System.assertEquals(expectedResult, result); // true 
2
  • 3
    Minor nitpick: calling remove on a key that doesn't exist doesn't throw an exception, so really no need to check for it first. Also, there's actually a nifty trick I learned ages ago: url.getParameters().keySet().removeAll(keysToRemove); is actually a one-liner that doesn't require a for loop at all.
    – sfdcfox
    Jun 29, 2018 at 17:58
  • Thats real nifty! Ill be sure to use that going forward. Making an edit to the code now. Jun 29, 2018 at 18:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .