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

  24. import org.openspcoop2.message.OpenSPCoop2MessageFactory;
  25. import org.openspcoop2.message.OpenSPCoop2RestMimeMultipartMessage;
  26. import org.openspcoop2.message.exception.MessageException;
  27. import org.openspcoop2.utils.io.DumpByteArrayOutputStream;

  28. /**
  29.  * Implementazione dell'OpenSPCoop2Message utilizzabile per messaggi multipart
  30.  *
  31.  * @author Andrea Poli (poli@link.it)
  32.  * @author $Author$
  33.  * @version $Rev$, $Date$
  34.  */
  35. public class OpenSPCoop2Message_mimeMultipart_impl extends AbstractBaseOpenSPCoop2RestMessage<MultipartContent> implements OpenSPCoop2RestMimeMultipartMessage {

  36.     public OpenSPCoop2Message_mimeMultipart_impl(OpenSPCoop2MessageFactory messageFactory) {
  37.         super(messageFactory);
  38.     }
  39.     public OpenSPCoop2Message_mimeMultipart_impl(OpenSPCoop2MessageFactory messageFactory, InputStream is,String contentType) throws MessageException {
  40.         super(messageFactory, is, contentType);
  41.     }
  42.    
  43.     @Override
  44.     protected MultipartContent buildContent() throws MessageException{
  45.         try{
  46.             return new MultipartContent(this._getInputStream(), this.contentType);
  47.         }catch(Exception e){
  48.             throw new MessageException(e.getMessage(),e);
  49.         }finally {
  50.             try {
  51.                 this._getInputStream().close();
  52.             }catch(Exception eClose) {
  53.                 // close
  54.             }
  55.         }
  56.     }
  57.     @Override
  58.     protected MultipartContent buildContent(DumpByteArrayOutputStream contentBuffer) throws MessageException{
  59.         return new MultipartContent(contentBuffer, this.contentType);
  60.     }
  61.    
  62.     @Override
  63.     protected String buildContentAsString() throws MessageException{
  64.         try{
  65.             ByteArrayOutputStream bout = new ByteArrayOutputStream();
  66.             this.serializeContent(bout, false);
  67.             bout.flush();
  68.             bout.close();
  69.             return bout.toString(this.contentTypeCharsetName);
  70.         }catch(Exception e){
  71.             throw new MessageException(e.getMessage(),e);
  72.         }
  73.     }
  74.     @Override
  75.     protected byte[] buildContentAsByteArray() throws MessageException{
  76.         try{
  77.             ByteArrayOutputStream bout = new ByteArrayOutputStream();
  78.             this.serializeContent(bout, false);
  79.             bout.flush();
  80.             bout.close();
  81.             return bout.toByteArray();
  82.         }catch(Exception e){
  83.             throw new MessageException(e.getMessage(),e);
  84.         }
  85.     }

  86.     @Override
  87.     protected void serializeContent(OutputStream os, boolean consume) throws MessageException {
  88.         try{
  89.             this.content.writeTo(os, consume);
  90.         }catch(Exception e){
  91.             throw new MessageException(e.getMessage(),e);
  92.         }
  93.     }

  94.    
  95. }