ContextInInterceptor.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.service.context;

  21. import org.apache.cxf.interceptor.Fault;
  22. import org.apache.cxf.message.Message;
  23. import org.apache.cxf.phase.AbstractPhaseInterceptor;
  24. import org.apache.cxf.phase.Phase;
  25. import org.openspcoop2.utils.LoggerWrapperFactory;
  26. import org.openspcoop2.utils.logger.beans.context.core.AbstractContext;
  27. import org.openspcoop2.utils.logger.beans.context.core.Service;
  28. import org.slf4j.MDC;


  29. /**
  30.  * ContextInInterceptor
  31.  *
  32.  * @author Lorenzo Nardi (nardi@link.it)
  33.  * @author $Author$
  34.  * @version $Rev$, $Date$
  35.  */
  36. public class ContextInInterceptor extends AbstractPhaseInterceptor<Message> {
  37.    
  38.     private IContextFactory contextFactory = new ContextFactory();
  39.     private ContextConfig contextConfig = null;
  40.        
  41.     public ContextInInterceptor() {
  42.         super(Phase.RECEIVE);
  43.     }

  44.     public IContextFactory getContextFactory() {
  45.         return this.contextFactory;
  46.     }
  47.     public void setContextFactory(IContextFactory contextFactory) {
  48.         this.contextFactory = contextFactory;
  49.     }
  50.    
  51.     public ContextConfig getContextConfig() {
  52.         return this.contextConfig;
  53.     }
  54.     public void setContextConfig(ContextConfig contextConfig) {
  55.         this.contextConfig = contextConfig;
  56.     }

  57.     @Override
  58.     public void handleMessage(Message message) throws Fault {
  59.         try {
  60.            
  61.             IContext context = this.contextFactory.newContext();
  62.             if(context.getApplicationContext() instanceof AbstractContext) {
  63.                 AbstractContext applicationContext = (AbstractContext) context.getApplicationContext();
  64.                 if(applicationContext.getTransaction()!=null) {
  65.                    
  66.                     applicationContext.getTransaction().setContext(this.contextConfig.getContext());
  67.                    
  68.                     applicationContext.getTransaction().setDomain(this.contextConfig.getDomain());
  69.                     applicationContext.getTransaction().setRole(this.contextConfig.getRole());
  70.                    
  71.                     if(this.contextConfig.getServiceType()!=null || this.contextConfig.getServiceName()!=null || this.contextConfig.getServiceVersion()!=null) {
  72.                         Service service = new Service();
  73.                         service.setType(this.contextConfig.getServiceType());
  74.                         service.setName(this.contextConfig.getServiceName());
  75.                         service.setVersion(this.contextConfig.getServiceVersion());
  76.                         applicationContext.getTransaction().setService(service);
  77.                     }
  78.                    
  79.                     applicationContext.getTransaction().setClusterId(this.contextConfig.getClusterId());
  80.                 }
  81.             }
  82.             ContextThreadLocal.set(context);
  83.             MDC.put(MD5Constants.TRANSACTION_ID, context.getTransactionId());
  84.         } catch (Throwable e) {
  85.             LoggerWrapperFactory.getLogger(ContextInInterceptor.class).error(e.getMessage(),e);
  86.             throw new Fault(e);
  87.         }
  88.     }
  89. }