001/**
002 *
003 * Copyright 2021 Florian Schmaus
004 *
005 * This file is part of smack-examples.
006 *
007 * smack-examples is free software; you can redistribute it and/or modify
008 * it under the terms of the GNU General Public License as published by
009 * the Free Software Foundation; either version 3 of the License, or
010 * (at your option) any later version.
011 *
012 * This program is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
015 * GNU General Public License for more details.
016 *
017 * You should have received a copy of the GNU General Public License
018 * along with this program; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
020 */
021package org.igniterealtime.smack.examples;
022
023import java.io.IOException;
024import java.net.URISyntaxException;
025
026import org.jivesoftware.smack.SmackException;
027import org.jivesoftware.smack.XMPPException;
028
029import org.jivesoftware.smack.c2s.ModularXmppClientToServerConnection;
030import org.jivesoftware.smack.c2s.ModularXmppClientToServerConnectionConfiguration;
031import org.jivesoftware.smack.util.TLSUtils;
032import org.jivesoftware.smack.websocket.XmppWebSocketTransportModuleDescriptor;
033
034public class WebSocketConnection {
035
036    public static void main(String[] args)
037                    throws URISyntaxException, SmackException, IOException, XMPPException, InterruptedException {
038        String jid, password, websocketEndpoint, messageTo = null;
039        if (args.length < 3 || args.length > 4) {
040            throw new IllegalArgumentException();
041        }
042
043        jid = args[0];
044        password = args[1];
045        websocketEndpoint = args[2];
046        if (args.length >= 4) {
047            messageTo = args[3];
048        }
049
050        TLSUtils.setDefaultTrustStoreTypeToJksIfRequired();
051
052        testWebSocketConnection(jid, password, websocketEndpoint, messageTo);
053    }
054
055    public static void testWebSocketConnection(String jid, String password, String websocketEndpoint)
056                    throws URISyntaxException, SmackException, IOException, XMPPException, InterruptedException {
057        testWebSocketConnection(jid, password, websocketEndpoint, null);
058    }
059
060    public static void testWebSocketConnection(String jid, String password, String websocketEndpoint, String messageTo)
061                    throws URISyntaxException, SmackException, IOException, XMPPException, InterruptedException {
062        ModularXmppClientToServerConnectionConfiguration.Builder builder = ModularXmppClientToServerConnectionConfiguration.builder();
063        builder.removeAllModules()
064               .setXmppAddressAndPassword(jid, password)
065               ;
066
067        XmppWebSocketTransportModuleDescriptor.Builder websocketBuilder = XmppWebSocketTransportModuleDescriptor.getBuilder(builder);
068        websocketBuilder.explicitlySetWebSocketEndpointAndDiscovery(websocketEndpoint, false);
069        builder.addModule(websocketBuilder.build());
070
071        ModularXmppClientToServerConnectionConfiguration config = builder.build();
072        ModularXmppClientToServerConnection connection = new ModularXmppClientToServerConnection(config);
073
074        connection.setReplyTimeout(5 * 60 * 1000);
075
076        XmppTools.modularConnectionTest(connection, messageTo);
077    }
078}