HttpStoreCache.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.security.keystore.cache;

  21. import java.util.List;

  22. import org.openspcoop2.security.SecurityException;
  23. import org.openspcoop2.security.keystore.CRLCertstore;
  24. import org.openspcoop2.security.keystore.HttpStore;
  25. import org.openspcoop2.security.keystore.MerlinTruststore;
  26. import org.openspcoop2.utils.transport.http.HttpOptions;

  27. /**
  28.  * HttpStoreCache
  29.  *
  30.  * @author Andrea Poli (apoli@link.it)
  31.  * @author $Author$
  32.  * @version $Rev$, $Date$
  33.  */
  34. public class HttpStoreCache extends AbstractKeystoreCache<HttpStore> {

  35.     @Override
  36.     public HttpStore createKeystore(String key, Object... params) throws SecurityException{
  37.         if(params==null){
  38.             throw new SecurityException("Params is null");
  39.         }
  40.         String endpoint = key;
  41.         if(params.length==0){
  42.             return new HttpStore(endpoint);
  43.         }
  44.         else if(params.length==1){
  45.             if( ! (params[0] instanceof MerlinTruststore) && ! (params[0] instanceof Boolean) && ! (params[0] instanceof HttpOptions[]) && ! (params[0] instanceof List<?>) ){
  46.                 throw new SecurityException("Param[0] must be MerlinTruststore (trustStoreSsl) or Boolean (trustAll) or HttpOptions[] or List<HttpOptions>");
  47.             }
  48.             if(params[0] instanceof MerlinTruststore) {
  49.                 MerlinTruststore trustStoreSsl = (MerlinTruststore) params[0];
  50.                 return new HttpStore(endpoint, trustStoreSsl);
  51.             }
  52.             else if(params[0] instanceof Boolean) {
  53.                 Boolean trustAll = (Boolean) params[0];
  54.                 return new HttpStore(endpoint, trustAll);
  55.             }
  56.             else {
  57.                 if(params[0]!=null) {
  58.                     HttpOptions[] options = null;
  59.                     if(params[0] instanceof List) {
  60.                         List<?> l = (List<?>) params[0];
  61.                         if(!l.isEmpty()) {
  62.                             options = new HttpOptions[1];
  63.                             for (int i = 0; i < l.size(); i++) {
  64.                                 Object o = l.get(i);
  65.                                 if(o instanceof HttpOptions) {
  66.                                     options[i] = (HttpOptions) o;
  67.                                 }
  68.                                 else {
  69.                                     if(o == null) {
  70.                                         throw new SecurityException("Param[0] with null element at position '"+i+"'");
  71.                                     }
  72.                                     else {
  73.                                         throw new SecurityException("Param[0] with wrong type '"+o.getClass().getName()+"' at position '"+i+"'");
  74.                                     }
  75.                                 }
  76.                             }
  77.                         }
  78.                     }
  79.                     else {
  80.                         options = (HttpOptions[]) params[0];
  81.                     }
  82.                     return new HttpStore(endpoint, options);
  83.                 }
  84.                 else {
  85.                     return new HttpStore(endpoint);
  86.                 }
  87.             }
  88.         }
  89.         else if(params.length==2){
  90.             if( ! (params[0] instanceof MerlinTruststore) && ! (params[0] instanceof Boolean) && ! (params[0] instanceof Integer)){
  91.                 throw new SecurityException("Param[0] must be Integer (connectionTimeout) or MerlinTruststore (trustStoreSsl) or Boolean (trustAll)");
  92.             }
  93.             if(params[0] instanceof MerlinTruststore){
  94.                 if( ! (params[1] instanceof CRLCertstore) && ! (params[1] instanceof HttpOptions[]) && params[1]!=null ){
  95.                     throw new SecurityException("Param[1] must be CRLCertstore (crlStoreSsl) or HttpOptions[]");
  96.                 }
  97.                 MerlinTruststore trustStoreSsl = (MerlinTruststore) params[0];
  98.                 if(params[1] instanceof CRLCertstore) {
  99.                     CRLCertstore crlStoreSsl = (CRLCertstore) params[1];
  100.                     return new HttpStore(endpoint, trustStoreSsl, crlStoreSsl);
  101.                 }
  102.                 else {
  103.                     if(params[1]!=null ) {
  104.                         HttpOptions[] options = (HttpOptions[]) params[1];
  105.                         return new HttpStore(endpoint, trustStoreSsl, options);
  106.                     }
  107.                     else {
  108.                         return new HttpStore(endpoint, trustStoreSsl);
  109.                     }
  110.                 }
  111.             }
  112.             else if(params[0] instanceof Boolean){
  113.                 if( ! (params[1] instanceof HttpOptions[]) && params[1]!=null ){
  114.                     throw new SecurityException("Param[1] must be HttpOptions[]");
  115.                 }
  116.                 Boolean trustAll = (Boolean) params[0];
  117.                 if(params[1]!=null ) {
  118.                     HttpOptions[] options = (HttpOptions[]) params[1];
  119.                     return new HttpStore(endpoint, trustAll, options);
  120.                 }else {
  121.                     return new HttpStore(endpoint, trustAll);
  122.                 }
  123.             }
  124.             else {
  125.                 if( ! (params[1] instanceof Integer) ){
  126.                     throw new SecurityException("Param[1] must be Integer (readTimeout)");
  127.                 }
  128.                 Integer connectionTimeout = (Integer) params[0];
  129.                 Integer readTimeout = (Integer) params[1];
  130.                 return new HttpStore(endpoint, connectionTimeout, readTimeout);
  131.             }
  132.         }
  133.         else if(params.length==3){
  134.             if( ! (params[0] instanceof Integer)){
  135.                 throw new SecurityException("Param[0] must be Integer (connectionTimeout)");
  136.             }
  137.             if( ! (params[1] instanceof Integer) ){
  138.                 throw new SecurityException("Param[1] must be Integer (readTimeout)");
  139.             }
  140.             if( ! (params[2] instanceof MerlinTruststore) && ! (params[2] instanceof Boolean) && ! (params[2] instanceof HttpOptions[]) && params[2]!=null){
  141.                 throw new SecurityException("Param[2] must be MerlinTruststore (trustStoreSsl) or Boolean (trustAll) or HttpOptions[]");
  142.             }
  143.            
  144.             Integer connectionTimeout = (Integer) params[0];
  145.             Integer readTimeout = (Integer) params[1];
  146.             if(params[2] instanceof MerlinTruststore){
  147.                 MerlinTruststore trustStoreSsl = (MerlinTruststore) params[2];
  148.                 return new HttpStore(endpoint,
  149.                         connectionTimeout, readTimeout,
  150.                         trustStoreSsl);
  151.             }
  152.             else if(params[2] instanceof Boolean){
  153.                 Boolean trustAll = (Boolean) params[2];
  154.                 return new HttpStore(endpoint,
  155.                         connectionTimeout, readTimeout,
  156.                         trustAll);
  157.             }
  158.             else {
  159.                 if(params[2]!=null ) {
  160.                     HttpOptions[] options = (HttpOptions[]) params[2];
  161.                     return new HttpStore(endpoint,
  162.                             connectionTimeout, readTimeout,
  163.                             options);
  164.                 }
  165.                 else {
  166.                     return new HttpStore(endpoint,
  167.                             connectionTimeout, readTimeout);
  168.                 }
  169.             }
  170.         }
  171.         else if(params.length==4){
  172.             if( ! (params[0] instanceof Integer)){
  173.                 throw new SecurityException("Param[0] must be Integer (connectionTimeout)");
  174.             }
  175.             if( ! (params[1] instanceof Integer) ){
  176.                 throw new SecurityException("Param[1] must be Integer (readTimeout)");
  177.             }
  178.             if( ! (params[2] instanceof MerlinTruststore) && ! (params[2] instanceof Boolean) ){
  179.                 throw new SecurityException("Param[2] must be MerlinTruststore (trustStoreSsl) or Boolean (trustAll)");
  180.             }
  181.             Integer connectionTimeout = (Integer) params[0];
  182.             Integer readTimeout = (Integer) params[1];
  183.            
  184.             if( params[2] instanceof MerlinTruststore) {
  185.                 MerlinTruststore trustStoreSsl = (MerlinTruststore) params[2];
  186.                 if( ! (params[3] instanceof CRLCertstore) &&  ! (params[3] instanceof HttpOptions[]) && params[3]!=null ){
  187.                     throw new SecurityException("Param[3] must be CRLCertstore (crlStoreSsl) or HttpOptions[]");
  188.                 }
  189.                 if(params[3] instanceof CRLCertstore) {
  190.                     CRLCertstore crlStoreSsl = (CRLCertstore) params[3];
  191.                     return new HttpStore(endpoint,
  192.                             connectionTimeout, readTimeout,
  193.                             trustStoreSsl, crlStoreSsl);
  194.                 }
  195.                 else {
  196.                     if(params[3]!=null ) {
  197.                         HttpOptions[] options = (HttpOptions[]) params[3];
  198.                         return new HttpStore(endpoint,
  199.                                 connectionTimeout, readTimeout,
  200.                                 trustStoreSsl, options);
  201.                     }
  202.                     else {
  203.                         return new HttpStore(endpoint,
  204.                                 connectionTimeout, readTimeout,
  205.                                 trustStoreSsl);
  206.                     }
  207.                 }
  208.             }
  209.             else {
  210.                 Boolean trustAll = (Boolean) params[2];
  211.                 if( ! (params[3] instanceof HttpOptions[]) && params[3]!=null ){
  212.                     throw new SecurityException("Param[3] must be HttpOptions[]");
  213.                 }
  214.                 if(params[3]!=null ) {
  215.                     HttpOptions[] options = (HttpOptions[]) params[3];
  216.                     return new HttpStore(endpoint,
  217.                             connectionTimeout, readTimeout,
  218.                             trustAll, options);
  219.                 }
  220.                 else {
  221.                     return new HttpStore(endpoint,
  222.                             connectionTimeout, readTimeout,
  223.                             trustAll);
  224.                 }
  225.             }
  226.         }
  227.         else if(params.length==5){
  228.             if( ! (params[0] instanceof Integer)){
  229.                 throw new SecurityException("Param[0] must be Integer (connectionTimeout)");
  230.             }
  231.             if( ! (params[1] instanceof Integer) ){
  232.                 throw new SecurityException("Param[1] must be Integer (readTimeout)");
  233.             }
  234.             if( ! (params[2] instanceof MerlinTruststore) ){
  235.                 throw new SecurityException("Param[2] must be MerlinTruststore (trustStoreSsl)");
  236.             }
  237.             if( ! (params[3] instanceof CRLCertstore)){
  238.                 throw new SecurityException("Param[3] must be CRLCertstore (crlStoreSsl)");
  239.             }
  240.             if( ! (params[4] instanceof HttpOptions[]) && params[4]!=null ){
  241.                 throw new SecurityException("Param[4] must be HttpOptions[]");
  242.             }
  243.             Integer connectionTimeout = (Integer) params[0];
  244.             Integer readTimeout = (Integer) params[1];
  245.             MerlinTruststore trustStoreSsl = (MerlinTruststore) params[2];
  246.             CRLCertstore crlStoreSsl = (CRLCertstore) params[3];
  247.             if(params[4]!=null ) {
  248.                 HttpOptions[] options = (HttpOptions[]) params[4];
  249.                 return new HttpStore(endpoint,
  250.                         connectionTimeout, readTimeout,
  251.                         trustStoreSsl, crlStoreSsl,
  252.                         options);
  253.             }
  254.             else {
  255.                 return new HttpStore(endpoint,
  256.                         connectionTimeout, readTimeout,
  257.                         trustStoreSsl, crlStoreSsl);
  258.             }
  259.         }
  260.         else{
  261.             throw new SecurityException("Params [lenght:"+params.length+"] not supported");
  262.         }
  263.     }

  264.     @Override
  265.     public String getPrefixKey() {
  266.         return "HttpStore ";
  267.     }
  268. }