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; 032 033import org.jivesoftware.smackx.iqregister.AccountManager; 034 035import org.jxmpp.jid.DomainBareJid; 036import org.jxmpp.jid.impl.JidCreate; 037import org.jxmpp.jid.parts.Localpart; 038 039public class XmppTools { 040 041 public static void main(String[] args) throws SmackException, IOException, XMPPException, InterruptedException, 042 KeyManagementException, NoSuchAlgorithmException { 043 boolean one = createAccount("xmpp.foobar.io", "smack1", "smack1"); 044 boolean two = createAccount("xmpp.foobar.io", "smack2", "smack2"); 045 // CHECKSTYLE:OFF 046 System.out.println("Account created: " + one + ' ' + two); 047 // CHECKSTYLE:ON 048 } 049 050 public static boolean supportsIbr(String xmppDomain) throws SmackException, IOException, XMPPException, 051 InterruptedException, KeyManagementException, NoSuchAlgorithmException { 052 DomainBareJid xmppDomainJid = JidCreate.domainBareFrom(xmppDomain); 053 return supportsIbr(xmppDomainJid); 054 } 055 056 public static boolean supportsIbr(DomainBareJid xmppDomain) throws SmackException, IOException, XMPPException, 057 InterruptedException, KeyManagementException, NoSuchAlgorithmException { 058 XMPPTCPConnectionConfiguration.Builder configBuilder = XMPPTCPConnectionConfiguration.builder() 059 .setXmppDomain(xmppDomain); 060 TLSUtils.acceptAllCertificates(configBuilder); 061 XMPPTCPConnectionConfiguration config = configBuilder.build(); 062 XMPPTCPConnection connection = new XMPPTCPConnection(config); 063 connection.connect(); 064 try { 065 return supportsIbr(connection); 066 } finally { 067 connection.disconnect(); 068 } 069 } 070 071 public static boolean supportsIbr(XMPPConnection connection) 072 throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException { 073 AccountManager accountManager = AccountManager.getInstance(connection); 074 return accountManager.supportsAccountCreation(); 075 } 076 077 public static boolean createAccount(String xmppDomain, String username, String password) 078 throws KeyManagementException, NoSuchAlgorithmException, SmackException, IOException, XMPPException, 079 InterruptedException { 080 DomainBareJid xmppDomainJid = JidCreate.domainBareFrom(xmppDomain); 081 Localpart localpart = Localpart.from(username); 082 return createAccount(xmppDomainJid, localpart, password); 083 } 084 085 public static boolean createAccount(DomainBareJid xmppDomain, Localpart username, String password) 086 throws KeyManagementException, NoSuchAlgorithmException, SmackException, IOException, XMPPException, 087 InterruptedException { 088 XMPPTCPConnectionConfiguration.Builder configBuilder = XMPPTCPConnectionConfiguration.builder() 089 .setXmppDomain(xmppDomain); 090 TLSUtils.acceptAllCertificates(configBuilder); 091 XMPPTCPConnectionConfiguration config = configBuilder.build(); 092 XMPPTCPConnection connection = new XMPPTCPConnection(config); 093 connection.connect(); 094 try { 095 if (!supportsIbr(connection)) 096 return false; 097 098 AccountManager accountManager = AccountManager.getInstance(connection); 099 accountManager.createAccount(username, password); 100 return true; 101 } finally { 102 connection.disconnect(); 103 } 104 } 105}