DumpReceivedMessageToFileSystemInterceptor.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.FileInputStream;
  23. import java.io.FileOutputStream;
  24. import java.io.InputStream;

  25. import org.apache.cxf.message.Message;
  26. import org.apache.cxf.phase.AbstractPhaseInterceptor;
  27. import org.apache.cxf.phase.Phase;
  28. import org.openspcoop2.utils.resources.FileSystemUtilities;
  29. import org.slf4j.Logger;

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

  39.     private static final Logger log = org.slf4j.LoggerFactory.getLogger(DumpReceivedMessageToFileSystemInterceptor.class);
  40.    
  41.     public DumpReceivedMessageToFileSystemInterceptor() {
  42.         super(Phase.RECEIVE);
  43.     }

  44.     @Override
  45.     public void handleMessage(Message message) {
  46.         String msgAsString = message.toString();
  47.         String logMsg = String.format("RECEIVE message %s", msgAsString);
  48.         log.info(logMsg);
  49.         message.put(Message.ENCODING, "UTF-8");
  50.         InputStream paramInputStream = message.getContent(InputStream.class);

  51.         if(paramInputStream!=null) {
  52.             File f = null;
  53.             try {
  54.                 f = FileSystemUtilities.createTempFile("ReceivedMessage", ".dump");
  55.             }catch(Exception e) {
  56.                 log.error(e.getMessage(),e);
  57.                 return;
  58.             }
  59.             try (FileOutputStream fos = new FileOutputStream(f);){
  60.                 byte[] buffer = new byte[256];
  61.                 while (true) {
  62.                     int bytesRead = paramInputStream.read(buffer);
  63.                     if (bytesRead == -1) break;
  64.                     fos.write(buffer, 0, bytesRead);
  65.                 }
  66.                 fos.flush();
  67.                 paramInputStream.close();
  68.             }catch(Exception e) {
  69.                 log.error(e.getMessage(),e);
  70.                 return;
  71.             }
  72.    
  73.             logMsg = String.format("Serialized in [%s]",f.getAbsolutePath());
  74.             log.info(logMsg);
  75.             try {
  76.                 message.setContent(InputStream.class, new FileInputStream(f));
  77.             } catch (Exception e) {
  78.                 log.error(e.getMessage(),e);
  79.             }
  80.         }

  81.     }

  82. }