SerialiableFormFile.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.web.ctrlstat.core;

  21. import java.io.ByteArrayInputStream;
  22. import java.io.FileNotFoundException;
  23. import java.io.IOException;
  24. import java.io.InputStream;
  25. import java.io.Serializable;

  26. import org.apache.struts.upload.FormFile;
  27. import org.openspcoop2.utils.UtilsException;
  28. import org.openspcoop2.utils.mime.MimeTypes;
  29. import org.openspcoop2.utils.transport.http.HttpConstants;

  30. /**
  31.  * WrapperFormFile
  32.  *
  33.  * @author $Author$
  34.  * @version $Rev$, $Date$
  35.  *
  36.  */
  37. public class SerialiableFormFile implements FormFile, Serializable {

  38.     /**
  39.      *
  40.      */
  41.     private static final long serialVersionUID = 1L;
  42.    
  43.     private String contentType;
  44.     private String fileName;
  45.     private byte[] content;
  46.    
  47.     public SerialiableFormFile(FormFile ff) throws UtilsException {
  48.         try {
  49. //          if(ff instanceof SerialiableFormFile) {
  50. //              SerialiableFormFile sff = (SerialiableFormFile) ff;
  51. //              this.contentType = sff.getContentType();
  52. //              this.fileName = sff.getFileName();
  53. //              this.content = sff.getFileData();
  54. //          }
  55. //          else {
  56.             this.contentType = ff.getContentType();
  57.             this.fileName = ff.getFileName();
  58.             this.content = ff.getFileData();
  59.             //ff.destroy();
  60. //          }
  61.         }catch(Exception e) {
  62.             throw new UtilsException(e.getMessage(),e);
  63.         }
  64.     }
  65.    
  66.     // Costruttori utilizzati in RS API
  67.     public SerialiableFormFile(String fileName, byte[] content) throws UtilsException {
  68.         this(getContentType(fileName),fileName,content);
  69.     }
  70.     public SerialiableFormFile(String contentType, String fileName, byte[] content) {
  71.         this.fileName = fileName;
  72.         this.content = content;
  73.         this.contentType = contentType;
  74.     }
  75.    
  76.     private static String getContentType(String fileName) throws UtilsException {
  77.         String mimeType = null;
  78.         if(fileName.contains(".")){
  79.             String ext = null;
  80.             try{
  81.                 ext = fileName.substring(fileName.lastIndexOf(".")+1,fileName.length());
  82.             }catch(Exception e){
  83.                 // ignore
  84.             }
  85.             MimeTypes mimeTypes = MimeTypes.getInstance();
  86.             if(ext!=null && mimeTypes.existsExtension(ext)){
  87.                 mimeType = mimeTypes.getMimeType(ext);
  88.                 //System.out.println("CUSTOM ["+mimeType+"]");      
  89.             }
  90.             else{
  91.                 mimeType = HttpConstants.CONTENT_TYPE_X_DOWNLOAD;
  92.             }
  93.         }
  94.         else{
  95.             mimeType = HttpConstants.CONTENT_TYPE_X_DOWNLOAD;
  96.         }
  97.         return mimeType;
  98.     }
  99.    
  100.     @Override
  101.     public void destroy() {
  102.        
  103.     }

  104.     @Override
  105.     public String getContentType() {
  106.         return this.contentType;
  107.     }

  108.     @Override
  109.     public byte[] getFileData() throws FileNotFoundException, IOException {
  110.         return this.content;
  111.     }

  112.     @Override
  113.     public String getFileName() {
  114.         return this.fileName;
  115.     }

  116.     @Override
  117.     public int getFileSize() {
  118.         return this.content.length;
  119.     }

  120.     @Override
  121.     public InputStream getInputStream() throws FileNotFoundException, IOException {
  122.         return new ByteArrayInputStream(this.content);
  123.     }

  124.     @Override
  125.     public void setContentType(String contentType) {
  126.         this.contentType = contentType;
  127.     }

  128.     @Override
  129.     public void setFileName(String fileName) {
  130.         this.fileName = fileName;
  131.     }

  132.     @Override
  133.     public void setFileSize(int arg0) {
  134.         // nop;
  135.     }

  136. }