001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.camel.component.netty;
018    
019    import java.net.SocketAddress;
020    
021    import org.apache.camel.CamelExchangeException;
022    import org.apache.camel.Exchange;
023    import org.apache.camel.NoTypeConversionAvailableException;
024    import org.jboss.netty.channel.Channel;
025    import org.jboss.netty.channel.ChannelFuture;
026    import org.slf4j.Logger;
027    import org.slf4j.LoggerFactory;
028    
029    /**
030     * Helper class used internally by camel-netty using Netty.
031     *
032     * @version 
033     */
034    public final class NettyHelper {
035    
036        private static final transient Logger LOG = LoggerFactory.getLogger(NettyHelper.class);
037    
038        private NettyHelper() {
039            // Utility class
040        }
041    
042        /**
043         * Gets the string body to be used when sending with the textline codec.
044         *
045         * @param body                 the current body
046         * @param exchange             the exchange
047         * @param delimiter            the textline delimiter
048         * @param autoAppendDelimiter  whether absent delimiter should be auto appended
049         * @return the string body to send
050         * @throws NoTypeConversionAvailableException is thrown if the current body could not be converted to a String type
051         */
052        public static String getTextlineBody(Object body, Exchange exchange, TextLineDelimiter delimiter, boolean autoAppendDelimiter) throws NoTypeConversionAvailableException {
053            String s = exchange.getContext().getTypeConverter().mandatoryConvertTo(String.class, exchange, body);
054    
055            // auto append delimiter if missing?
056            if (autoAppendDelimiter) {
057                if (TextLineDelimiter.LINE.equals(delimiter)) {
058                    // line delimiter so ensure it ends with newline
059                    if (!s.endsWith("\n")) {
060                        LOG.trace("Auto appending missing newline delimiter to body");
061                        s = s + "\n";
062                    }
063                } else {
064                    // null delimiter so ensure it ends with null
065                    if (!s.endsWith("\u0000")) {
066                        LOG.trace("Auto appending missing null delimiter to body");
067                        s = s + "\u0000";
068                    }
069                }
070            }
071    
072            return s;
073        }
074    
075        /**
076         * Writes the given body to Netty channel. Will wait until the body has been written.
077         *
078         * @param channel         the Netty channel
079         * @param remoteAddress   the remote address when using UDP
080         * @param body            the body to write (send)
081         * @param exchange        the exchange
082         * @throws CamelExchangeException is thrown if the body could not be written for some reasons
083         *                                (eg remote connection is closed etc.)
084         */
085        public static void writeBodySync(Channel channel, SocketAddress remoteAddress, Object body, Exchange exchange) throws CamelExchangeException {
086            // the write operation is asynchronous. Use future to wait until the session has been written
087            ChannelFuture future;
088            if (remoteAddress != null) {
089                future = channel.write(body, remoteAddress);
090            } else {
091                future = channel.write(body);
092            }
093    
094            // wait for the write
095            LOG.trace("Waiting for write to complete");
096            future.awaitUninterruptibly();
097    
098            // if it was not a success then thrown an exception
099            if (!future.isSuccess()) {
100                LOG.warn("Cannot write body: " + body + " using channel: " + channel);
101                throw new CamelExchangeException("Cannot write body", exchange, future.getCause());
102            }
103        }
104    
105        /**
106         * Closes the given channel
107         *
108         * @param channel the channel to close
109         */
110        public static void close(Channel channel) {
111            if (channel != null) {
112                LOG.trace("Closing channel: {}", channel);
113                channel.close().awaitUninterruptibly();
114            }
115        }
116    
117    }