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