DumpSendMessageToFileSystemInterceptor.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.logger;

  21. import java.io.File;
  22. import java.io.FileOutputStream;
  23. import java.io.OutputStream;

  24. import org.apache.cxf.io.CacheAndWriteOutputStream;
  25. import org.apache.cxf.io.CachedOutputStream;
  26. import org.apache.cxf.io.CachedOutputStreamCallback;
  27. import org.apache.cxf.message.Message;
  28. import org.apache.cxf.phase.AbstractPhaseInterceptor;
  29. import org.apache.cxf.phase.Phase;
  30. import org.openspcoop2.utils.resources.FileSystemUtilities;
  31. import org.slf4j.Logger;

  32. /**
  33.  * DumpSendMessageToFileSystemInterceptor
  34.  *
  35.  * @author Poli Andrea (apoli@link.it)
  36.  * @author $Author$
  37.  * @version $Rev$, $Date$
  38.  **/
  39. @SuppressWarnings("rawtypes")
  40. public class DumpSendMessageToFileSystemInterceptor extends AbstractPhaseInterceptor {

  41.     static final Logger log = org.slf4j.LoggerFactory.getLogger(DumpSendMessageToFileSystemInterceptor.class);
  42.    
  43.     public DumpSendMessageToFileSystemInterceptor() {
  44.         super(Phase.PRE_STREAM);
  45.     }

  46.     @Override
  47.     public void handleMessage(Message message) {
  48.         String msgAsString = message.toString();
  49.         String logMsg = String.format("SEND message %s", msgAsString);
  50.         log.info(logMsg);
  51.         message.put(Message.ENCODING, "UTF-8");
  52.         OutputStream outputStream = message.getContent(OutputStream.class);
  53.         if(outputStream!=null) {
  54.             final CacheAndWriteOutputStream newOut = new CacheAndWriteOutputStream(outputStream);
  55.             message.setContent(OutputStream.class, newOut);
  56.             newOut.registerCallback(new LoggingCallback());
  57.         }  
  58.     }

  59. }
  60. class LoggingCallback implements CachedOutputStreamCallback {
  61.    
  62.     @Override
  63.     public void onFlush(CachedOutputStream cos) {
  64.     }

  65.     @Override
  66.     public void onClose(CachedOutputStream cos) {
  67.         File f = null;
  68.         try {
  69.             f = FileSystemUtilities.createTempFile("SendMessage", ".dump");
  70.             FileOutputStream fos =new FileOutputStream(f);
  71.             cos.writeCacheTo(fos);
  72.             fos.flush();
  73.             cos.flush();
  74.             fos.close();
  75.             String logMsg = String.format("Serialized in [%s]",f.getAbsolutePath());
  76.             DumpSendMessageToFileSystemInterceptor.log.info(logMsg);
  77.         }catch(Exception e) {
  78.             DumpSendMessageToFileSystemInterceptor.log.error(e.getMessage(),e);
  79.         }
  80.     }
  81. }
  82.