xmppclient.java
来自「RESIN 3.2 最新源码」· Java 代码 · 共 723 行 · 第 1/2 页
JAVA
723 行
&& "error".equals(_in.getLocalName()))) { if (tag == XMLStreamReader.START_ELEMENT) { System.out.println("<" + _in.getLocalName() + ">"); } else if (tag == XMLStreamReader.END_ELEMENT) { System.out.println("</" + _in.getLocalName() + ">"); } } return new IOException(L.l("<error> is unexpected", _in.getLocalName())); } else return new IOException(L.l("<{0}> is unexpected", _in.getLocalName())); } public boolean isClosed() { return _s == null; } public void close() { if (log.isLoggable(Level.FINE)) log.fine(this + " close"); try { Socket s; ReadStream is; WriteStream os; synchronized (this) { s = _s; _s = null; is = _is; _is = null; os = _os; _os = null; } if (os != null) { try { os.close(); } catch (IOException e) {} } if (is != null) { is.close(); } if (s != null) { s.close(); } } catch (Exception e) { log.log(Level.WARNING, e.toString(), e); } } private void debug(XMLStreamReader in) throws IOException, XMLStreamException { if (XMLStreamReader.START_ELEMENT == in.getEventType()) { StringBuilder sb = new StringBuilder(); sb.append("<"); if (in.getPrefix() != null && ! "".equals(in.getPrefix())) sb.append(in.getPrefix()).append(":"); sb.append(in.getLocalName()); if (in.getNamespaceURI() != null) sb.append("{").append(in.getNamespaceURI()).append("}"); for (int i = 0; i < in.getAttributeCount(); i++) { sb.append(" "); sb.append(in.getAttributeLocalName(i)); sb.append("='"); sb.append(in.getAttributeValue(i)); sb.append("'"); } sb.append(">"); log.finest(this + " " + sb); } else if (XMLStreamReader.END_ELEMENT == in.getEventType()) { log.finest(this + " </" + in.getLocalName() + ">"); } else if (XMLStreamReader.CHARACTERS == in.getEventType()) log.finest(this + " text='" + in.getText() + "'"); else log.finest(this + " tag=" + in.getEventType()); } private boolean readStream() throws IOException, XMLStreamException { int tag; XMLStreamReader in = _in; if (in == null) return false; while ((tag = in.next()) > 0) { if (_isFinest) debug(in); if (tag == XMLStreamReader.START_ELEMENT) { String localName = in.getLocalName(); if ("stream".equals(localName)) { readStreamFeatures(); return true; } else { log.fine(XmppClient.this + " expected stream at tag <" + _in.getLocalName() + ">"); close(); return false; } } else if (tag == XMLStreamReader.END_ELEMENT) { log.fine(XmppClient.this + " unexpected end </" + _in.getLocalName() + ">"); close(); return false; } } if (tag < 0) { close(); } return false; } private boolean readSuccess() throws IOException, XMLStreamException { int tag; XMLStreamReader in = _in; if (in == null) return false; while ((tag = in.next()) > 0) { if (_isFinest) debug(in); if (tag == XMLStreamReader.START_ELEMENT) { String localName = in.getLocalName(); if ("success".equals(localName)) { _reader.skipToEnd("success"); return true; } else { log.fine(XmppClient.this + " expected success at tag <" + _in.getLocalName() + ">"); close(); return false; } } else if (tag == XMLStreamReader.END_ELEMENT) { log.fine(XmppClient.this + " unexpected end </" + _in.getLocalName() + ">"); close(); return false; } } if (tag < 0) { close(); } return false; } public BamStream getBrokerStream() { return _toBroker; } @Override public String toString() { return getClass().getSimpleName() + "[" + _address + "," + _port + "]"; } @Override protected void finalize() throws Throwable { super.finalize(); close(); } class BindCallback extends AbstractBamStream { @Override public void queryResult(long id, String to, String from, Serializable value) { if (value instanceof ImBindQuery) { ImBindQuery bind = (ImBindQuery) value; _jid = bind.getJid(); } } } class Listener implements Runnable { private boolean _isFinest; public void run() { _isFinest = log.isLoggable(Level.FINEST); try { while (! isClosed()) { readPacket(); } } catch (Exception e) { log.log(Level.WARNING, e.toString(), e); } finally { close(); } } private void readPacket() throws IOException, XMLStreamException { int tag; XMLStreamReader in = _in; if (in == null) return; while ((tag = in.next()) > 0) { if (_isFinest) debug(in); if (tag == XMLStreamReader.START_ELEMENT) { String localName = in.getLocalName(); if ("success".equals(localName)) { skipToEnd("success"); _stanzaQueue.add(new SuccessStanza(in)); } else if ("stream".equals(localName)) { readStreamFeatures(); _stanzaQueue.add(new StreamStanza(in)); } else if ("iq".equals(localName)) { _reader.handleIq(); } else if ("message".equals(localName)) { _reader.handleMessage(); } else if ("presence".equals(localName)) { _reader.handlePresence(); } else { log.fine(XmppClient.this + " unknown tag <" + _in.getLocalName() + ">"); close(); return; } } else if (tag == XMLStreamReader.END_ELEMENT) { log.fine(XmppClient.this + " unexpected end </" + _in.getLocalName() + ">"); close(); return; } } if (tag < 0) { close(); } } private Stanza readIq(XMLStreamReader in) throws IOException, XMLStreamException { String type = in.getAttributeValue(null, "type"); if ("error".equals(type)) { skipToEnd("iq"); return new IqErrorStanza(in); } else if ("result".equals(type)) { String id = in.getAttributeValue(null, "id"); int tag = in.nextTag(); if (_isFinest) debug(in); if (tag == XMLStreamReader.END_ELEMENT && "iq".equals(in.getLocalName())) { return new EmptyStanza(); } if (tag != XMLStreamReader.START_ELEMENT) throw new IllegalStateException("expected start"); String name = in.getLocalName(); if ("bind".equals(name)) { return readBind(in, id); } else if ("session".equals(name)) { skipToEnd("iq"); return new SessionStanza(); } else { skipToEnd("iq"); return new IqErrorStanza(); } } else { throw new UnsupportedOperationException(type); } } private Stanza readBind(XMLStreamReader in, String id) throws IOException, XMLStreamException { BindStanza bind = new BindStanza(); bind.setId(id); skipToEnd("bind"); skipToEnd("iq"); return bind; } private void skipToEnd(String tagName) throws IOException, XMLStreamException { XMLStreamReader in = _in; if (in == null) return; int tag; while ((tag = in.next()) > 0) { if (_isFinest) debug(in); if (tag == XMLStreamReader.START_ELEMENT) { } else if (tag == XMLStreamReader.END_ELEMENT) { if (tagName.equals(in.getLocalName())) return; } } } }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?