ExternalResource.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.id.IDUtilities;
  24. import org.openspcoop2.utils.id.UniqueIdentifierManager;
  25. import org.openspcoop2.utils.transport.http.ExternalResourceConfig;
  26. import org.openspcoop2.utils.transport.http.ExternalResourceUtils;

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

  35.     private static final long serialVersionUID = 1L;

  36.     public static final String EXTERNAL_RESOURCE_PROTOCOL = "external_resource://";
  37.    
  38.     private String id;
  39.     private byte[] resource;
  40.    
  41.     public ExternalResource(String resource, ExternalResourceConfig externalConfig) throws SecurityException {
  42.         this(generateId() ,resource, externalConfig);
  43.     }
  44.     public ExternalResource(String id, String resource, ExternalResourceConfig externalConfig) throws SecurityException {
  45.         this.id = id;
  46.         try {
  47.             this.resource = ExternalResourceUtils.readResource(resource, externalConfig);
  48.         }catch(Exception e) {
  49.             throw new SecurityException(e.getMessage(),e);
  50.         }
  51.     }

  52.     private static String generateId() throws SecurityException {
  53.         String uniqueId = null;
  54.         if(UniqueIdentifierManager.isInitialized()) {
  55.             try {
  56.                 uniqueId = UniqueIdentifierManager.newUniqueIdentifier().getAsString();
  57.             }catch (Exception e) {
  58.                 throw new SecurityException(e.getMessage(),e);
  59.             }
  60.         }
  61.         if(uniqueId==null) {
  62.             uniqueId = IDUtilities.getUniqueSerialNumber()+"";
  63.         }
  64.         return uniqueId;
  65.     }
  66.        
  67.     public String getId() {
  68.         return this.id;
  69.     }
  70.     public void setId(String id) {
  71.         this.id = id;
  72.     }
  73.     public byte[] getResource() {
  74.         return this.resource;
  75.     }
  76.     public void setResource(byte[] resource) {
  77.         this.resource = resource;
  78.     }
  79.     public String getPathId() {
  80.         return EXTERNAL_RESOURCE_PROTOCOL+this.id;
  81.     }
  82.    
  83. }