001/** 002 * 003 * Copyright 2016 Florian Schmaus 004 * 005 * Licensed under the Apache License, Version 2.0 (the "License"); 006 * you may not use this file except in compliance with the License. 007 * You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.igniterealtime.smack.smackrepl; 018 019import java.io.IOException; 020import java.security.KeyManagementException; 021import java.security.NoSuchAlgorithmException; 022 023import org.jivesoftware.smack.SmackException; 024import org.jivesoftware.smack.SmackException.NoResponseException; 025import org.jivesoftware.smack.SmackException.NotConnectedException; 026import org.jivesoftware.smack.XMPPConnection; 027import org.jivesoftware.smack.XMPPException; 028import org.jivesoftware.smack.XMPPException.XMPPErrorException; 029import org.jivesoftware.smack.tcp.XMPPTCPConnection; 030import org.jivesoftware.smack.tcp.XMPPTCPConnectionConfiguration; 031import org.jivesoftware.smack.util.TLSUtils; 032import org.jivesoftware.smackx.iqregister.AccountManager; 033import org.jxmpp.jid.DomainBareJid; 034import org.jxmpp.jid.impl.JidCreate; 035import org.jxmpp.jid.parts.Localpart; 036 037public class XmppTools { 038 039 public static void main(String[] args) throws SmackException, IOException, XMPPException, InterruptedException, 040 KeyManagementException, NoSuchAlgorithmException { 041 boolean one = createAccount("xmpp.foobar.io", "smack1", "smack1"); 042 boolean two = createAccount("xmpp.foobar.io", "smack2", "smack2"); 043 // CHECKSTYLE:OFF 044 System.out.println("Account created: " + one + ' ' + two); 045 // CHECKSTYLE:ON 046 } 047 048 public static boolean supportsIbr(String xmppDomain) throws SmackException, IOException, XMPPException, 049 InterruptedException, KeyManagementException, NoSuchAlgorithmException { 050 DomainBareJid xmppDomainJid = JidCreate.domainBareFrom(xmppDomain); 051 return supportsIbr(xmppDomainJid); 052 } 053 054 public static boolean supportsIbr(DomainBareJid xmppDomain) throws SmackException, IOException, XMPPException, 055 InterruptedException, KeyManagementException, NoSuchAlgorithmException { 056 XMPPTCPConnectionConfiguration.Builder configBuilder = XMPPTCPConnectionConfiguration.builder() 057 .setXmppDomain(xmppDomain); 058 TLSUtils.acceptAllCertificates(configBuilder); 059 XMPPTCPConnectionConfiguration config = configBuilder.build(); 060 XMPPTCPConnection connection = new XMPPTCPConnection(config); 061 connection.connect(); 062 try { 063 return supportsIbr(connection); 064 } finally { 065 connection.disconnect(); 066 } 067 } 068 069 public static boolean supportsIbr(XMPPConnection connection) 070 throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException { 071 AccountManager accountManager = AccountManager.getInstance(connection); 072 return accountManager.supportsAccountCreation(); 073 } 074 075 public static boolean createAccount(String xmppDomain, String username, String password) 076 throws KeyManagementException, NoSuchAlgorithmException, SmackException, IOException, XMPPException, 077 InterruptedException { 078 DomainBareJid xmppDomainJid = JidCreate.domainBareFrom(xmppDomain); 079 Localpart localpart = Localpart.from(username); 080 return createAccount(xmppDomainJid, localpart, password); 081 } 082 083 public static boolean createAccount(DomainBareJid xmppDomain, Localpart username, String password) 084 throws KeyManagementException, NoSuchAlgorithmException, SmackException, IOException, XMPPException, 085 InterruptedException { 086 XMPPTCPConnectionConfiguration.Builder configBuilder = XMPPTCPConnectionConfiguration.builder() 087 .setXmppDomain(xmppDomain); 088 TLSUtils.acceptAllCertificates(configBuilder); 089 XMPPTCPConnectionConfiguration config = configBuilder.build(); 090 XMPPTCPConnection connection = new XMPPTCPConnection(config); 091 connection.connect(); 092 try { 093 if (!supportsIbr(connection)) 094 return false; 095 096 AccountManager accountManager = AccountManager.getInstance(connection); 097 accountManager.createAccount(username, password); 098 return true; 099 } finally { 100 connection.disconnect(); 101 } 102 } 103}