📄 httpsession.java
字号:
}
return count;
}
private void deliver(HttpConnection connection, Collection<Deliverable> deliverable)
throws HttpConnectionClosedException {
connection.deliverBody(createDeliverable(deliverable));
Delivered delivered = new Delivered(deliverable);
delivered.setRequestID(connection.getRequestId());
while (sentElements.size() > hold) {
sentElements.remove(0);
}
sentElements.add(delivered);
}
private void fireConnectionOpened(HttpConnection connection) {
lastActivity = System.currentTimeMillis();
for (SessionListener listener : listeners) {
listener.connectionOpened(this, connection);
}
}
private void checkPollingInterval() throws HttpBindException {
long time = System.currentTimeMillis();
if (((time - lastPoll) / 1000) < maxPollingInterval) {
throw new HttpBindException("Too frequent polling minimum interval is "
+ maxPollingInterval + ", current interval " + ((time - lastPoll) / 1000),
BoshBindingError.policyViolation);
}
lastPoll = time;
}
private synchronized void deliver(String text) {
if (text == null) {
// Do nothing if someone asked to send nothing :)
return;
}
deliver(new Deliverable(text));
}
private synchronized void deliver(Packet stanza) {
deliver(new Deliverable(Arrays.asList(stanza)));
}
private void deliver(Deliverable stanza) {
Collection<Deliverable> deliverable = Arrays.asList(stanza);
boolean delivered = false;
for (HttpConnection connection : connectionQueue) {
try {
if (connection.getRequestId() == lastRequestID + 1) {
lastRequestID = connection.getRequestId();
deliver(connection, deliverable);
delivered = true;
break;
}
}
catch (HttpConnectionClosedException e) {
/* Connection was closed, try the next one */
}
}
if (!delivered) {
pendingElements.add(stanza);
}
}
private void fireConnectionClosed(HttpConnection connection) {
lastActivity = System.currentTimeMillis();
for (SessionListener listener : listeners) {
listener.connectionClosed(this, connection);
}
}
private String createDeliverable(Collection<Deliverable> elements) {
StringBuilder builder = new StringBuilder();
builder.append("<body xmlns='" + "http://jabber.org/protocol/httpbind" + "'>");
for (Deliverable child : elements) {
builder.append(child.getDeliverable());
}
builder.append("</body>");
return builder.toString();
}
private synchronized void closeConnection() {
if (isClosed) {
return;
}
isClosed = true;
if (pendingElements.size() > 0) {
failDelivery();
}
for (SessionListener listener : listeners) {
listener.sessionClosed(this);
}
this.listeners.clear();
}
private void failDelivery() {
for (Deliverable deliverable : pendingElements) {
Collection<Packet> packet = deliverable.getPackets();
if (packet != null) {
failDelivery(packet);
}
}
for (HttpConnection toClose : connectionQueue) {
if (!toClose.isDelivered()) {
Delivered delivered = retrieveDeliverable(toClose.getRequestId());
if (delivered != null) {
failDelivery(delivered.getPackets());
}
else {
Log.warn("Packets could not be found for session " + getStreamID() + " cannot" +
"be delivered to client");
}
}
toClose.close();
fireConnectionClosed(toClose);
}
pendingElements.clear();
}
private void failDelivery(Collection<Packet> packets) {
if (packets == null) {
// Do nothing if someone asked to deliver nothing :)
return;
}
for (Packet packet : packets) {
try {
backupDeliverer.deliver(packet);
}
catch (UnauthorizedException e) {
Log.error("Unable to deliver message to backup deliverer", e);
}
}
}
private static String createEmptyBody() {
Element body = DocumentHelper.createElement("body");
body.addNamespace("", "http://jabber.org/protocol/httpbind");
return body.asXML();
}
/**
* A virtual server connection relates to a http session which its self can relate to many http
* connections.
*/
public static class HttpVirtualConnection extends VirtualConnection {
private InetAddress address;
public HttpVirtualConnection(InetAddress address) {
this.address = address;
}
public void closeVirtualConnection() {
((HttpSession) session).closeConnection();
}
public byte[] getAddress() throws UnknownHostException {
return address.getAddress();
}
public String getHostAddress() throws UnknownHostException {
return address.getHostAddress();
}
public String getHostName() throws UnknownHostException {
return address.getHostName();
}
public void systemShutdown() {
((HttpSession) session).closeConnection();
}
public void deliver(Packet packet) throws UnauthorizedException {
((HttpSession) session).deliver(packet);
}
public void deliverRawText(String text) {
((HttpSession) session).deliver(text);
}
}
private class Deliverable implements Comparable<Deliverable> {
private final String text;
private final Collection<String> packets;
private long requestID;
public Deliverable(String text) {
this.text = text;
this.packets = null;
}
public Deliverable(Collection<Packet> elements) {
this.text = null;
this.packets = new ArrayList<String>();
for (Packet packet : elements) {
this.packets.add(packet.toXML());
}
}
public String getDeliverable() {
if (text == null) {
StringBuilder builder = new StringBuilder();
for (String packet : packets) {
builder.append(packet);
}
return builder.toString();
}
else {
return text;
}
}
public void setRequestID(long requestID) {
this.requestID = requestID;
}
public long getRequestID() {
return requestID;
}
public Collection<Packet> getPackets() {
List<Packet> answer = new ArrayList<Packet>();
for (String packetXML : packets) {
try {
Packet packet = null;
// Parse the XML stanza
Element element = localParser.get().read(new StringReader(packetXML)).getRootElement();
String tag = element.getName();
if ("message".equals(tag)) {
packet = new Message(element, true);
}
else if ("presence".equals(tag)) {
packet = new Presence(element, true);
}
else if ("iq".equals(tag)) {
packet = new IQ(element, true);
}
// Add the reconstructed packet to the result
answer.add(packet);
}
catch (Exception e) {
Log.error("Error while parsing Privacy Property", e);
}
}
return answer;
}
public int compareTo(Deliverable o) {
return (int) (o.getRequestID() - requestID);
}
}
private class Delivered {
private long requestID;
private Collection<Deliverable> deliverables;
public Delivered(Collection<Deliverable> deliverables) {
this.deliverables = deliverables;
}
public void setRequestID(long requestID) {
this.requestID = requestID;
}
public long getRequestID() {
return requestID;
}
public Collection<Packet> getPackets() {
List<Packet> packets = new ArrayList<Packet>();
for (Deliverable deliverable : deliverables) {
if (deliverable.packets != null) {
packets.addAll(deliverable.getPackets());
}
}
return packets;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -