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

  24. import org.openspcoop2.message.OpenSPCoop2MessageFactory;
  25. import org.openspcoop2.message.OpenSPCoop2RestBinaryMessage;
  26. import org.openspcoop2.message.exception.MessageException;
  27. import org.openspcoop2.message.exception.MessageNotSupportedException;
  28. import org.openspcoop2.utils.Utilities;
  29. import org.openspcoop2.utils.io.DumpByteArrayOutputStream;

  30. /**
  31.  * Implementazione dell'OpenSPCoop2Message utilizzabile per messaggi binari
  32.  *
  33.  * @author Andrea Poli (poli@link.it)
  34.  * @author $Author$
  35.  * @version $Rev$, $Date$
  36.  */
  37. public class OpenSPCoop2Message_binary_impl extends AbstractBaseOpenSPCoop2RestMessage<BinaryContent> implements OpenSPCoop2RestBinaryMessage {

  38.     public OpenSPCoop2Message_binary_impl(OpenSPCoop2MessageFactory messageFactory) {
  39.         super(messageFactory);
  40.         //this.supportReadOnly = false; // il contenuto e' gia un binary.
  41.         this.supportReadOnly = true; // in modo da sfruttare il buffer
  42.     }
  43.     public OpenSPCoop2Message_binary_impl(OpenSPCoop2MessageFactory messageFactory, InputStream is,String contentType) throws MessageException {
  44.         super(messageFactory, is, contentType);
  45.         //this.supportReadOnly = false; // il contenuto e' gia un binary.
  46.         this.supportReadOnly = true; // in modo da sfruttare il buffer
  47.     }
  48.    
  49.     @Override
  50.     protected BinaryContent buildContent() throws MessageException{
  51.         try{
  52.             return new BinaryContent(this._getInputStream(), this.contentType);
  53.         }catch(Exception e){
  54.             throw new MessageException(e.getMessage(),e);
  55.         }finally {
  56.             try {
  57.                 this._getInputStream().close();
  58.             }catch(Exception eClose) {
  59.                 // close
  60.             }
  61.         }
  62.     }
  63.     @Override
  64.     protected BinaryContent buildContent(DumpByteArrayOutputStream contentBuffer) throws MessageException{
  65.         return new BinaryContent(contentBuffer, this.contentType);
  66.     }

  67.     @Override
  68.     protected String buildContentAsString() throws MessageException{
  69.         try{
  70.             return Utilities.getAsString(new ByteArrayInputStream(this.content.getContent()), this.contentTypeCharsetName);
  71.         }catch(Exception e){
  72.             throw new MessageException(e.getMessage(),e);
  73.         }
  74.     }
  75.     @Override
  76.     protected byte[] buildContentAsByteArray() throws MessageException{
  77.         return this.content.getContent();
  78.     }
  79.    
  80.     @Override
  81.     protected void serializeContent(OutputStream os, boolean consume) throws MessageException {
  82.         try{
  83.             this.content.writeTo(os, consume);
  84.         }catch(Exception e){
  85.             throw new MessageException(e.getMessage(),e);
  86.         }
  87.     }

  88.     public void updateContent(byte[] content) throws MessageException, MessageNotSupportedException {
  89.         if(this.content!=null && this.content.contentBuffer!=null) {
  90.             this.content.contentBuffer.clearResources();
  91.             this.content = null;
  92.         }
  93.         this.content = new BinaryContent(new ByteArrayInputStream(content), this.contentType);
  94.     }
  95.    
  96. }