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.irc;
018    
019    import java.util.Map;
020    
021    import org.apache.camel.Exchange;
022    import org.apache.camel.impl.DefaultMessage;
023    import org.apache.camel.util.ExchangeHelper;
024    import org.schwering.irc.lib.IRCUser;
025    
026    public class IrcMessage extends DefaultMessage {
027        private String messageType;
028        private String target;
029        private IRCUser user;
030        private String whoWasKickedNick;
031        private String message;
032        private int num;
033        private String value;
034    
035        public IrcMessage() {
036        }
037    
038        public IrcMessage(String messageType, IRCUser user, String message) {
039            this.messageType = messageType;
040            this.user = user;
041            this.message = message;
042        }
043    
044        public IrcMessage(String messageType, String target, IRCUser user, String message) {
045            this.messageType = messageType;
046            this.target = target;
047            this.user = user;
048            this.message = message;
049        }
050    
051        public IrcMessage(String messageType, String target, IRCUser user, String whoWasKickedNick, String message) {
052            this.messageType = messageType;
053            this.target = target;
054            this.user = user;
055            this.whoWasKickedNick = whoWasKickedNick;
056            this.message = message;
057        }
058    
059        public IrcMessage(String messageType, String target, IRCUser user) {
060            this.messageType = messageType;
061            this.target = target;
062            this.user = user;
063        }
064    
065        public IrcMessage(String messageType, int num, String value, String message) {
066            this.messageType = messageType;
067            this.num = num;
068            this.value = value;
069            this.message = message;
070        }
071    
072        public String getMessageType() {
073            return messageType;
074        }
075    
076        public void setMessageType(String messageType) {
077            this.messageType = messageType;
078        }
079    
080        public String getTarget() {
081            return target;
082        }
083    
084        public void setTarget(String target) {
085            this.target = target;
086        }
087    
088        public IRCUser getUser() {
089            return user;
090        }
091    
092        public void setUser(IRCUser user) {
093            this.user = user;
094        }
095    
096        public String getWhoWasKickedNick() {
097            return whoWasKickedNick;
098        }
099    
100        public void setWhoWasKickedNick(String whoWasKickedNick) {
101            this.whoWasKickedNick = whoWasKickedNick;
102        }
103    
104        public String getMessage() {
105            return message;
106        }
107    
108        public void setMessage(String message) {
109            this.message = message;
110        }
111    
112        @Override
113        protected Object createBody() {
114            Exchange exchange = getExchange();
115            IrcBinding binding = ExchangeHelper.getBinding(getExchange(), IrcBinding.class);
116            return binding != null ? binding.extractBodyFromIrc(exchange, this) : null;
117        }
118    
119        @Override
120        public IrcMessage newInstance() {
121            return new IrcMessage();
122        }
123    
124        @Override
125        protected void populateInitialHeaders(Map<String, Object> map) {
126            map.put(IrcConstants.IRC_MESSAGE_TYPE, messageType);
127            if (target != null) {
128                map.put(IrcConstants.IRC_TARGET, target);
129            }
130            if (whoWasKickedNick != null) {
131                map.put(IrcConstants.IRC_USER_KICKED, whoWasKickedNick);
132            }
133            if (user != null) {
134                map.put(IrcConstants.IRC_USER_HOST, user.getHost());
135                map.put(IrcConstants.IRC_USER_NICK, user.getNick());
136                map.put(IrcConstants.IRC_USER_SERVERNAME, user.getServername());
137                map.put(IrcConstants.IRC_USER_USERNAME, user.getUsername());
138            }
139            if (value != null) {
140                map.put(IrcConstants.IRC_NUM, num);
141                map.put(IrcConstants.IRC_VALUE, value);
142            }
143        }
144    
145        @Override
146        public String toString() {
147            if (message != null) {
148                return "IrcMessage: " + message;
149            } else {
150                return "IrcMessage: " + getBody();
151            }
152        }
153    }