DimensioneMessaggiUtils.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.pdd.core.controllo_traffico;

  21. import java.util.List;

  22. import org.openspcoop2.pdd.core.CostantiPdD;
  23. import org.openspcoop2.utils.LimitExceededIOException;
  24. import org.openspcoop2.utils.LimitedInputStreamEngine;
  25. import org.openspcoop2.utils.transport.http.HttpConstants;
  26. import org.slf4j.Logger;

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

  35.     private DimensioneMessaggiUtils() {}
  36.        
  37.     public static final boolean REQUEST = true;
  38.     public static final boolean RESPONSE = false;
  39.    
  40.     private static final String PREFIX_RILEVATO_HEADER_CONTENT_LENGTH = "Rilevato header "+HttpConstants.CONTENT_LENGTH+" ";
  41.     public static void verifyByContentLength(Logger log, List<String> l, SogliaDimensioneMessaggio requestLimitSize, LimitExceededNotifier notifier,
  42.             org.openspcoop2.utils.Map<Object> ctx, boolean request) throws LimitExceededIOException {
  43.         // in presenza di più header utilizzo quello con il valore maggiore
  44.         if(l!=null && !l.isEmpty()) {
  45.             long size = readContentLength(log, l, requestLimitSize);
  46.             if(l.size()>1) {
  47.                 String msgWarn = PREFIX_RILEVATO_HEADER_CONTENT_LENGTH+" multiplo "+l+"; viene usato il valore più grande '"+size+"'";
  48.                 log.warn(msgWarn);
  49.             }
  50.             long limitBytes = requestLimitSize.getSogliaKb()*1024; // trasformo kb in bytes
  51.             if(size>limitBytes) {
  52.                 LimitedInputStreamEngine.contentLenghtLimitExceeded(request ? CostantiPdD.PREFIX_LIMITED_REQUEST : CostantiPdD.PREFIX_LIMITED_RESPONSE,
  53.                         ctx, notifier, limitBytes);
  54.             }
  55.         }
  56.     }
  57.     private static long readContentLength(Logger log, List<String> l, SogliaDimensioneMessaggio requestLimitSize) {
  58.         long size = -1;
  59.         String error = null;
  60.         Exception exp = null;
  61.         for (String hdr : l) {
  62.             long lX = -1;
  63.             try {
  64.                 lX = Long.parseLong(hdr);
  65.                 if(lX<0) {
  66.                     error = PREFIX_RILEVATO_HEADER_CONTENT_LENGTH+"con un valore negativo '"+hdr+"'";
  67.                     // in caso di negative devo usare max length, sennò è un modo per superare la policy
  68.                     lX = Long.MAX_VALUE;
  69.                 }
  70.                 else if(lX==0l && !requestLimitSize.isUseContentLengthHeaderAcceptZeroValue()) {
  71.                     error = PREFIX_RILEVATO_HEADER_CONTENT_LENGTH+"con valore 0";
  72.                     // in caso di valore zero non accettato devo usare max length, sennò è un modo per superare la policy
  73.                     lX = Long.MAX_VALUE;
  74.                 }
  75.             }catch(Exception e) {
  76.                 error = PREFIX_RILEVATO_HEADER_CONTENT_LENGTH+"con valore malformato '"+hdr+"': "+e.getMessage();
  77.                 exp = e;
  78.                 // in caso di errore devo usare max length, sennò è un modo per superare la policy
  79.                 lX = Long.MAX_VALUE;
  80.             }
  81.             if(lX > size) {
  82.                 size = lX;
  83.             }
  84.             if(error!=null) {
  85.                 break;
  86.             }
  87.         }
  88.         return parseReadResultContentLength(log, size, error, exp);
  89.     }
  90.     private static long parseReadResultContentLength(Logger log, long size, String error, Exception exp) {
  91.         if(size<0) {
  92.             error = "Calcolo dimensione content lenght fallita? Rilevata dimensione minore di zero";
  93.             // in caso di errore devo usare max length, sennò è un modo per superare la policy
  94.             size = Long.MAX_VALUE;
  95.         }
  96.         if(error!=null) {
  97.             if(exp!=null) {
  98.                 log.error(error,exp);
  99.             }
  100.             else {
  101.                 log.error(error);
  102.             }
  103.         }
  104.         return size;
  105.     }
  106. }