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 | |
| 20 | import io.netty.buffer.ByteBuf; |
| 21 | import io.netty.channel.ChannelHandlerContext; |
| 22 | import io.netty.handler.codec.MessageToByteEncoder; |
| 23 | |
| 24 | import io.netty.util.CharsetUtil; |
| 25 | import org.onosproject.xmpp.core.stream.XmppStreamEvent; |
| 26 | import org.slf4j.Logger; |
| 27 | import org.slf4j.LoggerFactory; |
| 28 | import org.xmpp.packet.Packet; |
| 29 | |
| 30 | import static com.google.common.base.Preconditions.checkNotNull; |
| 31 | |
| 32 | /** |
| 33 | * Encodes XMPP message and writes XML data to channel. |
| 34 | */ |
| 35 | public class XmppEncoder extends MessageToByteEncoder { |
| 36 | |
| 37 | private final Logger logger = LoggerFactory.getLogger(getClass()); |
| 38 | |
| 39 | @Override |
| 40 | protected void encode(ChannelHandlerContext ctx, Object msg, ByteBuf out) throws Exception { |
| 41 | byte[] bytes = null; |
| 42 | |
| 43 | if (msg instanceof XmppStreamEvent) { |
| 44 | XmppStreamEvent streamEvent = (XmppStreamEvent) msg; |
| 45 | logger.info("SENDING: {}", streamEvent.toXml()); |
| 46 | bytes = streamEvent.toXml().getBytes(CharsetUtil.UTF_8); |
| 47 | } |
| 48 | |
| 49 | if (msg instanceof Packet) { |
| 50 | Packet pkt = (Packet) msg; |
| 51 | logger.info("SENDING /n, {}", pkt.toString()); |
| 52 | bytes = pkt.toXML().getBytes(CharsetUtil.UTF_8); |
| 53 | } |
| 54 | |
| 55 | out.writeBytes(checkNotNull(bytes)); |
| 56 | } |
| 57 | } |