AttachmentsConfigReaderUtils.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 java.util.Map;

  24. import org.openspcoop2.message.soap.reference.AttachmentReference;
  25. import org.openspcoop2.message.soap.reference.Reference;
  26. import org.openspcoop2.security.message.MessageSecurityContext;
  27. import org.openspcoop2.security.message.constants.SecurityConstants;

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

  36.     public static List<String> getListCIDAttachmentsForSecurity(MessageSecurityContext wssContext) throws Exception{
  37.         List<String> cidSecurity = new ArrayList<>();
  38.         if(wssContext.getReferences()!=null && wssContext.getReferences().size()>0){
  39.             for (Reference reference : wssContext.getReferences()) {
  40.                 if(reference instanceof AttachmentReference){
  41.                     AttachmentReference ar = (AttachmentReference)reference;
  42.                     //System.out.println("ADD TYPE-REF["+ar.getType()+"] ["+ar.getReference()+"]");
  43.                     cidSecurity.add(ar.getReference());
  44.                 }
  45.             }
  46.         }
  47.         return cidSecurity;
  48.     }
  49.    
  50.     public static AttachmentProcessingPart getSecurityOnAttachments(MessageSecurityContext wssContext) throws Exception{

  51.         AttachmentProcessingPart ap = null;

  52.         Map<String,Object> wssProperties = null;
  53.         if(wssContext.isFunctionAsClient())
  54.             wssProperties = wssContext.getOutgoingProperties();
  55.         else
  56.             wssProperties = wssContext.getIncomingProperties();
  57.         if (wssProperties != null && wssProperties.size() > 0) {
  58.             if(wssProperties.containsKey(SecurityConstants.ENCRYPTION_PARTS)){
  59.                 String value = (String) wssProperties.get(SecurityConstants.ENCRYPTION_PARTS);
  60.                 List<ProcessingPart<?,?>> listProcessingParts = ProcessingPartUtils.getEncryptionInstance().getProcessingParts(value);
  61.                 AttachmentProcessingPart apFound = findAttachmentProcessingPart(listProcessingParts, ap, SecurityConstants.ENCRYPTION_PARTS);
  62.                 if(ap==null){
  63.                     ap = apFound;
  64.                 }
  65.             }
  66.             else if(wssProperties.containsKey(SecurityConstants.SIGNATURE_PARTS)){
  67.                 String value = (String) wssProperties.get(SecurityConstants.SIGNATURE_PARTS);
  68.                 List<ProcessingPart<?,?>> listProcessingParts = ProcessingPartUtils.getEncryptionInstance().getProcessingParts(value);
  69.                 AttachmentProcessingPart apFound = findAttachmentProcessingPart(listProcessingParts, ap, SecurityConstants.SIGNATURE_PARTS);
  70.                 if(ap==null){
  71.                     ap = apFound;
  72.                 }
  73.             }
  74.         }
  75.        
  76.         if(ap==null) {
  77.             if(wssContext.getManualAttachmentsEncryptPart()!=null) {
  78.                 List<ProcessingPart<?,?>> listProcessingParts = ProcessingPartUtils.getEncryptionInstance().getProcessingParts(wssContext.getManualAttachmentsEncryptPart());
  79.                 AttachmentProcessingPart apFound = findAttachmentProcessingPart(listProcessingParts, ap, SecurityConstants.ENCRYPTION_PARTS);
  80.                 if(ap==null){
  81.                     ap = apFound;
  82.                 }
  83.             }
  84.             else if(wssContext.getManualAttachmentsSignaturePart()!=null) {
  85.                 List<ProcessingPart<?,?>> listProcessingParts = ProcessingPartUtils.getEncryptionInstance().getProcessingParts(wssContext.getManualAttachmentsSignaturePart());
  86.                 AttachmentProcessingPart apFound = findAttachmentProcessingPart(listProcessingParts, ap, SecurityConstants.SIGNATURE_PARTS);
  87.                 if(ap==null){
  88.                     ap = apFound;
  89.                 }
  90.             }
  91.         }

  92.         return ap;
  93.     }

  94.     public static  AttachmentProcessingPart findAttachmentProcessingPart(List<ProcessingPart<?,?>> listProcessingParts, AttachmentProcessingPart ap, String parts) throws Exception{
  95.         AttachmentProcessingPart apFound = null;
  96.         boolean found = false;
  97.         for (ProcessingPart<?, ?> processingPart : listProcessingParts) {
  98.             if(processingPart instanceof AttachmentProcessingPart){
  99.                 if(found){
  100.                     throw new Exception("Only one configuration for attachments is allowed in "+parts);
  101.                 }
  102.                 apFound = (AttachmentProcessingPart) processingPart;
  103.                 if(ap!=null){
  104.                     // trovato anche in signature
  105.                     if(ap.isAllAttachments()){
  106.                         if(!apFound.isAllAttachments()){
  107.                             throw new Exception("The configuration of signature and encryption for the attachments must be the same (found difference in "+SecurityConstants.ENCRYPTION_PARTS
  108.                                     +" and "+SecurityConstants.SIGNATURE_PARTS+")");
  109.                         }
  110.                     }
  111.                     else {
  112.                         int apIntValue = ap.getPart().intValue();
  113.                         if(apFound.isAllAttachments() || apIntValue!=apFound.getPart().intValue() ){
  114.                             throw new Exception("The configuration of signature and encryption for the attachments must be the same (found difference in "+SecurityConstants.ENCRYPTION_PARTS
  115.                                     +" and "+SecurityConstants.SIGNATURE_PARTS+")");
  116.                         }
  117.                     }
  118.                 }

  119.                 found = true;
  120.             }
  121.         }
  122.         return apFound;
  123.     }

  124. }