OpenSPCoop2DataContentHandler.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.message;

  21. import java.awt.datatransfer.DataFlavor;
  22. import java.io.ByteArrayOutputStream;
  23. import java.io.IOException;
  24. import java.io.InputStream;
  25. import java.io.OutputStream;

  26. import javax.activation.ActivationDataFlavor;
  27. import javax.activation.DataContentHandler;
  28. import javax.activation.DataSource;

  29. import org.openspcoop2.utils.io.Base64Utilities;
  30. import org.openspcoop2.utils.resources.Loader;
  31. import org.openspcoop2.utils.transport.http.HttpConstants;


  32. /**
  33.  * DataContentHandler per la gestione degli attachments
  34.  *
  35.  *
  36.  * @author Poli Andrea (apoli@link.it)
  37.  * @author $Author$
  38.  * @version $Rev$, $Date$
  39.  */

  40. public class OpenSPCoop2DataContentHandler implements DataContentHandler{

  41.     public static final String OPENSPCOOP2_SIGNATURE = "===SIGNATURE=OPENSPCOOP2===";
  42.    
  43.     public OpenSPCoop2DataContentHandler(){}

  44.     public static Object getContent(java.io.InputStream inputstream) throws IOException {
  45.         try{
  46.             ByteArrayOutputStream bout = new ByteArrayOutputStream();
  47.             byte[] bRead = new byte[8094];
  48.             int byteLetti = 0;
  49.             while( (byteLetti =inputstream.read(bRead))!= -1 ){
  50.                 bout.write(bRead, 0, byteLetti);
  51.             }
  52.             bout.flush();
  53.             bout.close();
  54.            
  55.             if(bout.size() <= OpenSPCoop2DataContentHandler.OPENSPCOOP2_SIGNATURE.length()){
  56.                 throw new Exception("OpenSPCoop2DataContentHandler Signature non presente (length is too small)");
  57.             }
  58.             boolean giaCodificato = true;
  59.             String attach = bout.toString();
  60.             for(int i=0; i<OpenSPCoop2DataContentHandler.OPENSPCOOP2_SIGNATURE.length(); i++){  
  61.                 if( (attach.charAt(i)) !=  OpenSPCoop2DataContentHandler.OPENSPCOOP2_SIGNATURE.charAt(i) ){
  62.                     giaCodificato = false;
  63.                     break;
  64.                 }
  65.             }
  66.             if(giaCodificato==false){
  67.                 throw new Exception("OpenSPCoop2DataContentHandler Signature non presente");
  68.             }else{
  69.                 attach = attach.substring(OpenSPCoop2DataContentHandler.OPENSPCOOP2_SIGNATURE.length(),attach.length());
  70.             }
  71.            
  72.             OpenSPCoop2DataContentHandlerInputStream bin = new OpenSPCoop2DataContentHandlerInputStream(Base64Utilities.decode(attach));
  73.             return bin;

  74.         }catch(Exception e){
  75.             throw new IOException("@@@ OpenSPCoop2DataContentHandler.getContent() error: "+e.getMessage());
  76.         }
  77.     }
  78.    
  79.     @Override
  80.     public Object getContent(DataSource datasource) throws IOException{
  81.         try{
  82.             java.io.InputStream inputstream = datasource.getInputStream();
  83.             return OpenSPCoop2DataContentHandler.getContent(inputstream);

  84.         }catch(Exception e){
  85.             throw new IOException("@@@ OpenSPCoop2DataContentHandler.getContent() error: "+e.getMessage());
  86.         }
  87.     }

  88.     @Override
  89.     public Object getTransferData(DataFlavor dataflavor, DataSource datasource)
  90.     throws IOException
  91.     {
  92.         //logger.info("@@@ BinDataContentHandler.getTransferData: non implementato");
  93.         return null;
  94.     }

  95.     @Override
  96.     public DataFlavor[] getTransferDataFlavors()
  97.     {
  98.         DataFlavor adataflavor[] = new DataFlavor[1];
  99.         try
  100.         {
  101.             adataflavor[0] = new ActivationDataFlavor(Loader.getInstance().forName("org.openspcoop2.message.OpenSPCoop2DataContentHandler"),
  102.                     HttpConstants.CONTENT_TYPE_OPENSPCOOP2_TUNNEL_SOAP, "OpenSPCoop2AttachmentsTunnel");
  103.         }
  104.         catch(Exception exception) { }
  105.         return adataflavor;
  106.     }

  107.     @Override
  108.     public void writeTo(Object obj, String s, OutputStream outputstream)
  109.     throws IOException
  110.     {
  111.         try{
  112.             byte content[] = null;
  113.             if(obj instanceof InputStream){
  114.                 InputStream inputstream = (InputStream) obj;
  115.                 ByteArrayOutputStream bout = new ByteArrayOutputStream();
  116.                 byte[] bRead = new byte[8094];
  117.                 int byteLetti = 0;
  118.                 while( (byteLetti =inputstream.read(bRead))!= -1 ){
  119.                     bout.write(bRead, 0, byteLetti);
  120.                 }
  121.                 bout.flush();
  122.                 bout.close();
  123.                 content = bout.toByteArray();
  124.             }else{
  125.                 content = (byte[])obj;
  126.             }
  127.             // Deve essere codificato solo una volta!!
  128.             boolean giaCodificato = true;
  129.             if( !(content.length<OpenSPCoop2DataContentHandler.OPENSPCOOP2_SIGNATURE.length()+1) ){
  130.                 for(int i=0; i<OpenSPCoop2DataContentHandler.OPENSPCOOP2_SIGNATURE.length(); i++){  
  131.                     if( ((char)content[i]) !=  OpenSPCoop2DataContentHandler.OPENSPCOOP2_SIGNATURE.charAt(i) ){
  132.                         giaCodificato = false;
  133.                         break;
  134.                     }
  135.                 }
  136.             }
  137.            
  138.             if(giaCodificato){
  139.                 outputstream.write(content);
  140.             }else{
  141.                 String encoded =  OpenSPCoop2DataContentHandler.OPENSPCOOP2_SIGNATURE + Base64Utilities.encodeAsString(content);
  142.                 outputstream.write(encoded.getBytes());
  143.             }

  144.         }
  145.         catch(Exception exception)
  146.         {
  147.             throw new IOException("@@@ OpenSPCoop2DataContentHandler.writeTo: Unable to run the Binary decoding on a stream " + exception.getMessage());
  148.         }
  149.     }
  150. }