ElementProcessingPart.java

  1. /*
  2.  * GovWay - A customizable API Gateway
  3.  * https://govway.org
  4.  *
  5.  * Copyright (c) 2005-2025 Link.it srl (https://link.it).
  6.  *
  7.  * This program is free software: you can redistribute it and/or modify
  8.  * it under the terms of the GNU General Public License version 3, as published by
  9.  * the Free Software Foundation.
  10.  *
  11.  * This program is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  * GNU General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU General Public License
  17.  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  18.  *
  19.  */
  20. package org.openspcoop2.security.message.utils;

  21. import javax.xml.namespace.QName;

  22. import org.openspcoop2.message.OpenSPCoop2SoapMessage;
  23. import org.w3c.dom.Element;
  24. import org.w3c.dom.NodeList;

  25. /**
  26.  * ElementProcessingPart
  27.  *
  28.  * @author Andrea Poli (apoli@link.it)
  29.  * @author $Author$
  30.  * @version $Rev$, $Date$
  31.  */
  32. public class ElementProcessingPart extends ProcessingPart<QName, Element>{
  33.    
  34.     public ElementProcessingPart(QName qname, boolean content) {
  35.         this.part = qname;
  36.         this.content = content;
  37.     }

  38.     private static Element getFirstChild(Element elem, String ns, String localName) throws Exception {
  39.         if (elem.getLocalName() != null && elem.getLocalName().equals(localName) &&
  40.             ((ns == null && elem.getNamespaceURI() == null) ||
  41.             (ns != null && ns.equals(elem.getNamespaceURI())))) {
  42.             return elem;
  43.         }

  44.         NodeList nl = elem.getElementsByTagNameNS(ns, localName);
  45.         if (nl != null && nl.getLength() == 1 && nl.item(0) instanceof Element) {
  46.             return (Element) nl.item(0);
  47.         } else {
  48.             throw new Exception("Expected element : {" + ns + "}" +
  49.                 localName + " not found as a child of : " + elem.toString());
  50.         }
  51.     }

  52.     @Override
  53.     public Element getOutput(OpenSPCoop2SoapMessage message) throws Exception {
  54.         return getFirstChild(message.getSOAPPart().getEnvelope().getOwnerDocument().getDocumentElement(), this.part.getNamespaceURI(), this.part.getLocalPart());
  55.     }


  56. }