public final XMPPTCPConnectionConfiguration.Builder getConnectionConfiguration() throws KeyManagementException, NoSuchAlgorithmException { XMPPTCPConnectionConfiguration.Builder builder = XMPPTCPConnectionConfiguration.builder(); if (configuration.tlsContext != null) { builder.setCustomSSLContext(configuration.tlsContext); } builder.setSecurityMode(configuration.securityMode); builder.setXmppDomain(service); return builder; }
@Override public XMPPTCPConnectionConfiguration build() { return new XMPPTCPConnectionConfiguration(this); } }
/** * Returns true if Stream Management is supported by the server. * * @return true if Stream Management is supported by the server. */ public boolean isSmAvailable() { return hasFeature(StreamManagementFeature.ELEMENT, StreamManagement.NAMESPACE); }
public static boolean supportsIbr(DomainBareJid xmppDomain) throws SmackException, IOException, XMPPException, InterruptedException, KeyManagementException, NoSuchAlgorithmException { XMPPTCPConnectionConfiguration.Builder configBuilder = XMPPTCPConnectionConfiguration.builder() .setXmppDomain(xmppDomain); TLSUtils.acceptAllCertificates(configBuilder); XMPPTCPConnectionConfiguration config = configBuilder.build(); XMPPTCPConnection connection = new XMPPTCPConnection(config); connection.connect(); try { return supportsIbr(connection); } finally { connection.disconnect(); } }
static XMPPTCPConnection getConnectedConnection(SmackIntegrationTestEnvironment environment, int connectionId) throws KeyManagementException, NoSuchAlgorithmException, InterruptedException, SmackException, IOException, XMPPException { Configuration config = environment.configuration; XMPPTCPConnectionConfiguration.Builder builder = getConnectionConfigurationBuilder(config); XMPPTCPConnection connection = new XMPPTCPConnection(builder.build()); connection.connect(); UsernameAndPassword uap = IntTestUtil.registerAccount(connection, environment, connectionId); connection.login(uap.username, uap.password); return connection; }
/** * Creates a new XMPP connection over TCP. * <p> * This is the simplest constructor for connecting to an XMPP server. Alternatively, * you can get fine-grained control over connection settings using the * {@link #XMPPTCPConnection(XMPPTCPConnectionConfiguration)} constructor. * </p> * @param username * @param password * @param serviceName * @throws XmppStringprepException */ public XMPPTCPConnection(CharSequence username, String password, String serviceName) throws XmppStringprepException { this(XMPPTCPConnectionConfiguration.builder().setUsernameAndPassword(username, password).setXmppDomain( JidCreate.domainBareFrom(serviceName)).build()); }
/** * Initializes the connection by creating a stanza reader and writer and opening a * XMPP stream to the server. * * @throws XMPPException if establishing a connection to the server fails. * @throws SmackException if the server fails to respond back or if there is anther error. * @throws IOException */ private void initConnection() throws IOException { boolean isFirstInitialization = packetReader == null || packetWriter == null; compressionHandler = null; // Set the reader and writer instance variables initReaderAndWriter(); if (isFirstInitialization) { packetWriter = new PacketWriter(); packetReader = new PacketReader(); } // Start the writer thread. This will open an XMPP stream to the server packetWriter.init(); // Start the reader thread. The startup() method will block until we // get an opening stream packet back from server packetReader.init(); }
@Override protected void throwAlreadyConnectedExceptionIfAppropriate() throws AlreadyConnectedException { if (isConnected() && !disconnectedButResumeable) { throw new AlreadyConnectedException(); } }
@Override protected void sendStanzaInternal(Stanza packet) throws NotConnectedException, InterruptedException { packetWriter.sendStreamElement(packet); if (isSmEnabled()) { for (StanzaFilter requestAckPredicate : requestAckPredicates) { if (requestAckPredicate.accept(packet)) { requestSmAcknowledgementInternal(); break; } } } }
@Override public void sendNonza(Nonza element) throws NotConnectedException, InterruptedException { packetWriter.sendStreamElement(element); }
/** * Set if Stream Management resumption should be used if supported by the server. * * @param useSmResumption true to use Stream Management resumption. */ public void setUseStreamManagementResumption(boolean useSmResumption) { if (useSmResumption) { // Also enable SM is resumption is enabled setUseStreamManagement(useSmResumption); } this.useSmResumption = useSmResumption; }
/** * Set if Stream Management resumption should be used by default for new connections. * * @param useSmResumptionDefault true to use Stream Management resumption for new connections. */ public static void setUseStreamManagementResumptionDefault(boolean useSmResumptionDefault) { if (useSmResumptionDefault) { // Also enable SM is resumption is enabled setUseStreamManagementDefault(useSmResumptionDefault); } XMPPTCPConnection.useSmResumptionDefault = useSmResumptionDefault; }
protected void throwNotConnectedExceptionIfDoneAndResumptionNotPossible() throws NotConnectedException { final boolean done = done(); if (done) { final boolean smResumptionPossible = isSmResumptionPossible(); // Don't throw a NotConnectedException is there is an resumable stream available if (!smResumptionPossible) { throw new NotConnectedException(XMPPTCPConnection.this, "done=" + done + " smResumptionPossible=" + smResumptionPossible); } } }
@Override public void connectionCallback(XMPPTCPConnection connection) throws Exception { if (!connection.isSmAvailable()) { throw new TestNotPossibleException("XEP-198: Stream Mangement not supported by service"); } } });
/** * Performs an unclean disconnect and shutdown of the connection. Does not send a closing stream stanza. */ public synchronized void instantShutdown() { shutdown(true); }
/** * Returns true if the connection is disconnected by a Stream resumption via Stream Management is possible. * * @return true if disconnected but resumption possible. */ public boolean isDisconnectedButSmResumptionPossible() { return disconnectedButResumeable && isSmResumptionPossible(); }
public static Builder builder() { return new Builder(); }
@Override protected void throwNotConnectedExceptionIfAppropriate() throws NotConnectedException { if (packetWriter == null) { throw new NotConnectedException(); } packetWriter.throwNotConnectedExceptionIfDoneAndResumptionNotPossible(); }
static XMPPTCPConnectionConfiguration.Builder getConnectionConfigurationBuilder(Configuration config) { XMPPTCPConnectionConfiguration.Builder builder = XMPPTCPConnectionConfiguration.builder(); if (config.tlsContext != null) { builder.setCustomSSLContext(config.tlsContext); } builder.setSecurityMode(config.securityMode); builder.setXmppDomain(config.service); switch (config.debugger) { case enhanced: builder.setDebuggerFactory(EnhancedDebugger.Factory.INSTANCE); break; case console: builder.setDebuggerFactory(ConsoleDebugger.Factory.INSTANCE); break; case none: // Nothing to do :). break; } return builder; }
private void requestSmAcknowledgementInternal() throws NotConnectedException, InterruptedException { packetWriter.sendStreamElement(AckRequest.INSTANCE); }