ServiceInfoInInterceptor.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 java.util.Date;
  22. import java.util.HashSet;
  23. import java.util.List;
  24. import java.util.Map;
  25. import java.util.Set;

  26. import javax.servlet.http.HttpServletRequest;

  27. import org.apache.cxf.common.injection.NoJSR250Annotations;
  28. import org.apache.cxf.ext.logging.event.DefaultLogEventMapper;
  29. import org.apache.cxf.ext.logging.event.LogEvent;
  30. import org.apache.cxf.interceptor.Fault;
  31. import org.apache.cxf.message.Message;
  32. import org.apache.cxf.phase.AbstractPhaseInterceptor;
  33. import org.apache.cxf.phase.Phase;
  34. import org.apache.cxf.transport.http.AbstractHTTPDestination;
  35. import org.openspcoop2.utils.LoggerWrapperFactory;
  36. import org.openspcoop2.utils.UtilsException;
  37. import org.openspcoop2.utils.logger.beans.context.core.AbstractTransaction;
  38. import org.openspcoop2.utils.logger.beans.context.core.AbstractTransactionWithClient;
  39. import org.openspcoop2.utils.logger.beans.context.core.HttpClient;
  40. import org.openspcoop2.utils.logger.beans.context.core.Operation;
  41. import org.openspcoop2.utils.logger.beans.context.core.Request;
  42. import org.openspcoop2.utils.logger.beans.context.core.Service;
  43. import org.openspcoop2.utils.logger.constants.context.FlowMode;
  44. import org.openspcoop2.utils.transport.TransportUtils;
  45. import org.openspcoop2.utils.transport.http.HttpRequestMethod;
  46. import org.openspcoop2.utils.transport.http.HttpUtilities;


  47. /**
  48.  * ServiceInfoInInterceptor
  49.  *
  50.  * @author Lorenzo Nardi (nardi@link.it)
  51.  * @author $Author$
  52.  * @version $Rev$, $Date$
  53.  */
  54. @NoJSR250Annotations
  55. public class ServiceInfoInInterceptor extends AbstractPhaseInterceptor<Message> {
  56.     //extends org.apache.cxf.ext.logging.LoggingInInterceptor {

  57.     public ServiceInfoInInterceptor() {
  58.         //super();
  59.         super(Phase.RECEIVE);
  60.     }

  61.     @Override
  62.     public void handleMessage(Message message) throws Fault {
  63.        
  64.         try {
  65.        
  66.             /*
  67.             java.util.Iterator<String> it = message.keySet().iterator();
  68.             while (it.hasNext()) {
  69.                 String string = (String) it.next();
  70.                 System.out.println("KEY["+string+"]");
  71.                 Object o = message.get(string);
  72.                 if(o==null) {
  73.                     System.out.println("["+string+"] NULL");
  74.                 }
  75.                 else {
  76.                     System.out.println("["+string+"] '"+o.getClass().getName()+"': "+o);
  77.                 }
  78.             }
  79.             */
  80.            
  81.             Set<String> sensitiveProtocolHeaders = new HashSet<String>();
  82.             final LogEvent event = new DefaultLogEventMapper().map(message, sensitiveProtocolHeaders);
  83.            
  84.    
  85.             IContext ctx = ContextThreadLocal.get();
  86.    
  87.             Map<String, String> headers = event.getHeaders();
  88.            
  89.             AbstractTransaction transaction = ctx.getApplicationContext().getTransaction();
  90.            
  91.             if (event.getServiceName() != null) {
  92.                 transaction.setProtocol("SOAP");
  93.                
  94.                 if(transaction.getService()==null) {
  95.                     transaction.setService(new Service());
  96.                 }
  97.                 transaction.getService().setName(event.getServiceName().getLocalPart());
  98.             }
  99.             else {
  100.                 transaction.setProtocol("REST");
  101.             }
  102.    
  103.             if(transaction.getOperation()==null) {
  104.                 transaction.setOperation(new Operation());
  105.             }
  106.             transaction.getOperation().setMode(FlowMode.INPUT_OUTPUT);
  107.             transaction.getOperation().setName(event.getOperationName());
  108.            
  109.             AbstractTransactionWithClient transactionWithClient = null;
  110.             if(transaction instanceof AbstractTransactionWithClient) {
  111.                 transactionWithClient = (AbstractTransactionWithClient) transaction;
  112.                
  113.                 if(transactionWithClient.getClient()==null) {
  114.                     transactionWithClient.setClient(new HttpClient());
  115.                 }
  116.                
  117.                 transactionWithClient.getClient().setInvocationEndpoint(event.getAddress());
  118.                 transactionWithClient.getClient().setPrincipal(event.getPrincipal());
  119.                 if(event.getPortName() != null) {
  120.                     transactionWithClient.getClient().setInterfaceName(event.getPortName().getLocalPart());
  121.                 }
  122.                
  123.                 if(transactionWithClient.getClient() instanceof HttpClient) {
  124.                     ((HttpClient)transactionWithClient.getClient()).setTransportRequestMethod(HttpRequestMethod.valueOf(event.getHttpMethod().toUpperCase()));
  125.                     if(headers!=null && headers.size()>0) {
  126.                         ((HttpClient)transactionWithClient.getClient()).setTransportClientAddress(getIPClientAddressFromHeader(headers));
  127.                     }
  128.                 }
  129.                
  130.                 if(transactionWithClient.getRequest()==null) {
  131.                     transactionWithClient.setRequest(new Request());
  132.                 }
  133.                 if(transactionWithClient.getRequest().getDate()==null) {
  134.                     transactionWithClient.getRequest().setDate(new Date());
  135.                 }
  136.             }
  137.            
  138.             Object o = message.get(AbstractHTTPDestination.HTTP_REQUEST);
  139.             if(o!=null && o instanceof HttpServletRequest) {
  140.                 HttpServletRequest httpServletRequest = (HttpServletRequest) o;
  141.                
  142.                 if(transactionWithClient!=null) {
  143.                     ((HttpClient)transactionWithClient.getClient()).setSocketClientAddress(httpServletRequest.getRemoteAddr());
  144.                    
  145.                     int contentLength = httpServletRequest.getContentLength();
  146.                     if(contentLength>=0) {
  147.                         transactionWithClient.getRequest().setSize(Long.valueOf(contentLength));
  148.                     }
  149.                 }
  150.             }

  151.            
  152.         } catch (Throwable e) {
  153.             LoggerWrapperFactory.getLogger(ServiceInfoInInterceptor.class).error(e.getMessage(),e);
  154.             throw new Fault(e);
  155.         }
  156.     }

  157.     private static String getIPClientAddressFromHeader(Map<String, String> req) throws UtilsException {
  158.        
  159.         List<String> headers = HttpUtilities.getClientAddressHeaders();
  160.         if(headers.size()>0){
  161.             for (String header : headers) {
  162.                 String transportAddr = TransportUtils.getObjectAsString(req, header);
  163.                 if(transportAddr!=null){
  164.                     return transportAddr;
  165.                 }
  166.             }
  167.         }
  168.         return null;
  169.     }

  170. }