HttpStore.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;

  21. import java.io.Serializable;

  22. import org.openspcoop2.security.SecurityException;
  23. import org.openspcoop2.utils.transport.http.HttpOptions;
  24. import org.openspcoop2.utils.transport.http.HttpResponse;
  25. import org.openspcoop2.utils.transport.http.HttpUtilities;

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

  34.     /**
  35.      *
  36.      */
  37.     private static final long serialVersionUID = 1L;
  38.    
  39.     private byte[] storeBytes;
  40.     private String endpoint;
  41.     private Boolean trustAll = null;
  42.     private MerlinTruststore trustStoreSsl = null;
  43.     private CRLCertstore crlTrustStoreSsl = null;
  44.     private int connectionTimeout = HttpUtilities.HTTP_CONNECTION_TIMEOUT;
  45.     private int readTimeout = HttpUtilities.HTTP_READ_CONNECTION_TIMEOUT;
  46.     private HttpOptions [] options;
  47.    
  48.     @Override
  49.     public String toString() {
  50.         StringBuilder bf = new StringBuilder();
  51.         bf.append("HttpStore (").append(this.endpoint).append(")");
  52.         if(this.trustAll!=null) {
  53.             bf.append(" ").append(this.trustAll);
  54.         }
  55.         if(this.trustStoreSsl!=null) {
  56.             bf.append(" ").append(this.trustStoreSsl.toString());
  57.         }
  58.         if(this.crlTrustStoreSsl!=null) {
  59.             bf.append(" ").append(this.crlTrustStoreSsl.getWrappedCRLCertStore()!=null ? this.crlTrustStoreSsl.getWrappedCRLCertStore().toString() : this.crlTrustStoreSsl.toString());
  60.         }
  61.         if(this.options!=null && this.options.length>0) {
  62.             for (HttpOptions httpOptions : this.options) {
  63.                 bf.append(" ").append(httpOptions.toString());
  64.             }
  65.         }
  66.         return bf.toString();
  67.     }
  68.    
  69.     public HttpStore(String endpoint,
  70.             HttpOptions ... options) throws SecurityException{
  71.         this(endpoint,
  72.                 null, null,
  73.                 null, null,
  74.                 options);
  75.     }
  76.     public HttpStore(String endpoint,
  77.             Integer connectionTimeout, Integer readTimeout,
  78.             HttpOptions ... options) throws SecurityException{
  79.         this(endpoint,
  80.                 connectionTimeout, readTimeout,
  81.                 null, null,
  82.                 options);
  83.     }
  84.     public HttpStore(String endpoint,
  85.             Integer connectionTimeout, Integer readTimeout,
  86.             MerlinTruststore trustStoreSsl,
  87.             HttpOptions ... options) throws SecurityException{
  88.         this(endpoint,
  89.                 connectionTimeout, readTimeout,
  90.                 trustStoreSsl, null,
  91.                 options);
  92.     }
  93.     public HttpStore(String endpoint,
  94.             MerlinTruststore trustStoreSsl,
  95.             HttpOptions ... options) throws SecurityException{
  96.         this(endpoint,
  97.                 null, null,
  98.                 trustStoreSsl, null,
  99.                 options);
  100.     }
  101.     public HttpStore(String endpoint,
  102.             MerlinTruststore trustStoreSsl, CRLCertstore crlTrustStoreSsl,
  103.             HttpOptions ... options) throws SecurityException{
  104.         this(endpoint,
  105.                 null, null,
  106.                 trustStoreSsl, crlTrustStoreSsl,
  107.                 options);
  108.     }
  109.     public HttpStore(String endpoint,
  110.             Integer connectionTimeout, Integer readTimeout,
  111.             MerlinTruststore trustStoreSsl, CRLCertstore crlTrustStoreSsl,
  112.             HttpOptions ... options) throws SecurityException{
  113.         this(endpoint,
  114.                 connectionTimeout, readTimeout,
  115.                 null,
  116.                 trustStoreSsl, crlTrustStoreSsl,
  117.                 options);
  118.     }
  119.     public HttpStore(String endpoint,
  120.             Boolean trustAll,
  121.             HttpOptions ... options) throws SecurityException{
  122.         this(endpoint,
  123.                 null, null,
  124.                 trustAll,
  125.                 null, null,
  126.                 options);
  127.     }
  128.     public HttpStore(String endpoint,
  129.             Integer connectionTimeout, Integer readTimeout,
  130.             Boolean trustAll,
  131.             HttpOptions ... options) throws SecurityException{
  132.         this(endpoint,
  133.                 connectionTimeout, readTimeout,
  134.                 trustAll,
  135.                 null, null,
  136.                 options);
  137.     }
  138.    
  139.     private HttpStore(String endpoint,
  140.             Integer connectionTimeout, Integer readTimeout,
  141.             Boolean trustAll,
  142.             MerlinTruststore trustStoreSsl, CRLCertstore crlTrustStoreSsl,
  143.             HttpOptions ... options) throws SecurityException{
  144.        
  145.         this.endpoint = endpoint;
  146.         if(connectionTimeout!=null) {
  147.             this.connectionTimeout = connectionTimeout.intValue();
  148.         }
  149.         if(readTimeout!=null) {
  150.             this.readTimeout = readTimeout.intValue();
  151.         }
  152.         this.trustAll = trustAll;
  153.         this.trustStoreSsl = trustStoreSsl;
  154.         this.crlTrustStoreSsl = crlTrustStoreSsl;
  155.        
  156.         this.options = options;
  157.        
  158.         try{
  159.             if(endpoint==null){
  160.                 throw new SecurityException("Endpoint per lo Store non indicato");
  161.             }
  162.            
  163.             HttpResponse httpResponse = null;
  164.             if(
  165.                     (
  166.                             this.endpoint.startsWith("https:") &&
  167.                             this.trustStoreSsl==null &&
  168.                             (this.trustAll==null || !this.trustAll.booleanValue())
  169.                     )
  170.                     ||
  171.                     this.endpoint.startsWith("http:") ) {
  172.                 httpResponse = HttpUtilities.getHTTPResponse(this.endpoint,this.readTimeout, this.connectionTimeout, this.options);
  173.             }
  174.             else if(this.trustAll!=null) {
  175.                 httpResponse = HttpUtilities.getHTTPSResponse_trustAllCerts(this.endpoint, this.readTimeout, this.connectionTimeout, this.options);
  176.             }
  177.             else {
  178.                 httpResponse = HttpUtilities.getHTTPSResponse(this.endpoint, this.readTimeout, this.connectionTimeout,
  179.                         this.trustStoreSsl.getTrustStore().getKeystore(),
  180.                         this.crlTrustStoreSsl!=null ? this.crlTrustStoreSsl.getCertStore() : null,
  181.                         this.options);
  182.             }
  183.             if(httpResponse==null || httpResponse.getContent()==null) {
  184.                 throw new SecurityException("Store '"+this.endpoint+"' unavailable");
  185.             }
  186.             if(httpResponse.getResultHTTPOperation()!=200) {
  187.                 throw new SecurityException("Retrieve store '"+this.endpoint+"' failed (returnCode:"+httpResponse.getResultHTTPOperation()+")");
  188.             }
  189.             this.storeBytes = httpResponse.getContent();

  190.         }catch(Exception e){
  191.             throw new SecurityException(e.getMessage(),e);
  192.         }
  193.        
  194.     }
  195.    
  196.     public byte[] getStoreBytes() {
  197.         return this.storeBytes;
  198.     }

  199. }