TimeoutInputStream.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.FilterInputStream;
  22. import java.io.IOException;
  23. import java.io.InputStream;

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

  33.     public static final MapKey<String> EXCEPTION_KEY = Map.newMapKey("TimeoutIOException");
  34.     public static final MapKey<String> ERROR_MSG_KEY = Map.newMapKey("TimeoutInputStream");
  35.     public static final String ERROR_MSG = "Read timed out";
  36.    
  37.     public TimeoutInputStream(InputStream is, int timeoutMs) throws IOException {
  38.         this(is, timeoutMs, null, null);
  39.     }
  40.     public TimeoutInputStream(InputStream is, int timeoutMs, Map<Object> ctx, ITimeoutNotifier notifier) throws IOException {
  41.         this(is, timeoutMs, null, ctx, notifier);
  42.     }
  43.     public TimeoutInputStream(InputStream is, int timeoutMs, String prefixError, Map<Object> ctx, ITimeoutNotifier notifier) throws IOException {
  44.         super(new TimeoutInputStreamEngine(is, timeoutMs, prefixError, ctx, notifier));
  45.     }
  46.    
  47.     public InputStream getIsWrapped() {
  48.         InputStream is = super.in;
  49.         if(is instanceof TimeoutInputStreamEngine) {
  50.             return ((TimeoutInputStreamEngine)is).getIsWrapped();
  51.         }
  52.         return is;
  53.     }
  54.    
  55.     public void disableCheckTimeout() {
  56.         InputStream is = super.in;
  57.         if(is instanceof TimeoutInputStreamEngine) {
  58.             ((TimeoutInputStreamEngine)is).disableCheckTimeout();
  59.         }
  60.     }
  61.     public void updateThreshold(int timeoutMs) throws IOException {
  62.         InputStream is = super.in;
  63.         if(is instanceof TimeoutInputStreamEngine) {
  64.             ((TimeoutInputStreamEngine)is).updateThreshold(timeoutMs);
  65.         }
  66.     }
  67.     public void updateContext(Map<Object> ctx) {
  68.         InputStream is = super.in;
  69.         if(is instanceof TimeoutInputStreamEngine) {
  70.             ((TimeoutInputStreamEngine)is).updateContext(ctx);
  71.         }
  72.     }
  73.     public void updateNotifier(ITimeoutNotifier notifier) {
  74.         InputStream is = super.in;
  75.         if(is instanceof TimeoutInputStreamEngine) {
  76.             ((TimeoutInputStreamEngine)is).updateNotifier(notifier);
  77.         }
  78.     }

  79. }