SOAPHeader.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.protocol.modipa.utils;

  21. import java.io.BufferedReader;
  22. import java.io.StringReader;
  23. import java.util.ArrayList;
  24. import java.util.List;

  25. import org.openspcoop2.protocol.sdk.ProtocolException;

  26. /**
  27.  * SOAPHeader
  28.  *
  29.  * @author Poli Andrea (apoli@link.it)
  30.  * @author $Author$
  31.  * @version $Rev$, $Date$
  32.  */
  33. public class SOAPHeader {

  34.     private String localName;
  35.     private String namespace;
  36.    
  37.     public String getLocalName() {
  38.         return this.localName;
  39.     }
  40.     public String getNamespace() {
  41.         return this.namespace;
  42.     }
  43.    
  44.     public SOAPHeader(String namespace, String localName) {
  45.         this.namespace = namespace;
  46.         this.localName = localName;
  47.     }
  48.    
  49.     public static void remove(List<SOAPHeader> list, String namespace, String localName) {
  50.         if(list!=null && !list.isEmpty()) {
  51.             boolean find = true;
  52.             while (find) {
  53.                 find = false;
  54.                 int i=0;
  55.                 for (; i < list.size(); i++) {
  56.                     SOAPHeader hdr = list.get(i);
  57.                     if(hdr.getLocalName().equals(localName) && hdr.getNamespace().equals(namespace)) {
  58.                         find = true;
  59.                         break;
  60.                     }
  61.                 }
  62.                 if(find) {
  63.                     list.remove(i);
  64.                 }
  65.             }
  66.         }
  67.     }
  68.    
  69.     public static List<SOAPHeader> parse(String value) throws ProtocolException{
  70.         List<SOAPHeader> list = new ArrayList<SOAPHeader>();
  71.         if(value!=null && value.length()>0) {
  72.             try(
  73.                     StringReader sr = new StringReader(value);
  74.                     BufferedReader reader = new BufferedReader(sr);){
  75.                 String line = reader.readLine();
  76.                 while (line != null) {
  77.                     try {
  78.                         line = line.trim();
  79.                         if(line.startsWith("#")) {
  80.                             continue;
  81.                         }
  82.                         if(!line.startsWith("{")){
  83.                             throw new Exception("Wrong format (expected '{namespace}localName') in row '"+line+"'");
  84.                         }
  85.                         if(!line.contains("}")){
  86.                             throw new Exception("Wrong format (expected '{namespace}localName') in row '"+line+"'");
  87.                         }
  88.                         StringBuilder sbNamespace= new StringBuilder();
  89.                         StringBuilder sbLocalname= new StringBuilder();
  90.                         boolean localName = false;
  91.                         for (int i = 1; i < line.length(); i++) {
  92.                             char c = line.charAt(i);
  93.                             if(c == '}') {
  94.                                 localName = true;
  95.                                 continue;
  96.                             }
  97.                             if(localName) {
  98.                                 sbLocalname.append(c);
  99.                             }
  100.                             else {
  101.                                 sbNamespace.append(c);
  102.                             }
  103.                         }
  104.                         if(sbNamespace.length()<=0) {
  105.                             throw new Exception("Wrong format (expected '{namespace}localName') in row '"+line+"' (namespace not found)");
  106.                         }
  107.                         if(sbNamespace.toString().contains("{") || sbNamespace.toString().contains("}")) {
  108.                             throw new Exception("Wrong format (expected '{namespace}localName') in row '"+line+"'");
  109.                         }
  110.                         if(sbLocalname.length()<=0) {
  111.                             throw new Exception("Wrong format (expected '{namespace}localName') in row '"+line+"' (localName not found)");
  112.                         }
  113.                         if(sbLocalname.toString().contains("{") || sbLocalname.toString().contains("}")) {
  114.                             throw new Exception("Wrong format (expected '{namespace}localName') in row '"+line+"'");
  115.                         }
  116.                         SOAPHeader hdr = new SOAPHeader(sbNamespace.toString(), sbLocalname.toString());
  117.                         list.add(hdr);
  118.                        
  119.                     }finally {
  120.                         line = reader.readLine();
  121.                     }
  122.                 }
  123.             }catch(Exception e) {
  124.                 throw new ProtocolException(e.getMessage(), e);
  125.             }
  126.         }
  127.         return list;
  128.     }
  129. }