summaryrefslogtreecommitdiffstats
blob: e3a65a6d31491fe2f9ff12e5020b9772cb3185f2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/*
 * Copyright (c) 2010-2014 Isode Limited.
 * All rights reserved.
 * See the COPYING file for more information.
 */
/*
 * Copyright (c) 2015 Tarun Gupta.
 * Licensed under the simplified BSD license.
 * See Documentation/Licenses/BSD-simplified.txt for more information.
 */

package com.isode.stroke.client;

import com.isode.stroke.client.CoreClient;
import com.isode.stroke.client.XMLBeautifier;
import com.isode.stroke.base.SafeByteArray;
import com.isode.stroke.signals.SignalConnection;
import com.isode.stroke.signals.Slot1;

public class ClientXMLTracer {

	private XMLBeautifier beautifier;
	private boolean bosh;
	private SignalConnection onDataReadConnection;
	private SignalConnection onDataWrittenConnection;

	public ClientXMLTracer(CoreClient client) {
		this(client, false);
	}

	public ClientXMLTracer(CoreClient client, boolean bosh) {
		this.bosh = bosh;
		beautifier = new XMLBeautifier(true, true);
		onDataReadConnection = client.onDataRead.connect(new Slot1<SafeByteArray>() {
			@Override
			public void call(SafeByteArray s) {
				printData('<', s);
			}
		});
		onDataWrittenConnection = client.onDataWritten.connect(new Slot1<SafeByteArray>() {
			@Override
			public void call(SafeByteArray s) {
				printData('>', s);
			}
		});
	}

	private void printData(char direction, final SafeByteArray data) {
		printLine(direction);
		if (bosh) {
			String line = data.toString(); 
	// Disabled because it swallows bits of XML (namespaces, if I recall)
	//		size_t endOfHTTP = line.find("\r\n\r\n");
	//		if (false && endOfHTTP != std::string::npos) {
	//			std::cerr << line.substr(0, endOfHTTP) << std::endl << beautifier->beautify(line.substr(endOfHTTP)) << std::endl;
	//		}
	//		else {
				System.err.println(line);
	//		}
		}
		else {
			System.err.println(beautifier.beautify(data.toString()));
		}
	}

	private void printLine(char c) {
		for (int i = 0; i < 80; ++i) {
			System.err.println(c);
		}
		System.err.println();
	}	
}