blob: 4ff8d69fa9fca6197139a68376cd5c4ba32d3b2a [file] [log] [blame]
Tomek Osińskie9ccf412018-01-13 19:44:11 +01001/*
2 * Copyright 2018-present Open Networking Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package org.onosproject.xmpp.core.stream;
18
19import org.dom4j.Attribute;
20import org.dom4j.Element;
21import org.dom4j.Namespace;
22import org.dom4j.io.OutputFormat;
23import org.dom4j.io.XMLWriter;
24import org.xmpp.packet.JID;
25
26import java.io.IOException;
27import java.io.StringWriter;
28import java.util.List;
29
Ray Milkey8124ac92018-03-06 17:56:03 -080030import org.slf4j.Logger;
31import org.slf4j.LoggerFactory;
32
Tomek Osińskie9ccf412018-01-13 19:44:11 +010033/**
34 * Abstracts XMPP stream open event.
35 */
36public class XmppStreamOpen implements XmppStreamEvent {
37
Ray Milkey8124ac92018-03-06 17:56:03 -080038 private final Logger log = LoggerFactory.getLogger(XmppStreamOpen.class);
39
Tomek Osińskie9ccf412018-01-13 19:44:11 +010040 public static final String QNAME = "stream";
41
42 private Element element;
43
44 public XmppStreamOpen(Element element) {
45 this.element = element;
46 }
47
48 @Override
49 public String toXml() {
50 StringWriter out = new StringWriter();
51 XMLWriter writer = new XMLWriter(out, OutputFormat.createCompactFormat());
52 try {
53 out.write("<");
54 writer.write(element.getQualifiedName());
55 for (Attribute attr : (List<Attribute>) element.attributes()) {
56 writer.write(attr);
57 }
58 writer.write(Namespace.get(this.element.getNamespacePrefix(), this.element.getNamespaceURI()));
59 writer.write(Namespace.get("jabber:client"));
60 out.write(">");
61 } catch (IOException ex) {
Ray Milkey8124ac92018-03-06 17:56:03 -080062 log.info("Error writing XML", ex);
Tomek Osińskie9ccf412018-01-13 19:44:11 +010063 }
64 return out.toString();
65 }
66
67 public JID getFromJid() {
68 String jid = this.element.attribute("from").getValue();
69 return new JID(jid);
70 }
71
72 public Element getElement() {
73 return this.element;
74 }
75
76 public JID getToJid() {
77 String jid = this.element.attribute("to").getValue();
78 return new JID(jid);
79 }
80}