MimeMultipart.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.utils.mime;

  21. import java.io.InputStream;
  22. import java.io.OutputStream;
  23. import java.util.Enumeration;

  24. import javax.mail.BodyPart;
  25. import javax.mail.internet.InternetHeaders;
  26. import javax.mail.internet.MimeBodyPart;
  27. import javax.mail.util.ByteArrayDataSource;

  28. import org.openspcoop2.utils.UtilsException;


  29. /**
  30.  * MimeMultipartContent
  31.  *
  32.  * @author Andrea Poli (poli@link.it)
  33.  * @author $Author$
  34.  * @version $Rev$, $Date$
  35.  */
  36. public class MimeMultipart {
  37.    
  38.     private javax.mail.internet.MimeMultipart mimeMultipartObject = null;
  39.    
  40.    
  41.     public MimeMultipart() throws UtilsException{
  42.         try{
  43.             this.mimeMultipartObject = new javax.mail.internet.MimeMultipart();
  44.         }catch(Exception e){
  45.             throw new UtilsException(e.getMessage(),e);
  46.         }
  47.     }
  48.    
  49.     public MimeMultipart(String subType) throws UtilsException{
  50.         try{
  51.             // multipart/<subType>
  52.             this.mimeMultipartObject = new javax.mail.internet.MimeMultipart(subType);
  53.         }catch(Exception e){
  54.             throw new UtilsException(e.getMessage(),e);
  55.         }
  56.     }
  57.    
  58.     public MimeMultipart(InputStream is, String contentType) throws UtilsException{
  59.         try{
  60.             javax.activation.DataSource ds = new ByteArrayDataSource(is, contentType);
  61.             this.mimeMultipartObject = new javax.mail.internet.MimeMultipart(ds);
  62.         }catch(Exception e){
  63.             throw new UtilsException(e.getMessage(),e);
  64.         }
  65.     }
  66.    
  67.    
  68.    
  69.     /* Attachments REST */
  70.    
  71.     public void addBodyPart(BodyPart bodyPart) throws UtilsException{
  72.         try{
  73.             this.mimeMultipartObject.addBodyPart(bodyPart);
  74.         }catch(Exception e){
  75.             throw new UtilsException(e.getMessage(),e);
  76.         }
  77.     }
  78.     public void addBodyPart(BodyPart bodyPart, int index) throws UtilsException{
  79.         try{
  80.             this.mimeMultipartObject.addBodyPart(bodyPart, index);
  81.         }catch(Exception e){
  82.             throw new UtilsException(e.getMessage(),e);
  83.         }
  84.     }
  85.    
  86.     public BodyPart createBodyPart(java.io.InputStream is) throws UtilsException{
  87.         try{
  88.             return new MimeBodyPart(is);
  89.         }catch(Exception e){
  90.             throw new UtilsException(e.getMessage(),e);
  91.         }
  92.     }
  93.     public BodyPart createBodyPart(InternetHeaders headers, byte[] content) throws UtilsException{
  94.         try{
  95.             return new MimeBodyPart(headers,content);
  96.         }catch(Exception e){
  97.             throw new UtilsException(e.getMessage(),e);
  98.         }
  99.     }
  100.    
  101.     public int countBodyParts() throws UtilsException{
  102.         try{
  103.             return this.mimeMultipartObject.getCount();
  104.         }catch(Exception e){
  105.             throw new UtilsException(e.getMessage(),e);
  106.         }
  107.     }
  108.    
  109.     public BodyPart getBodyPart(int index) throws UtilsException{
  110.         try{
  111.             return this.mimeMultipartObject.getBodyPart(index);
  112.         }catch(Exception e){
  113.             throw new UtilsException(e.getMessage(),e);
  114.         }
  115.     }
  116.     public BodyPart getBodyPart(String contentID) throws UtilsException{
  117.         try{
  118.             return this.mimeMultipartObject.getBodyPart(contentID);
  119.         }catch(Exception e){
  120.             throw new UtilsException(e.getMessage(),e);
  121.         }
  122.     }
  123.    
  124.     public void removeBodyPart(int index) throws UtilsException{
  125.         try{
  126.             this.mimeMultipartObject.removeBodyPart(index);
  127.         }catch(Exception e){
  128.             throw new UtilsException(e.getMessage(),e);
  129.         }
  130.     }
  131.     public void removeBodyPart(String contentID) throws UtilsException{
  132.         try{
  133.             BodyPart bodyPart = this.getBodyPart(contentID);
  134.             if(bodyPart!=null){
  135.                 this.mimeMultipartObject.removeBodyPart(bodyPart);
  136.             }
  137.         }catch(Exception e){
  138.             throw new UtilsException(e.getMessage(),e);
  139.         }
  140.     }
  141.     public void removeBodyPart(BodyPart bodyPart) throws UtilsException{
  142.         try{
  143.             this.mimeMultipartObject.removeBodyPart(bodyPart);
  144.         }catch(Exception e){
  145.             throw new UtilsException(e.getMessage(),e);
  146.         }
  147.     }

  148.     public void writeTo(OutputStream os) throws UtilsException{
  149.         try{
  150.             this.mimeMultipartObject.writeTo(os);
  151.         }catch(Exception e){
  152.             throw new UtilsException(e.getMessage(),e);
  153.         }
  154.     }
  155.    
  156.     public String getContentType() throws UtilsException{
  157.         try{
  158.             return this.mimeMultipartObject.getContentType();
  159.         }catch(Exception e){
  160.             throw new UtilsException(e.getMessage(),e);
  161.         }
  162.     }
  163.    
  164.     private static final String CONTENT_ID = "Content-ID"; // Non e' utilizzabile HttpConstants per problemi di dipendenza di compilazione
  165.     private static final String CONTENT_LOCATION = "Content-Location"; // Non e' utilizzabile HttpConstants per problemi di dipendenza di compilazione
  166.     private static final String CONTENT_DISPOSITION = "Content-Disposition"; // Non e' utilizzabile HttpConstants per problemi di dipendenza di compilazione
  167.    
  168.     public String getContentID(BodyPart bodyPart) throws UtilsException{
  169.         return this.getHeaderValue(CONTENT_ID, bodyPart);
  170.     }
  171.    
  172.     public String getContentLocation(BodyPart bodyPart) throws UtilsException{
  173.         return this.getHeaderValue(CONTENT_LOCATION, bodyPart);
  174.     }
  175.    
  176.     public String getContentDisposition(BodyPart bodyPart) throws UtilsException{
  177.         return this.getHeaderValue(CONTENT_DISPOSITION, bodyPart);
  178.     }
  179.    
  180.     private String getHeaderValue(String headerName, BodyPart bodyPart) throws UtilsException{
  181.         try{
  182.             Enumeration<?> enHdr = bodyPart.getAllHeaders();
  183.             while (enHdr.hasMoreElements()) {
  184.                 Object o = enHdr.nextElement();
  185.                 String header = null;
  186.                 javax.mail.Header mh = null;
  187.                 if(o instanceof String){
  188.                     header = (String) o;
  189.                     if(match(headerName, header)){
  190.                         return bodyPart.getHeader(header)[0];
  191.                     }
  192.                 }
  193.                 else if(o instanceof  javax.mail.Header){
  194.                     mh = ( javax.mail.Header) o;
  195.                     header = mh.getName();
  196.                     if(match(headerName, header)){
  197.                         return mh.getValue();
  198.                     }
  199.                 }
  200.             }
  201.             return null;
  202.         }catch(Exception e){
  203.             throw new UtilsException(e.getMessage(),e);
  204.         }
  205.     }
  206.    
  207.     private boolean match(String headerName, String check){
  208.         return headerName.equalsIgnoreCase(check) ;
  209.     }
  210. }