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.ctl.handlers; |
| 18 | |
| 19 | import io.netty.channel.ChannelHandlerContext; |
| 20 | import io.netty.handler.codec.MessageToMessageDecoder; |
| 21 | import org.dom4j.DocumentFactory; |
| 22 | import org.dom4j.Element; |
| 23 | import org.dom4j.QName; |
| 24 | import org.onosproject.xmpp.core.XmppConstants; |
| 25 | import org.onosproject.xmpp.core.ctl.XmppValidator; |
| 26 | import org.onosproject.xmpp.core.ctl.exception.UnsupportedStanzaTypeException; |
| 27 | import org.onosproject.xmpp.core.ctl.exception.XmppValidationException; |
| 28 | import org.onosproject.xmpp.core.stream.XmppStreamClose; |
| 29 | import org.onosproject.xmpp.core.stream.XmppStreamError; |
| 30 | import org.onosproject.xmpp.core.stream.XmppStreamOpen; |
| 31 | import org.slf4j.Logger; |
| 32 | import org.slf4j.LoggerFactory; |
| 33 | import org.xmpp.packet.IQ; |
| 34 | import org.xmpp.packet.Message; |
| 35 | import org.xmpp.packet.Packet; |
| 36 | import org.xmpp.packet.Presence; |
| 37 | |
| 38 | import javax.xml.stream.events.Attribute; |
| 39 | import javax.xml.stream.events.Namespace; |
| 40 | import javax.xml.stream.events.StartElement; |
| 41 | import javax.xml.stream.events.XMLEvent; |
| 42 | import java.util.Iterator; |
| 43 | import java.util.List; |
| 44 | |
| 45 | import static com.google.common.base.Preconditions.checkNotNull; |
| 46 | |
| 47 | /** |
| 48 | * Translates XML Element to XMPP Packet. |
| 49 | */ |
| 50 | public class XmppDecoder extends MessageToMessageDecoder { |
| 51 | |
| 52 | private final Logger logger = LoggerFactory.getLogger(getClass()); |
| 53 | |
| 54 | private XmppValidator validator = new XmppValidator(); |
| 55 | |
| 56 | @Override |
| 57 | protected void decode(ChannelHandlerContext channelHandlerContext, Object object, List out) throws Exception { |
| 58 | if (object instanceof Element) { |
| 59 | Element root = (Element) object; |
| 60 | |
| 61 | try { |
| 62 | Packet packet = recognizeAndReturnXmppPacket(root); |
| 63 | validate(packet); |
| 64 | out.add(packet); |
| 65 | } catch (UnsupportedStanzaTypeException e) { |
| 66 | throw e; |
| 67 | } catch (Exception e) { |
| 68 | throw new XmppValidationException(false); |
| 69 | } |
| 70 | |
| 71 | } else if (object instanceof XMLEvent) { |
| 72 | |
| 73 | XMLEvent event = (XMLEvent) object; |
| 74 | if (event.isStartElement()) { |
| 75 | final StartElement element = event.asStartElement(); |
| 76 | |
| 77 | if (element.getName().getLocalPart().equals(XmppConstants.STREAM_QNAME)) { |
| 78 | DocumentFactory df = DocumentFactory.getInstance(); |
| 79 | QName qname = (element.getName().getPrefix() == null) ? |
| 80 | df.createQName(element.getName().getLocalPart(), |
| 81 | element.getName().getNamespaceURI()) : |
| 82 | df.createQName(element.getName().getLocalPart(), |
| 83 | element.getName().getPrefix(), element.getName().getNamespaceURI()); |
| 84 | |
| 85 | Element newElement = df.createElement(qname); |
| 86 | |
| 87 | Iterator nsIt = element.getNamespaces(); |
| 88 | // add all relevant XML namespaces to Element |
| 89 | while (nsIt.hasNext()) { |
| 90 | Namespace ns = (Namespace) nsIt.next(); |
| 91 | newElement.addNamespace(ns.getPrefix(), ns.getNamespaceURI()); |
| 92 | } |
| 93 | |
| 94 | Iterator attrIt = element.getAttributes(); |
| 95 | // add all attributes to Element |
| 96 | while (attrIt.hasNext()) { |
| 97 | Attribute attr = (Attribute) attrIt.next(); |
| 98 | newElement.addAttribute(attr.getName().getLocalPart(), attr.getValue()); |
| 99 | } |
| 100 | XmppStreamOpen xmppStreamOpen = new XmppStreamOpen(newElement); |
| 101 | validator.validateStream(xmppStreamOpen); |
| 102 | out.add(xmppStreamOpen); |
| 103 | } |
| 104 | } else if (event.isEndElement()) { |
| 105 | out.add(new XmppStreamClose()); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | } |
| 110 | |
| 111 | @Override |
| 112 | public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { |
| 113 | logger.info("Exception caught: {}", cause.getMessage()); |
| 114 | if (cause.getCause() instanceof XmppValidationException) { |
| 115 | if (((XmppValidationException) cause.getCause()).isStreamValidationException()) { |
| 116 | XmppStreamError.Condition condition = XmppStreamError.Condition.bad_format; |
| 117 | XmppStreamError error = new XmppStreamError(condition); |
| 118 | ctx.channel().writeAndFlush(error); |
| 119 | ctx.channel().writeAndFlush(new XmppStreamClose()); |
| 120 | return; |
| 121 | } |
| 122 | } |
| 123 | logger.info("Not a StreamValidationException. Sending exception upstream."); |
| 124 | ctx.fireExceptionCaught(cause); |
| 125 | } |
| 126 | |
| 127 | |
| 128 | private void validate(Packet packet) throws UnsupportedStanzaTypeException, XmppValidationException { |
| 129 | validator.validate(packet); |
| 130 | } |
| 131 | |
| 132 | protected Packet recognizeAndReturnXmppPacket(Element root) |
| 133 | throws UnsupportedStanzaTypeException, IllegalArgumentException { |
| 134 | checkNotNull(root); |
| 135 | |
| 136 | Packet packet = null; |
| 137 | if (root.getName().equals(XmppConstants.IQ_QNAME)) { |
| 138 | packet = new IQ(root); |
| 139 | } else if (root.getName().equals(XmppConstants.MESSAGE_QNAME)) { |
| 140 | packet = new Message(root); |
| 141 | } else if (root.getName().equals(XmppConstants.PRESENCE_QNAME)) { |
| 142 | packet = new Presence(root); |
| 143 | } else { |
| 144 | throw new UnsupportedStanzaTypeException("Unrecognized XMPP Packet"); |
| 145 | } |
| 146 | logger.info("XMPP Packet received\n" + root.asXML()); |
| 147 | return packet; |
| 148 | } |
| 149 | |
| 150 | } |