MultipartUtilities.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.openapi.validator;

  21. import java.util.List;

  22. import javax.activation.DataHandler;
  23. import javax.mail.BodyPart;
  24. import javax.mail.internet.MimeBodyPart;
  25. import javax.mail.util.ByteArrayDataSource;

  26. import org.openspcoop2.utils.mime.MimeMultipart;
  27. import org.openspcoop2.utils.transport.http.HttpConstants;

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

  37.     public static MimeMultipart buildMimeMultipart(String subtype,
  38.             String contentTxt, String contentTypeTxt, String nameTxt, String fileNameTxt,
  39.             String contentJson, String contentTypeJson, String nameJson, String fileNameJson,
  40.             byte[] contentPdf, String contentTypePdf, String namePdf, String fileNamePdf,
  41.             byte[] contentPdf2, String contentTypePdf2, String namePdf2, String fileNamePdf2,
  42.             byte[] contentOther, String contentTypeOther, String nameOther, String fileNameOther) throws Exception {
  43.        
  44.         MimeMultipart mm = new MimeMultipart(subtype);
  45.        
  46.         if(contentTxt!=null) {
  47.             BodyPart bodyTxt = new MimeBodyPart();
  48.             String ct = contentTypeTxt;
  49.             if(ct==null) {
  50.                 ct = HttpConstants.CONTENT_TYPE_PLAIN;
  51.             }
  52.             bodyTxt.setDataHandler(new DataHandler(new ByteArrayDataSource(contentTxt.getBytes(), ct)));
  53.             if(contentTypeTxt!=null) {
  54.                 bodyTxt.addHeader(HttpConstants.CONTENT_TYPE, contentTypeTxt);
  55.             }
  56.             if(nameTxt!=null) {
  57.                 if(!"".equals(nameTxt)) {
  58.                     String hV = HttpConstants.CONTENT_TYPE_MULTIPART_PARAMETER_FORM_DATA+"; "+HttpConstants.CONTENT_DISPOSITION_NAME_PREFIX+""+nameTxt;
  59.                     String fileName = fileNameTxt;
  60.                     if(fileName!=null) {
  61.                         hV = hV + "; "+HttpConstants.CONTENT_DISPOSITION_FILENAME_PREFIX+fileName;
  62.                     }
  63.                     bodyTxt.addHeader(HttpConstants.CONTENT_DISPOSITION,hV);
  64.                 }
  65.                 else {
  66.                     bodyTxt.addHeader(HttpConstants.CONTENT_DISPOSITION, HttpConstants.CONTENT_TYPE_MULTIPART_PARAMETER_FORM_DATA);
  67.                 }
  68.             }
  69.             bodyTxt.addHeader("X-Custom-Header", "222");
  70.             mm.addBodyPart(bodyTxt);
  71.         }
  72.        
  73.         if(contentJson!=null) {
  74.             BodyPart bodyJson = new MimeBodyPart();
  75.             String ct = contentTypeJson;
  76.             if(ct==null) {
  77.                 ct = HttpConstants.CONTENT_TYPE_JSON;
  78.             }
  79.             bodyJson.setDataHandler(new DataHandler(new ByteArrayDataSource(contentJson.getBytes(), ct)));
  80.             if(contentTypeJson!=null) {
  81.                 bodyJson.addHeader(HttpConstants.CONTENT_TYPE, contentTypeJson);
  82.             }
  83.             if(nameJson!=null) {
  84.                 if(!"".equals(nameJson)) {
  85.                     String hV = HttpConstants.CONTENT_TYPE_MULTIPART_PARAMETER_FORM_DATA+"; "+HttpConstants.CONTENT_DISPOSITION_NAME_PREFIX+""+nameJson;
  86.                     String fileName = fileNameJson;
  87.                     if(fileName!=null) {
  88.                         hV = hV + "; "+HttpConstants.CONTENT_DISPOSITION_FILENAME_PREFIX+fileName;
  89.                     }
  90.                     bodyJson.addHeader(HttpConstants.CONTENT_DISPOSITION,hV);
  91.                 }
  92.                 else {
  93.                     bodyJson.addHeader(HttpConstants.CONTENT_DISPOSITION, HttpConstants.CONTENT_TYPE_MULTIPART_PARAMETER_FORM_DATA);
  94.                 }
  95.             }
  96.             mm.addBodyPart(bodyJson);
  97.         }

  98.         if(contentPdf!=null) {
  99.             BodyPart bodyPdf = new MimeBodyPart();
  100.             String ct = contentTypePdf;
  101.             if(ct==null) {
  102.                 ct = HttpConstants.CONTENT_TYPE_APPLICATION_OCTET_STREAM;
  103.             }
  104.             bodyPdf.setDataHandler(new DataHandler(new ByteArrayDataSource(contentPdf, ct)));
  105.             if(contentTypePdf!=null) {
  106.                 bodyPdf.addHeader(HttpConstants.CONTENT_TYPE, contentTypePdf);
  107.             }
  108.             if(namePdf!=null) {
  109.                 if(!"".equals(namePdf)) {
  110.                     String hV = HttpConstants.CONTENT_TYPE_MULTIPART_PARAMETER_FORM_DATA+"; "+HttpConstants.CONTENT_DISPOSITION_NAME_PREFIX+""+namePdf;
  111.                     String fileName = fileNamePdf;
  112.                     if(fileName!=null) {
  113.                         hV = hV + "; "+HttpConstants.CONTENT_DISPOSITION_FILENAME_PREFIX+fileName;
  114.                     }
  115.                     bodyPdf.addHeader(HttpConstants.CONTENT_DISPOSITION,hV);
  116.                 }
  117.                 else {
  118.                     bodyPdf.addHeader(HttpConstants.CONTENT_DISPOSITION, HttpConstants.CONTENT_TYPE_MULTIPART_PARAMETER_FORM_DATA);
  119.                 }
  120.             }
  121.             mm.addBodyPart(bodyPdf);
  122.         }
  123.        
  124.         if(contentPdf2!=null) {
  125.             BodyPart bodyPdf2 = new MimeBodyPart();
  126.             String ct = contentTypePdf2;
  127.             if(ct==null) {
  128.                 ct = HttpConstants.CONTENT_TYPE_APPLICATION_OCTET_STREAM;
  129.             }
  130.             bodyPdf2.setDataHandler(new DataHandler(new ByteArrayDataSource(contentPdf2, ct)));
  131.             if(contentTypePdf2!=null) {
  132.                 bodyPdf2.addHeader(HttpConstants.CONTENT_TYPE, contentTypePdf2);
  133.             }
  134.             if(namePdf2!=null) {
  135.                 if(!"".equals(namePdf2)) {
  136.                     String hV = HttpConstants.CONTENT_TYPE_MULTIPART_PARAMETER_FORM_DATA+"; "+HttpConstants.CONTENT_DISPOSITION_NAME_PREFIX+""+namePdf2;
  137.                     String fileName = fileNamePdf2;
  138.                     if(fileName!=null) {
  139.                         hV = hV + "; "+HttpConstants.CONTENT_DISPOSITION_FILENAME_PREFIX+fileName;
  140.                     }
  141.                     bodyPdf2.addHeader(HttpConstants.CONTENT_DISPOSITION,hV);
  142.                 }
  143.                 else {
  144.                     bodyPdf2.addHeader(HttpConstants.CONTENT_DISPOSITION, HttpConstants.CONTENT_TYPE_MULTIPART_PARAMETER_FORM_DATA);
  145.                 }
  146.             }
  147.             mm.addBodyPart(bodyPdf2);
  148.         }
  149.        
  150.         if(contentOther!=null) {
  151.             BodyPart bodyOther = new MimeBodyPart();
  152.             String ct = contentTypeOther;
  153.             if(ct==null) {
  154.                 ct = HttpConstants.CONTENT_TYPE_APPLICATION_OCTET_STREAM;
  155.             }
  156.             bodyOther.setDataHandler(new DataHandler(new ByteArrayDataSource(contentOther, ct)));
  157.             if(contentTypeOther!=null) {
  158.                 bodyOther.addHeader(HttpConstants.CONTENT_TYPE, contentTypeOther);
  159.             }
  160.             if(nameOther!=null) {
  161.                 if(!"".equals(nameOther)) {
  162.                     String hV = HttpConstants.CONTENT_TYPE_MULTIPART_PARAMETER_FORM_DATA+"; "+HttpConstants.CONTENT_DISPOSITION_NAME_PREFIX+""+nameOther;
  163.                     String fileName = fileNameOther;
  164.                     if(fileName!=null) {
  165.                         hV = hV + "; "+HttpConstants.CONTENT_DISPOSITION_FILENAME_PREFIX+fileName;
  166.                     }
  167.                     bodyOther.addHeader(HttpConstants.CONTENT_DISPOSITION,hV);
  168.                 }
  169.                 else {
  170.                     bodyOther.addHeader(HttpConstants.CONTENT_DISPOSITION, HttpConstants.CONTENT_TYPE_MULTIPART_PARAMETER_FORM_DATA);
  171.                 }
  172.             }
  173.             mm.addBodyPart(bodyOther);
  174.         }
  175.        
  176.         return mm;
  177.        
  178.     }
  179.    
  180.     public static final String templateNumero = "NUMERO";
  181.     public static MimeMultipart buildMimeMultipart(String subtype,
  182.             List<byte[]> contents, String contentType, String name, String fileName
  183.             ) throws Exception {
  184.        
  185.         MimeMultipart mm = new MimeMultipart(subtype);
  186.        
  187.         int attachNumero = contents.size();
  188.        
  189.         for (int k = 0; k < attachNumero; k++) {
  190.             byte[] content = contents.get(k);
  191.             if(content!=null) {
  192.                 BodyPart body = new MimeBodyPart();
  193.                 String ct = contentType;
  194.                 if(ct==null) {
  195.                     ct = HttpConstants.CONTENT_TYPE_APPLICATION_OCTET_STREAM;
  196.                 }
  197.                 body.setDataHandler(new DataHandler(new ByteArrayDataSource(content, ct)));
  198.                 if(contentType!=null) {
  199.                     body.addHeader(HttpConstants.CONTENT_TYPE, contentType);
  200.                 }
  201.                 if(name!=null) {
  202.                     if(!"".equals(name)) {
  203.                         String hV = HttpConstants.CONTENT_TYPE_MULTIPART_PARAMETER_FORM_DATA+"; "+HttpConstants.CONTENT_DISPOSITION_NAME_PREFIX+""+name;
  204.                         if(fileName!=null) {
  205.                             hV = hV + "; "+HttpConstants.CONTENT_DISPOSITION_FILENAME_PREFIX+fileName.replace(templateNumero, k+"");
  206.                         }
  207.                         body.addHeader(HttpConstants.CONTENT_DISPOSITION,hV);
  208.                     }
  209.                     else {
  210.                         body.addHeader(HttpConstants.CONTENT_DISPOSITION, HttpConstants.CONTENT_TYPE_MULTIPART_PARAMETER_FORM_DATA);
  211.                     }
  212.                 }
  213.                 mm.addBodyPart(body);
  214.             }
  215.         }
  216.        
  217.         return mm;
  218.     }
  219.    
  220. }