MessageSecurityContext_wss4j.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.message.wss4j;

  21. import org.slf4j.Logger;
  22. import org.apache.wss4j.dom.engine.WSSConfig;
  23. import org.openspcoop2.security.SecurityException;
  24. import org.openspcoop2.security.message.IMessageSecurityContext;
  25. import org.openspcoop2.security.message.MessageSecurityContext;

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

  34.     private Logger log = null;

  35.     @Override
  36.     public void init(MessageSecurityContext wssContext) throws SecurityException {

  37.         this.log = wssContext.getLog();
  38.        
  39.         try{        
  40.             MessageSecurityContext_wss4j.initWsuIdAllocator(wssContext.getPrefixWsuId(),this.log);
  41.         }catch(Exception e){
  42.             this.log.error("Inizializzazione wsu id allocator non riuscita: "+e.getMessage(),e);
  43.             throw new SecurityException(e.getMessage(),e);
  44.         }

  45.     }


  46.     /** WSS Id Allocator */
  47.     private static org.apache.wss4j.dom.WsuIdAllocator wsuIdAllocator = null;
  48.     private static String prefixWsuId = null;
  49.     public static synchronized void initWsuIdAllocator(String prefixWsuIdParam,Logger log) throws Exception{
  50.         if(MessageSecurityContext_wss4j.wsuIdAllocator==null){
  51.             WSSConfig config = WSSConfig.getNewInstance();
  52.             MessageSecurityContext_wss4j.prefixWsuId=prefixWsuIdParam;
  53.             if(prefixWsuIdParam==null || "".equals(prefixWsuIdParam)){
  54.                 MessageSecurityContext_wss4j.wsuIdAllocator = config.getIdAllocator(); // Default di wss4j
  55.             }
  56.             else{
  57.                 MessageSecurityContext_wss4j.wsuIdAllocator = new org.openspcoop2.security.message.WsuIdAllocator(prefixWsuIdParam);
  58.                 config.setIdAllocator(MessageSecurityContext_wss4j.wsuIdAllocator);
  59.             }
  60.             log.info("WsuIdAllocator="+config.getIdAllocator().getClass().getName());
  61.         }
  62.         else{
  63.             if(MessageSecurityContext_wss4j.prefixWsuId==null){
  64.                 throw new Exception("WsuIdAllocator istanziato con la classe ["+MessageSecurityContext_wss4j.wsuIdAllocator.getClass().getName()+"] e variabile prefixWsuId non istanziata??");
  65.             }
  66.             else if(!MessageSecurityContext_wss4j.prefixWsuId.equals(prefixWsuIdParam)){
  67.                 throw new Exception("WsuIdAllocator gia' istanziato con la classe ["+MessageSecurityContext_wss4j.wsuIdAllocator.getClass().getName()
  68.                         +"] e variabile prefixWsuId uguale al valore ["+MessageSecurityContext_wss4j.prefixWsuId+"]. Nuovo valore ["+prefixWsuIdParam+"] non impostabile.");
  69.             }
  70.         }
  71.     }

  72. }