MultipartUtils.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.ByteArrayOutputStream;
  22. import java.io.InputStream;


  23. /**
  24.  * Libreria contenente metodi utili per la gestione degli Attachments.
  25.  *
  26.  *
  27.  * @author Poli Andrea (apoli@link.it)
  28.  * @author $Author$
  29.  * @version $Rev$, $Date$
  30.  */

  31. public class MultipartUtils {
  32.    
  33.     private MultipartUtils() {}

  34.     /**
  35.      * Trova la stringa utilizzata, all'interno del parametro <var>input</var>,
  36.      * per delimitare i vari attachments presenti.
  37.      *
  38.      * @param input byte[] del messaggio da esaminare
  39.      * @return String del boundary, in caso di ricerca con successo, null altrimenti.
  40.      *
  41.      */
  42.     public static String findBoundary(byte [] input){

  43.         ByteArrayOutputStream boundary = null;
  44.         try{

  45.             boundary = new ByteArrayOutputStream();
  46.             int i=0;
  47.             while(i<input.length){
  48.                 if(input[i]!='\n') {
  49.                     boundary.write(input[i]);
  50.                 }
  51.                 if(input[i]=='\n' || i==input.length){
  52.                     if(boundary.toString().startsWith("--"))
  53.                         break;
  54.                     else
  55.                         boundary.reset();
  56.                 }
  57.                 i++;
  58.             }

  59.             boundary.flush();
  60.             boundary.close();
  61.            
  62.             return getBoundary(boundary);

  63.         }catch(Exception e){
  64.             try{
  65.                 if(boundary!=null)
  66.                     boundary.close();
  67.             }catch(Exception eis){
  68.                 // ignore
  69.             }
  70.             return null;
  71.         }
  72.     }
  73.     private static String getBoundary(ByteArrayOutputStream boundary) {
  74.         String bS = null;
  75.         if(boundary.size() != 0) {
  76.             bS = boundary.toString();
  77.         }
  78.         return bS;
  79.     }


  80.     /**
  81.      * Trova la stringa utilizzata, all'interno del parametro <var>input</var>,
  82.      * per delimitare i vari attachments presenti.
  83.      *
  84.      * @param input InputStream del messaggio da esaminare
  85.      * @return String del boundary, in caso di ricerca con successo, null altrimenti.
  86.      *
  87.      */
  88.     public static String findBoundary(InputStream input){

  89.         ByteArrayOutputStream boundary = null;
  90.         try{

  91.             boundary = new ByteArrayOutputStream();
  92.             byte date = 0;
  93.             while((date=(byte)input.read())!=-1){
  94.                 boundary.write(date);
  95.                 if(((char)date)=='\n'){
  96.                     if(boundary.toString().startsWith("--"))
  97.                         break;
  98.                     else
  99.                         boundary.reset();
  100.                 }
  101.             }

  102.             String bS = null;
  103.             if(boundary.size() != 0)
  104.                 bS = boundary.toString().substring(0,boundary.toString().length()-2);

  105.             boundary.close();
  106.             return bS;

  107.         }catch(Exception e){
  108.             try{
  109.                 if(boundary!=null)
  110.                     boundary.close();
  111.             }catch(Exception eis){
  112.                 // ignore
  113.             }
  114.             return null;
  115.         }
  116.     }




  117.     /**
  118.      * Esamina il contenuto del messaggio <var>input</var>, per vedere se sono presenti o meno degli attachments.
  119.      *
  120.      * @param input byte[] del messaggio da esaminare
  121.      * @return true in caso di presenza di attachments, false altrimenti.
  122.      *
  123.      */
  124.     public static boolean messageWithAttachment(byte [] input){
  125.         // read first line
  126.         if(input.length < 10)
  127.             return false;

  128.         // Cerco -- nei primi 10 caratteri, e che non incontro <
  129.         for(int i=0; i<9; i++){
  130.             if( ((char)input[i] == '-') &&  ((char)input[i+1] == '-') ){
  131.                 return true;
  132.             }else if( (char)input[i] == '<'  )
  133.                 return false;
  134.         }

  135.         return false;
  136.     }




  137.     /**
  138.      * Trova il Content-ID utilizzato, all'interno del parametro <var>input</var>
  139.      *
  140.      * @param input byte[] del messaggio da esaminare
  141.      * @return String del ContentID, in caso di ricerca con successo, null altrimenti.
  142.      *
  143.      */
  144.     public static String firstContentID(byte [] input){

  145.         ByteArrayOutputStream line = null;
  146.         try{

  147.             String idfirst = null;
  148.             int index=0;
  149.             boolean found = false;
  150.             while(!found && index<input.length){
  151.                 line  = new ByteArrayOutputStream();
  152.                 index = readLineFirstContentID(input, line, index);
  153.                 if (line.toString().toLowerCase().startsWith("Content-Id:".toLowerCase())){
  154.                     found = true;
  155.                     String[] rr = line.toString().split(" ");
  156.                     idfirst = rr[1];
  157.                 }
  158.                 line.close();
  159.             }  

  160.             return idfirst;

  161.         }catch(Exception e){
  162.             try{
  163.                 if(line!=null)
  164.                     line.close();
  165.             }catch(Exception eis){
  166.                 // ignore
  167.             }
  168.             return null;
  169.         }

  170.     }
  171.     private static int readLineFirstContentID(byte [] input, ByteArrayOutputStream line, int index) {
  172.         while(true){
  173.             if(input[index]=='\r'){
  174.                 index++;
  175.                 if(input[index]=='\n'){
  176.                     index++;//elimino anche \n
  177.                 }
  178.                 break;
  179.             }
  180.             line.write(input[index]);
  181.             index++;
  182.         }
  183.         return index;
  184.     }


  185. }