DumpInInterceptor.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.dump;

  21. import java.io.IOException;
  22. import java.nio.charset.StandardCharsets;
  23. import java.util.HashSet;
  24. import java.util.Set;

  25. import org.apache.cxf.common.injection.NoJSR250Annotations;
  26. import org.apache.cxf.common.util.StringUtils;
  27. import org.apache.cxf.ext.logging.AbstractLoggingInterceptor;
  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.io.CachedOutputStream;
  32. import org.apache.cxf.io.CachedWriter;
  33. import org.apache.cxf.message.Message;
  34. import org.openspcoop2.utils.LoggerWrapperFactory;
  35. import org.openspcoop2.utils.service.context.server.ServerConfig;


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

  45.     private DumpConfig dumpConfig;
  46.     private ServerConfig serverConfig;
  47.    
  48.     public DumpInInterceptor() {
  49.         super();
  50.     }

  51.     public DumpConfig getDumpConfig() {
  52.         return this.dumpConfig;
  53.     }
  54.     public void setDumpConfig(DumpConfig dumpConfig) {
  55.         this.dumpConfig = dumpConfig;
  56.         if(dumpConfig.getLimit()!=null) {
  57.             super.setLimit(dumpConfig.getLimit());
  58.         }
  59.         else {
  60.             super.setLimit(-1);
  61.         }
  62.     }
  63.    
  64.     public ServerConfig getServerConfig() {
  65.         return this.serverConfig;
  66.     }
  67.     public void setServerConfig(ServerConfig serverConfig) {
  68.         this.serverConfig = serverConfig;
  69.     }

  70.     @Override
  71.     public void handleMessage(Message message) throws Fault {

  72.         try {
  73.        
  74.             Set<String> sensitiveProtocolHeaders = new HashSet<String>();
  75.             final LogEvent event = new DefaultLogEventMapper().map(message, sensitiveProtocolHeaders);
  76.             if (shouldLogContent(event)) {
  77.                 internal_addContent(message, event);
  78.             } else {
  79.                 event.setPayload(AbstractLoggingInterceptor.CONTENT_SUPPRESSED);
  80.             }
  81.            
  82.             DumpUtilities utilities = null;
  83.             if(this.serverConfig!=null) {
  84.                 this.serverConfig.setDumpConfig(this.dumpConfig); // update
  85.                 utilities = new DumpUtilities(this.serverConfig);
  86.             }
  87.             else {
  88.                 utilities = new DumpUtilities(this.dumpConfig);
  89.             }
  90.            
  91.             DumpRequest request = new DumpRequest();
  92.            
  93.             if(event.getPayload()!=null) {
  94.                 request.setPayload(event.getPayload().getBytes());
  95.             }
  96.             request.setContentType(event.getContentType());
  97.             request.setHeaders(event.getHeaders());
  98.            
  99.             utilities.processBeforeSend(request);
  100.            
  101.         } catch (Throwable e) {
  102.             LoggerWrapperFactory.getLogger(DumpInInterceptor.class).error(e.getMessage(),e);
  103.             throw new Fault(e);
  104.         }

  105.     }

  106.     private void internal_addContent(Message message, final LogEvent event) {
  107.         try {
  108.             CachedOutputStream cos = message.getContent(CachedOutputStream.class);
  109.             if (cos != null) {
  110.                 internal_handleOutputStream(event, message, cos);
  111.             } else {
  112.                 CachedWriter writer = message.getContent(CachedWriter.class);
  113.                 if (writer != null) {
  114.                     internal_handleWriter(event, writer);
  115.                 }
  116.             }
  117.         } catch (IOException e) {
  118.             throw new Fault(e);
  119.         }
  120.     }

  121.     private void internal_handleOutputStream(final LogEvent event, Message message, CachedOutputStream cos) throws IOException {
  122.         String encoding = (String) message.get(Message.ENCODING);
  123.         if (StringUtils.isEmpty(encoding)) {
  124.             encoding = StandardCharsets.UTF_8.name();
  125.         }
  126.         StringBuilder payload = new StringBuilder();
  127.         cos.writeCacheTo(payload, encoding, this.limit);
  128.         cos.close();
  129.         event.setPayload(payload.toString());
  130.         boolean isTruncated = cos.size() > this.limit && this.limit != -1;
  131.         event.setTruncated(isTruncated);
  132.         event.setFullContentFile(cos.getTempFile());
  133.     }

  134.     private void internal_handleWriter(final LogEvent event, CachedWriter writer) throws IOException {
  135.         boolean isTruncated = writer.size() > this.limit && this.limit != -1;
  136.         StringBuilder payload = new StringBuilder();
  137.         writer.writeCacheTo(payload, this.limit);
  138.         event.setPayload(payload.toString());
  139.         event.setTruncated(isTruncated);
  140.         event.setFullContentFile(writer.getTempFile());
  141.     }

  142. }