LimitedInputStreamEngine.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.  * LimitedInputStream
  26.  *
  27.  *
  28.  * @author Poli Andrea (apoli@link.it)
  29.  * @author $Author$
  30.  * @version $Rev$, $Date$
  31.  */
  32. public class LimitedInputStreamEngine extends FilterInputStream {

  33.     private long limitBytes;
  34.     private long count;
  35.    
  36.     private InputStream isWrapped = null;
  37.     private String prefixError = "";
  38.     private Map<Object> ctx;
  39.     private boolean checkDisabled = false;

  40.     private ILimitExceededNotifier notifier;
  41.    
  42.     protected LimitedInputStreamEngine(InputStream inputStream, long limitBytes, String prefixError, Map<Object> ctx, ILimitExceededNotifier notifier) throws IOException {
  43.         super(inputStream);
  44.         this.limitBytes = limitBytes;
  45.        
  46.         this.isWrapped = inputStream;
  47.         if(prefixError!=null) {
  48.             this.prefixError = prefixError;
  49.         }
  50.         this.ctx = ctx;
  51.         if(this.limitBytes<=0) {
  52.             throw new IOException("Invalid limit");
  53.         }
  54.        
  55.         this.notifier = notifier;
  56.     }
  57.    
  58.     public InputStream getIsWrapped() {
  59.         return this.isWrapped;
  60.     }

  61.     protected void disableCheck() {
  62.         this.checkDisabled = true;
  63.     }
  64.     protected void updateThreshold(long limitBytes) throws IOException {
  65.         if(this.limitBytes<=0) {
  66.             throw new IOException("Invalid limit");
  67.         }
  68.         this.limitBytes = limitBytes;
  69.     }
  70.     protected void updateContext(Map<Object> ctx) {
  71.         this.ctx = ctx;
  72.     }
  73.     protected void updateNotifier(ILimitExceededNotifier notifier) {
  74.         this.notifier = notifier;
  75.     }

  76.     private void checkLimit() throws IOException {
  77.         if(this.checkDisabled) {
  78.             return; // e' stato disabilitato dopo averlo creato
  79.         }
  80.         if (this.count > this.limitBytes) {
  81.            
  82.             /**System.out.println("Raggiunto limite dopo aver letto: "+this.count);*/
  83.            
  84.             payloadTooLarge(this.prefixError, this.ctx, this.notifier, this.count);
  85.            
  86.         }
  87.     }
  88.     public static void payloadTooLarge(String prefixError, Map<Object> ctx, ILimitExceededNotifier notifier, long count) throws LimitExceededIOException {
  89.         limitExceeded(prefixError, LimitedInputStream.ERROR_PAYLOAD_TOO_LARGE_MSG, false, ctx, notifier, count);
  90.     }
  91.     public static void contentLenghtLimitExceeded(String prefixError, Map<Object> ctx, ILimitExceededNotifier notifier, long count) throws LimitExceededIOException {
  92.         limitExceeded(prefixError, LimitedInputStream.ERROR_CONTENT_LENGTH_EXCEEDED_MSG, true, ctx, notifier, count);
  93.     }
  94.     private static void limitExceeded(String prefixError, String error, boolean contentLengthExceeded, Map<Object> ctx, ILimitExceededNotifier notifier, long count) throws LimitExceededIOException {
  95.         String errorMsg = prefixError+error;
  96.         if(ctx!=null) {
  97.             ctx.put(LimitedInputStream.ERROR_MSG_KEY, errorMsg);
  98.         }
  99.         LimitExceededIOException exc = new LimitExceededIOException(errorMsg);
  100.         if(ctx!=null) {
  101.             ctx.put(LimitedInputStream.EXCEPTION_KEY, exc);
  102.         }
  103.        
  104.         if(notifier!=null) {
  105.             notifier.notify(count, contentLengthExceeded);
  106.         }
  107.        
  108.         throw exc;
  109.     }

  110.     @Override
  111.     public int read() throws IOException {
  112.         int res = super.read();
  113.         if (res != -1) {
  114.             this.count++;
  115.             checkLimit();
  116.         }
  117.         return res;
  118.     }

  119.     @Override
  120.     public int read(byte[] b, int off, int len) throws IOException {
  121.         int res = super.read(b, off, len);
  122.         if (res > 0) {
  123.             this.count += res;
  124.             checkLimit();
  125.         }
  126.         return res;
  127.     }

  128. }