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

  24. import org.openspcoop2.message.exception.MessageException;
  25. import org.openspcoop2.utils.Utilities;
  26. import org.openspcoop2.utils.io.DumpByteArrayOutputStream;

  27. /**
  28.  * AbstractLazyContent
  29.  *
  30.  * @author Andrea Poli (poli@link.it)
  31.  * @author $Author$
  32.  * @version $Rev$, $Date$
  33.  */
  34. public abstract class AbstractLazyContent<T> {

  35.     public static boolean BUILD_LAZY = true;
  36.    
  37.     protected T content;
  38.     protected DumpByteArrayOutputStream contentBuffer;
  39.     protected byte[] contentByteArray; // inefficiente, viene memorizzato due volte, se e' attivo anche il contentBuffer, usare anzi quel costruttore
  40.     protected String contentType;
  41.    
  42.     protected AbstractLazyContent() {}
  43.     public void init(InputStream is, String contentType) throws MessageException {
  44.         try{
  45.             this.contentType = contentType;
  46.             this.contentByteArray = Utilities.getAsByteArray(is);
  47.         }catch(Exception e){
  48.             throw new MessageException(e.getMessage(),e);
  49.         }
  50.     }
  51.     public void init(DumpByteArrayOutputStream contentBuffer, String contentType) throws MessageException {
  52.         try{
  53.             this.contentType = contentType;
  54.             this.contentBuffer = contentBuffer;
  55.         }catch(Exception e){
  56.             throw new MessageException(e.getMessage(),e);
  57.         }
  58.     }
  59.     public void init(T content) {
  60.         this.content = content;
  61.     }
  62.    
  63.     public abstract T buildContent(InputStream is) throws MessageException;
  64.     public abstract T buildContent(byte[] c) throws MessageException;
  65.     public abstract void writeContentTo(OutputStream os, boolean consume) throws MessageException;
  66.    
  67.     public void buildContent() throws MessageException {
  68.         if(this.content==null) {
  69.             if(this.contentBuffer!=null) {
  70.                 try{
  71.                     if(this.contentBuffer.isSerializedOnFileSystem()) {
  72.                         try(InputStream is = new FileInputStream(this.contentBuffer.getSerializedFile())){
  73.                             this.content = buildContent(is);
  74.                         }
  75.                     }
  76.                     else {
  77.                         this.content = buildContent(this.contentBuffer.toByteArray());
  78.                     }
  79.                     this.contentBuffer = null;
  80.                 }catch(Exception e){
  81.                     throw new MessageException(e.getMessage(),e);
  82.                 }
  83.             }
  84.             else if(this.contentByteArray!=null) {
  85.                 try{
  86.                     this.content = buildContent(this.contentByteArray);
  87.                     this.contentByteArray = null;
  88.                 }catch(Exception e){
  89.                     throw new MessageException(e.getMessage(),e);
  90.                 }
  91.             }
  92.         }
  93.     }
  94.     public T getContent() throws MessageException {
  95.         if(this.content==null) {
  96.             buildContent();
  97.         }
  98.         return this.content;
  99.     }
  100.    
  101.     public void writeTo(OutputStream os, boolean consume) throws MessageException {
  102.         writeTo(true, os, consume);
  103.     }
  104.     protected void writeTo(boolean checkContent, OutputStream os, boolean consume) throws MessageException {
  105.         try{
  106.             if(checkContent && this.content!=null) {
  107.                 this.writeContentTo(os, consume);
  108.                 if(consume) {
  109.                     this.content=null;
  110.                 }
  111.             }
  112.             else if(this.contentBuffer!=null) {
  113.                 this.contentBuffer.writeTo(os);
  114.                 if(consume) {
  115.                     this.contentBuffer=null;
  116.                 }
  117.             }
  118.             else if(this.contentByteArray!=null) {
  119.                 os.write(this.contentByteArray);
  120.                 if(consume) {
  121.                     this.contentByteArray=null;
  122.                 }
  123.             }
  124.         }catch(Exception e){
  125.             throw new MessageException(e.getMessage(),e);
  126.         }
  127.     }
  128.    
  129. }