File Coverage

File:blib/lib/OpenSRF/Transport/SlimJabber/XMPPMessage.pm
Coverage:18.7%

linestmtbrancondsubpodtimecode
1package OpenSRF::Transport::SlimJabber::XMPPMessage;
2
9
9
9
9
9
9
52
32
76
64
31
84
use strict; use warnings;
3
9
9
9
58
35
84
use OpenSRF::Utils::Logger qw/$logger/;
4
9
9
9
63
37
82
use OpenSRF::EX qw/:try/;
5
9
9
9
9
9
9
65
31
51
62
31
51
use strict; use warnings;
6
9
9
9
60
33
89
use XML::LibXML;
7
8
9
87
use constant JABBER_MESSAGE =>
9    "<message to='%s' from='%s' router_command='%s' router_class='%s' osrf_xid='%s'>".
10
9
9
72
31
    "<thread>%s</thread><body>%s</body></message>";
11
12sub new {
13
0
0
    my $class = shift;
14
0
    my %args = @_;
15
0
    my $self = bless({}, $class);
16
17
0
    if($args{xml}) {
18
0
        $self->parse_xml($args{xml});
19
20    } else {
21
0
        $self->{to} = $args{to} || '';
22
0
        $self->{from} = $args{from} || '';
23
0
        $self->{thread} = $args{thread} || '';
24
0
        $self->{body} = $args{body} || '';
25
0
        $self->{osrf_xid} = $args{osrf_xid} || '';
26
0
        $self->{router_command} = $args{router_command} || '';
27
0
        $self->{router_class} = $args{router_class} || '';
28    }
29
30
0
    return $self;
31}
32
33sub to {
34
0
0
    my($self, $to) = @_;
35
0
    $self->{to} = $to if defined $to;
36
0
    return $self->{to};
37}
38sub from {
39
0
0
    my($self, $from) = @_;
40
0
    $self->{from} = $from if defined $from;
41
0
    return $self->{from};
42}
43sub thread {
44
0
0
    my($self, $thread) = @_;
45
0
    $self->{thread} = $thread if defined $thread;
46
0
    return $self->{thread};
47}
48sub body {
49
0
0
    my($self, $body) = @_;
50
0
    $self->{body} = $body if defined $body;
51
0
    return $self->{body};
52}
53sub status {
54
0
0
    my($self, $status) = @_;
55
0
    $self->{status} = $status if defined $status;
56
0
    return $self->{status};
57}
58sub type {
59
0
0
    my($self, $type) = @_;
60
0
    $self->{type} = $type if defined $type;
61
0
    return $self->{type};
62}
63sub err_type {
64
0
0
    my($self, $err_type) = @_;
65
0
    $self->{err_type} = $err_type if defined $err_type;
66
0
    return $self->{err_type};
67}
68sub err_code {
69
0
0
    my($self, $err_code) = @_;
70
0
    $self->{err_code} = $err_code if defined $err_code;
71
0
    return $self->{err_code};
72}
73sub osrf_xid {
74
0
0
    my($self, $osrf_xid) = @_;
75
0
    $self->{osrf_xid} = $osrf_xid if defined $osrf_xid;
76
0
    return $self->{osrf_xid};
77}
78sub router_command {
79
0
0
    my($self, $router_command) = @_;
80
0
    $self->{router_command} = $router_command if defined $router_command;
81
0
    return $self->{router_command};
82}
83sub router_class {
84
0
0
    my($self, $router_class) = @_;
85
0
    $self->{router_class} = $router_class if defined $router_class;
86
0
    return $self->{router_class};
87}
88
89
90sub to_xml {
91
0
0
    my $self = shift;
92
93
0
    my $body = $self->{body};
94
0
    $body =~ s/&/&amp;/sog;
95
0
    $body =~ s/</&lt;/sog;
96
0
    $body =~ s/>/&gt;/sog;
97
98
0
    return sprintf(
99        JABBER_MESSAGE,
100        $self->{to},
101        $self->{from},
102        $self->{router_command},
103        $self->{router_class},
104        $self->{osrf_xid},
105        $self->{thread},
106        $body
107    );
108}
109
110sub parse_xml {
111
0
0
    my($self, $xml) = @_;
112
0
    my($doc, $err);
113
114    try {
115
0
        $doc = XML::LibXML->new->parse_string($xml);
116    } catch Error with {
117
0
        my $err = shift;
118
0
        $logger->error("Error parsing message xml: $xml --- $err");
119
0
    };
120
0
    throw $err if $err;
121
122
0
    my $root = $doc->documentElement;
123
124
0
    $self->{body} = $root->findnodes('/message/body').'';
125
0
    $self->{thread} = $root->findnodes('/message/thread').'';
126
0
    $self->{from} = $root->getAttribute('router_from');
127
0
    $self->{from} = $root->getAttribute('from') unless $self->{from};
128
0
    $self->{to} = $root->getAttribute('to');
129
0
    $self->{type} = $root->getAttribute('type');
130
0
    $self->{osrf_xid} = $root->getAttribute('osrf_xid');
131}
132
133
1341;