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

  23. import org.openspcoop2.message.exception.MessageException;
  24. import org.openspcoop2.utils.io.DumpByteArrayOutputStream;
  25. import org.openspcoop2.utils.resources.FileSystemUtilities;

  26. /**
  27.  * BinaryContent
  28.  *
  29.  * @author Andrea Poli (poli@link.it)
  30.  * @author $Author$
  31.  * @version $Rev$, $Date$
  32.  */
  33. public class BinaryContent extends AbstractLazyContent<byte[]> {

  34.     public BinaryContent(InputStream is, String contentType) throws MessageException {
  35.         try{
  36.             this.init(is, contentType);
  37.         }catch(Exception e){
  38.             throw new MessageException(e.getMessage(),e);
  39.         }
  40.     }
  41.     public BinaryContent(DumpByteArrayOutputStream contentBuffer, String contentType) throws MessageException {
  42.         try{
  43.             this.init(contentBuffer, contentType);
  44.         }catch(Exception e){
  45.             throw new MessageException(e.getMessage(),e);
  46.         }
  47.     }
  48.    
  49.     @Override
  50.     public byte[] buildContent(InputStream is) throws MessageException {
  51.         return new byte[1]; // serve solo a non farlo avere null
  52.     }
  53.     @Override
  54.     public byte[] buildContent(byte[] c) throws MessageException {
  55.         return new byte[1]; // serve solo a non farlo avere null
  56.     }
  57.     @Override
  58.     public void writeContentTo(OutputStream os, boolean consume) throws MessageException {
  59.         super.writeTo(false, os, consume);
  60.     }
  61.    
  62.     @Override
  63.     public byte[] getContent() throws MessageException {
  64.         if(this.contentBuffer!=null) {
  65.             try {
  66.                 if(this.contentBuffer.isSerializedOnFileSystem()) {
  67.                     return FileSystemUtilities.readBytesFromFile(this.contentBuffer.getSerializedFile());
  68.                 }
  69.                 else {
  70.                     return this.contentBuffer.toByteArray();
  71.                 }
  72.             }catch(Exception e){
  73.                 throw new MessageException(e.getMessage(),e);
  74.             }
  75.         }
  76.         else {
  77.             return this.contentByteArray;
  78.         }
  79.     }
  80.    
  81. }