INT-1554 added more tests and refactored/cleaned XmppPresenceMapper. Removed Presence Type header as unneccessery, since the only two payloads that are supported is Presence itself or Presentce.Type
This commit is contained in:
@@ -38,10 +38,6 @@ public class XmppHeaders {
|
||||
|
||||
public static final String TYPE = PREFIX + "type";
|
||||
|
||||
// public static final String ROSTER_CHANGE_TYPE = PREFIX + "roster_change_type";
|
||||
|
||||
// public static final String ROSTER = PREFIX + "roster";
|
||||
|
||||
public static final String PRESENCE = PREFIX + "presence";
|
||||
|
||||
public static final String PRESENCE_LANGUAGE = PRESENCE + "language";
|
||||
@@ -50,8 +46,6 @@ public class XmppHeaders {
|
||||
|
||||
public static final String PRESENCE_MODE = PRESENCE + "mode";
|
||||
|
||||
public static final String PRESENCE_TYPE = PRESENCE + "type";
|
||||
|
||||
public static final String PRESENCE_STATUS = PRESENCE + "status";
|
||||
|
||||
public static final String PRESENCE_FROM = PRESENCE + "from";
|
||||
|
||||
@@ -138,7 +138,6 @@ public class XmppNamespaceHandler extends NamespaceHandlerSupport {
|
||||
|
||||
// presence headers
|
||||
this.addElementToHeaderMapping("presence-mode", XmppHeaders.PRESENCE_MODE, Presence.Mode.class);
|
||||
this.addElementToHeaderMapping("presence-type", XmppHeaders.PRESENCE_TYPE, Presence.Type.class);
|
||||
this.addElementToHeaderMapping("presence-from", XmppHeaders.PRESENCE_FROM);
|
||||
this.addElementToHeaderMapping("presence-status", XmppHeaders.PRESENCE_STATUS);
|
||||
this.addElementToHeaderMapping("presence-priority", XmppHeaders.PRESENCE_PRIORITY, Integer.class);
|
||||
|
||||
@@ -39,78 +39,60 @@ public class XmppPresenceMessageMapper implements OutboundMessageMapper<Presence
|
||||
|
||||
/**
|
||||
* Builds {@link Message} with payload of {@link Presence} while also
|
||||
* setting Presense attributes as {@link MessageHeaders}
|
||||
* setting Presence attributes as {@link MessageHeaders}
|
||||
*
|
||||
* @param presence the presence object
|
||||
* @return the Message
|
||||
* @throws Exception thrown if conversion should fail
|
||||
*/
|
||||
public Message<?> toMessage(Presence presence) throws Exception {
|
||||
@SuppressWarnings("unchecked")
|
||||
public Message<Presence> toMessage(Presence presence) throws Exception {
|
||||
MessageBuilder<?> presenceMessageBuilder = MessageBuilder.withPayload(presence);
|
||||
presenceMessageBuilder.setHeader(XmppHeaders.PRESENCE_PRIORITY, presence.getPriority());
|
||||
presenceMessageBuilder.setHeader(XmppHeaders.PRESENCE_STATUS, presence.getStatus());
|
||||
presenceMessageBuilder.setHeader(XmppHeaders.PRESENCE_MODE, presence.getMode());
|
||||
presenceMessageBuilder.setHeader(XmppHeaders.PRESENCE_TYPE, presence.getType());
|
||||
presenceMessageBuilder.setHeader(XmppHeaders.PRESENCE_FROM, presence.getFrom());
|
||||
|
||||
return presenceMessageBuilder.build();
|
||||
return (Message<Presence>) presenceMessageBuilder.build();
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds a {@link org.jivesoftware.smack.packet.Presence} object from the inbound Message headers, if possible.
|
||||
* Builds a {@link Presence} object from the inbound Message headers, if possible.
|
||||
*
|
||||
* @param message the Message whose headers and payload willl b
|
||||
* @return the presence object as constructed from the {@link org.springframework.integration.Message} object
|
||||
* @throws Exception if there is a problem
|
||||
*/
|
||||
public Presence fromMessage(Message<?> message) throws Exception {
|
||||
MessageHeaders messageHeaders = message.getHeaders();
|
||||
|
||||
Integer priority = (Integer) messageHeaders.get(XmppHeaders.PRESENCE_PRIORITY);
|
||||
String status = (String) messageHeaders.get(XmppHeaders.PRESENCE_STATUS);
|
||||
String language = (String) messageHeaders.get(XmppHeaders.PRESENCE_LANGUAGE);
|
||||
String from = (String) messageHeaders.get(XmppHeaders.PRESENCE_FROM);
|
||||
|
||||
Object modeObj = messageHeaders.get(XmppHeaders.PRESENCE_MODE);
|
||||
Presence.Mode mode = null;
|
||||
|
||||
Object typeObj = messageHeaders.get(XmppHeaders.PRESENCE_TYPE);
|
||||
Presence.Type type = null;
|
||||
|
||||
if (typeObj != null){
|
||||
if (typeObj instanceof String) {
|
||||
type = Presence.Type.valueOf((String) typeObj);
|
||||
}
|
||||
else if (typeObj instanceof Presence.Type) {
|
||||
type = (Presence.Type) typeObj;
|
||||
}
|
||||
else {
|
||||
throw new MessageMappingException("Unsupported type for Presence type. Only" +
|
||||
" String or Presence.Type is allowed, but was: " + typeObj.getClass().getName());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (modeObj != null){
|
||||
if (modeObj instanceof String) {
|
||||
mode = Presence.Mode.valueOf((String) modeObj);
|
||||
}
|
||||
else if (modeObj instanceof Presence.Mode) {
|
||||
mode = (Presence.Mode) modeObj;
|
||||
}
|
||||
else {
|
||||
throw new MessageMappingException("Unsupported type for Presence mode. Only" +
|
||||
" String or Presence.Mode is allowed, but was: " + modeObj.getClass().getName());
|
||||
}
|
||||
}
|
||||
|
||||
Object payload = message.getPayload();
|
||||
if (payload instanceof Presence) {
|
||||
return (Presence) payload;
|
||||
}
|
||||
else if (payload instanceof Presence.Type) {
|
||||
type = (Presence.Type) payload;
|
||||
return this.factoryPresence(from, status, priority, type, mode, language);
|
||||
Presence.Type presenceType = (Presence.Type) payload;
|
||||
MessageHeaders messageHeaders = message.getHeaders();
|
||||
|
||||
Integer priority = (Integer) messageHeaders.get(XmppHeaders.PRESENCE_PRIORITY);
|
||||
String status = (String) messageHeaders.get(XmppHeaders.PRESENCE_STATUS);
|
||||
String language = (String) messageHeaders.get(XmppHeaders.PRESENCE_LANGUAGE);
|
||||
String from = (String) messageHeaders.get(XmppHeaders.PRESENCE_FROM);
|
||||
|
||||
Object modeObj = messageHeaders.get(XmppHeaders.PRESENCE_MODE);
|
||||
Presence.Mode mode = null;
|
||||
|
||||
if (modeObj != null){
|
||||
if (modeObj instanceof String) {
|
||||
mode = Presence.Mode.valueOf((String) modeObj);
|
||||
}
|
||||
else if (modeObj instanceof Presence.Mode) {
|
||||
mode = (Presence.Mode) modeObj;
|
||||
}
|
||||
else {
|
||||
throw new MessageMappingException("Unsupported type for Presence mode. Only" +
|
||||
" String or Presence.Mode is allowed, but was: " + modeObj.getClass().getName());
|
||||
}
|
||||
}
|
||||
return this.factoryPresence(from, status, priority, presenceType, mode, language);
|
||||
}
|
||||
else {
|
||||
throw new MessageMappingException("Unsupported Payload type: " + payload.getClass().getName());
|
||||
|
||||
@@ -103,7 +103,7 @@ public class XmppRosterEventMessageDrivenEndpoint extends AbstractEndpoint {
|
||||
*
|
||||
* @param presence the {@link org.jivesoftware.smack.packet.Presence} object representing the new state (optional)
|
||||
*/
|
||||
protected void forwardRosterEventMessage(Presence presence) {
|
||||
private void forwardRosterEventMessage(Presence presence) {
|
||||
try {
|
||||
Message<?> msg = this.messageMapper.toMessage(presence);
|
||||
messagingTemplate.send(requestChannel, msg);
|
||||
|
||||
@@ -18,6 +18,7 @@ package org.springframework.integration.xmpp.ignore;
|
||||
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.jivesoftware.smack.packet.Presence;
|
||||
import org.jivesoftware.smack.packet.Presence.Type;
|
||||
import org.springframework.integration.Message;
|
||||
import org.springframework.integration.core.MessageSource;
|
||||
import org.springframework.integration.support.MessageBuilder;
|
||||
@@ -40,12 +41,11 @@ public class XmppRosterEventProducer implements MessageSource<String> {
|
||||
catch (InterruptedException e) {
|
||||
// eat it
|
||||
}
|
||||
return (Math.random() > .5) ? MessageBuilder.withPayload(StringUtils.EMPTY).setHeader(
|
||||
XmppHeaders.PRESENCE_MODE, Presence.Mode.chat).setHeader(XmppHeaders.PRESENCE_TYPE,
|
||||
Presence.Type.available).setHeader(XmppHeaders.PRESENCE_STATUS, "She Loves me").build()
|
||||
return (Math.random() > .5) ? MessageBuilder.withPayload("available").setHeader(
|
||||
XmppHeaders.PRESENCE_MODE, Presence.Mode.chat)
|
||||
.setHeader(XmppHeaders.PRESENCE_STATUS, "She Loves me").build()
|
||||
: MessageBuilder.withPayload(StringUtils.EMPTY).setHeader(XmppHeaders.PRESENCE_MODE, Presence.Mode.dnd)
|
||||
.setHeader(XmppHeaders.PRESENCE_TYPE, Presence.Type.available).setHeader(
|
||||
XmppHeaders.PRESENCE_STATUS, "She Loves me not").build();
|
||||
.setHeader(XmppHeaders.PRESENCE_STATUS, "She Loves me not").build();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -32,6 +32,14 @@ import org.springframework.integration.xmpp.XmppHeaders;
|
||||
*/
|
||||
public class XmppPresenceMessageMapperTests {
|
||||
|
||||
@Test
|
||||
public void testToMessage() throws Exception{
|
||||
Presence presence = new Presence(Type.available, "Hello", 1, Mode.chat);
|
||||
XmppPresenceMessageMapper mapper = new XmppPresenceMessageMapper();
|
||||
Message<Presence> presenceMessage = mapper.toMessage(presence);
|
||||
assertEquals(presence, presenceMessage.getPayload());
|
||||
// TODO look into why presence attributes are also duplicated as headers
|
||||
}
|
||||
@Test
|
||||
public void testFromMessageWithPayloadPresence() throws Exception{
|
||||
Presence presence = new Presence(Type.available, "Hello", 1, Mode.chat);
|
||||
@@ -50,7 +58,6 @@ public class XmppPresenceMessageMapperTests {
|
||||
.setHeader(XmppHeaders.PRESENCE_FROM, "oleg")
|
||||
.setHeader(XmppHeaders.PRESENCE_MODE, Mode.chat)
|
||||
.setHeader(XmppHeaders.PRESENCE_STATUS, "hello")
|
||||
.setHeader(XmppHeaders.PRESENCE_TYPE, Type.subscribed)
|
||||
.setHeader(XmppHeaders.PRESENCE_PRIORITY, 1)
|
||||
.build();
|
||||
XmppPresenceMessageMapper mapper = new XmppPresenceMessageMapper();
|
||||
@@ -66,7 +73,6 @@ public class XmppPresenceMessageMapperTests {
|
||||
.setHeader(XmppHeaders.PRESENCE_FROM, "oleg")
|
||||
.setHeader(XmppHeaders.PRESENCE_MODE, "chat")
|
||||
.setHeader(XmppHeaders.PRESENCE_STATUS, "hello")
|
||||
.setHeader(XmppHeaders.PRESENCE_TYPE, "subscribed")
|
||||
.setHeader(XmppHeaders.PRESENCE_PRIORITY, 1)
|
||||
.build();
|
||||
XmppPresenceMessageMapper mapper = new XmppPresenceMessageMapper();
|
||||
@@ -87,16 +93,6 @@ public class XmppPresenceMessageMapperTests {
|
||||
mapper.fromMessage(message);
|
||||
}
|
||||
|
||||
@Test(expected=MessageMappingException.class)
|
||||
public void testFromMessageWithPayloadPresenceTypeUnsupportedType() throws Exception{
|
||||
|
||||
Message<?> message = MessageBuilder.withPayload(Type.available)
|
||||
.setHeader(XmppHeaders.PRESENCE_TYPE, 1)
|
||||
.build();
|
||||
XmppPresenceMessageMapper mapper = new XmppPresenceMessageMapper();
|
||||
mapper.fromMessage(message);
|
||||
}
|
||||
|
||||
@Test(expected=MessageMappingException.class)
|
||||
public void testFromMessageWithUnsupportedPayload() throws Exception{
|
||||
Message<?> message = MessageBuilder.withPayload("hello").build();
|
||||
|
||||
Reference in New Issue
Block a user