Tomek Osiński | e9ccf41 | 2018-01-13 19:44:11 +0100 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package org.onosproject.xmpp.core.stream; |
| 18 | |
| 19 | import org.dom4j.Attribute; |
| 20 | import org.dom4j.Element; |
| 21 | import org.dom4j.Namespace; |
| 22 | import org.dom4j.io.OutputFormat; |
| 23 | import org.dom4j.io.XMLWriter; |
| 24 | import org.xmpp.packet.JID; |
| 25 | |
| 26 | import java.io.IOException; |
| 27 | import java.io.StringWriter; |
| 28 | import java.util.List; |
| 29 | |
Ray Milkey | 8124ac9 | 2018-03-06 17:56:03 -0800 | [diff] [blame^] | 30 | import org.slf4j.Logger; |
| 31 | import org.slf4j.LoggerFactory; |
| 32 | |
Tomek Osiński | e9ccf41 | 2018-01-13 19:44:11 +0100 | [diff] [blame] | 33 | /** |
| 34 | * Abstracts XMPP stream open event. |
| 35 | */ |
| 36 | public class XmppStreamOpen implements XmppStreamEvent { |
| 37 | |
Ray Milkey | 8124ac9 | 2018-03-06 17:56:03 -0800 | [diff] [blame^] | 38 | private final Logger log = LoggerFactory.getLogger(XmppStreamOpen.class); |
| 39 | |
Tomek Osiński | e9ccf41 | 2018-01-13 19:44:11 +0100 | [diff] [blame] | 40 | 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 Milkey | 8124ac9 | 2018-03-06 17:56:03 -0800 | [diff] [blame^] | 62 | log.info("Error writing XML", ex); |
Tomek Osiński | e9ccf41 | 2018-01-13 19:44:11 +0100 | [diff] [blame] | 63 | } |
| 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 | } |