TimeoutInputStreamEngine.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.utils;
  21. import java.io.IOException;
  22. import java.io.InputStream;
  23. import java.io.OutputStream;

  24. /**
  25.  * TimeoutInputStrem
  26.  *
  27.  *
  28.  * @author Poli Andrea (apoli@link.it)
  29.  * @author $Author$
  30.  * @version $Rev$, $Date$
  31.  */
  32. public class TimeoutInputStreamEngine extends InputStream {


  33.     private long createDateMs;
  34.     private int timeoutMs;
  35.     private InputStream isWrapped = null;
  36.     private String prefixError = "";
  37.     private Map<Object> ctx;
  38.     private boolean checkDisabled = false;
  39.    
  40.     private ITimeoutNotifier notifier;
  41.    
  42.     protected TimeoutInputStreamEngine(InputStream is, int timeoutMs, String prefixError, Map<Object> ctx, ITimeoutNotifier notifier) throws IOException {
  43.         this.createDateMs = System.currentTimeMillis();
  44.         this.timeoutMs = timeoutMs;
  45.         this.isWrapped = is;
  46.         if(prefixError!=null) {
  47.             this.prefixError = prefixError;
  48.         }
  49.         this.ctx = ctx;
  50.         if(this.timeoutMs<=0) {
  51.             throw new IOException("Invalid timeout");
  52.         }
  53.        
  54.         this.notifier = notifier;
  55.     }
  56.    
  57.     public InputStream getIsWrapped() {
  58.         return this.isWrapped;
  59.     }
  60.    
  61.     protected void disableCheckTimeout() {
  62.         this.checkDisabled = true;
  63.     }
  64.     protected void updateThreshold(int timeoutMs) throws IOException {
  65.         if(this.timeoutMs<=0) {
  66.             throw new IOException("Invalid timeout");
  67.         }
  68.         this.timeoutMs = timeoutMs;
  69.     }
  70.     protected void updateContext(Map<Object> ctx) {
  71.         this.ctx = ctx;
  72.     }
  73.     protected void updateNotifier(ITimeoutNotifier notifier) {
  74.         this.notifier = notifier;
  75.     }
  76.    
  77.     private void checkTimeout() throws IOException {
  78.         if(this.checkDisabled) {
  79.             return; // e' stato disabilitato dopo averlo creato
  80.         }
  81.         long now = System.currentTimeMillis() - this.createDateMs;
  82.         if(now>this.timeoutMs) {
  83.             String errorMsg = this.prefixError+TimeoutInputStream.ERROR_MSG;
  84.             if(this.ctx!=null) {
  85.                 this.ctx.put(TimeoutInputStream.ERROR_MSG_KEY, errorMsg);
  86.             }
  87.             TimeoutIOException exc = new TimeoutIOException(errorMsg);
  88.             if(this.ctx!=null) {
  89.                 this.ctx.put(TimeoutInputStream.EXCEPTION_KEY, exc);
  90.             }
  91.            
  92.             if(this.notifier!=null) {
  93.                 this.notifier.notify(now);
  94.             }
  95.            
  96.             throw exc;
  97.         }
  98.     }
  99.    
  100.     @Override
  101.     public int read() throws IOException {
  102.         checkTimeout();
  103.         return this.isWrapped.read();
  104.     }
  105.    
  106.     @Override
  107.     public int read(byte[] b) throws IOException {
  108.         checkTimeout();
  109.         return this.isWrapped.read(b);
  110.     }
  111.    
  112.     @Override
  113.     public int read(byte[] b, int off, int len) throws IOException {
  114.         checkTimeout();
  115.         return this.isWrapped.read(b, off, len);
  116.     }

  117.     @Override
  118.     public byte[] readAllBytes() throws IOException {
  119.         checkTimeout();
  120.         return this.isWrapped.readAllBytes();
  121.     }

  122.     @Override
  123.     public byte[] readNBytes(int len) throws IOException {
  124.         checkTimeout();
  125.         return this.isWrapped.readNBytes(len);
  126.     }

  127.     @Override
  128.     public int readNBytes(byte[] b, int off, int len) throws IOException {
  129.         checkTimeout();
  130.         return this.isWrapped.readNBytes(b, off, len);
  131.     }
  132.    
  133.     @Override
  134.     public long skip(long n) throws IOException {
  135.         checkTimeout();
  136.         return this.isWrapped.skip(n);
  137.     }

  138.     @Override
  139.     public int available() throws IOException {
  140.         checkTimeout();
  141.         return this.isWrapped.available();
  142.     }

  143.     @Override
  144.     public synchronized void reset() throws IOException {
  145.         checkTimeout();
  146.         this.isWrapped.reset();
  147.     }

  148.     @Override
  149.     public long transferTo(OutputStream out) throws IOException {
  150.         checkTimeout();
  151.         return this.isWrapped.transferTo(out);
  152.     }

  153.     @Override
  154.     public void close() throws IOException {
  155.         checkTimeout(); // devo lanciare eccezione prima di chiamare la close, altrimenti rimane bloccato sulla close
  156.         this.isWrapped.close();
  157.     }
  158.    
  159.     // METODI SENZA CONTROLLO
  160.    
  161.     @Override
  162.     public synchronized void mark(int readlimit) {
  163.         this.isWrapped.mark(readlimit);
  164.     }
  165.    
  166.     @Override
  167.     public boolean markSupported() {
  168.         return this.isWrapped.markSupported();
  169.     }

  170. }