我在解析消息时注意到 Google App Engine 日志中出现错误。堆栈跟踪对诊断问题没有帮助,因此我受 Google 实现 InboundMessageParser 的启发,编写了一个小型消息转储程序。 .
public class ChatRequestParser extends HttpRequestParser {
public static Map<String, String> parseMessage(HttpServletRequest request)
throws IOException {
try {
Map<String, String> message = new HashMap<String, String>();
MimeMultipart multipart = parseMultipartRequest(request);
int parts = multipart.getCount();
for (int i = 0; i < parts; i++) {
BodyPart part = multipart.getBodyPart(i);
String fieldName = getFieldName(part);
String fieldValue = getTextContent(part);
message.put(fieldName, fieldValue);
}
return message;
} catch (MessagingException ex) {
throw new IOException("Could not parse incoming request.", ex);
}
}
}
我发现 Google+ 为每条消息发送两条消息,其中只有一条包含正文(Gmail Talk 客户端仅发送一条消息)。
这是第一条消息,没有正文:
{to=xxx@appspot.com, stanza=<message to="xxx@appspot.com" type="chat"
from="yyy@gmail.com/TalkGadgetD9F45A83" xmlns="jabber:client">
<cha:composing xmlns:cha="http://jabber.org/protocol/chatstates"/>
<nos:x value="disabled" xmlns:nos="google:nosave"/>
<arc:record otr="false" xmlns:arc="http://jabber.org/protocol/archive"/>
</message>, from=yyy@gmail.com/TalkGadgetD9F45A83}
第二个是(我的有效负载有很多星号,邮件已更改):
{to=xxx@appspot.com, body=**********************************,
stanza=<message to="xxx@appspot.com" type="chat"
id="7279D79D0.17809585028724073_:sl" from="yyy@gmail.com/TalkGadgetD9F45A83"
xmlns="jabber:client"><body>**********************************</body>
<cha:active xmlns:cha="http://jabber.org/protocol/chatstates"/>
<nos:x value="disabled" xmlns:nos="google:nosave"/><arc:record otr="false"
xmlns:arc="http://jabber.org/protocol/archive"/></message>,
from=yyy@gmail.com/TalkGadgetD9F45A83}
由于第一条消息没有正文,因此在 XMPPService 上调用 parseMessage() 会引发异常。有人注意到这个问题吗?
现在我捕获了 IllegalArgumentException 并丢弃了无意义的消息,但真正的问题是,对有效消息的回复不会返回到 Google+ 客户端,而与 Gmail 完美配合,并且还可以使用 Linux 上的 Jabber 客户端。
我已提交 issue 6467 .
最佳答案
当没有设置主体并且调用 parseMessage 时,我可以重现崩溃,并且我正在修复它。感谢您找到它!
但是,我无法重现“发送回复不起作用”的错误。我有这样的代码:
XMPPService xmpp = XMPPServiceFactory.getXMPPService();
Message message = xmpp.parseMessage(req);
Message reply = new MessageBuilder().withFromJid(message.getRecipientJids()[0])
.withRecipientJids(message.getFromJid())
.withBody("Back at you!")
.build();
xmpp.sendMessage(reply);
我在 Google+ 和 Gmail 中都收到了回复。你做了什么不同的事情?
关于java - 从 GAE 到 Google+ 的 XMPP 回复不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8374745/