code: public static String asValidHref(String urlString) {
          try {
              return new URL(urlString).toExternalForm();
          } catch (MalformedURLException e) {
              LOG.debug("Malformed url - {}, empty string will be returned", urlString);
              return "";
          }
      }
docs: Method to filter invalid url for XSS. This url can be inserted to href safely
  
  @param urlString unsafe url
  
  @return safe url