ServiceInfoOutInterceptor.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.Set;

  24. import org.apache.cxf.common.injection.NoJSR250Annotations;
  25. import org.apache.cxf.ext.logging.event.DefaultLogEventMapper;
  26. import org.apache.cxf.ext.logging.event.LogEvent;
  27. import org.apache.cxf.interceptor.Fault;
  28. import org.apache.cxf.message.Message;
  29. import org.apache.cxf.phase.AbstractPhaseInterceptor;
  30. import org.apache.cxf.phase.Phase;
  31. import org.openspcoop2.utils.logger.beans.context.core.AbstractTransaction;
  32. import org.openspcoop2.utils.logger.beans.context.core.AbstractTransactionWithClient;
  33. import org.openspcoop2.utils.logger.beans.context.core.HttpClient;
  34. import org.openspcoop2.utils.logger.beans.context.core.Response;


  35. /**
  36.  * ServiceInfoOutInterceptor
  37.  *
  38.  * @author Lorenzo Nardi (nardi@link.it)
  39.  * @author $Author$
  40.  * @version $Rev$, $Date$
  41.  */
  42. @NoJSR250Annotations
  43. public class ServiceInfoOutInterceptor extends AbstractPhaseInterceptor<Message> {
  44.     // extends org.apache.cxf.ext.logging.LoggingOutInterceptor {

  45.     public ServiceInfoOutInterceptor() {
  46.         //super();
  47.         super(Phase.SEND);
  48.     }

  49.     @Override
  50.     public void handleMessage(Message message) throws Fault {
  51.        
  52.         /*
  53.         java.util.Iterator<String> it = message.keySet().iterator();
  54.         while (it.hasNext()) {
  55.             String string = (String) it.next();
  56.             System.out.println("RESPONSE KEY["+string+"]");
  57.             Object o = message.get(string);
  58.             if(o==null) {
  59.                 System.out.println("["+string+"] NULL");
  60.             }
  61.             else {
  62.                 System.out.println("["+string+"] '"+o.getClass().getName()+"': "+o);
  63.             }
  64.         }
  65.         */
  66.        
  67.         Set<String> sensitiveProtocolHeaders = new HashSet<String>();
  68.         final LogEvent event = new DefaultLogEventMapper().map(message, sensitiveProtocolHeaders);
  69.        
  70.         IContext ctx = ContextThreadLocal.get();
  71.    
  72.         AbstractTransaction transaction = ctx.getApplicationContext().getTransaction();
  73.        
  74.         AbstractTransactionWithClient transactionWithClient = null;
  75.         if(transaction instanceof AbstractTransactionWithClient) {
  76.             transactionWithClient = (AbstractTransactionWithClient) transaction;
  77.            
  78.             if(transactionWithClient.getClient() instanceof HttpClient) {
  79.                
  80.                 HttpClient httpClient = (HttpClient) transactionWithClient.getClient();
  81.                
  82.                 try {
  83.                     if(event.getResponseCode()!=null) {
  84.                         httpClient.setResponseStatusCode(Integer.parseInt(event.getResponseCode()));
  85.                     }
  86.                 }catch(Throwable t) {
  87.                 }
  88.                
  89.                 // NON FUNZIONA
  90. //              if(httpClient.getResponseStatusCode()<=0) {
  91. //                  Integer responseCode = (Integer) message.get(Message.RESPONSE_CODE);
  92. //                  if (null == responseCode) {
  93. //                      Object o = message.get(org.apache.cxf.transport.http.AbstractHTTPDestination.HTTP_RESPONSE);
  94. //                      if(o!=null && o instanceof javax.servlet.http.HttpServletResponseWrapper) {
  95. //                          javax.servlet.http.HttpServletResponseWrapper responseWrapper =
  96. //                             (javax.servlet.http.HttpServletResponseWrapper) o;
  97. //                          responseCode = responseWrapper.getStatus();
  98. //                      }
  99. //                  }
  100. //                  if(responseCode!=null) {
  101. //                      httpClient.setResponseStatusCode(responseCode);
  102. //                  }
  103. //              }
  104.             }
  105.            
  106.             if(transactionWithClient.getResponse()==null) {
  107.                 transactionWithClient.setResponse(new Response());
  108.             }
  109.             if(transactionWithClient.getResponse().getDate()==null) {
  110.                 transactionWithClient.getResponse().setDate(new Date());
  111.             }
  112.         }
  113.     }
  114. }