LimitedInputStream.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 LimitedInputStream extends FilterInputStream {

  33.     public static final MapKey<String> EXCEPTION_KEY = Map.newMapKey("LimitExceededIOException");
  34.     public static final MapKey<String> ERROR_MSG_KEY = Map.newMapKey("LimitedInputStream");
  35.     public static final String ERROR_PAYLOAD_TOO_LARGE_MSG = "Payload too large";
  36.     public static final String ERROR_CONTENT_LENGTH_EXCEEDED_MSG = "Content-Length exceeds the allowed limit";
  37.    
  38.     public LimitedInputStream(InputStream is, long limitBytes) throws IOException {
  39.         this(is, limitBytes, null, null, null);
  40.     }
  41.     public LimitedInputStream(InputStream is, long limitBytes, Map<Object> ctx) throws IOException {
  42.         this(is, limitBytes, null, ctx, null);
  43.     }
  44.     public LimitedInputStream(InputStream is, long limitBytes, Map<Object> ctx, ILimitExceededNotifier notifier) throws IOException {
  45.         this(is, limitBytes, null, ctx, notifier);
  46.     }
  47.     public LimitedInputStream(InputStream is, long limitBytes, String prefixError, Map<Object> ctx, ILimitExceededNotifier notifier) throws IOException {
  48.         super(new LimitedInputStreamEngine(is, limitBytes, prefixError, ctx, notifier));
  49.     }
  50.    
  51.     public InputStream getIsWrapped() {
  52.         InputStream is = super.in;
  53.         if(is instanceof LimitedInputStreamEngine) {
  54.             return ((LimitedInputStreamEngine)is).getIsWrapped();
  55.         }
  56.         return is;
  57.     }
  58.    
  59.     public void disableCheck() {
  60.         InputStream is = super.in;
  61.         if(is instanceof LimitedInputStreamEngine) {
  62.             ((LimitedInputStreamEngine)is).disableCheck();
  63.         }
  64.     }
  65.     public void updateThreshold(long limitBytes) throws IOException {
  66.         InputStream is = super.in;
  67.         if(is instanceof LimitedInputStreamEngine) {
  68.             ((LimitedInputStreamEngine)is).updateThreshold(limitBytes);
  69.         }
  70.     }
  71.     public void updateContext(Map<Object> ctx) {
  72.         InputStream is = super.in;
  73.         if(is instanceof LimitedInputStreamEngine) {
  74.             ((LimitedInputStreamEngine)is).updateContext(ctx);
  75.         }
  76.     }
  77.     public void updateNotifier(ILimitExceededNotifier notifier) {
  78.         InputStream is = super.in;
  79.         if(is instanceof LimitedInputStreamEngine) {
  80.             ((LimitedInputStreamEngine)is).updateNotifier(notifier);
  81.         }
  82.     }

  83. }