Search in sources :

Example 1 with ModularXmppClientToServerConnectionConfiguration

use of org.jivesoftware.smack.c2s.ModularXmppClientToServerConnectionConfiguration in project Smack by igniterealtime.

the class XmppConnectionDescriptor method buildWebsocketDescriptor.

public static XmppConnectionDescriptor<ModularXmppClientToServerConnection, ModularXmppClientToServerConnectionConfiguration, ModularXmppClientToServerConnectionConfiguration.Builder> buildWebsocketDescriptor(String nickname, Class<? extends WebSocketFactory> factoryClass) throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException {
    WebSocketFactory factory;
    try {
        Field instanceField = factoryClass.getField("INSTANCE");
        factory = (WebSocketFactory) instanceField.get(null);
    } catch (NoSuchFieldException | IllegalArgumentException | IllegalAccessException e) {
        factory = factoryClass.getConstructor().newInstance();
    }
    WebSocketFactory finalFactory = factory;
    return XmppConnectionDescriptor.buildWith(ModularXmppClientToServerConnection.class, ModularXmppClientToServerConnectionConfiguration.class, ModularXmppClientToServerConnectionConfiguration.Builder.class).withNickname(nickname).applyExtraConfguration(cb -> {
        cb.removeAllModules();
        ModularXmppClientToServerConnectionModuleDescriptor webSocketModuleDescriptor = XmppWebSocketTransportModuleDescriptor.getBuilder(cb).setWebSocketFactory(finalFactory).build();
        cb.addModule(webSocketModuleDescriptor);
    }).build();
}
Also used : Arrays(java.util.Arrays) XmppWebSocketTransportModuleDescriptor(org.jivesoftware.smack.websocket.XmppWebSocketTransportModuleDescriptor) Collection(java.util.Collection) Field(java.lang.reflect.Field) Constructor(java.lang.reflect.Constructor) InvocationTargetException(java.lang.reflect.InvocationTargetException) ArrayList(java.util.ArrayList) ConnectionConfiguration(org.jivesoftware.smack.ConnectionConfiguration) List(java.util.List) Consumer(org.jivesoftware.smack.util.Consumer) AbstractXMPPConnection(org.jivesoftware.smack.AbstractXMPPConnection) Modifier(java.lang.reflect.Modifier) ModularXmppClientToServerConnectionConfiguration(org.jivesoftware.smack.c2s.ModularXmppClientToServerConnectionConfiguration) ModularXmppClientToServerConnectionModuleDescriptor(org.jivesoftware.smack.c2s.ModularXmppClientToServerConnectionModuleDescriptor) Method(java.lang.reflect.Method) Collections(java.util.Collections) XMPPConnection(org.jivesoftware.smack.XMPPConnection) ModularXmppClientToServerConnection(org.jivesoftware.smack.c2s.ModularXmppClientToServerConnection) WebSocketFactory(org.jivesoftware.smack.websocket.impl.WebSocketFactory) Field(java.lang.reflect.Field) ModularXmppClientToServerConnectionModuleDescriptor(org.jivesoftware.smack.c2s.ModularXmppClientToServerConnectionModuleDescriptor) ModularXmppClientToServerConnectionConfiguration(org.jivesoftware.smack.c2s.ModularXmppClientToServerConnectionConfiguration) WebSocketFactory(org.jivesoftware.smack.websocket.impl.WebSocketFactory) ModularXmppClientToServerConnection(org.jivesoftware.smack.c2s.ModularXmppClientToServerConnection)

Example 2 with ModularXmppClientToServerConnectionConfiguration

use of org.jivesoftware.smack.c2s.ModularXmppClientToServerConnectionConfiguration in project Smack by igniterealtime.

the class SmackIntegrationTestXmppConnectionManagerTest method simpleXmppConnectionDescriptorTest.

@Test
public void simpleXmppConnectionDescriptorTest() throws ClassNotFoundException, NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, KeyManagementException, NoSuchAlgorithmException, XmppStringprepException, InstantiationException {
    XmppConnectionDescriptor<ModularXmppClientToServerConnection, ModularXmppClientToServerConnectionConfiguration, ModularXmppClientToServerConnectionConfiguration.Builder> descriptor = XmppConnectionDescriptor.buildWith(ModularXmppClientToServerConnection.class, ModularXmppClientToServerConnectionConfiguration.class, ModularXmppClientToServerConnectionConfiguration.Builder.class).applyExtraConfguration(b -> b.removeAllModules().addModule(XmppTcpTransportModuleDescriptor.class)).build();
    Configuration sinttestConfiguration = Configuration.builder().setService("example.org").build();
    ModularXmppClientToServerConnection connection = descriptor.construct(sinttestConfiguration);
    assertEquals("example.org", connection.getXMPPServiceDomain().toString());
}
Also used : Test(org.junit.jupiter.api.Test) XmppTcpTransportModuleDescriptor(org.jivesoftware.smack.tcp.XmppTcpTransportModuleDescriptor) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) XmppStringprepException(org.jxmpp.stringprep.XmppStringprepException) ModularXmppClientToServerConnectionConfiguration(org.jivesoftware.smack.c2s.ModularXmppClientToServerConnectionConfiguration) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) KeyManagementException(java.security.KeyManagementException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ModularXmppClientToServerConnection(org.jivesoftware.smack.c2s.ModularXmppClientToServerConnection) ModularXmppClientToServerConnectionConfiguration(org.jivesoftware.smack.c2s.ModularXmppClientToServerConnectionConfiguration) ModularXmppClientToServerConnectionConfiguration(org.jivesoftware.smack.c2s.ModularXmppClientToServerConnectionConfiguration) ModularXmppClientToServerConnection(org.jivesoftware.smack.c2s.ModularXmppClientToServerConnection) Test(org.junit.jupiter.api.Test)

Example 3 with ModularXmppClientToServerConnectionConfiguration

use of org.jivesoftware.smack.c2s.ModularXmppClientToServerConnectionConfiguration in project Smack by igniterealtime.

the class ModularXmppClientToServerConnectionTool method printStateGraph.

public static void printStateGraph(PrintWriter pw, boolean breakStateName) throws XmppStringprepException {
    ModularXmppClientToServerConnectionConfiguration.Builder configurationBuilder = ModularXmppClientToServerConnectionConfiguration.builder().setUsernameAndPassword("user", "password").setXmppDomain("example.org");
    ModularXmppClientToServerConnectionConfiguration configuration = configurationBuilder.build();
    configuration.printStateGraphInDotFormat(pw, breakStateName);
    pw.flush();
}
Also used : ModularXmppClientToServerConnectionConfiguration(org.jivesoftware.smack.c2s.ModularXmppClientToServerConnectionConfiguration)

Example 4 with ModularXmppClientToServerConnectionConfiguration

use of org.jivesoftware.smack.c2s.ModularXmppClientToServerConnectionConfiguration in project Smack by igniterealtime.

the class WebSocketConnectionTest method testWebSocketConnection.

public static void testWebSocketConnection(String jid, String password, String websocketEndpoint, String messageTo) throws URISyntaxException, SmackException, IOException, XMPPException, InterruptedException {
    ModularXmppClientToServerConnectionConfiguration.Builder builder = ModularXmppClientToServerConnectionConfiguration.builder();
    builder.removeAllModules().setXmppAddressAndPassword(jid, password).setDebuggerFactory(ConsoleDebugger.Factory.INSTANCE);
    XmppWebSocketTransportModuleDescriptor.Builder websocketBuilder = XmppWebSocketTransportModuleDescriptor.getBuilder(builder);
    websocketBuilder.explicitlySetWebSocketEndpointAndDiscovery(websocketEndpoint, false);
    builder.addModule(websocketBuilder.build());
    ModularXmppClientToServerConnectionConfiguration config = builder.build();
    ModularXmppClientToServerConnection connection = new ModularXmppClientToServerConnection(config);
    connection.setReplyTimeout(5 * 60 * 1000);
    connection.addConnectionStateMachineListener((event, c) -> {
        Logger.getAnonymousLogger().info("Connection event: " + event);
    });
    connection.connect();
    connection.login();
    if (messageTo != null) {
        Message message = connection.getStanzaFactory().buildMessageStanza().to(messageTo).setBody("It is alive! " + XmppDateTime.formatXEP0082Date(new Date())).build();
        connection.sendStanza(message);
    }
    Thread.sleep(1000);
    connection.disconnect();
    ModularXmppClientToServerConnection.Stats connectionStats = connection.getStats();
    ServiceDiscoveryManager.Stats serviceDiscoveryManagerStats = ServiceDiscoveryManager.getInstanceFor(connection).getStats();
    // CHECKSTYLE:OFF
    System.out.println("WebSocket successfully finished, yeah!\n" + connectionStats + '\n' + serviceDiscoveryManagerStats);
// CHECKSTYLE:ON
}
Also used : XmppWebSocketTransportModuleDescriptor(org.jivesoftware.smack.websocket.XmppWebSocketTransportModuleDescriptor) Message(org.jivesoftware.smack.packet.Message) ModularXmppClientToServerConnectionConfiguration(org.jivesoftware.smack.c2s.ModularXmppClientToServerConnectionConfiguration) ModularXmppClientToServerConnection(org.jivesoftware.smack.c2s.ModularXmppClientToServerConnection) Date(java.util.Date) ServiceDiscoveryManager(org.jivesoftware.smackx.disco.ServiceDiscoveryManager)

Example 5 with ModularXmppClientToServerConnectionConfiguration

use of org.jivesoftware.smack.c2s.ModularXmppClientToServerConnectionConfiguration in project Smack by igniterealtime.

the class XmppWebSocketTransportModuleTest method createWebSocketModuleConnectionInstanceTest.

@Test
public void createWebSocketModuleConnectionInstanceTest() throws URISyntaxException, XmppStringprepException {
    ModularXmppClientToServerConnectionConfiguration.Builder builder = ModularXmppClientToServerConnectionConfiguration.builder();
    builder.removeAllModules();
    builder.addModule(XmppWebSocketTransportModuleDescriptor.class);
    builder.setXmppAddressAndPassword("user5@localhost.org", "user5");
    builder.setHost("localhost.org");
    XmppWebSocketTransportModuleDescriptor.Builder websocketBuilder = XmppWebSocketTransportModuleDescriptor.getBuilder(builder);
    websocketBuilder.explicitlySetWebSocketEndpointAndDiscovery(new URI("wss://localhost.org:7443/ws/"), false);
    ModularXmppClientToServerConnectionConfiguration config = builder.build();
    ModularXmppClientToServerConnection connection = new ModularXmppClientToServerConnection(config);
    assertNotNull(connection);
}
Also used : ModularXmppClientToServerConnectionConfiguration(org.jivesoftware.smack.c2s.ModularXmppClientToServerConnectionConfiguration) URI(java.net.URI) ModularXmppClientToServerConnection(org.jivesoftware.smack.c2s.ModularXmppClientToServerConnection) Test(org.junit.jupiter.api.Test)

Aggregations

ModularXmppClientToServerConnectionConfiguration (org.jivesoftware.smack.c2s.ModularXmppClientToServerConnectionConfiguration)7 ModularXmppClientToServerConnection (org.jivesoftware.smack.c2s.ModularXmppClientToServerConnection)6 XmppWebSocketTransportModuleDescriptor (org.jivesoftware.smack.websocket.XmppWebSocketTransportModuleDescriptor)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 Test (org.junit.jupiter.api.Test)2 BufferedWriter (java.io.BufferedWriter)1 OutputStreamWriter (java.io.OutputStreamWriter)1 PrintWriter (java.io.PrintWriter)1 Constructor (java.lang.reflect.Constructor)1 Field (java.lang.reflect.Field)1 Method (java.lang.reflect.Method)1 Modifier (java.lang.reflect.Modifier)1 URI (java.net.URI)1 KeyManagementException (java.security.KeyManagementException)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 Date (java.util.Date)1