ProcessingPartUtils.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 java.util.ArrayList;
  22. import java.util.List;

  23. import javax.xml.namespace.QName;

  24. import org.openspcoop2.security.message.constants.SecurityConstants;

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


  33.     private String processingPartsConstant;
  34.     private String processingPartsElementConstant;
  35.     private String processingPartsContentConstant;
  36.     private String processingPartsNamespaceAttachConstant;
  37.     private String processingPartsCompleteConstant;

  38.     public ProcessingPartUtils(String processingPartsConstant,
  39.             String processingPartsElementConstant,
  40.             String processingPartsContentConstant,
  41.             String processingPartsNamespaceAttachConstant,
  42.             String processingPartsCompleteConstant) {

  43.         this.processingPartsConstant = processingPartsConstant;
  44.         this.processingPartsElementConstant = processingPartsElementConstant;
  45.         this.processingPartsContentConstant = processingPartsContentConstant;
  46.         this.processingPartsNamespaceAttachConstant = processingPartsNamespaceAttachConstant;
  47.         this.processingPartsCompleteConstant = processingPartsCompleteConstant;
  48.     }

  49.     public List<ProcessingPart<?,?>> getProcessingParts(String processingPartString) throws Exception {
  50.         List<ProcessingPart<?,?>> lst = new ArrayList<ProcessingPart<?,?>>();
  51.         String[] split = processingPartString.split(";");
  52.         for (int i = 0; i < split.length; i++) {
  53.             String[]split2 = split[i].trim().split("}");
  54.             String tipo = split2[0].trim().substring(1); // Element o Content
  55.             if("".equals(tipo)){
  56.                 // caso speciale wss4j {}cid:Attachments
  57.                 tipo = SecurityConstants.PART_CONTENT;
  58.             }
  59.            
  60.             if(!this.processingPartsContentConstant.equals(tipo) && !this.processingPartsElementConstant.equals(tipo)){
  61.                 throw new Exception(this.processingPartsConstant+"["+i+"] possiede un tipo non supportato (supportati:"+this.processingPartsContentConstant
  62.                         +","+this.processingPartsElementConstant+"): "+tipo);
  63.             }
  64.            
  65.             if(split2.length==3){
  66.                 String namespace = split2[1].trim().substring(1);
  67.                 if(this.processingPartsNamespaceAttachConstant.equals(namespace)){
  68.                     String indice = split2[2].trim().substring(1);
  69.                     lst.add(getAttachmentProcessingPart(tipo, indice, i));
  70.                 } else {
  71.                     String nome = split2[2].trim();
  72.                     lst.add(getProcessingPart(tipo, namespace, nome));
  73.                 }
  74.             }
  75.             else {
  76.                 // caso speciale wss4j {}cid:Attachments ?
  77.                 if(SecurityConstants.CID_ATTACH_WSS4J.equalsIgnoreCase(split2[1].trim())){
  78.                     lst.add(getAttachmentProcessingPart(tipo, SecurityConstants.ATTACHMENT_INDEX_ALL, i));
  79.                 }
  80.                 else{
  81.                     throw new Exception("Part ["+split[i]+"] with wrong format");
  82.                 }
  83.             }
  84.         }
  85.        
  86.         return lst;
  87.     }

  88.     private ElementProcessingPart getProcessingPart(String tipo, String namespace, String nome) throws Exception {
  89.         QName elemento = new QName(namespace,nome);
  90.         return new ElementProcessingPart(elemento, this.processingPartsContentConstant.equals(tipo));
  91.     }

  92.     private AttachmentProcessingPart getAttachmentProcessingPart(String tipo, String indice, int indexPart) throws Exception {

  93.         boolean complete = this.processingPartsCompleteConstant.equals(tipo);
  94.         if(SecurityConstants.ATTACHMENT_INDEX_ALL.equals(indice) || "".equals(indice)){
  95.             return new AttachmentProcessingPart(complete);
  96.         } else{
  97.             int indexAllegato = -1;
  98.             try{
  99.                 indexAllegato = Integer.parseInt(indice);
  100.                 return new AttachmentProcessingPart(indexAllegato, complete);
  101.             }catch(NumberFormatException e){
  102.                 throw new Exception("Property "+this.processingPartsConstant+ "["+indexPart+"] (Attach) con un indice ["+indice+"] che non risulta essere ne un numero intero ne un carattere speciale", e);
  103.             }

  104.         }

  105.     }
  106.    
  107.     public static ProcessingPartUtils getSignatureInstance() {
  108.         return new ProcessingPartUtils(SecurityConstants.SIGNATURE_PARTS, SecurityConstants.SIGNATURE_PART_ELEMENT, SecurityConstants.SIGNATURE_PART_CONTENT, SecurityConstants.SIGNATURE_NAMESPACE_ATTACH, SecurityConstants.SIGNATURE_PART_COMPLETE);
  109.     }
  110.    
  111.     public static ProcessingPartUtils getEncryptionInstance() {
  112.         return new ProcessingPartUtils(SecurityConstants.ENCRYPTION_PARTS, SecurityConstants.ENCRYPTION_PART_ELEMENT, SecurityConstants.ENCRYPTION_PART_CONTENT, SecurityConstants.ENCRYPTION_NAMESPACE_ATTACH, SecurityConstants.ENCRYPTION_PART_COMPLETE);
  113.     }
  114.    
  115. }