【问题标题】:Cannot send message using mod_rest in Ejabberd 14.05无法在 Ejabberd 14.05 中使用 mod_rest 发送消息
【发布时间】:2016-10-25 08:00:30
【问题描述】:

大家好,我正在尝试从 Java 应用程序发送 xmpp 消息。我已经在 ejabberd 中配置了 mod_rest。 Java发送xmpp消息的方法如下:

public String sendmessage() {
        String response = null;
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_XML);
        String xmppMessage = "<message from='user1@<ip>'"+
                " to='user2@<ip>'"+
                ">"+ 
              "<body>Hello</body>"+
            "</message>";

        headers.set("Host",ip+":"+port);
        headers.set("Accept-Charset","*/*");
        headers.setContentType(MediaType.APPLICATION_XML);

        HttpEntity<String> request = new HttpEntity<String>(xmppMessage,headers);
        response = restTemplate.postForObject("http://ip:port/rest", request, String.class);

        return response;
    }

当我收到 HTTP 406 错误时:

Jun 23, 2016 11:32:14 AM org.springframework.web.client.RestTemplate handleResponseError
WARNING: POST request for "http://ip/rest" resulted in 406 (Not Acceptable); invoking error handler
Exception in thread "main" org.springframework.web.client.HttpClientErrorException: 406 Not Acceptable
    at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:91)
    at org.springframework.web.client.RestTemplate.handleResponseError(RestTemplate.java:576)
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:532)
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:489)
    at org.springframework.web.client.RestTemplate.postForObject(RestTemplate.java:318)
    at ejabberd.rest.service.impl.JabberServiceImpl.sendmessage(JabberServiceImpl.java:377)
    at ejabberd.rest.service.impl.JabberServiceImpl.main(JabberServiceImpl.java:386)

在 ejabberd.log 中出现匹配错误

2016-06-23 11:55:16.838 [info] <0.813.0>@ejabberd_listener:accept:313 (#Port<0.6091>) Accepted connection ip1 -> ejabberdIP:port
2016-06-23 11:55:16.839 [debug] <0.1069.0>@ejabberd_http:init:145 S: [{[<<"rest">>],mod_rest},{[<<"captcha">>],ejabberd_captcha},{[<<"admin">>],ejabberd_web_admin},{[<<"http-bind">>],mod_http_bind},{[<<"http-poll">>],ejabberd_http_poll}]
2016-06-23 11:55:16.839 [info] <0.1069.0>@ejabberd_http:init:149 started: {gen_tcp,#Port<0.6091>}
2016-06-23 11:55:16.839 [debug] <0.1069.0>@ejabberd_http:process_header:277 (#Port<0.6091>) http query: 'POST' /rest
2016-06-23 11:55:16.839 [debug] <0.1069.0>@ejabberd_http:process_request:438 client data: <<"<message from='user1@ip' to='user2@ip'><body>Hello</body></message>">>
2016-06-23 11:55:16.840 [debug] <0.1069.0>@ejabberd_http:process:350 [<<"rest">>] matches [<<"rest">>]
2016-06-23 11:55:16.841 [debug] <0.1069.0>@ejabberd_http:process:354 [{'Content-Length',<<"97">>},{'Connection',<<"keep-alive">>},{'Host',<<"ip:port">>},{'User-Agent',<<"Java/1.7.0_45">>},{'Accept-Charset',<<"big5, big5-hkscs, euc-jp, euc-kr, gb18030, gb2312, gbk, ibm-thai, ibm00858, ibm01140, ibm01141, ibm01142, ibm01143, ibm01144, ibm01145, ibm01146, ibm01147, ibm01148, ibm01149, ibm037, ibm1026, ibm1047, ibm273, ibm277, ibm278, ibm280, ibm284, ibm285, ibm290, ibm297, ibm420, ibm424, ibm437, ibm500, ibm775, ibm850, ibm852, ibm855, ibm857, ibm860, ibm861, ibm862, ibm863, ibm864, ibm865, ibm866, ibm868, ibm869, ibm870, ibm871, ibm918, iso-2022-cn, iso-2022-jp, iso-2022-jp-2, iso-2022-kr, iso-8859-1, iso-8859-13, iso-8859-15, iso-8859-2, iso-8859-3, iso-8859-4, iso-8859-5, iso-8859-6, iso-8859-7, iso-8859-8, iso-8859-9, jis_x0201, jis_x0212-1990, koi8-r, koi8-u, shift_jis, tis-620, us-ascii, utf-16, utf-16be, utf-16le, utf-32, utf-32be, utf-32le, utf-8, windows-1250, windows-1251, windows-1252, windows-1253, windows-1254, windows-1255, windows-1256, windows-1257, windows-1258, windows-31j, x-big5-hkscs-2001, x-big5-solaris, x-euc-jp-linux, x-euc-tw, x-eucjp-open, x-ibm1006, x-ibm1025, x-ibm1046, x-ibm1097, x-ibm1098, x-ibm1112, x-ibm1122, x-ibm1123, x-ibm1124, x-ibm1364, x-ibm1381, x-ibm1383, x-ibm300, x-ibm33722, x-ibm737, x-ibm833, x-ibm834, x-ibm856, x-ibm874, x-ibm875, x-ibm921, x-ibm922, x-ibm930, x-ibm933, x-ibm935, x-ibm937, x-ibm939, x-ibm942, x-ibm942c, x-ibm943, x-ibm943c, x-ibm948, x-ibm949, x-ibm949c, x-ibm950, x-ibm964, x-ibm970, x-iscii91, x-iso-2022-cn-cns, x-iso-2022-cn-gb, x-iso-8859-11, x-jis0208, x-jisautodetect, x-johab, x-macarabic, x-maccentraleurope, x-maccroatian, x-maccyrillic, x-macdingbat, x-macgreek, x-machebrew, x-maciceland, x-macroman, x-macromania, x-macsymbol, x-macthai, x-macturkish, x-macukraine, x-ms932_0213, x-ms950-hkscs, x-ms950-hkscs-xp, x-mswin-936, x-pck, x-sjis_0213, x-utf-16le-bom, x-utf-32be-bom, x-utf-32le-bom, x-windows-50220, x-windows-50221, x-windows-874, x-windows-949, x-windows-950, x-windows-iso2022jp">>},{'Content-Type',<<"application/xml">>},{'Accept',<<"text/plain, */*">>}]
2016-06-23 11:55:16.841 [debug] <0.1069.0>@mod_rest:process:53 Error when processing REST request:
Data: <<"<message from='user1@ip' to='user2@ip'><body>Hello</body></message>">>
Error: {badmatch,false}

消息的格式有什么问题或缺少什么吗? 非常感谢大家的帮助

编辑 1

sessionID : Problem 'error function_clause' occurred executing the command.
Stacktrace: [{ejabberd_commands,'-check_access_commands/5-fun-0-',
                 [{rest,[{registered_users,[all]},{connected_users,[all]}]}],
                 [{file,"src/ejabberd_commands.erl"},{line,372}]},
             {lists,'-filter/2-lc$^0/1-0-',2,[{file,"lists.erl"},{line,1284}]},
             {ejabberd_commands,check_access_commands,5,
                 [{file,"src/ejabberd_commands.erl"},{line,371}]},
             {ejabberd_commands,execute_command,4,
                 [{file,"src/ejabberd_commands.erl"},{line,308}]},
             {ejabberd_ctl,call_command,3,
                 [{file,"src/ejabberd_ctl.erl"},{line,293}]},
             {ejabberd_ctl,try_call_command,3,
                 [{file,"src/ejabberd_ctl.erl"},{line,269}]},
             {ejabberd_ctl,process2,3,
                 [{file,"src/ejabberd_ctl.erl"},{line,218}]},
             {mod_rest,maybe_post_request,3,
                 [{file,"src/mod_rest.erl"},{line,140}]}]

【问题讨论】:

  • 为什么不使用 send_message 方法发送它而不是发送 xml 节
  • 只需检查是否可以使用 ejabberdctl 命令中的 send_message。如果是,那么您可以使用它来满足您的要求。

标签: erlang ejabberd


【解决方案1】:

我更喜欢使用 send_message 方法来发送消息而不是发送 xml 节。请在 mod_admin_extra 或 mod_admin 模块中查找 send_message 命令,并使用它从后台发送消息。

public String sendmessage() {
        String response = null;
        String request = "send_message chat " + actor_id + "@" + chatServer + "/backend " + target_id + "@" + chatServer + " " + " Message " + "\""
                    + "Hello" + "\"";
        response = restTemplate.postForObject("http://ip:port/rest", request, String.class);

        return response;
    }

【讨论】:

  • 嗨 kumar 现在我可以发送 xml 节来满足我的目的,但是命令 send_message_chat 命令不起作用。请检查编辑后的帖子是否有错误消息
  • 我使用的是旧版本的 mod_admin_extra,它有两个命令 send_message_chat 和 send_message_headline
  • 你能分享一下mod_admin_extra文件的链接吗?只看代码后,我可以在这里为您提供帮助。
猜你喜欢
  • 1970-01-01
  • 2016-09-21
  • 2015-07-01
  • 2016-10-21
  • 1970-01-01
  • 2011-10-17
  • 2017-05-04
  • 2012-03-27
  • 1970-01-01
相关资源
最近更新 更多