blob: 967296b796768d79595241c1c1b6f0b0cfeb6c02 [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
30/**
31 * Abstracts XMPP stream open event.
32 */
33public class XmppStreamOpen implements XmppStreamEvent {
34
35 public static final String QNAME = "stream";
36
37 private Element element;
38
39 public XmppStreamOpen(Element element) {
40 this.element = element;
41 }
42
43 @Override
44 public String toXml() {
45 StringWriter out = new StringWriter();
46 XMLWriter writer = new XMLWriter(out, OutputFormat.createCompactFormat());
47 try {
48 out.write("<");
49 writer.write(element.getQualifiedName());
50 for (Attribute attr : (List<Attribute>) element.attributes()) {
51 writer.write(attr);
52 }
53 writer.write(Namespace.get(this.element.getNamespacePrefix(), this.element.getNamespaceURI()));
54 writer.write(Namespace.get("jabber:client"));
55 out.write(">");
56 } catch (IOException ex) {
57 ex.printStackTrace();
58 }
59 return out.toString();
60 }
61
62 public JID getFromJid() {
63 String jid = this.element.attribute("from").getValue();
64 return new JID(jid);
65 }
66
67 public Element getElement() {
68 return this.element;
69 }
70
71 public JID getToJid() {
72 String jid = this.element.attribute("to").getValue();
73 return new JID(jid);
74 }
75}