StickyCookieConfig.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.behaviour.built_in.load_balance.sticky;

  21. import org.apache.cxf.common.util.StringUtils;
  22. import org.openspcoop2.pdd.core.behaviour.BehaviourException;

  23. /**
  24.  * Costanti
  25.  *
  26.  * @author Andrea Poli (apoli@link.it)
  27.  * @author $Author$
  28.  * @version $Rev$, $Date$
  29.  */
  30. public class StickyCookieConfig  {

  31.     private String name;
  32.     private String domain;
  33.     private String path;

  34.     public StickyCookieConfig(String cookie) throws BehaviourException {
  35.        
  36.         String formato = "name[[;domain=<domainValue>][;path=<pathValue>]]";
  37.        
  38.         if(cookie.contains(";")) {
  39.             String [] split = cookie.split(";");
  40.             if(split==null || split.length<=0) {
  41.                 throw new BehaviourException("Formato cookie fornito non valido (cookie:'"+cookie+"' errCode:1); il formato atteso è '"+formato+"'");
  42.             }
  43.             if(split.length>3) {
  44.                 throw new BehaviourException("Formato cookie fornito non valido (cookie:'"+cookie+"' error:'troppe parti ("+split.length+")'); il formato atteso è '"+formato+"'");
  45.             }
  46.             for (int i = 0; i < split.length; i++) {
  47.                 String v = split[i].trim();
  48.                 if(StringUtils.isEmpty(v)) {
  49.                     throw new BehaviourException("Formato cookie fornito non valido (cookie:'"+cookie+"' part:'"+v+"' error:'empty'); il formato atteso è '"+formato+"'");
  50.                 }
  51.                 if(i==0) {
  52.                     if(v.contains(";")) {
  53.                         throw new BehaviourException("Formato cookie fornito non valido (cookie:'"+cookie+"' part:'"+v+"' error:'; non atteso'); il formato atteso è '"+formato+"'");
  54.                     }
  55.                     this.name = v;
  56.                 }
  57.                 else {
  58.                     if(v.contains("=") == false) {
  59.                         throw new BehaviourException("Formato cookie fornito non valido (cookie:'"+cookie+"' part:'"+v+"' error:'= non trovato'); il formato atteso è '"+formato+"'");
  60.                     }
  61.                     String [] internalSplit = v.split("=");
  62.                     if(internalSplit==null) {
  63.                         throw new BehaviourException("Formato cookie fornito non valido (cookie:'"+cookie+"' part:'"+v+"' error:'struttura non valida (split null)'); il formato atteso è '"+formato+"'");
  64.                     }
  65.                     if(internalSplit.length!=2) {
  66.                         throw new BehaviourException("Formato cookie fornito non valido (cookie:'"+cookie+"' part:'"+v+"' error:'struttura non valida (split:"+internalSplit.length+")'); il formato atteso è '"+formato+"'");
  67.                     }
  68.                     if("domain".equals(internalSplit[0].trim())) {
  69.                         this.domain = internalSplit[1];
  70.                         if(this.domain!=null) {
  71.                             this.domain = this.domain.trim();
  72.                         }
  73.                         if(StringUtils.isEmpty(this.domain)) {
  74.                             this.domain = null;
  75.                         }
  76.                     }
  77.                     else if("path".equals(internalSplit[0].trim())) {
  78.                         this.path = internalSplit[1];
  79.                         if(this.path!=null) {
  80.                             this.path = this.path.trim();
  81.                         }
  82.                         if(StringUtils.isEmpty(this.path)) {
  83.                             this.path = null;
  84.                         }
  85.                     }
  86.                     else {
  87.                         throw new BehaviourException("Formato cookie fornito non valido (cookie:'"+cookie+"' part:'"+v+"' error:'elemento "+internalSplit[0]+" sconosciuto'); il formato atteso è '"+formato+"'");
  88.                     }
  89.                 }
  90.             }
  91.         }
  92.         else {
  93.             this.name = cookie;
  94.         }
  95.     }
  96.    
  97.     public String getName() {
  98.         return this.name;
  99.     }
  100.     public void setName(String name) {
  101.         this.name = name;
  102.     }
  103.     public String getDomain() {
  104.         return this.domain;
  105.     }
  106.     public void setDomain(String domain) {
  107.         this.domain = domain;
  108.     }
  109.     public String getPath() {
  110.         return this.path;
  111.     }
  112.     public void setPath(String path) {
  113.         this.path = path;
  114.     }
  115.    
  116. }