diff --git a/src/java/org/jivesoftware/multiplexer/ConnectionManager.java b/src/java/org/jivesoftware/multiplexer/ConnectionManager.java index 28f5c4c..ac3d9a7 100644 --- a/src/java/org/jivesoftware/multiplexer/ConnectionManager.java +++ b/src/java/org/jivesoftware/multiplexer/ConnectionManager.java @@ -332,7 +332,7 @@ int plainPort = JiveGlobals.getIntProperty("xmpp.httpbind.port.plain", 8080); int sslPort = JiveGlobals.getIntProperty("xmpp.httpbind.port.secure", 8443); - httpBindManager = new HttpBindManager(plainPort, sslPort); + httpBindManager = new HttpBindManager(serverName, plainPort, sslPort); try { httpBindManager.startup(); diff --git a/src/java/org/jivesoftware/multiplexer/net/http/HttpBindManager.java b/src/java/org/jivesoftware/multiplexer/net/http/HttpBindManager.java index 7406fe2..7515394 100644 --- a/src/java/org/jivesoftware/multiplexer/net/http/HttpBindManager.java +++ b/src/java/org/jivesoftware/multiplexer/net/http/HttpBindManager.java @@ -25,11 +25,13 @@ private int plainPort; private int sslPort; private Server server; + private String serverName; - public HttpBindManager(int plainPort, int sslPort) { + public HttpBindManager(String serverName, int plainPort, int sslPort) { this.plainPort = plainPort; this.sslPort = sslPort; this.server = new Server(); + this.serverName = serverName; } public void startup() throws Exception { @@ -38,7 +40,7 @@ server.setConnectors(new Connector[]{connector}); ServletHolder servletHolder = new ServletHolder( - new HttpBindServlet(new HttpSessionManager())); + new HttpBindServlet(new HttpSessionManager(serverName))); ServletHandler servletHandler = new ServletHandler(); servletHandler.addServletWithMapping(servletHolder, "/"); server.addHandler(servletHandler); diff --git a/src/java/org/jivesoftware/multiplexer/net/http/HttpBindServlet.java b/src/java/org/jivesoftware/multiplexer/net/http/HttpBindServlet.java index d720e3a..758e3b1 100644 --- a/src/java/org/jivesoftware/multiplexer/net/http/HttpBindServlet.java +++ b/src/java/org/jivesoftware/multiplexer/net/http/HttpBindServlet.java @@ -77,8 +77,40 @@ } private void createNewSession(HttpServletRequest request, HttpServletResponse response, - Element rootNode) + Element rootNode) throws IOException { + long rid = getLongAttribue(rootNode.attributeValue("rid"), -1); + if(rid <= 0) { + response.sendError(HttpServletResponse.SC_BAD_REQUEST, "Body missing RID (Request ID)"); + return; + } + + HttpConnection connection = new HttpConnection(rid); + connection.setSession(sessionManager.createSession(rootNode, connection)); + respond(response, connection); + } + + private void respond(HttpServletResponse response, HttpConnection connection) + throws IOException { + response.setStatus(HttpServletResponse.SC_OK); + response.setContentType("text/xml"); + response.setCharacterEncoding("utf-8"); + + byte [] content = connection.getDeliverable().getBytes(); + response.setContentLength(content.length); + response.getOutputStream().write(content); + } + + private long getLongAttribue(String value, long defaultValue) { + if(value == null || "".equals(value)) { + return defaultValue; + } + try { + return Long.valueOf(value); + } + catch (Exception ex) { + return defaultValue; + } } private Document createDocument(HttpServletRequest request) throws diff --git a/src/java/org/jivesoftware/multiplexer/net/http/HttpConnection.java b/src/java/org/jivesoftware/multiplexer/net/http/HttpConnection.java index 50a9fc3..c291051 100644 --- a/src/java/org/jivesoftware/multiplexer/net/http/HttpConnection.java +++ b/src/java/org/jivesoftware/multiplexer/net/http/HttpConnection.java @@ -9,7 +9,7 @@ package org.jivesoftware.multiplexer.net.http; import org.jivesoftware.multiplexer.Connection; -import org.dom4j.Element; + /** * @@ -17,6 +17,8 @@ public class HttpConnection { private Connection.CompressionPolicy compressionPolicy; private long requestId; + private String body; + private HttpSession session; public HttpConnection(long requestID) { this.requestId = requestID; @@ -40,7 +42,12 @@ return false; } - public void deliver(Element doc) { + public void deliverBody(String body) { + this.body = body; + } + + public String getDeliverable() { + return body; } public boolean isCompressed() { @@ -58,4 +65,22 @@ public long getRequestId() { return requestId; } + + /** + * Set the session that this connection belongs to. + * + * @param session the session that this connection belongs to. + */ + void setSession(HttpSession session) { + this.session = session; + } + + /** + * Returns the session that this connection belongs to. + * + * @return the session that this connection belongs to. + */ + public HttpSession getSession() { + return session; + } } diff --git a/src/java/org/jivesoftware/multiplexer/net/http/HttpSession.java b/src/java/org/jivesoftware/multiplexer/net/http/HttpSession.java index 52fb365..a0413d3 100644 --- a/src/java/org/jivesoftware/multiplexer/net/http/HttpSession.java +++ b/src/java/org/jivesoftware/multiplexer/net/http/HttpSession.java @@ -45,7 +45,6 @@ public void deliver(Element stanza) { } - /** * This attribute specifies the longest time (in seconds) that the connection manager is allowed * to wait before responding to any request during the session. This enables the client to diff --git a/src/java/org/jivesoftware/multiplexer/net/http/HttpSessionManager.java b/src/java/org/jivesoftware/multiplexer/net/http/HttpSessionManager.java index 6f5abda..7ad69c6 100644 --- a/src/java/org/jivesoftware/multiplexer/net/http/HttpSessionManager.java +++ b/src/java/org/jivesoftware/multiplexer/net/http/HttpSessionManager.java @@ -13,13 +13,13 @@ import org.jivesoftware.multiplexer.ConnectionManager; import org.jivesoftware.multiplexer.Session; import org.dom4j.Element; -import org.dom4j.DocumentHelper; /** * */ public class HttpSessionManager { - /** + + /** * Milliseconds a connection has to be idle to be closed. Default is 30 minutes. Sending * stanzas to the client is not considered as activity. We are only considering the connection * active when the client sends some data or hearbeats (i.e. whitespaces) to the server. @@ -48,13 +48,19 @@ */ private static int pollingInterval; + private String serverName; + static { // Set the default read idle timeout. If none was set then assume 30 minutes - inactivityTimeout = JiveGlobals.getIntProperty("xmpp.httpbind.client.idle", 30 * 1000); + inactivityTimeout = JiveGlobals.getIntProperty("xmpp.httpbind.client.idle", 30); maxRequests = JiveGlobals.getIntProperty("xmpp.httpbind.client.requests.max", 2); pollingInterval = JiveGlobals.getIntProperty("xmpp.httpbind.client.requests.polling", 5); } + public HttpSessionManager(String serverName) { + this.serverName = serverName; + } + public HttpSession getSession(String streamID) { Session session = Session.getSession(streamID); if(session instanceof HttpSession) { @@ -63,8 +69,7 @@ return null; } - public HttpSession createSession(String serverName, Element rootNode, HttpConnection connection) - { + public HttpSession createSession(Element rootNode, HttpConnection connection) { // TODO Check if IP address is allowed to connect to the server // Default language is English ("en"). @@ -76,8 +81,6 @@ int wait = getIntAttribute(rootNode.attributeValue("wait"), 60); int hold = getIntAttribute(rootNode.attributeValue("hold"), 1); - long rid = getLongAttribue(rootNode.attributeValue("rid"), -1); - ServerSurrogate serverSurrogate = ConnectionManager.getInstance().getServerSurrogate(); // Indicate the compression policy to use for this connection connection.setCompressionPolicy(serverSurrogate.getCompressionPolicy()); @@ -87,25 +90,11 @@ session.setHold(hold); // Store language and version information in the connection. session.setLanaguage(language); - session.addConnection(connection); - - session.deliver(createSessionCreationResponse(session, serverSurrogate)); + connection.deliverBody(createSessionCreationResponse(session, serverSurrogate)); return session; } - private long getLongAttribue(String value, long defaultValue) { - if(value == null || "".equals(value)) { - return defaultValue; - } - try { - return Long.valueOf(value); - } - catch (Exception ex) { - return defaultValue; - } - } - private HttpSession createSession(String serverName) { ServerSurrogate serverSurrogate = ConnectionManager.getInstance().getServerSurrogate(); // Create a ClientSession for this user. @@ -130,21 +119,25 @@ } } - private static Element createSessionCreationResponse(HttpSession session, + private static String createSessionCreationResponse(HttpSession session, ServerSurrogate serverSurrogate) { - Element element = DocumentHelper.createElement("body"); - element.addAttribute("xmlns", "http://jabber.org/protocol/httpbind"); - element.addAttribute("authID", session.getStreamID()); - element.addAttribute("sid", session.getStreamID()); - element.addAttribute("secure", "true"); - element.addAttribute("requests", String.valueOf(maxRequests)); - element.addAttribute("inactivity", String.valueOf(inactivityTimeout)); - element.addAttribute("polling", String.valueOf(pollingInterval)); - element.addAttribute("wait", String.valueOf(session.getWait())); + StringBuilder builder = new StringBuilder(); + builder.append(""); + builder.append(""); + builder.append(serverSurrogate.getSASLMechanismsElement(session).asXML()); + builder.append(""); + builder.append(""); - Element features = element.addElement("stream:features"); - features.appendContent(serverSurrogate.getSASLMechanismsElement(session)); - return element; + return builder.toString(); } }