class BoshHandler implements ServerRuntimeContextService {

    public void process(HttpServletRequest httpRequest, Stanza body) {
        if (!body.getNamespaceURI().equalsIgnoreCase(NamespaceURIs.XEP0124_BOSH)) {
            LOGGER.error("Invalid namespace for body wrapper '{}', must be '{}'!", body.getNamespaceURI(),
                    NamespaceURIs.XEP0124_BOSH);
            return;
        }
        if (!body.getName().equalsIgnoreCase("body")) {
            LOGGER.error("Invalid body wrapper '{}'!", body.getName());
            return;
        }
        if (body.getAttribute("rid") == null) {
            LOGGER.error("Invalid request that does not have a request identifier (rid) attribute!");
            return;
        }
        final long rid;
        try {
            rid = Long.parseLong(body.getAttributeValue("rid"));
        } catch (NumberFormatException e) {
            LOGGER.error("not a valid RID: " + body.getAttributeValue("rid"));
            // TODO handle properly by returning an error response
            throw new RuntimeException(e);
        }
        if (rid <= 0L) {
            LOGGER.warn("rid is not positive: " + rid);
            // TODO handle properly by returning an error response
            throw new RuntimeException("BOSH rid must be a positive, large number, not " + rid);
        }
        if (rid > 9007199254740991L) {
            LOGGER.warn("rid too large: " + rid);
            // continue anyway, this is not a problem with this implementation
        }
        BoshRequest newBoshRequest = new BoshRequest(httpRequest, body, rid);
        LOGGER.debug("SID = " + body.getAttributeValue("sid") + " - new BoshRequest created for RID = " + rid);

        // session creation request (first request). does not have a "sid" attribute
        if (body.getAttribute("sid") == null) {
            try {
                createSession(newBoshRequest);
            } catch (IOException e) {
                LOGGER.error("Exception thrown while processing the session creation request: " + e.getMessage());
                try {
                    final AsyncContext context = newBoshRequest.getHttpServletRequest().startAsync();
                    final ServletResponse response = context.getResponse();
                    if (response instanceof HttpServletResponse) {
                        HttpServletResponse httpServletResponse = (HttpServletResponse)response;
                        try {
                            httpServletResponse.sendError(400, "bad-request");
                            return;
                        } catch (IOException ioe) {
                            ioe.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                        }
                    } else {
                        // create temporary session to be able to reuse the code
                        createSessionContext().sendError("bad-request");
                    }
                } catch (Exception exe) {
                    LOGGER.warn("exception in async processing", exe);
                }
            }
            return;
        } 

        // in-session request, now find the server-side session
        final String sid = body.getAttributeValue("sid");
        BoshBackedSessionContext session = null;
        if (sid != null) session = sessions.get(sid);
        if (session == null) {
            LOGGER.warn("Received an invalid sid = '{}', terminating connection", sid);
            try {
                final AsyncContext context = newBoshRequest.getHttpServletRequest().startAsync();
                // create temporary session to be able to reuse the code
                createSessionContext().sendError("invalid session id");
            } catch (Exception exe) {
                LOGGER.warn("exception in async processing", exe);
            }
            return;
        }
        
        // process request for the session
        synchronized (session) {
            session.insertRequest(newBoshRequest);
            processIncomingClientStanzas(session, newBoshRequest.getBody());
        }
    }

    public  BoshHandler();

}