MultipartContent.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.rest;

  21. import java.io.ByteArrayInputStream;
  22. import java.io.FileInputStream;
  23. import java.io.InputStream;
  24. import java.io.OutputStream;

  25. import org.openspcoop2.message.exception.MessageException;
  26. import org.openspcoop2.utils.io.DumpByteArrayOutputStream;
  27. import org.openspcoop2.utils.mime.MimeMultipart;

  28. /**
  29.  * MimeMultipartContent
  30.  *
  31.  * @author Andrea Poli (poli@link.it)
  32.  * @author $Author$
  33.  * @version $Rev$, $Date$
  34.  */
  35. public class MultipartContent extends AbstractLazyContent<MimeMultipart> {

  36.     public MultipartContent(InputStream is, String contentType) throws MessageException {
  37.         try{
  38.             if(BUILD_LAZY) {
  39.                 this.init(is, contentType);
  40.             }
  41.             else {
  42.                 this.init(new MimeMultipart(is, contentType));
  43.             }
  44.         }catch(Exception e){
  45.             throw new MessageException(e.getMessage(),e);
  46.         }
  47.     }
  48.     public MultipartContent(DumpByteArrayOutputStream contentBuffer, String contentType) throws MessageException {
  49.         try{
  50.             if(BUILD_LAZY) {
  51.                 this.init(contentBuffer, contentType);
  52.             }
  53.             else {
  54.                 if(contentBuffer.isSerializedOnFileSystem()) {
  55.                     try(InputStream is = new FileInputStream(contentBuffer.getSerializedFile())){
  56.                         this.init(new MimeMultipart(is, contentType));
  57.                     }
  58.                 }
  59.                 else {
  60.                     try(InputStream is = new ByteArrayInputStream(contentBuffer.toByteArray())){
  61.                         this.init(new MimeMultipart(is, contentType));
  62.                     }
  63.                 }
  64.             }
  65.         }catch(Exception e){
  66.             throw new MessageException(e.getMessage(),e);
  67.         }
  68.     }
  69.    
  70.     @Override
  71.     public MimeMultipart buildContent(InputStream is) throws MessageException {
  72.         try {
  73.             return new MimeMultipart(is, this.contentType);
  74.         }catch(Exception e){
  75.             throw new MessageException(e.getMessage(),e);
  76.         }
  77.     }
  78.     @Override
  79.     public MimeMultipart buildContent(byte[] c) throws MessageException {
  80.         try(InputStream is = new ByteArrayInputStream(c)){
  81.             return new MimeMultipart(is, this.contentType);
  82.         }catch(Exception e){
  83.             throw new MessageException(e.getMessage(),e);
  84.         }
  85.     }
  86.     @Override
  87.     public void writeContentTo(OutputStream os, boolean consume) throws MessageException {
  88.         try {
  89.             this.content.writeTo(os);
  90.         }catch(Exception e){
  91.             throw new MessageException(e.getMessage(),e);
  92.         }
  93.     }
  94.    
  95.     public MimeMultipart getMimeMultipart() throws MessageException {
  96.         return this.getContent();
  97.     }
  98.    
  99. }