Parameter.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.web.lib.mvc;

  21. import java.io.Serializable;
  22. import java.io.UnsupportedEncodingException;
  23. import java.net.URLEncoder;
  24. import java.util.ArrayList;
  25. import java.util.List;

  26. /**
  27.  * Parameter
  28.  *
  29.  * @author Andrea Poli (apoli@link.it)
  30.  * @author $Author$
  31.  * @version $Rev$, $Date$
  32.  */
  33. public class Parameter implements Serializable {

  34.     /**
  35.      *
  36.      */
  37.     private static final long serialVersionUID = 1L;

  38.     private String name;
  39.     private String value;

  40.     public Parameter() {}
  41.    
  42.     public Parameter(String name,String value){
  43.         this.name = name;
  44.         this.value = value;
  45.     }

  46.     /****
  47.      *
  48.      * Costruttore per la costruzione di un parametro con link che va a finire nel titolo della pagina.
  49.      *  
  50.      *
  51.      * @param name
  52.      * @param baseUrl
  53.      * @param parameter
  54.      */
  55.     public Parameter(String name, String baseUrl, Parameter ... parameter){
  56.         this.name = name;

  57.         StringBuilder sb = new StringBuilder();

  58.         sb.append(baseUrl);

  59.         if(parameter != null && parameter.length > 0){
  60.             sb.append("?");

  61.             for (int i = 0; i < parameter.length; i++) {
  62.                 sb.append(parameter[i].toString());
  63.                 if(i < parameter.length -1){
  64.                     sb.append("&");
  65.                 }
  66.             }
  67.         }

  68.         this.value = sb.toString().replaceAll(" ", "%20");
  69.     }
  70.    
  71.     public Parameter(String name, String baseUrl, List<Parameter> parameterList){
  72.         this(name, baseUrl, parameterList.toArray(new Parameter[parameterList.size()]));
  73.     }
  74.    
  75.     public String getName() {
  76.         return this.name;
  77.     }

  78.     public void setName(String name) {
  79.         this.name = name;
  80.     }

  81.     public String getValue() {
  82.         return this.value;
  83.     }

  84.     public void setValue(String value) {
  85.         this.value = value;
  86.     }

  87.     @Override
  88.     public String toString(){
  89.         StringBuilder sb = new StringBuilder();
  90.         String val = null;
  91.         if(this.value!=null) {
  92.             try {
  93.                 val = URLEncoder.encode(this.value, "UTF-8");
  94.             } catch (UnsupportedEncodingException e) {
  95.                 val = this.value;
  96.             }
  97.         }
  98.         else {
  99.             val = "";
  100.         }
  101.         sb.append(this.name).append("=").append(val);
  102.         return sb.toString();
  103.     }

  104.     public static String estraiPathDaUrl(String url) {
  105.         if(url.contains("?")) {
  106.             return url.substring(0, url.indexOf("?"));
  107.         } else
  108.             return url;
  109.     }
  110.    
  111.     public static List<Parameter> estraiParametriDaUrl(String url) {
  112.         List<Parameter> lista = new ArrayList<>();
  113.         if(url.contains("?")) {
  114.             String queryString = url.substring(url.indexOf("?")+1);
  115.            
  116.             String [] pair = queryString.split("&");
  117.            
  118.             if(pair != null && pair.length > 0) {
  119.                 for (String pairTmp : pair) {
  120.                     String [] coppia = pairTmp.split("=");
  121.                     if(coppia != null && coppia.length > 0) {
  122.                         String key = coppia[0];
  123.                         String val = "";
  124.                         if(coppia.length == 2) {
  125.                             val = coppia[1];
  126.                         }
  127.                         lista.add(new Parameter(key, val));
  128.                     }
  129.                 }
  130.             }
  131.         }
  132.        
  133.         return lista;
  134.     }
  135.    
  136. }