예제 #1
0
  /** Filters input HTML using specified policy as white list of allowed tags. */
  @SuppressWarnings("unchecked")
  private String filter(String inputHtml, String policyFileName) {
    String filteredHtml = "";
    if (!StringUtils.isBlank(inputHtml)) {
      if (policyFileName == null) {
        LOG.warn("Provided policy file name is null.");
        policyFileName = DEFAULT_ANTISAMY_POLICY_FILE;
      }

      AntiSamy htmlScanner = getHtmlScannerByPolicyFileName(policyFileName);
      if (htmlScanner != null) {
        CleanResults scanResults;
        try {
          scanResults = htmlScanner.scan(inputHtml);
          filteredHtml = scanResults.getCleanHTML();
          ArrayList<String> scannerErrors = scanResults.getErrorMessages();
          if (!CollectionUtils.isNullOrEmpty(scannerErrors)) {
            LOG.trace("HTML input contains erorrs (" + scannerErrors.size() + "):");
            int i = 1;
            for (String error : scannerErrors) {
              LOG.trace("    " + i + ") " + error);
              i++;
            }
          }
        } catch (ScanException ex) {
          throw new HtmlScannerException(ex);
        } catch (PolicyException ex) {
          throw new HtmlScannerException(ex);
        }
      }
    }

    return filteredHtml;
  }