IdentityHttpReader.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.credential;

  21. import java.util.Properties;

  22. import javax.servlet.http.HttpServletRequest;

  23. import org.openspcoop2.utils.transport.http.HttpServletCredential;
  24. import org.slf4j.Logger;

  25. /**
  26.  * Implementazione dell'interfaccia {@link IPrincipalReader} che utilizza la classe {@link HttpServletCredential} come strumento per leggere un header http dalla request.
  27.  *
  28.  * @author Pintori Giuliano (pintori@link.it)
  29.  * @author $Author$
  30.  * @version $Rev$, $Date$
  31.  */
  32. public class IdentityHttpReader implements IPrincipalReader{


  33.     private Logger log = null;

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

  38.     public static final String HEADER_NAME = "header";
  39.     private String headerName = "GovWay-Console-Principal"; // default
  40.    
  41.     public IdentityHttpReader(Logger log){
  42.         this.log = log;
  43.         this.log.debug("IdentityHttpReader inizializzato.");
  44.     }

  45.     @Override
  46.     public void init(Object... parametri) throws PrincipalReaderException {
  47.         if(parametri!=null && parametri.length>0) {
  48.             Object pObject = parametri[0];
  49.             if(pObject instanceof Properties) {
  50.                 Properties prop = (Properties) pObject;
  51.                 String v = prop.getProperty(HEADER_NAME);
  52.                 if(v!=null && !"".equals(v.trim())) {
  53.                     this.headerName = v;
  54.                 }
  55.             }
  56.         }
  57.     }

  58.     @Override
  59.     public String getPrincipal(HttpServletRequest request) throws PrincipalReaderException {
  60.         this.log.debug("Estrazione principal in corso...");

  61.         try{
  62.             String username = request.getHeader(this.headerName);

  63.             this.log.debug("Username trovato nell'header ["+this.headerName+"]: ["+username+"]");
  64.            
  65.             return username;
  66.         }catch (Exception e) {
  67.             this.log.error("Errore durante la lettura del principal: "+ e.getMessage(),e);
  68.             throw new PrincipalReaderException(e);
  69.         }
  70.     }
  71. }