HttpHeaderTypes.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.transport.http;

  21. import java.io.BufferedReader;
  22. import java.io.InputStream;
  23. import java.io.InputStreamReader;
  24. import java.util.ArrayList;
  25. import java.util.HashMap;
  26. import java.util.List;
  27. import java.util.Map;

  28. import org.openspcoop2.utils.UtilsException;

  29. /**
  30.  * HttpHeaderTypes
  31.  *
  32.  * @author Poli Andrea (apoli@link.it)
  33.  * @author $Author$
  34.  * @version $Rev$, $Date$
  35.  */
  36. public class HttpHeaderTypes {

  37.     private Map<String, String> requestStandard = new HashMap<>();
  38.     private Map<String, String> requestNonStandard = new HashMap<>();
  39.    
  40.     private Map<String, String> responseStandard = new HashMap<>();
  41.     private Map<String, String> responseNonStandard = new HashMap<>();
  42.    
  43.     private HttpHeaderTypes() throws UtilsException{
  44.        
  45.         InputStream is =null;
  46.         BufferedReader br = null;
  47.         InputStreamReader ir = null;
  48.         try{
  49.             String file = "/org/openspcoop2/utils/transport/http/httpheader.types";
  50.             is = HttpHeaderTypes.class.getResourceAsStream(file);
  51.             if(is==null){
  52.                 throw new UtilsException("File ["+file+"] in classpath not found");
  53.             }
  54.            
  55.             ir = new InputStreamReader(is);
  56.             br = new BufferedReader(ir);
  57.             String line;
  58.             while ((line = br.readLine()) != null) {
  59.                 line = line.trim();
  60.                 if(!line.startsWith("#") && !"".equals(line)){
  61.                     String [] tmp = line.split(" ");
  62.                     if(tmp.length<4){
  63.                         throw new UtilsException("Line ["+line+"] format wrong");
  64.                     }
  65.                    
  66.                     String property = tmp[0];
  67.                    
  68.                     String richiestaRisposta = tmp[1];
  69.                    
  70.                     String standardNonStandard = tmp[2];
  71.                    
  72.                     int length = property.length() + 1 + richiestaRisposta.length() + 1 + standardNonStandard.length() + 1;
  73.                     String descrizione = line.substring(length);
  74.                    
  75.                     if("[request]".equalsIgnoreCase(richiestaRisposta)){
  76.                         if("[standard]".equalsIgnoreCase(standardNonStandard)){
  77.                             this.requestStandard.put(property, descrizione);
  78.                         }
  79.                         else if("[non-standard]".equalsIgnoreCase(standardNonStandard)){
  80.                             this.requestNonStandard.put(property, descrizione);
  81.                         }
  82.                         else{
  83.                             throw new UtilsException("Line ["+line+"] with wrong value ["+standardNonStandard+"] in third parameter (expected: [standard] o [non-standard] )");
  84.                         }
  85.                     }
  86.                     else if("[response]".equalsIgnoreCase(richiestaRisposta)){
  87.                         if("[standard]".equalsIgnoreCase(standardNonStandard)){
  88.                             this.responseStandard.put(property, descrizione);
  89.                         }
  90.                         else if("[non-standard]".equalsIgnoreCase(standardNonStandard)){
  91.                             this.responseNonStandard.put(property, descrizione);
  92.                         }
  93.                         else{
  94.                             throw new UtilsException("Line ["+line+"] with wrong value ["+standardNonStandard+"] in third parameter (expected: [standard] o [non-standard] )");
  95.                         }
  96.                     }
  97.                     else{
  98.                         throw new UtilsException("Line ["+line+"] with wrong value ["+richiestaRisposta+"] in second parameter (expected: [request] o [response] )");
  99.                     }
  100.                 }
  101.             }
  102.            
  103.         }catch(Exception e){
  104.             throw new UtilsException(e.getMessage(),e);
  105.         }finally{
  106.             try{
  107.                 if(br!=null){
  108.                     br.close();
  109.                 }
  110.             }catch(Exception eClose){
  111.                 // ignore
  112.             }
  113.             try{
  114.                 if(ir!=null){
  115.                     ir.close();
  116.                 }
  117.             }catch(Exception eClose){
  118.                 // ignore
  119.             }
  120.             try{
  121.                 if(is!=null){
  122.                     is.close();
  123.                 }
  124.             }catch(Exception eClose){
  125.                 // close
  126.             }
  127.         }
  128.        
  129.     }
  130.    
  131.     public List<String> getHeaders(){
  132.         List<String> list = new ArrayList<>();
  133.         list.addAll(this.getRequestHeaders());
  134.         list.addAll(this.getResponseHeaders());
  135.         return list;
  136.     }
  137.    
  138.     public List<String> getRequestHeaders(){
  139.         List<String> list = new ArrayList<>();
  140.         list.addAll(this.getRequestStandardHeaders());
  141.         list.addAll(this.getRequestNonStandardHeaders());
  142.         return list;
  143.     }
  144.     public List<String> getRequestStandardHeaders(){
  145.         List<String> list = new ArrayList<>();
  146.         list.addAll(this.requestStandard.keySet());
  147.         return list;
  148.     }
  149.     public List<String> getRequestNonStandardHeaders(){
  150.         List<String> list = new ArrayList<>();
  151.         list.addAll(this.requestNonStandard.keySet());
  152.         return list;
  153.     }
  154.    
  155.     public List<String> getResponseHeaders(){
  156.         List<String> list = new ArrayList<>();
  157.         list.addAll(this.getResponseStandardHeaders());
  158.         list.addAll(this.getResponseNonStandardHeaders());
  159.         return list;
  160.     }
  161.     public List<String> getResponseStandardHeaders(){
  162.         List<String> list = new ArrayList<>();
  163.         list.addAll(this.responseStandard.keySet());
  164.         return list;
  165.     }
  166.     public List<String> getResponseNonStandardHeaders(){
  167.         List<String> list = new ArrayList<>();
  168.         list.addAll(this.responseNonStandard.keySet());
  169.         return list;
  170.     }
  171.    

  172.    
  173.    
  174.     // static
  175.    
  176.     private static HttpHeaderTypes httpHeaderTypes = null;
  177.     private static synchronized void init() throws UtilsException{
  178.         if(httpHeaderTypes==null){
  179.             httpHeaderTypes = new HttpHeaderTypes();
  180.         }
  181.     }
  182.     public static HttpHeaderTypes getInstance() throws UtilsException{
  183.         if(httpHeaderTypes==null){
  184.             // spotbugs warning 'SING_SINGLETON_GETTER_NOT_SYNCHRONIZED'
  185.             synchronized (HttpHeaderTypes.class) {
  186.                 init();
  187.             }
  188.         }
  189.         return httpHeaderTypes;
  190.     }
  191.    
  192. }