diff --git a/src/java/org/jivesoftware/multiplexer/net/http/HttpBindServlet.java b/src/java/org/jivesoftware/multiplexer/net/http/HttpBindServlet.java index 20b8257..a21e8dc 100644 --- a/src/java/org/jivesoftware/multiplexer/net/http/HttpBindServlet.java +++ b/src/java/org/jivesoftware/multiplexer/net/http/HttpBindServlet.java @@ -18,6 +18,7 @@ import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.Element; +import org.dom4j.DocumentHelper; import org.mortbay.util.ajax.ContinuationSupport; import javax.servlet.http.HttpServlet; @@ -146,9 +147,14 @@ return; } - HttpConnection connection = new HttpConnection(rid, request.isSecure()); - connection.setSession(sessionManager.createSession(rootNode, connection)); - respond(response, connection); + try { + HttpConnection connection = new HttpConnection(rid, request.isSecure()); + connection.setSession(sessionManager.createSession(rootNode, connection)); + respond(response, connection); + } + catch (HttpBindException e) { + response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); + } } private void respond(HttpServletResponse response, HttpConnection connection) @@ -171,7 +177,9 @@ } private String createEmptyBody() { - return ""; + Element body = DocumentHelper.createElement("body"); + body.addNamespace("", "http://jabber.org/protocol/httpbind"); + return body.asXML(); } private long getLongAttribue(String value, long defaultValue) { diff --git a/src/java/org/jivesoftware/multiplexer/net/http/HttpSession.java b/src/java/org/jivesoftware/multiplexer/net/http/HttpSession.java index 06e1017..ccd26fb 100644 --- a/src/java/org/jivesoftware/multiplexer/net/http/HttpSession.java +++ b/src/java/org/jivesoftware/multiplexer/net/http/HttpSession.java @@ -167,7 +167,7 @@ private String createDeliverable(Collection elements) { Element body = DocumentHelper.createElement("body"); - body.addAttribute("xmlns", "http://jabber.org/protocol/httpbind"); + body.addNamespace("", "http://jabber.org/protocol/httpbind"); for(Element child : elements) { child = child.createCopy(); child.setParent(null); diff --git a/src/java/org/jivesoftware/multiplexer/net/http/HttpSessionManager.java b/src/java/org/jivesoftware/multiplexer/net/http/HttpSessionManager.java index 7f4a33c..2a9e795 100644 --- a/src/java/org/jivesoftware/multiplexer/net/http/HttpSessionManager.java +++ b/src/java/org/jivesoftware/multiplexer/net/http/HttpSessionManager.java @@ -11,10 +11,11 @@ package org.jivesoftware.multiplexer.net.http; import org.jivesoftware.util.JiveGlobals; +import org.jivesoftware.util.Log; import org.jivesoftware.multiplexer.ServerSurrogate; import org.jivesoftware.multiplexer.ConnectionManager; import org.jivesoftware.multiplexer.Session; -import org.dom4j.Element; +import org.dom4j.*; import java.util.*; @@ -76,7 +77,9 @@ return null; } - public HttpSession createSession(Element rootNode, HttpConnection connection) { + public HttpSession createSession(Element rootNode, HttpConnection connection) + throws HttpBindException + { // TODO Check if IP address is allowed to connect to the server // Default language is English ("en"). @@ -105,6 +108,10 @@ catch (HttpConnectionClosedException e) { /* This won't happen here. */ } + catch (DocumentException e) { + Log.error("Error creating document", e); + throw new HttpBindException("Internal server error", true, 500); + } timer.reset(session); return session; @@ -157,27 +164,31 @@ } } - private String createSessionCreationResponse(HttpSession session) { - StringBuilder builder = new StringBuilder(); - builder.append(""); - builder.append(""); - builder.append(serverSurrogate.getSASLMechanismsElement(session).asXML()); - builder.append(""); - builder.append(""); - builder.append(""); - builder.append(""); + private String createSessionCreationResponse(HttpSession session) throws DocumentException { + Element response = DocumentHelper.createElement("body"); + response.addNamespace("", "http://jabber.org/protocol/httpbind"); + response.addNamespace("stream", "http://etherx.jabber.org/streams"); + response.addAttribute("authid", session.getStreamID()); + response.addAttribute("sid", session.getStreamID()); + response.addAttribute("secure", Boolean.TRUE.toString()); + response.addAttribute("requests", String.valueOf(maxRequests)); + response.addAttribute("inactivity", String.valueOf(session.getInactivityTimeout())); + response.addAttribute("polling", String.valueOf(pollingInterval)); + response.addAttribute("wait", String.valueOf(session.getWait())); - return builder.toString(); + Element features = response.addElement("stream:features"); + + features.add(serverSurrogate.getSASLMechanismsElement(session).createCopy()); + + Element bind = DocumentHelper.createElement(new QName("bind", + new Namespace("", "urn:ietf:params:xml:ns:xmpp-bind"))); + features.add(bind); + + Element sessionElement = DocumentHelper.createElement(new QName("session", + new Namespace("", "urn:ietf:params:xml:ns:xmpp-session"))); + features.add(sessionElement); + + return response.asXML(); } public HttpConnection forwardRequest(long rid, HttpSession session, boolean isSecure,