polishing

This commit is contained in:
Mark Fisher
2010-12-17 10:34:47 -05:00
parent 2d59dced3d
commit 66aefa86c7
3 changed files with 25 additions and 23 deletions

View File

@@ -36,19 +36,19 @@ public abstract class AbstractXmppConnectionAwareMessageHandler extends Abstract
}
public AbstractXmppConnectionAwareMessageHandler(XMPPConnection xmppConnection) {
Assert.notNull(xmppConnection, "'xmppConnection' must no be null");
Assert.notNull(xmppConnection, "XMPPConnection must not be null");
this.xmppConnection = xmppConnection;
}
protected void onInit() throws Exception {
BeanFactory bf = this.getBeanFactory();
if (xmppConnection == null && bf != null) {
xmppConnection = bf.getBean(XmppContextUtils.XMPP_CONNECTION_BEAN_NAME, XMPPConnection.class);
BeanFactory beanFactory = this.getBeanFactory();
if (this.xmppConnection == null && beanFactory != null) {
this.xmppConnection = beanFactory.getBean(XmppContextUtils.XMPP_CONNECTION_BEAN_NAME, XMPPConnection.class);
}
Assert.notNull(xmppConnection, "Failed to resolve XMPPConnection. XMPPConnection must either be set expicitly " +
Assert.notNull(this.xmppConnection, "Failed to resolve XMPPConnection. XMPPConnection must either be set explicitly " +
"via 'xmpp-connection' attribute or implicitly by registering a bean with the name 'xmppConnection' and of type " +
"'org.jivesoftware.smack.XMPPConnection' in the Application Context");
"'org.jivesoftware.smack.XMPPConnection' in the Application Context.");
this.initialized = true;
}

View File

@@ -26,6 +26,9 @@ import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
/**
* MessageHandler that sends an XMPP Chat Message. Supported payload types are Smack Message
* (org.jivesoftware.smack.packet.Message) or String.
*
* @author Josh Long
* @author Mario Gray
* @author Oleg Zhurakousky
@@ -33,11 +36,11 @@ import org.springframework.util.StringUtils;
*/
public class ChatMessageSendingMessageHandler extends AbstractXmppConnectionAwareMessageHandler {
public ChatMessageSendingMessageHandler(){
public ChatMessageSendingMessageHandler() {
super();
}
public ChatMessageSendingMessageHandler(XMPPConnection xmppConnection){
public ChatMessageSendingMessageHandler(XMPPConnection xmppConnection) {
super(xmppConnection);
}
@@ -47,30 +50,28 @@ public class ChatMessageSendingMessageHandler extends AbstractXmppConnectionAwar
Assert.isTrue(this.initialized, this.getComponentName() + "#" + this.getComponentType() + " must be initialized");
Object messageBody = message.getPayload();
org.jivesoftware.smack.packet.Message xmppMessage = null;
if (messageBody instanceof org.jivesoftware.smack.packet.Message) {
xmppMessage = (org.jivesoftware.smack.packet.Message) messageBody;
}
else if (messageBody instanceof String) {
String chatTo = (String) message.getHeaders().get(XmppHeaders.CHAT_TO);
else if (messageBody instanceof String) {
String chatTo = message.getHeaders().get(XmppHeaders.CHAT_TO, String.class);
Assert.state(StringUtils.hasText(chatTo), "The '" + XmppHeaders.CHAT_TO + "' header must not be null");
xmppMessage = new org.jivesoftware.smack.packet.Message(chatTo);
String threadId = (String) message.getHeaders().get(XmppHeaders.CHAT_THREAD_ID);
if (StringUtils.hasText(threadId)){
String threadId = message.getHeaders().get(XmppHeaders.CHAT_THREAD_ID, String.class);
if (StringUtils.hasText(threadId)) {
xmppMessage.setThread(threadId);
}
xmppMessage.setBody((String) messageBody);
}
else {
throw new MessageHandlingException(message, "Only payloads of type java.lang.String or org.jivesoftware.smack.packet.Message " +
"are suported. Was '" + messageBody.getClass().getName() +
"' Consider adding a transformer prior to sending message to this handler");
"are supported. Received [" + messageBody.getClass().getName() +
"]. Consider adding a Transformer prior to this adapter.");
}
if (!this.xmppConnection.isConnected()){
if (!this.xmppConnection.isConnected()) {
this.xmppConnection.connect();
}
this.xmppConnection.sendPacket(xmppMessage);
}
}

View File

@@ -18,12 +18,13 @@ package org.springframework.integration.xmpp.outbound;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.packet.Presence;
import org.springframework.integration.Message;
import org.springframework.integration.xmpp.core.AbstractXmppConnectionAwareMessageHandler;
import org.springframework.util.Assert;
/**
* MessageHandler that publishes updated Presence values for a given connection.
* MessageHandler that publishes updated Presence values for a given XMPP connection.
*
* @author Josh Long
* @author Oleg Zhurakousky
@@ -44,12 +45,12 @@ public class PresenceSendingMessageHandler extends AbstractXmppConnectionAwareMe
protected void handleMessageInternal(Message<?> message) throws Exception {
Assert.isTrue(this.initialized, this.getComponentName() + " must be initialized");
Object payload = message.getPayload();
Assert.isInstanceOf(Presence.class, payload, "'payload' must be of type 'org.jivesoftware.smack.packet.Presence', was: "
+ payload.getClass().getName());
if (!this.xmppConnection.isConnected()){
Assert.isTrue(payload instanceof Presence,
"Payload must be of type 'org.jivesoftware.smack.packet.Presence', was: " + payload.getClass().getName());
if (!this.xmppConnection.isConnected()) {
this.xmppConnection.connect();
}
this.xmppConnection.sendPacket((Presence)payload);
this.xmppConnection.sendPacket((Presence) payload);
}
}