Exemplo n.º 1
0
  /**
   * Creates a redirect rule object from the element node which should have turned up in the
   * configuration file.
   *
   * <p>It does full checking of the inspected element to see if the required XML grammar is okay.
   * In a perfect world this would not be necessary, I would turn on XML validation and a file with
   * bad grammar would be rejected up-front for not matching the schema or DTD. But as things are,
   * people are not always setting up a local catalog for frequently used files. This then means
   * that every restart of every server worldwide which uses this software would trigger a HTTP
   * request to my server in order to fetch the DTD for validation, thus generating traffic I at the
   * end must pay for. To avoid that, XML validation is turned off and the bad grammar we gracefully
   * ignore here. As the result, the user is not notified about problems, the filter simply won't
   * work if there are any, but there is nothing I can do about that right now.
   *
   * @param elem: A rule element from the configuration file.
   * @return a new redirect rule object or null if the element cannot be recognised.
   */
  protected RedirectRule loadRule(Element elem) {

    // Ignore if required attributes are missing
    if (!elem.hasAttribute("match")) return null;

    String action = elem.getTagName();

    if (action.equals("forward")) {
      ForwardAction rule = new ForwardAction();
      if (!elem.hasAttribute("target")) {
        return null;
      }
      rule.match = Pattern.compile(elem.getAttribute("match"));
      rule.target = elem.getAttribute("target");

      rule.localAddress =
          elem.hasAttribute("local-address") ? elem.getAttribute("local-address") : null;
      rule.remoteRange =
          elem.hasAttribute("remote-address") ? elem.getAttribute("remote-address") : null;

      return rule;
    }

    if (action.equals("redirect")) {
      RedirectAction rule = new RedirectAction();
      if (!elem.hasAttribute("target")) {
        return null;
      }
      rule.match = Pattern.compile(elem.getAttribute("match"));
      rule.target = elem.getAttribute("target");

      rule.localAddress =
          elem.hasAttribute("local-address") ? elem.getAttribute("local-address") : null;
      rule.remoteRange =
          elem.hasAttribute("remote-address") ? elem.getAttribute("remote-address") : null;

      rule.permanent =
          elem.hasAttribute("permanent") ? elem.getAttribute("permanent").equals("yes") : false;
      rule.encodeUrl =
          elem.hasAttribute("encode-url") ? elem.getAttribute("encode-url").equals("yes") : false;
      rule.entireUrl =
          elem.hasAttribute("entire-url") ? elem.getAttribute("entire-url").equals("yes") : false;
      rule.cache = elem.hasAttribute("cache") ? elem.getAttribute("cache") : null;

      return rule;
    }

    if (action.equals("ignore")) {
      IgnoreAction rule = new IgnoreAction();

      rule.match = Pattern.compile(elem.getAttribute("match"));

      rule.localAddress =
          elem.hasAttribute("local-address") ? elem.getAttribute("local-address") : null;
      rule.remoteRange =
          elem.hasAttribute("remote-address") ? elem.getAttribute("remote-address") : null;
      return rule;
    }
    return null;
  }