HttpConnection.java 5.98 KB
Newer Older
Alex Wenckus's avatar
Alex Wenckus committed
1
/**
Matt Tucker's avatar
Matt Tucker committed
2 3 4
 * $RCSfile$
 * $Revision: $
 * $Date: $
Alex Wenckus's avatar
Alex Wenckus committed
5
 *
6
 * Copyright (C) 2005-2008 Jive Software. All rights reserved.
Matt Tucker's avatar
Matt Tucker committed
7 8
 *
 * This software is published under the terms of the GNU Public License (GPL),
9 10
 * a copy of which is included in this distribution, or a commercial license
 * agreement with Jive.
Alex Wenckus's avatar
Alex Wenckus committed
11
 */
Matt Tucker's avatar
Matt Tucker committed
12

13
package org.jivesoftware.openfire.http;
Alex Wenckus's avatar
Alex Wenckus committed
14 15 16 17

import org.mortbay.util.ajax.Continuation;

/**
18 19 20
 * Represents one HTTP connection with a client using the HTTP Binding service. The client will wait
 * on {@link #getResponse()} until the server forwards a message to it or the wait time on the
 * session timesout.
Alex Wenckus's avatar
Alex Wenckus committed
21 22 23 24 25 26 27 28 29 30
 *
 * @author Alexander Wenckus
 */
public class HttpConnection {
    private long requestId;
    private String body;
    private HttpSession session;
    private Continuation continuation;
    private boolean isClosed;
    private boolean isSecure = false;
31
    private boolean isDelivered;
Alex Wenckus's avatar
Alex Wenckus committed
32

33 34
    private static final String CONNECTION_CLOSED = "connection closed";

35 36 37 38 39 40
    /**
     * Constructs an HTTP Connection.
     *
     * @param requestId the ID which uniquely identifies this request.
     * @param isSecure true if this connection is using HTTPS
     */
Alex Wenckus's avatar
Alex Wenckus committed
41 42 43
    public HttpConnection(long requestId, boolean isSecure) {
        this.requestId = requestId;
        this.isSecure = isSecure;
44
        this.isDelivered = false;
Alex Wenckus's avatar
Alex Wenckus committed
45 46 47 48 49 50 51 52 53 54 55
    }

    /**
     * The connection should be closed without delivering a stanza to the requestor.
     */
    public void close() {
        if (isClosed) {
            return;
        }

        try {
56
            deliverBody(CONNECTION_CLOSED);
Alex Wenckus's avatar
Alex Wenckus committed
57 58 59 60 61 62
        }
        catch (HttpConnectionClosedException e) {
            /* Shouldn't happen */
        }
    }

63 64 65 66 67 68
    /**
     * Returns true if this connection has been closed, either a response was delivered to the
     * client or the server closed the connection aburbtly.
     *
     * @return true if this connection has been closed.
     */
Alex Wenckus's avatar
Alex Wenckus committed
69 70 71 72
    public boolean isClosed() {
        return isClosed;
    }

73 74 75 76 77
    /**
     * Returns true if this connection is using HTTPS.
     *
     * @return true if this connection is using HTTPS.
     */
Alex Wenckus's avatar
Alex Wenckus committed
78 79 80 81
    public boolean isSecure() {
        return isSecure;
    }

82 83 84 85
    public boolean isDelivered() {
        return isDelivered;
    }

Alex Wenckus's avatar
Alex Wenckus committed
86 87 88 89 90 91 92 93 94 95 96
    /**
     * Delivers content to the client. The content should be valid XMPP wrapped inside of a body.
     * A <i>null</i> value for body indicates that the connection should be closed and the client
     * sent an empty body.
     *
     * @param body the XMPP content to be forwarded to the client inside of a body tag.
     *
     * @throws HttpConnectionClosedException when this connection to the client has already recieved
     * a deliverable to forward to the client
     */
    public void deliverBody(String body) throws HttpConnectionClosedException {
97 98 99
        if(body == null) {
            throw new IllegalArgumentException("Body cannot be null!");
        }
Alex Wenckus's avatar
Alex Wenckus committed
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
        // We only want to use this function once so we will close it when the body is delivered.
        if (isClosed) {
            throw new HttpConnectionClosedException("The http connection is no longer " +
                    "available to deliver content");
        }
        else {
            isClosed = true;
        }

        if (continuation != null) {
            continuation.setObject(body);
            continuation.resume();
        }
        else {
            this.body = body;
        }
    }

    /**
     * A call that will cause a wait, or in the case of Jetty the thread to be freed, if there is no
120
     * deliverable currently available. Once the response becomes available, it is returned.
Alex Wenckus's avatar
Alex Wenckus committed
121 122 123 124 125
     *
     * @return the deliverable to send to the client
     * @throws HttpBindTimeoutException to indicate that the maximum wait time requested by the
     * client has been surpassed and an empty response should be returned.
     */
126
    public String getResponse() throws HttpBindTimeoutException {
Alex Wenckus's avatar
Alex Wenckus committed
127
        if (body == null && continuation != null) {
128
            try {
129
                body = waitForResponse();
130 131 132 133 134
            }
            catch (HttpBindTimeoutException e) {
                this.isClosed = true;
                throw e;
            }
Alex Wenckus's avatar
Alex Wenckus committed
135
        }
136
        else if (body == null) {
Alex Wenckus's avatar
Alex Wenckus committed
137 138 139 140 141
            throw new IllegalStateException("Continuation not set, cannot wait for deliverable.");
        }
        return body;
    }

142 143 144 145 146
    /**
     * Returns the ID which uniquely identifies this connection.
     *
     * @return the ID which uniquely identifies this connection.
     */
Alex Wenckus's avatar
Alex Wenckus committed
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
    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;
    }

    void setContinuation(Continuation continuation) {
        this.continuation = continuation;
    }
172 173 174 175 176

    private String waitForResponse() throws HttpBindTimeoutException {
        if (continuation.suspend(session.getWait() * 1000)) {
            String deliverable = (String) continuation.getObject();
            // This will occur when the hold attribute of a session has been exceded.
177
            this.isDelivered = true;
178 179 180
            if (deliverable == null) {
                throw new HttpBindTimeoutException();
            }
181 182 183
            else if(CONNECTION_CLOSED.equals(deliverable)) {
                return null;
            }
184 185
            return deliverable;
        }
186
        this.isDelivered = true;
187
        throw new HttpBindTimeoutException("Request " + requestId + " exceeded response time from " +
188 189
                "server of " + session.getWait() + " seconds.");
    }
Alex Wenckus's avatar
Alex Wenckus committed
190
}