AttachmentProcessingPart.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.Iterator;
  23. import java.util.List;

  24. import javax.xml.soap.AttachmentPart;

  25. import org.openspcoop2.message.OpenSPCoop2SoapMessage;
  26. import org.openspcoop2.security.message.constants.SecurityConstants;

  27. /**
  28.  * AttachmentProcessingPart
  29.  *
  30.  * @author Andrea Poli (apoli@link.it)
  31.  * @author $Author$
  32.  * @version $Rev$, $Date$
  33.  */
  34. public class AttachmentProcessingPart extends ProcessingPart<Integer, List<AttachmentPart>>{

  35.     private boolean allAttachments;
  36.    
  37.     public AttachmentProcessingPart(boolean content) {
  38.         this.allAttachments = true;
  39.         this.content = content;
  40.     }

  41.     public AttachmentProcessingPart(Integer index, boolean content) {
  42.         this.allAttachments = false;
  43.         this.part = index;
  44.         this.content = content;
  45.     }

  46.     public boolean isAllAttachments() {
  47.         return this.allAttachments;
  48.     }

  49.     public void setAllAttachments(boolean allAttachments) {
  50.         this.allAttachments = allAttachments;
  51.     }

  52.     @Override
  53.     public List<AttachmentPart> getOutput(OpenSPCoop2SoapMessage message) throws Exception {

  54.         Iterator<?> it = message.getAttachments();
  55.         if(it.hasNext()==false){
  56.             throw new Exception("Property "+SecurityConstants.SIGNATURE_PARTS+ " o "+SecurityConstants.ENCRYPTION_PARTS+
  57.                     " contiene la richiesta di firmare o cifrare attachments, ma il messaggio non ne contiene");
  58.         }

  59.         int indexAllegatoEncrypt = 1;
  60.         List<AttachmentPart> lst = new ArrayList<AttachmentPart>();
  61.         while (it.hasNext()) {
  62.             AttachmentPart ap = (AttachmentPart) it.next();

  63.             if(this.allAttachments){
  64.                 lst.add(ap);
  65.             }else{
  66.                 if(this.getPart().equals(indexAllegatoEncrypt)) {
  67.                     lst.add(ap);
  68.                 }
  69.                 indexAllegatoEncrypt++;
  70.             }
  71.         }
  72.         return lst;
  73.     }
  74. }