tigase 启用Http Rest API 问题整理


1.由于Stream.groovy的description属性,可能是版本问题不兼容,将脚本Stream.groovy脚本中的description注释掉

2.通过http发送消息时不希望对用户进行认证
修改Stream.groovy脚本的requireRole设置未null
3.closure 修改execPost ,去掉user 参数

配置:在 init.properties文件中 --comp-name-1=http
--comp-class-1=tigase.http.HttpMessageReceiver
http/ports[i]=8080
http/api-keys[s]=open_access
#http/8080/domain=xmpp.iyw
http/api-key=open_access
http/server-class[s]=tigase.http.jetty.JettyStandaloneHttpServer


//Stream.groovy用于发送消息的脚本


class Stream extends tigase.http.rest.Handler {


public Stream() {
/**
*description = [
*regex : "/{to_jid}",
*POST : [ info:'Send XMPP stanza', description: """Sends passed HTTP content as XMPP stanza."""]]
*/
regex = /\/(.*)/
isAsync = true
decodeContent = false
//requireRole 可是设置未null 、user 、admin ,如果这是了 非 null,在调用http发送消息时,需要对用户进行认证,我这里不需要认证直接发送,所以设置未null
requiredRole = null
//requireRole设置未null后 需要去掉user参数 ,注释掉
execPost = { Service service, callback, /*user,*/ HttpServletRequest request, to ->
char[] data = https://www.it610.com/article/request.getReader().getText()?.toCharArray();
if (data =https://www.it610.com/article/= null || data.length == 0) {
callback({ req, HttpServletResponse resp ->
resp.sendError(HttpServletResponse.SC_BAD_REQUEST);
})
return;
}


SimpleParser parser= SingletonFactory.getParserInstance();
DomBuilderHandler domHandler = new DomBuilderHandler();
parser.parse(domHandler, data, 0, data.length);
Element packetEl = domHandler.getParsedElements().poll();
if (packetEl == null) {
callback({ req, HttpServletResponse resp ->
resp.sendError(HttpServletResponse.SC_BAD_REQUEST);
})
return;
}


if (to != null) {

to = to.trim();
if (!to.isEmpty()) {
【tigase 启用Http Rest API 问题整理】packetEl.setAttribute("to", to);
}
}


Packet packet = Packet.packetInstance(packetEl);
def responseHandler = (packet.getElemName() == "iq" && packet.getAttribute("from") == null) ? { Packet result ->
callback({ req, HttpServletResponse resp ->
def outBytes = result.getElement().toString().getBytes();
resp.setContentType("application/xml");
resp.setContentLength(outBytes.length);
resp.getOutputStream().write(outBytes);
});
} : null;


service.sendPacket(packet, null, responseHandler);


if (responseHandler == null) {
callback("");
}
}
}
}




    推荐阅读