blob: 00c8e0a52c3b6f2556b2ce75df40e1c6705f5db6 [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.ctl.handlers;
18
19
20import io.netty.buffer.ByteBuf;
21import io.netty.channel.ChannelHandlerContext;
22import io.netty.handler.codec.MessageToByteEncoder;
23
24import io.netty.util.CharsetUtil;
25import org.onosproject.xmpp.core.stream.XmppStreamEvent;
26import org.slf4j.Logger;
27import org.slf4j.LoggerFactory;
28import org.xmpp.packet.Packet;
29
30import static com.google.common.base.Preconditions.checkNotNull;
31
32/**
33 * Encodes XMPP message and writes XML data to channel.
34 */
35public 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}