ModISecurityRestToken.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.protocol.modipa;

  21. import org.openspcoop2.protocol.sdk.ProtocolException;
  22. import org.openspcoop2.protocol.sdk.constants.TipoSerializzazione;
  23. import org.openspcoop2.utils.transport.http.HttpConstants;

  24. /**
  25.  * ModISecurityRestToken
  26.  *
  27.  * @author Poli Andrea (apoli@link.it)
  28.  * @author $Author$
  29.  * @version $Rev$, $Date$
  30.  */
  31. public class ModISecurityRestToken extends AbstractModISecurityToken<String> {

  32.     private String tokenIntegrityHeaderName;
  33.     private String tokenIntegrity;
  34.     private String headerName;
  35.    
  36.     public ModISecurityRestToken(String tokenAuthorization,
  37.             String tokenIntegrityHeaderName, String tokenIntegrity) {
  38.         super(tokenAuthorization);
  39.         this.tokenIntegrityHeaderName = tokenIntegrityHeaderName;
  40.         this.tokenIntegrity = tokenIntegrity;
  41.     }
  42.     public ModISecurityRestToken(String headerName, String token) {
  43.         super(token);
  44.         this.headerName = headerName;
  45.     }

  46.     @Override
  47.     public String toString(TipoSerializzazione tipoSerializzazione) throws ProtocolException{
  48.         switch (tipoSerializzazione) {
  49.         case DEFAULT:
  50.             StringBuilder sb = new StringBuilder();
  51.             if(this.tokenIntegrity!=null) {
  52.                 // 2 header
  53.                 sb.append(HttpConstants.AUTHORIZATION).append(": ").append(HttpConstants.AUTHORIZATION_PREFIX_BEARER).append(this.getToken());
  54.                 sb.append("\n");
  55.                 sb.append(this.tokenIntegrityHeaderName).append(": ").append(this.tokenIntegrity);
  56.             }
  57.             else {
  58.                 if(HttpConstants.AUTHORIZATION.equals(this.headerName)) {
  59.                     sb.append(HttpConstants.AUTHORIZATION).append(": ").append(HttpConstants.AUTHORIZATION_PREFIX_BEARER).append(this.getToken());
  60.                 }
  61.                 else {
  62.                     sb.append(this.headerName).append(": ").append(this.tokenIntegrity);
  63.                 }
  64.             }
  65.            
  66.             if(this.tokenAudit!=null) {
  67.                 sb.append("\n");
  68.                 sb.append(this.tokenAuditHeaderName).append(": ").append(this.tokenAudit);
  69.             }
  70.            
  71.             return sb.toString();
  72.         default:
  73.             throw new ProtocolException("Tipo di serializzazione ["+tipoSerializzazione+"] non supportata");
  74.         }
  75.     }
  76.    
  77.     @Override
  78.     public byte[] toByteArray(TipoSerializzazione tipoSerializzazione) throws ProtocolException{
  79.         switch (tipoSerializzazione) {
  80.         case DEFAULT:
  81.             /**return this.getToken().getBytes();*/
  82.             return this.toString(tipoSerializzazione).getBytes();
  83.         default:
  84.             throw new ProtocolException("Tipo di serializzazione ["+tipoSerializzazione+"] non supportata");
  85.         }
  86.     }
  87.    
  88. }