Context.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 javax.servlet.http.HttpServletRequest;
  22. import javax.servlet.http.HttpServletResponse;
  23. import javax.ws.rs.core.UriInfo;

  24. import org.openspcoop2.utils.UtilsException;
  25. import org.openspcoop2.utils.logger.ILogger;
  26. import org.openspcoop2.utils.logger.beans.context.core.AbstractContext;
  27. import org.openspcoop2.utils.logger.beans.context.core.AbstractTransactionWithClient;
  28. import org.openspcoop2.utils.logger.beans.context.core.Operation;
  29. import org.openspcoop2.utils.logger.beans.context.core.Service;
  30. import org.openspcoop2.utils.service.logger.ServiceLogger;
  31. import org.slf4j.Logger;
  32. import org.springframework.security.core.Authentication;
  33. import org.springframework.security.core.context.SecurityContextHolder;

  34. /**
  35.  * Context
  36.  *
  37.  * @author Andrea Poli (apoli@link.it)
  38.  * @author $Author$
  39.  * @version $Rev$, $Date$
  40.  */
  41. public class Context implements IContext {

  42.    
  43.     private HttpServletRequest servletRequest;
  44.     private HttpServletResponse servletResponse;
  45.    
  46.     private UriInfo uriInfo;
  47.    
  48.     private String transactionId;
  49.    
  50.     private String className;
  51.     private String methodName;
  52.     private String restPath;
  53.    
  54.     private AbstractContext applicationContext;
  55.     private ILogger applicationLogger;
  56.    
  57.     private ServiceLogger logger; // core
  58.    
  59.     private Authentication authentication;
  60.    
  61.     private boolean loggerPrefixEnabled;
  62.    
  63.    
  64.     public Context(ILogger applicationLogger, boolean loggerPrefixEnabled) throws UtilsException {
  65.         this.applicationLogger = applicationLogger;
  66.         this.applicationContext = (AbstractContext) this.applicationLogger.getContext();
  67.         this.transactionId = this.applicationContext.getIdTransaction();
  68.         if(this.transactionId==null) {
  69.             throw new UtilsException("TransactionId undefined");
  70.         }
  71.         this.loggerPrefixEnabled = loggerPrefixEnabled;
  72.     }
  73.    
  74.     public void update(HttpServletRequest servletRequest, HttpServletResponse servletResponse, UriInfo uriInfo, int level, Logger log) {
  75.         this.servletRequest = servletRequest;
  76.         this.servletResponse = servletResponse;
  77.         this.uriInfo = uriInfo;
  78.         StackTraceElement[] stackTrace = new Throwable().getStackTrace();
  79.         if(stackTrace!=null && stackTrace.length>=(level+1)) {
  80.             this.className = stackTrace[level].getClassName();
  81.             this.methodName = stackTrace[level].getMethodName();
  82.         }
  83.         this.logger = new ServiceLogger(this.transactionId, this.methodName, this.className, log, this.loggerPrefixEnabled);
  84.         this.authentication = SecurityContextHolder.getContext().getAuthentication();
  85.        
  86.         AbstractTransactionWithClient transaction = (AbstractTransactionWithClient) this.applicationContext.getTransaction();
  87.        
  88.         if(this.className!=null) {
  89.             if(transaction.getService()==null) {
  90.                 transaction.setService(new Service());
  91.             }
  92.             if(transaction.getService().getName()==null) {
  93.                 if(this.className.lastIndexOf(".")>0) {
  94.                     transaction.getService().setName(this.className.substring(this.className.lastIndexOf(".")+1, this.className.length()));
  95.                 }
  96.                 else {
  97.                     transaction.getService().setName(this.className);
  98.                 }
  99.             }
  100.         }
  101.         if(this.methodName!=null) {
  102.             if(transaction.getOperation()==null) {
  103.                 transaction.setOperation(new Operation());
  104.             }
  105.             transaction.getOperation().setName(this.methodName);
  106.         }
  107.    
  108.     }
  109.    
  110.     public void setRestPath(String restPath) {
  111.         this.restPath = restPath;
  112.     }
  113.    
  114.     @Override
  115.     public HttpServletRequest getServletRequest() {
  116.         return this.servletRequest;
  117.     }
  118.    
  119.     @Override
  120.     public HttpServletResponse getServletResponse() {
  121.         return this.servletResponse;
  122.     }
  123.    
  124.     @Override
  125.     public UriInfo getUriInfo() {
  126.         return this.uriInfo;
  127.     }
  128.    
  129.     @Override
  130.     public String getTransactionId() {
  131.         return this.transactionId;
  132.     }
  133.    
  134.     @Override
  135.     public String getClassName() {
  136.         return this.className;
  137.     }
  138.     @Override
  139.     public String getMethodName() {
  140.         return this.methodName;
  141.     }
  142.     @Override
  143.     public String getRestPath() {
  144.         return this.restPath;
  145.     }
  146.    
  147.     @Override
  148.     public AbstractContext getApplicationContext(){
  149.         return this.applicationContext;
  150.     }
  151.     @Override
  152.     public ILogger getApplicationLogger() throws UtilsException {
  153.         return this.applicationLogger;
  154.     }
  155.    
  156.     @Override
  157.     public ServiceLogger getLogger() {
  158.         return this.logger;
  159.     }
  160.    
  161.     @Override
  162.     public Authentication getAuthentication() {
  163.         return this.authentication;
  164.     }
  165.    
  166. }