CarrotChat v2.8.80.240429版本的 MD5 值为:fdf98761f01e715a89df24b85b0d206e

以下内容为反编译后的 XMPPBOSHConnection.java 源代码,内容仅作参考


package org.jivesoftware.smack.bosh;

import android.util.Log;
import com.kinggrid.iapppdf.droids.mupdf.codec.cosobject.PdfBoolean;
import java.io.IOException;
import java.io.PipedReader;
import java.io.PipedWriter;
import java.io.Reader;
import java.io.StringReader;
import java.io.Writer;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.igniterealtime.jbosh.AbstractBody;
import org.igniterealtime.jbosh.BOSHClient;
import org.igniterealtime.jbosh.BOSHClientConfig;
import org.igniterealtime.jbosh.BOSHClientConnEvent;
import org.igniterealtime.jbosh.BOSHClientConnListener;
import org.igniterealtime.jbosh.BOSHClientRequestListener;
import org.igniterealtime.jbosh.BOSHClientResponseListener;
import org.igniterealtime.jbosh.BOSHException;
import org.igniterealtime.jbosh.BOSHMessageEvent;
import org.igniterealtime.jbosh.BodyQName;
import org.igniterealtime.jbosh.ComposableBody;
import org.jivesoftware.smack.AbstractXMPPConnection;
import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Element;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.packet.Nonza;
import org.jivesoftware.smack.packet.Presence;
import org.jivesoftware.smack.packet.Stanza;
import org.jivesoftware.smack.packet.StreamError;
import org.jivesoftware.smack.sasl.packet.SaslStreamElements;
import org.jivesoftware.smack.util.PacketParserUtils;
import org.jxmpp.jid.DomainBareJid;
import org.jxmpp.jid.parts.Resourcepart;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;

public class XMPPBOSHConnection extends AbstractXMPPConnection {
    public static final String BOSH_URI = "http://jabber.org/protocol/httpbind";
    private static final Logger LOGGER = Logger.getLogger(XMPPBOSHConnection.class.getName());
    public static final String XMPP_BOSH_NS = "urn:xmpp:xbosh";
    private String TAG;
    private BOSHClient client;
    private final BOSHConfiguration config;
    private boolean done;
    private boolean isFirstInitialization;
    private boolean notified;
    private Thread readerConsumer;
    private PipedWriter readerPipe;
    protected String sessionID;

    private class BOSHConnectionListener implements BOSHClientConnListener {
        private BOSHConnectionListener() {
        }

        @Override
        public void connectionEvent(BOSHClientConnEvent bOSHClientConnEvent) {
            Exception exc;
            try {
                if (bOSHClientConnEvent.isConnected()) {
                    ((AbstractXMPPConnection) XMPPBOSHConnection.this).connected = true;
                    ((AbstractXMPPConnection) XMPPBOSHConnection.this).wasAuthenticated = false;
                    XMPPBOSHConnection.this.log("smack-bosh set connect is true in con event connected,is first:" + XMPPBOSHConnection.this.isFirstInitialization + ",was auth:" + ((AbstractXMPPConnection) XMPPBOSHConnection.this).wasAuthenticated);
                    if (XMPPBOSHConnection.this.isFirstInitialization) {
                        XMPPBOSHConnection.this.isFirstInitialization = false;
                    } else if (((AbstractXMPPConnection) XMPPBOSHConnection.this).wasAuthenticated) {
                        try {
                            XMPPBOSHConnection.this.login();
                        } catch (Exception e10) {
                            throw new RuntimeException(e10);
                        }
                    }
                } else {
                    if (bOSHClientConnEvent.isError()) {
                        Throwable cause = bOSHClientConnEvent.getCause();
                        if (cause instanceof Exception) {
                            exc = (Exception) cause;
                        } else {
                            exc = new Exception(cause);
                        }
                        XMPPBOSHConnection.this.notifyConnectionError(exc);
                    }
                    XMPPBOSHConnection.LOGGER.finer("smack-bosh set connect is false in error");
                    ((AbstractXMPPConnection) XMPPBOSHConnection.this).connected = false;
                }
                XMPPBOSHConnection.this.notified = true;
                synchronized (XMPPBOSHConnection.this) {
                    XMPPBOSHConnection.this.notifyAll();
                }
            } catch (Throwable th) {
                XMPPBOSHConnection.this.notified = true;
                synchronized (XMPPBOSHConnection.this) {
                    XMPPBOSHConnection.this.notifyAll();
                    throw th;
                }
            }
        }
    }

    private class BOSHPacketReader implements BOSHClientResponseListener {
        private BOSHPacketReader() {
        }

        @Override
        public void responseReceived(BOSHMessageEvent bOSHMessageEvent) {
            int next;
            AbstractBody body = bOSHMessageEvent.getBody();
            if (body != null) {
                try {
                    XMPPBOSHConnection xMPPBOSHConnection = XMPPBOSHConnection.this;
                    if (xMPPBOSHConnection.sessionID == null) {
                        xMPPBOSHConnection.sessionID = body.getAttribute(BodyQName.create(XMPPBOSHConnection.BOSH_URI, "sid"));
                    }
                    if (((AbstractXMPPConnection) XMPPBOSHConnection.this).streamId == null) {
                        ((AbstractXMPPConnection) XMPPBOSHConnection.this).streamId = body.getAttribute(BodyQName.create(XMPPBOSHConnection.BOSH_URI, "authid"));
                    }
                    XmlPullParser newPullParser = XmlPullParserFactory.newInstance().newPullParser();
                    newPullParser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true);
                    String xml = body.toXML();
                    newPullParser.setInput(new StringReader(xml));
                    newPullParser.getEventType();
                    do {
                        next = newPullParser.next();
                        char c10 = 2;
                        if (next == 2) {
                            String name = newPullParser.getName();
                            switch (name.hashCode()) {
                                case -1867169789:
                                    if (name.equals("success")) {
                                        c10 = 4;
                                        break;
                                    }
                                    break;
                                case -1276666629:
                                    if (name.equals(Presence.ELEMENT)) {
                                        break;
                                    }
                                    break;
                                case -1086574198:
                                    if (name.equals(SaslStreamElements.SASLFailure.ELEMENT)) {
                                        c10 = 6;
                                        break;
                                    }
                                    break;
                                case -290659267:
                                    if (name.equals("features")) {
                                        c10 = 5;
                                        break;
                                    }
                                    break;
                                case 3368:
                                    if (name.equals(IQ.IQ_ELEMENT)) {
                                        c10 = 1;
                                        break;
                                    }
                                    break;
                                case 96784904:
                                    if (name.equals("error")) {
                                        c10 = 7;
                                        break;
                                    }
                                    break;
                                case 954925063:
                                    if (name.equals("message")) {
                                        c10 = 0;
                                        break;
                                    }
                                    break;
                                case 1402633315:
                                    if (name.equals(SaslStreamElements.Challenge.ELEMENT)) {
                                        c10 = 3;
                                        break;
                                    }
                                    break;
                            }
                            c10 = 65535;
                            switch (c10) {
                                case 0:
                                case 1:
                                case 2:
                                    XMPPBOSHConnection.this.parseAndProcessStanza(newPullParser);
                                    break;
                                case 3:
                                    XMPPBOSHConnection.this.getSASLAuthentication().challengeReceived(newPullParser.nextText());
                                    break;
                                case 4:
                                    XMPPBOSHConnection.this.send(ComposableBody.builder().setNamespaceDefinition("xmpp", XMPPBOSHConnection.XMPP_BOSH_NS).setAttribute(BodyQName.createWithPrefix(XMPPBOSHConnection.XMPP_BOSH_NS, "restart", "xmpp"), PdfBoolean.TRUE).setAttribute(BodyQName.create(XMPPBOSHConnection.BOSH_URI, "to"), XMPPBOSHConnection.this.getXMPPServiceDomain().toString()).build());
                                    XMPPBOSHConnection.this.getSASLAuthentication().authenticated(new SaslStreamElements.Success(newPullParser.nextText()));
                                    break;
                                case 5:
                                    XMPPBOSHConnection.this.parseFeatures(newPullParser);
                                    break;
                                case 6:
                                    if (!"urn:ietf:params:xml:ns:xmpp-sasl".equals(newPullParser.getNamespace(null))) {
                                        break;
                                    } else {
                                        XMPPBOSHConnection.this.getSASLAuthentication().authenticationFailed(PacketParserUtils.parseSASLFailure(newPullParser));
                                        break;
                                    }
                                case 7:
                                    if (xml.contains(StreamError.ELEMENT)) {
                                        throw new XMPPException.StreamErrorException(PacketParserUtils.parseStreamError(newPullParser));
                                    }
                                    throw new XMPPException.XMPPErrorException(null, PacketParserUtils.parseError(newPullParser).build());
                            }
                        }
                    } while (next != 1);
                } catch (Exception e10) {
                    XMPPBOSHConnection.this.log("check is connected in excp:" + XMPPBOSHConnection.this.isConnected());
                    if (XMPPBOSHConnection.this.isConnected()) {
                        XMPPBOSHConnection.this.notifyConnectionError(e10);
                    }
                }
            }
        }
    }

    public XMPPBOSHConnection(String str, String str2, boolean z10, String str3, int i10, String str4, DomainBareJid domainBareJid) {
        this(BOSHConfiguration.builder().setUseHttps(z10).setHost(str3).setPort(i10).setFile(str4).setXmppDomain(domainBareJid).setUsernameAndPassword(str, str2).build());
    }

    public void log(String str) {
        Log.i(this.TAG, str);
    }

    private void logException(Exception exc) {
        LOGGER.finest("error message is " + exc.getMessage());
        StackTraceElement[] stackTrace = exc.getStackTrace();
        if (stackTrace != null) {
            for (StackTraceElement stackTraceElement : stackTrace) {
                log("thread:" + Thread.currentThread().getName() + "-" + stackTraceElement.getClassName() + "." + stackTraceElement.getMethodName() + "(" + stackTraceElement.getFileName() + ":" + stackTraceElement.getLineNumber() + ")");
            }
        }
    }

    private void sendElement(Element element) {
        try {
            send(ComposableBody.builder().setPayloadXML(element.toXML().toString()).build());
            if (element instanceof Stanza) {
                firePacketSendingListeners((Stanza) element);
            }
        } catch (BOSHException e10) {
            LOGGER.log(Level.SEVERE, "BOSHException in sendStanzaInternal", (Throwable) e10);
        }
    }

    @Override
    protected void connectInternal() throws SmackException, InterruptedException {
        this.done = false;
        this.notified = false;
        try {
            BOSHClient bOSHClient = this.client;
            if (bOSHClient != null) {
                bOSHClient.close();
                this.client = null;
                this.done = false;
                this.notified = false;
            }
            this.sessionID = null;
            BOSHClientConfig.Builder create = BOSHClientConfig.Builder.create(this.config.getURI(), this.config.getXMPPServiceDomain().toString());
            if (this.config.isProxyEnabled()) {
                create.setProxy(this.config.getProxyAddress(), this.config.getProxyPort());
            }
            BOSHClient create2 = BOSHClient.create(create.build());
            this.client = create2;
            create2.addBOSHClientConnListener(new BOSHConnectionListener());
            this.client.addBOSHClientResponseListener(new BOSHPacketReader());
            if (this.config.isDebuggerEnabled()) {
                initDebugger();
                if (this.isFirstInitialization) {
                    if (this.debugger.getReaderListener() != null) {
                        addAsyncStanzaListener(this.debugger.getReaderListener(), null);
                    }
                    if (this.debugger.getWriterListener() != null) {
                        addPacketSendingListener(this.debugger.getWriterListener(), null);
                    }
                }
            }
            this.client.send(ComposableBody.builder().setNamespaceDefinition("xmpp", XMPP_BOSH_NS).setAttribute(BodyQName.createWithPrefix(XMPP_BOSH_NS, "version", "xmpp"), "1.0").build());
            synchronized (this) {
                if (!this.connected) {
                    long currentTimeMillis = System.currentTimeMillis() + getReplyTimeout();
                    while (!this.notified) {
                        long currentTimeMillis2 = System.currentTimeMillis();
                        if (currentTimeMillis2 >= currentTimeMillis) {
                            break;
                        } else {
                            wait(currentTimeMillis - currentTimeMillis2);
                        }
                    }
                }
            }
            if (!this.connected && !this.done) {
                this.done = true;
                throw new SmackException("Timeout reached for the connection to " + getHost() + ":" + getPort() + ".");
            }
            this.tlsHandled.reportSuccess();
            this.saslFeatureReceived.reportSuccess();
        } catch (Exception e10) {
            throw new SmackException.ConnectionException(e10);
        }
    }

    @Override
    public void disconnect() {
        super.disconnect();
        BOSHClient bOSHClient = this.client;
        if (bOSHClient != null) {
            bOSHClient.akClose();
        }
    }

    @Override
    public void initDebugger() {
        this.writer = new Writer() {
            @Override
            public void close() {
            }

            @Override
            public void flush() {
            }

            @Override
            public void write(char[] cArr, int i10, int i11) {
            }
        };
        try {
            this.readerPipe = new PipedWriter();
            this.reader = new PipedReader(this.readerPipe);
        } catch (IOException unused) {
        }
        super.initDebugger();
        this.client.addBOSHClientResponseListener(new BOSHClientResponseListener() {
            @Override
            public void responseReceived(BOSHMessageEvent bOSHMessageEvent) {
                if (bOSHMessageEvent.getBody() != null) {
                    try {
                        XMPPBOSHConnection.this.readerPipe.write(bOSHMessageEvent.getBody().toXML());
                        XMPPBOSHConnection.this.readerPipe.flush();
                    } catch (Exception unused2) {
                    }
                }
            }
        });
        this.client.addBOSHClientRequestListener(new BOSHClientRequestListener() {
            @Override
            public void requestSent(BOSHMessageEvent bOSHMessageEvent) {
                if (bOSHMessageEvent.getBody() != null) {
                    try {
                        String xml = bOSHMessageEvent.getBody().toXML();
                        if (xml.contains("<response xmlns='urn:ietf:params:xml:ns:xmpp-sasl'>")) {
                            xml = "this is our login-info";
                        }
                        if (xml.length() < 4096) {
                            XMPPBOSHConnection.LOGGER.log(Level.ALL, "AK-SMACK, SENT(" + hashCode() + "):" + xml);
                        }
                        ((AbstractXMPPConnection) XMPPBOSHConnection.this).writer.write(bOSHMessageEvent.getBody().toXML());
                    } catch (Exception unused2) {
                    }
                }
            }
        });
        Thread thread = new Thread() {
            private Thread thread = this;
            private int bufferLength = 1024;

            @Override
            public void run() {
                try {
                    char[] cArr = new char[this.bufferLength];
                    while (XMPPBOSHConnection.this.readerConsumer == this.thread && !XMPPBOSHConnection.this.done) {
                        ((AbstractXMPPConnection) XMPPBOSHConnection.this).reader.read(cArr, 0, this.bufferLength);
                    }
                } catch (IOException unused2) {
                }
            }
        };
        this.readerConsumer = thread;
        thread.setDaemon(true);
        this.readerConsumer.start();
    }

    @Override
    public boolean isSecureConnection() {
        return false;
    }

    @Override
    public boolean isUsingCompression() {
        return false;
    }

    @Override
    protected void loginInternal(String str, String str2, Resourcepart resourcepart) throws XMPPException, SmackException, IOException, InterruptedException {
        this.saslAuthentication.authenticate(str, str2, this.config.getAuthzid(), null);
        bindResourceAndEstablishSession(resourcepart);
        afterSuccessfulLogin(false);
    }

    protected void notifyConnectionError(Exception exc) {
        shutdown();
        callConnectionClosedOnErrorListener(exc);
    }

    protected void send(ComposableBody composableBody) throws BOSHException {
        if (this.connected) {
            if (composableBody != null) {
                if (this.sessionID != null) {
                    composableBody = composableBody.rebuild().setAttribute(BodyQName.create(BOSH_URI, "sid"), this.sessionID).build();
                }
                this.client.send(composableBody);
                return;
            }
            throw new NullPointerException("Body mustn't be null!");
        }
        throw new IllegalStateException("Not connected to a server!");
    }

    @Override
    public void sendNonza(Nonza nonza) throws SmackException.NotConnectedException {
        if (!this.done) {
            sendElement(nonza);
            return;
        }
        throw new SmackException.NotConnectedException();
    }

    @Override
    protected void sendStanzaInternal(Stanza stanza) throws SmackException.NotConnectedException {
        sendElement(stanza);
    }

    @Override
    protected void shutdown() {
        setWasAuthenticated();
        this.sessionID = null;
        this.done = true;
        this.authenticated = false;
        this.connected = false;
        this.isFirstInitialization = false;
        LOGGER.finer("smack-bosh set connect is false in shutdown");
        PipedWriter pipedWriter = this.readerPipe;
        if (pipedWriter != null) {
            try {
                pipedWriter.close();
            } catch (Throwable unused) {
            }
            this.reader = null;
        }
        Reader reader = this.reader;
        if (reader != null) {
            try {
                reader.close();
            } catch (Throwable unused2) {
            }
            this.reader = null;
        }
        Writer writer = this.writer;
        if (writer != null) {
            try {
                writer.close();
            } catch (Throwable unused3) {
            }
            this.writer = null;
        }
        this.readerConsumer = null;
    }

    public XMPPBOSHConnection(BOSHConfiguration bOSHConfiguration) {
        super(bOSHConfiguration);
        this.isFirstInitialization = true;
        this.done = false;
        this.TAG = "XMPPBOSHConnection";
        this.sessionID = null;
        this.config = bOSHConfiguration;
    }
}