// this code runs a DNS resolver, might be blocking private synchronized void initXMPPConnection() { // allow custom server / custom port to override SRV record if (configuration.customServer.length() > 0) connectionConfiguration = new ConnectionConfiguration( configuration.customServer, configuration.port, configuration.server); else connectionConfiguration = new ConnectionConfiguration(configuration.server); // use SRV connectionConfiguration.setReconnectionAllowed(false); connectionConfiguration.setSendPresence(false); connectionConfiguration.setCompressionEnabled(false); // disable for now connectionConfiguration.setDebuggerEnabled(configuration.smackdebug); if (configuration.require_ssl) this.connectionConfiguration.setSecurityMode(ConnectionConfiguration.SecurityMode.required); try { SSLContext sc = SSLContext.getInstance(Constant.TLS); MemorizingTrustManager mtm = JApplication.getApp(service).memorizingTrustManager; sc.init(null, new X509TrustManager[] {mtm}, new java.security.SecureRandom()); this.connectionConfiguration.setCustomSSLContext(sc); this.connectionConfiguration.setHostnameVerifier( mtm.wrapHostnameVerifier(new org.apache.http.conn.ssl.StrictHostnameVerifier())); } catch (java.security.GeneralSecurityException e) { Log.d(TAG, "initialize MemorizingTrustManager: " + e); } this.extXMPPConnection = new StreamHandler.ExtXMPPConnection(connectionConfiguration); this.streamHandler = new StreamHandler(extXMPPConnection, configuration.smackdebug); streamHandler.addAckReceivedListener( new StreamHandler.AckReceivedListener() { public void ackReceived(long handled, long total) { gotServerPong("" + handled); } }); configuration.reconnect_required = false; initServiceDiscovery(); }
// this code runs a DNS resolver, might be blocking private synchronized void initXMPPConnection() { // allow custom server / custom port to override SRV record if (mConfig.customServer.length() > 0) mXMPPConfig = new ConnectionConfiguration(mConfig.customServer, mConfig.port, mConfig.server); else mXMPPConfig = new ConnectionConfiguration(mConfig.server); // use SRV mXMPPConfig.setReconnectionAllowed(false); mXMPPConfig.setSendPresence(false); mXMPPConfig.setCompressionEnabled(false); // disable for now mXMPPConfig.setDebuggerEnabled(mConfig.smackdebug); if (mConfig.require_ssl) this.mXMPPConfig.setSecurityMode(ConnectionConfiguration.SecurityMode.required); // register MemorizingTrustManager for HTTPS try { SSLContext sc = SSLContext.getInstance("TLS"); sc.init( null, new X509TrustManager[] {YaximApplication.getApp(mService).mMTM}, new java.security.SecureRandom()); this.mXMPPConfig.setCustomSSLContext(sc); } catch (java.security.GeneralSecurityException e) { debugLog("initialize MemorizingTrustManager: " + e); } this.mXMPPConnection = new XmppStreamHandler.ExtXMPPConnection(mXMPPConfig); this.mStreamHandler = new XmppStreamHandler(mXMPPConnection, mConfig.smackdebug); mStreamHandler.addAckReceivedListener( new XmppStreamHandler.AckReceivedListener() { public void ackReceived(long handled, long total) { gotServerPong("" + handled); } }); mConfig.reconnect_required = false; initServiceDiscovery(); }
/** * Initialize the XMPP connection. * * @param jid the jid to use * @param server the server to use (not using dns srv) may be null * @param port the port * @return the XMPPConnection prepared to connect */ private Connection prepareConnection(String jid, String server, int port) { boolean useProxy = settings.getBoolean(BeemApplication.PROXY_USE_KEY, false); ProxyInfo proxyinfo = ProxyInfo.forNoProxy(); if (useProxy) { String stype = settings.getString(BeemApplication.PROXY_TYPE_KEY, "HTTP"); String phost = settings.getString(BeemApplication.PROXY_SERVER_KEY, ""); String puser = settings.getString(BeemApplication.PROXY_USERNAME_KEY, ""); String ppass = settings.getString(BeemApplication.PROXY_PASSWORD_KEY, ""); int pport = Integer.parseInt(settings.getString(BeemApplication.PROXY_PORT_KEY, "1080")); ProxyInfo.ProxyType type = ProxyType.valueOf(stype); proxyinfo = new ProxyInfo(type, phost, pport, puser, ppass); } String serviceName = StringUtils.parseServer(jid); if (port != -1 || !TextUtils.isEmpty(server)) { if (port == -1) port = 5222; if (TextUtils.isEmpty(server)) server = serviceName; config = new ConnectionConfiguration(server, port, serviceName, proxyinfo); } else { config = new ConnectionConfiguration(serviceName, proxyinfo); } if (settings.getBoolean(BeemApplication.SMACK_DEBUG_KEY, false)) config.setDebuggerEnabled(true); return new XMPPConnection(config); }
/** Constructor for XMPPNotificationManager. */ protected XMPPNotificationManager() { Map<String, String> mdc = Logging.getCopyOfContextMap(); try { mdc.put(Logging.PREFIX_KEY, LOG4J_CATEGORY); // Load up some properties File config = null; try { config = ConfigFileConstants.getFile(ConfigFileConstants.XMPP_CONFIG_FILE_NAME); } catch (IOException e) { LOG.warn("{} not readable", ConfigFileConstants.XMPP_CONFIG_FILE_NAME, e); } if (Boolean.getBoolean("useSystemXMPPConfig") || !config.canRead()) { this.props.putAll(System.getProperties()); } else { FileInputStream fis = null; try { fis = new FileInputStream(config); this.props.load(fis); } catch (FileNotFoundException e) { LOG.warn("unable to load {}", config, e); } catch (IOException e) { LOG.warn("unable to load {}", config, e); } finally { IOUtils.closeQuietly(fis); } } xmppServer = this.props.getProperty("xmpp.server"); String xmppServiceName = this.props.getProperty("xmpp.servicename", xmppServer); xmppUser = this.props.getProperty("xmpp.user"); xmppPassword = this.props.getProperty("xmpp.pass"); xmppPort = Integer.valueOf(this.props.getProperty("xmpp.port", XMPP_PORT)); ConnectionConfiguration xmppConfig = new ConnectionConfiguration(xmppServer, xmppPort, xmppServiceName); boolean debuggerEnabled = Boolean.parseBoolean(props.getProperty("xmpp.debuggerEnabled")); xmppConfig.setDebuggerEnabled(debuggerEnabled); xmppConfig.setSASLAuthenticationEnabled( Boolean.parseBoolean(props.getProperty("xmpp.SASLEnabled", "true"))); xmppConfig.setSelfSignedCertificateEnabled( Boolean.parseBoolean(props.getProperty("xmpp.selfSignedCertificateEnabled"))); if (Boolean.parseBoolean(props.getProperty("xmpp.TLSEnabled"))) { xmppConfig.setSecurityMode(SecurityMode.enabled); } else { xmppConfig.setSecurityMode(SecurityMode.disabled); } if (this.props.containsKey("xmpp.truststorePassword")) { xmppConfig.setTruststorePassword(this.props.getProperty("xmpp.truststorePassword")); } else { xmppConfig.setTruststorePassword(TRUST_STORE_PASSWORD); } LOG.debug("XMPP Manager connection config: {}", xmppConfig.toString()); xmpp = new XMPPConnection(xmppConfig); // Connect to xmpp server connectToServer(); } finally { Logging.setContextMap(mdc); } }
/** * Connects to GCM Cloud Connection Server using the supplied credentials. * * @throws XMPPException */ public void connect() throws XMPPException { config = new ConnectionConfiguration(Properties.GCM_SERVER, Properties.GCM_PORT); config.setSecurityMode(SecurityMode.enabled); config.setReconnectionAllowed(true); config.setRosterLoadedAtLogin(false); config.setSendPresence(false); config.setSocketFactory(SSLSocketFactory.getDefault()); // NOTE: Set to true to launch a window with information about packets sent and received config.setDebuggerEnabled(false); // -Dsmack.debugEnabled=true XMPPConnection.DEBUG_ENABLED = false; connection = new XMPPConnection(config); connection.connect(); connection.addConnectionListener( new ConnectionListener() { public void reconnectionSuccessful() { logger.info("Reconnecting.."); } public void reconnectionFailed(Exception e) { logger.log(Level.INFO, "Reconnection failed.. ", e); } public void reconnectingIn(int seconds) { logger.log(Level.INFO, "Reconnecting in %d secs", seconds); } public void connectionClosedOnError(Exception e) { logger.log(Level.INFO, "Connection closed on error."); } public void connectionClosed() { logger.info("Connection closed."); } }); // Handle incoming packets connection.addPacketListener( new PacketListener() { public void processPacket(Packet packet) { logger.log(Level.INFO, "Received: " + packet.toXML()); Message incomingMessage = (Message) packet; GcmPacketExtension gcmPacket = (GcmPacketExtension) incomingMessage.getExtension(Properties.GCM_NAMESPACE); String json = gcmPacket.getJson(); try { @SuppressWarnings("unchecked") Map<String, Object> jsonObject = (Map<String, Object>) JSONValue.parseWithException(json); // present for "ack"/"nack", null otherwise Object messageType = jsonObject.get("message_type"); if (messageType == null) { // Normal upstream data message handleIncomingDataMessage(jsonObject); // Send ACK to CCS String messageId = jsonObject.get("message_id").toString(); String from = jsonObject.get("from").toString(); String ack = createJsonAck(from, messageId); send(ack); } else if ("ack".equals(messageType.toString())) { // Process Ack handleAckReceipt(jsonObject); } else if ("nack".equals(messageType.toString())) { // Process Nack handleNackReceipt(jsonObject); } else { logger.log(Level.WARNING, "Unrecognized message type (%s)", messageType.toString()); } } catch (ParseException e) { logger.log(Level.SEVERE, "Error parsing JSON " + json, e); } catch (Exception e) { logger.log(Level.SEVERE, "Couldn't send echo.", e); } } }, new PacketTypeFilter(Message.class)); // Log all outgoing packets connection.addPacketInterceptor( new PacketInterceptor() { public void interceptPacket(Packet packet) { logger.log(Level.INFO, "Sent: {0}", packet.toXML()); } }, new PacketTypeFilter(Message.class)); connection.login(Properties.userName, Properties.password); }
/** Constructor for XMPPNotificationManager. */ protected XMPPNotificationManager() { // get the category logger String oldPrefix = ThreadCategory.getPrefix(); ThreadCategory.setPrefix(LOG4J_CATEGORY); try { // Load up some properties File config = null; try { config = ConfigFileConstants.getFile(ConfigFileConstants.XMPP_CONFIG_FILE_NAME); } catch (IOException e) { log().warn(ConfigFileConstants.XMPP_CONFIG_FILE_NAME + " not readable", e); } if (Boolean.getBoolean("useSystemXMPPConfig") || !config.canRead()) { this.props.putAll(System.getProperties()); } else { FileInputStream fis = null; try { fis = new FileInputStream(config); this.props.load(fis); } catch (FileNotFoundException e) { log().warn("unable to load " + config, e); } catch (IOException e) { log().warn("unable to load " + config, e); } finally { IOUtils.closeQuietly(fis); } } xmppServer = this.props.getProperty("xmpp.server"); xmppServiceName = this.props.getProperty("xmpp.servicename", xmppServer); xmppUser = this.props.getProperty("xmpp.user"); xmppPassword = this.props.getProperty("xmpp.pass"); xmppPort = Integer.valueOf(this.props.getProperty("xmpp.port", XMPP_PORT)); xmppConfig = new ConnectionConfiguration(xmppServer, xmppPort, xmppServiceName); boolean debuggerEnabled = Boolean.parseBoolean(props.getProperty("xmpp.debuggerEnabled")); xmppConfig.setDebuggerEnabled(debuggerEnabled); if (debuggerEnabled) { log().setLevel(ThreadCategory.Level.DEBUG); } xmppConfig.setSASLAuthenticationEnabled( Boolean.parseBoolean(props.getProperty("xmpp.SASLEnabled", "true"))); xmppConfig.setSelfSignedCertificateEnabled( Boolean.parseBoolean(props.getProperty("xmpp.selfSignedCertificateEnabled"))); if (Boolean.parseBoolean(props.getProperty("xmpp.TLSEnabled"))) { xmppConfig.setSecurityMode(SecurityMode.enabled); } else { xmppConfig.setSecurityMode(SecurityMode.disabled); } if (this.props.containsKey("xmpp.truststorePassword")) { xmppConfig.setTruststorePassword(this.props.getProperty("xmpp.truststorePassword")); } else { xmppConfig.setTruststorePassword(TRUST_STORE_PASSWORD); } if (log().isDebugEnabled()) { log().debug("XMPP Manager connection config: " + xmppConfig.toString()); } xmpp = new XMPPConnection(xmppConfig); // Connect to xmpp server connectToServer(); } finally { ThreadCategory.setPrefix(oldPrefix); } }