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