SmackWebSocketDebugger.java

  1. /**
  2.  *
  3.  * Copyright 2020 Aditya Borikar, 2021 Florian Schmaus
  4.  *
  5.  * Licensed under the Apache License, Version 2.0 (the "License");
  6.  * you may not use this file except in compliance with the License.
  7.  * You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.jivesoftware.smack.websocket.impl;

  18. import java.io.IOException;
  19. import java.util.logging.Level;
  20. import java.util.logging.Logger;

  21. import org.jivesoftware.smack.debugger.SmackDebugger;

  22. import org.jxmpp.xml.splitter.XmlPrettyPrinter;
  23. import org.jxmpp.xml.splitter.XmppXmlSplitter;

  24. public class SmackWebSocketDebugger {

  25.     private static final Logger LOGGER = Logger.getLogger(SmackWebSocketDebugger.class.getName());

  26.     private final SmackDebugger debugger;
  27.     private final XmppXmlSplitter incomingXmlSplitter;
  28.     private final XmppXmlSplitter outgoingXmlSplitter;

  29.     SmackWebSocketDebugger(SmackDebugger smackDebugger) {
  30.         this.debugger = smackDebugger;

  31.         XmlPrettyPrinter incomingTextPrinter = XmlPrettyPrinter.builder()
  32.                         .setPrettyWriter(sb -> debugger.incomingStreamSink(sb))
  33.                         .setTabWidth(4)
  34.                         .build();
  35.         incomingXmlSplitter = new XmppXmlSplitter(incomingTextPrinter);

  36.         XmlPrettyPrinter outgoingTextPrinter = XmlPrettyPrinter.builder()
  37.                         .setPrettyWriter(sb -> debugger.outgoingStreamSink(sb))
  38.                         .setTabWidth(4)
  39.                         .build();
  40.         outgoingXmlSplitter = new XmppXmlSplitter(outgoingTextPrinter);
  41.     }

  42.     void incoming(String text) {
  43.         try {
  44.             incomingXmlSplitter.write(text);
  45.         } catch (IOException e) {
  46.             // Connections shouldn't be terminated due to exceptions encountered during debugging. hence only log them.
  47.             LOGGER.log(Level.WARNING, "IOException encountered while parsing received text: " + text, e);
  48.         }
  49.     }

  50.     void outgoing(String text) {
  51.         try {
  52.             outgoingXmlSplitter.write(text);
  53.         } catch (IOException e) {
  54.             // Connections shouldn't be terminated due to exceptions encountered during debugging, hence only log them.
  55.             LOGGER.log(Level.WARNING, "IOException encountered while parsing outgoing text: " + text, e);
  56.         }
  57.     }
  58. }