Package org.jivesoftware.smack

Examples of org.jivesoftware.smack.XMPPConnection


            InterruptedException {
        ConnectionConfiguration localConnectionConfiguration = new ConnectionConfiguration("localhost", 5222);
        localConnectionConfiguration.setKeystorePath(keystorePath);
        localConnectionConfiguration.setTruststorePath(keystorePath);
        localConnectionConfiguration.setTruststorePassword(keystorePassword);
        XMPPConnection localClient = new XMPPConnection(localConnectionConfiguration);

        localClient.connect();
        localClient.login(localUser.getNode(), "password1");
        localClient.addPacketListener(new PacketListener() {
            public void processPacket(Packet packet) {
                System.out.println("# " + packet);
            }
        }, new PacketFilter() {
            public boolean accept(Packet arg0) {
                return true;
            }
        });
       
       
        ConnectionConfiguration remoteConnectionConfiguration = new ConnectionConfiguration(remoteServer.getFullQualifiedName(), 5222);
        remoteConnectionConfiguration.setKeystorePath(keystorePath);
        remoteConnectionConfiguration.setTruststorePath(keystorePath);
        remoteConnectionConfiguration.setTruststorePassword(keystorePassword);
        XMPPConnection remoteClient = new XMPPConnection(remoteConnectionConfiguration);

        remoteClient.connect();
        remoteClient.login(remoteUser.getNode(), remotePassword);
       
        Thread.sleep(3000);
       
        Message msg = new Message(remoteUser.getFullQualifiedName());
//        Message msg = new Message(localUser.getFullQualifiedName());
        msg.setBody("Hello world");
       
        localClient.sendPacket(msg);
//        remoteClient.sendPacket(msg);
       
       
        Thread.sleep(8000);
        remoteClient.disconnect();
        localClient.disconnect();
    }
View Full Code Here


        connectionConfiguration.setKeystorePath("src/main/config/bogus_mina_tls.cert");
        connectionConfiguration.setTruststorePath("src/main/config/bogus_mina_tls.cert");
        connectionConfiguration.setTruststorePassword("boguspw");

        XMPPConnection.DEBUG_ENABLED = true;
        XMPPConnection client = new XMPPConnection(connectionConfiguration);

        client.connect();

        client.login(username, password);
        return client;
    }
View Full Code Here

        config.setCompressionEnabled(true);
        config.setSASLAuthenticationEnabled(tls);
        config.setSelfSignedCertificateEnabled(true);


        conn = new XMPPConnection(config);
        try {
            conn.connect();
            conn.login(username, password, "Freedomotic");
            // Create a new presence. Pass in false to indicate we're unavailable.
            Presence presence = new Presence(Presence.Type.available);
View Full Code Here

    public void start() throws JBIException {
        try {
            if (connection == null) {
                if (port > 0) {
                    connection = new XMPPConnection(host, port);
                }
                else {
                    connection = new XMPPConnection(host);
                }
            }
            if (login && !connection.isAuthenticated()) {
                if (user != null) {
                    AccountManager accountManager = new AccountManager(connection);
View Full Code Here

    if(XMPPConstants.XMPP_SERVER_TYPE_JABBER.equals(serverCredentials.getServerType())){
      ConnectionListener connectionListener = null;
      try
      {
        //XMPPConnection.DEBUG_ENABLED = true;
        xmppConnection = new XMPPConnection(serverCredentials.getServerUrl());
        xmppConnection.connect();
        connectionListener = new ConnectionListener(){

          public void connectionClosed() {
            log.debug("Connection closed normally");
          }

          public void connectionClosedOnError(
              Exception e1) {
            log.debug("Connection to "+serverCredentials.getServerUrl()
                + " closed with error.",e1);
            log.debug("Retrying to connect in 2 secs");
            try
            {
              Thread.sleep(2000);
              xmppConnection = new XMPPConnection(serverCredentials.getServerUrl());
              log.debug("connected to "+serverCredentials.getServerUrl());
            } catch (InterruptedException e2) {
              log.debug("Sleep interrupted.",e2);
            }
          }

          public void reconnectingIn(int seconds) {     
          }

          public void reconnectionFailed(Exception e) {
          }

          public void reconnectionSuccessful() {
          }
        };
        xmppConnection.addConnectionListener(connectionListener);
      }
      catch(XMPPException e){
        log.error("Failed to connect to server :"+serverCredentials.getServerUrl(), e);
        throw new AxisFault("Failed to connect to server :"+serverCredentials.getServerUrl());
      }

      //Pause for a small time before trying to login.
      //This prevents random ssl exception from Smack API
      try {
        Thread.sleep(100);
      } catch (InterruptedException e5) {
        log.debug("Sleep interrupted ",e5);
      }

      if(xmppConnection.isConnected()){
        if(! xmppConnection.isAuthenticated()){
          try {
            xmppConnection.login(serverCredentials.getAccountName()+"@"+
                serverCredentials.getServerUrl(),
                serverCredentials.getPassword(),
                serverCredentials.getResource(),
                true);
          } catch (XMPPException e) {
            try {
              log.error("Login failed for "
                  +serverCredentials.getAccountName()
                  +"@"+serverCredentials.getServerUrl()
                  +".Retrying in 2 secs",e);
              Thread.sleep(2000);
              xmppConnection.login(serverCredentials.getAccountName()+"@"+
                  serverCredentials.getServerUrl(),
                  serverCredentials.getPassword(),
                  serverCredentials.getResource(),
                  true);
            } catch (InterruptedException e1) {
              log.error("Sleep interrupted.",e1);
            } catch (XMPPException e2) {
              log.error("Login failed for : "+serverCredentials.getAccountName()
                  +"@"+serverCredentials.getServerUrl(),e2);
              throw new AxisFault("Login failed for : "+serverCredentials.getAccountName()
                  +"@"+serverCredentials.getServerUrl());
            }
          }
          //Listen for Message type packets from specified server url
          //packetFilter = new AndFilter(new PacketTypeFilter(Message.class),
          //    new FromContainsFilter(serverCredentials.getServerUrl()));
          packetFilter = new FromContainsFilter(serverCredentials.getServerUrl());         
        }
      }
    }else if(XMPPConstants.XMPP_SERVER_TYPE_GOOGLETALK.equals(serverCredentials.getServerType())){
      try {
        ConnectionConfiguration connectionConfiguration =
          new ConnectionConfiguration(XMPPConstants.GOOGLETALK_URL
            ,XMPPConstants.GOOGLETALK_PORT
            ,XMPPConstants.GOOGLETALK_SERVICE_NAME);       
        xmppConnection = new XMPPConnection(connectionConfiguration);
        xmppConnection.connect();

        xmppConnection.login(serverCredentials.getAccountName()
            , serverCredentials.getPassword()
            ,serverCredentials.getResource(),
View Full Code Here

     * @param msgCtx the axis2 message context
     * @throws AxisFault on error
     */
    public void sendMessage(MessageContext msgCtx, String targetAddress,
        OutTransportInfo outTransportInfo) throws AxisFault {
    XMPPConnection xmppConnection = null;
    XMPPOutTransportInfo xmppOutTransportInfo = null;
   
    //if on client side,create connection to xmpp server
    if(!msgCtx.isServerSide()){
      connectUsingClientOptions(msgCtx);
    }
   
    Message message = new Message();
    Options options = msgCtx.getOptions();     
      String serviceName = XMPPUtils.getServiceName(targetAddress);     
     
    if (targetAddress != null) {
      xmppOutTransportInfo = new XMPPOutTransportInfo(targetAddress);
      xmppOutTransportInfo.setConnectionFactory(connectionFactory);
    } else if (msgCtx.getTo() != null &&
        !msgCtx.getTo().hasAnonymousAddress()) {
      //TODO
    } else if (msgCtx.isServerSide()) {
      xmppOutTransportInfo = (XMPPOutTransportInfo)
      msgCtx.getProperty(Constants.OUT_TRANSPORT_INFO);
    }
     
     
    if(msgCtx.isServerSide()){
      xmppConnection = xmppOutTransportInfo.getConnectionFactory().getXmppConnection();
      message.setProperty(XMPPConstants.IS_SERVER_SIDE, new Boolean(false));
      message.setProperty(XMPPConstants.IN_REPLY_TO, xmppOutTransportInfo.getInReplyTo());
    }else{
      xmppConnection = xmppOutTransportInfo.getConnectionFactory().getXmppConnection();
      message.setProperty(XMPPConstants.IS_SERVER_SIDE, new Boolean(true));
      message.setProperty(XMPPConstants.SERVICE_NAME, serviceName);
      message.setProperty(XMPPConstants.ACTION, options.getAction());
    }
   
      if(xmppConnection == null){
        handleException("Connection to XMPP Server is not established.");       
      }
   
    //initialize the chat manager using connection
    ChatManager chatManager = xmppConnection.getChatManager();
    Chat chat = chatManager.createChat(xmppOutTransportInfo.getDestinationAccount(), null);   
   
    try
    {
      OMElement msgElement = msgCtx.getEnvelope();
      if (msgCtx.isDoingREST()) {
        msgElement = msgCtx.getEnvelope().getBody().getFirstElement();
      }
      boolean waitForResponse =
        msgCtx.getOperationContext() != null &&
        WSDL2Constants.MEP_URI_OUT_IN.equals(
            msgCtx.getOperationContext().getAxisOperation().getMessageExchangePattern());
     
     
      String soapMessage = msgElement.toString();
      //int endOfXMLDeclaration = soapMessage.indexOf("?>");
      //String modifiedSOAPMessage = soapMessage.substring(endOfXMLDeclaration+2);
      message.setBody(soapMessage)
     
      XMPPClientSidePacketListener xmppClientSidePacketListener = null;
      if(waitForResponse && !msgCtx.isServerSide()){
        PacketFilter filter = new PacketTypeFilter(message.getClass());       
        xmppClientSidePacketListener = new XMPPClientSidePacketListener(msgCtx);
        xmppConnection.addPacketListener(xmppClientSidePacketListener,filter);
      }     

      chat.sendMessage(message);
      log.debug("Sent message :"+message.toXML());

      //If this is on client side, wait for the response from server.
      //Is this the best way to do this?
      if(waitForResponse && !msgCtx.isServerSide()){
        while(! xmppClientSidePacketListener.isResponseReceived()){
          try {
            Thread.sleep(1000);
          } catch (InterruptedException e) {
            log.debug("Sleep interrupted",e);
          }   
        }
        xmppConnection.disconnect();
      }

    } catch (XMPPException e) {
      log.error("Error occurred while sending the message : "+message.toXML(),e);
      handleException("Error occurred while sending the message : "+message.toXML(),e);
    }finally{
      if(!msgCtx.isServerSide()){
        xmppConnection.disconnect();
      }
    }
    } 
View Full Code Here

       //connConfig.setSecurityMode(SecurityMode.disabled);
       connConfig.setSecurityMode(SecurityMode.required);
             connConfig.setSASLAuthenticationEnabled(false);
             connConfig.setCompressionEnabled(false);
              try {
                XMPPConnection xc = new XMPPConnection(connConfig);
          xc.connect(); // 建立和server端的额连接
          this.setConnection(xc);
          // namespace为androidpn:iq:notification
          ProviderManager.getInstance().addIQProvider("notification",
              "androidpn:iq:notification",new NotificationIQProvider());
      } catch (Exception e) {
View Full Code Here

        initComponents();
    }

    public void connectToServerNoPort(String strIP){

        this.xmppconnection = new XMPPConnection(strIP);

        try {
            xmppconnection.connect();
            System.out.println("connection successful");
View Full Code Here

                ConnectionConfiguration conConfig = new ConnectionConfiguration(txtServerIP.getText(), Integer.parseInt(txtPort.getText()));
                conConfig.setSASLAuthenticationEnabled(true);
                SASLAuthentication.supportSASLMechanism("PLAIN", 0);
                conConfig.setReconnectionAllowed(true);

                xmppconnection = new XMPPConnection(conConfig);
                xmppconnection.connect();
                blnLogin = true;
            }
            else{
                blnLogin = false;
View Full Code Here

        config.setCompressionEnabled(Boolean.getBoolean("test.compressionEnabled"));
        config.setSendPresence(sendInitialPresence());
        if (getSocketFactory() == null) {
            config.setSocketFactory(getSocketFactory());
        }
        return new XMPPConnection(config);
    }
View Full Code Here

TOP

Related Classes of org.jivesoftware.smack.XMPPConnection

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.