package org.bedework.util.deployment;

import org.bedework.util.xml.XmlUtil;

import org.w3c.dom.Element;
import org.w3c.dom.Node;

import java.io.File;

/** Represent a web.xml file.
 *
 * @author douglm
 */
public class WebXml extends XmlFile {
  private final PropertiesChain props;

  public WebXml(final File webInf,
                final PropertiesChain props) throws Throwable {
    super(webInf, "web.xml", false);
    this.props = props;
  }

  public void update() throws Throwable {
    setConfigName();
    setTransportGuarantee();
    setSecurityDomain();
  }

  public void setConfigName() throws Throwable {
    findBwappname:chrome://web-developer/content/generated/view-response-headers.html
    for (final Element el : XmlUtil.getElementsArray(root)) {
      if (!"context-param".equals(el.getNodeName())) {
        continue findBwappname;
      }

      final Node pn = XmlUtil.getOneTaggedNode(el, "param-name");

      if (pn == null) {
        continue findBwappname;
      }

      final String pname = XmlUtil.getElementContent((Element)pn);

      if (!"bwappname".equals(pname)) {
        continue findBwappname;
      }

      propsReplaceContent(el, "param-value", props);
    }
  }

  public void setTransportGuarantee() throws Throwable {
    final Node sc = XmlUtil.getOneTaggedNode(root, "security-constraint");

    if (sc == null) {
      return;
    }

    final Node udc = XmlUtil.getOneTaggedNode(sc, "user-data-constraint");

    if (udc == null) {
      return;
    }

    propsReplaceContent((Element)udc, "transport-guarantee", props);
  }

  public void setSecurityDomain() throws Throwable {
    final Node n = XmlUtil.getOneTaggedNode(root, "login-config");

    if (n == null) {
      return;
    }

    propsReplaceContent((Element)n, "realm-name", props);
  }
}

@@ -123,16 +123,45 @@
 om.Node;
+%0Aimport org.w3c.dom.NodeList;
 %0A%0Aimport
@@ -667,75 +667,8 @@
 ame:
-chrome://web-developer/content/generated/view-response-headers.html
 %0A   
@@ -1242,113 +1242,229 @@
 Node
- sc = XmlUtil.getOneTaggedNode(root, %22security-constraint%22);%0A%0A    if (sc == null) %7B%0A      return
+List children = root.getChildNodes();%0A%0A    for (int i = 0; i %3C children.getLength(); i++) %7B%0A      final Node n = children.item(i);%0A%0A      if (!%22security-constraint%22.equals(n.getNodeName())) %7B%0A        continue
 ;%0A    
+ 
+ 
 %7D%0A%0A
+  
     
@@ -1509,10 +1509,9 @@
 ode(
-sc
+n
 , %22u
@@ -1534,24 +1534,26 @@
 int%22);%0A%0A    
+  
 if (udc == n
@@ -1561,39 +1561,47 @@
 ll) %7B%0A      
-return;%0A
+  continue;%0A  
     %7D%0A%0A
+  
     propsRep
@@ -1653,24 +1653,30 @@
 e%22, props);%0A
+    %7D%0A
   %7D%0A%0A  publi

Fix post build - handle multiple security-constraint sections.