ServerInfoUtilities.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.server;


  21. import java.util.Date;
  22. import java.util.List;
  23. import java.util.UUID;

  24. import org.apache.cxf.interceptor.Fault;
  25. import org.openspcoop2.utils.LoggerWrapperFactory;
  26. import org.openspcoop2.utils.logger.beans.context.application.ApplicationTransaction;
  27. import org.openspcoop2.utils.logger.beans.context.batch.BatchTransaction;
  28. import org.openspcoop2.utils.logger.beans.context.core.AbstractTransaction;
  29. import org.openspcoop2.utils.logger.beans.context.core.BaseServer;
  30. import org.openspcoop2.utils.logger.beans.context.core.ConnectionMessage;
  31. import org.openspcoop2.utils.logger.beans.context.core.HttpServer;
  32. import org.openspcoop2.utils.logger.beans.context.proxy.ProxyTransaction;
  33. import org.openspcoop2.utils.service.context.ContextThreadLocal;
  34. import org.openspcoop2.utils.service.context.IContext;

  35. /**
  36.  * ServerInfoUtilities
  37.  *
  38.  * @author Lorenzo Nardi (nardi@link.it)
  39.  * @author $Author$
  40.  * @version $Rev$, $Date$
  41.  */
  42. public class ServerInfoUtilities {
  43.    
  44.     private ServerConfig serverConfig;

  45.     public static void checkOperationId(ServerConfig serverConfig) {
  46.         if(serverConfig.getOperationId()==null) {
  47.             serverConfig.setOperationId(UUID.randomUUID().toString());
  48.         }
  49.     }
  50.    
  51.     public ServerInfoUtilities(ServerConfig serverConfig) {
  52.         this.serverConfig = serverConfig;
  53.         ServerInfoUtilities.checkOperationId(this.serverConfig);
  54.     }

  55.    
  56.     public void processBeforeSend(ServerInfoRequest request) throws Fault {
  57.        
  58.         try {

  59.             if(this.serverConfig==null) {
  60.                 throw new Exception("ServerConfig undefined");
  61.             }
  62.             if(this.serverConfig.getOperationId()==null) {
  63.                 throw new Exception("ServerConfig.operationId undefined");
  64.             }
  65.                
  66.             IContext ctx = ContextThreadLocal.get();
  67.    
  68.             AbstractTransaction transaction = ctx.getApplicationContext().getTransaction();
  69.            
  70.             List<BaseServer> list = null;
  71.             BaseServer server = null;
  72.             if(transaction instanceof ApplicationTransaction) {
  73.                 list = ((ApplicationTransaction)transaction).getServers();
  74.             }
  75.             else if(transaction instanceof BatchTransaction) {
  76.                 list = ((BatchTransaction)transaction).getServers();
  77.             }
  78.             else if(transaction instanceof ProxyTransaction) {
  79.                 server = ((ProxyTransaction)transaction).getServer();
  80.             }
  81.             if(list!=null && list.size()>0) {
  82.                 for (BaseServer baseServer : list) {
  83.                     if(this.serverConfig.getOperationId().equals(baseServer.getIdOperation())) {
  84.                         server = baseServer;
  85.                         break;
  86.                     }
  87.                 }
  88.             }

  89.             if(server==null) {
  90.                 server = new HttpServer();
  91.                 if(transaction instanceof ApplicationTransaction) {
  92.                     ((ApplicationTransaction)transaction).addServer(server);
  93.                 }
  94.                 else if(transaction instanceof BatchTransaction) {
  95.                     ((BatchTransaction)transaction).addServer(server);
  96.                 }
  97.                 else if(transaction instanceof ProxyTransaction) {
  98.                     ((ProxyTransaction)transaction).setServer(server);
  99.                 }
  100.             }
  101.             server.setName(this.serverConfig.getServerId());
  102.             server.setIdOperation(this.serverConfig.getOperationId());
  103.             server.setEndpoint(request.getAddress());
  104.             if(server instanceof HttpServer) {
  105.                 ((HttpServer)server).setTransportRequestMethod(request.getHttpRequestMethod());
  106.             }
  107.            
  108.             if(server.getRequest()==null) {
  109.                 server.setRequest(new ConnectionMessage());
  110.             }
  111.             server.getRequest().setDate(new Date());
  112.            
  113.         } catch (Throwable e) {
  114.             LoggerWrapperFactory.getLogger(ServerInfoInInterceptor.class).error(e.getMessage(),e);
  115.             throw new Fault(e);
  116.         }
  117.        
  118.     }
  119.    
  120.     public void processAfterSend(ServerInfoResponse response) throws Fault {
  121.        
  122.         try {
  123.            
  124.             if(this.serverConfig==null) {
  125.                 throw new Exception("ServerConfig undefined");
  126.             }
  127.             if(this.serverConfig.getOperationId()==null) {
  128.                 throw new Exception("ServerConfig.operationId undefined");
  129.             }
  130.            
  131.             IContext ctx = ContextThreadLocal.get();
  132.    
  133.             AbstractTransaction transaction = ctx.getApplicationContext().getTransaction();
  134.            
  135.             List<BaseServer> list = null;
  136.             BaseServer server = null;
  137.             if(transaction instanceof ApplicationTransaction) {
  138.                 list = ((ApplicationTransaction)transaction).getServers();
  139.             }
  140.             else if(transaction instanceof BatchTransaction) {
  141.                 list = ((BatchTransaction)transaction).getServers();
  142.             }
  143.             else if(transaction instanceof ProxyTransaction) {
  144.                 server = ((ProxyTransaction)transaction).getServer();
  145.             }
  146.             if(list!=null && list.size()>0) {
  147.                 for (BaseServer baseServer : list) {
  148.                     if(this.serverConfig.getOperationId().equals(baseServer.getIdOperation())) {
  149.                         server = baseServer;
  150.                         break;
  151.                     }
  152.                 }
  153.             }
  154.            
  155.             if(server==null) {
  156.                 throw new Exception("Server '"+this.serverConfig.getOperationId()+"' not found");
  157.             }
  158.            
  159.             if(server.getResponse()==null) {
  160.                 server.setResponse(new ConnectionMessage());
  161.             }
  162.             server.getResponse().setDate(new Date());
  163.            
  164.             if(server instanceof HttpServer) {
  165.                 try {
  166.                     if(response.getResponseCode()!=null) {
  167.                         ((HttpServer)server).setResponseStatusCode(response.getResponseCode());
  168.                     }
  169.                 }catch(Throwable t) {
  170.                 }
  171.             }
  172.            
  173.         } catch (Throwable e) {
  174.             LoggerWrapperFactory.getLogger(ServerInfoInInterceptor.class).error(e.getMessage(),e);
  175.             throw new Fault(e);
  176.         }
  177.        
  178.     }
  179.    
  180. }