Package org.jivesoftware.smack

Examples of org.jivesoftware.smack.XMPPConnection


            throw new IMException(
                    "The format of the userID is incorrect. The userID must be of the form userName@jabberServer");
        }
        String username = userID.substring(0, index);
        String serverName = userID.substring(index + 1);
        connection = new XMPPConnection(serverName);
        try {
            connection.connect();
            connection.login(username, password);
        } catch (XMPPException e) {
            loginProcessed = true;
View Full Code Here


     * @throws AxisFault on error
     */
    public void sendMessage(MessageContext msgCtx, String targetAddress,
           
        OutTransportInfo outTransportInfo) throws AxisFault {
    XMPPConnection xmppConnection = null;
    XMPPOutTransportInfo xmppOutTransportInfo = null;
    XMPPConnectionFactory connectionFactory;
   
    //if on client side,create connection to xmpp server
    if(msgCtx.isServerSide()){
        xmppOutTransportInfo = (XMPPOutTransportInfo)msgCtx.getProperty(org.apache.axis2.Constants.OUT_TRANSPORT_INFO);
        connectionFactory = xmppOutTransportInfo.getConnectionFactory();
    }else{
        getConnectionDetailsFromClientOptions(msgCtx);
        connectionFactory = defaultConnectionFactory;
    }
   
    synchronized (this) {
            xmppConnection = connectionFactory.getXmppConnection();
            if(xmppConnection == null){
                connectionFactory.connect(serverCredentials);  
                xmppConnection = connectionFactory.getXmppConnection();
            }
        }
   
    Message message = new Message();
    Options options = msgCtx.getOptions();     
      String serviceName = XMPPUtils.getServiceName(targetAddress);   
     
      SOAPVersion version = msgCtx.getEnvelope().getVersion();
      if(version instanceof SOAP12Version){
        message.setProperty(XMPPConstants.CONTENT_TYPE, HTTPConstants.MEDIA_TYPE_APPLICATION_SOAP_XML+ "; action="+ msgCtx.getSoapAction());
      }else{
        message.setProperty(XMPPConstants.CONTENT_TYPE, HTTPConstants.MEDIA_TYPE_TEXT_XML);
      }
     
     
    if (targetAddress != null) {
      xmppOutTransportInfo = new XMPPOutTransportInfo(targetAddress);
      xmppOutTransportInfo.setConnectionFactory(defaultConnectionFactory);
    } else if (msgCtx.getTo() != null &&
        !msgCtx.getTo().hasAnonymousAddress()) {
      //TODO
    } else if (msgCtx.isServerSide()) {
      xmppOutTransportInfo = (XMPPOutTransportInfo)
      msgCtx.getProperty(Constants.OUT_TRANSPORT_INFO);
    }
    try{
    if(msgCtx.isServerSide()){
      message.setProperty(XMPPConstants.IS_SERVER_SIDE, new Boolean(false));
      message.setProperty(XMPPConstants.IN_REPLY_TO, xmppOutTransportInfo.getInReplyTo());
      message.setProperty(XMPPConstants.SEQUENCE_ID, xmppOutTransportInfo.getSequenceID());
    }else{
      //message is going to be processed on server side
      message.setProperty(XMPPConstants.IS_SERVER_SIDE,new Boolean(true));
      //we are sending a soap envelope as a message
      message.setProperty(XMPPConstants.CONTAINS_SOAP_ENVELOPE, new Boolean(true));
      message.setProperty(XMPPConstants.SERVICE_NAME, serviceName);
      String action = options.getAction();
      if (action == null) {
        AxisOperation axisOperation = msgCtx.getAxisOperation();
        if (axisOperation != null) {
          action = axisOperation.getSoapAction();
        }
      }
      if (action != null) {
        message.setProperty(XMPPConstants.ACTION, action);
      }
    }   
      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);
   
      boolean waitForResponse =
        msgCtx.getOperationContext() != null &&
        WSDL2Constants.MEP_URI_OUT_IN.equals(
            msgCtx.getOperationContext().getAxisOperation().getMessageExchangePattern());
     
      OMElement msgElement;     
      String messageToBeSent = "";
     
      if(XMPPConstants.XMPP_CONTENT_TYPE_STRING.equals(xmppOutTransportInfo.getContentType())){
        //if request is received from a chat client, whole soap envelope
        //should not be sent.
        OMElement soapBodyEle = msgCtx.getEnvelope().getBody();
        OMElement responseEle = soapBodyEle.getFirstElement();
        if(responseEle != null){
          msgElement = responseEle.getFirstElement();         
        }else{
          msgElement = responseEle;
        }
      }else{
        //if request received from a ws client whole soap envelope
        //must be sent.
        msgElement = msgCtx.getEnvelope();
     
      messageToBeSent = msgElement.toString();
      message.setBody(messageToBeSent);
     
      String key = null;
      if(waitForResponse && !msgCtx.isServerSide()){
        PacketFilter filter = new PacketTypeFilter(message.getClass());       
        xmppConnection.addPacketListener(xmppClientSidePacketListener,filter);
        key = UUID.randomUUID().toString();
        xmppClientSidePacketListener.listenForResponse(key, msgCtx);
        message.setProperty(XMPPConstants.SEQUENCE_ID, key);
      }     

View Full Code Here

     * @param msgCtx
     * @param responseMsg
     * @throws AxisFault
     */
    private static void sendChatMessage(MessageContext msgCtx,String responseMsg) throws AxisFault {
        XMPPConnection xmppConnection = null;
        XMPPOutTransportInfo xmppOutTransportInfo = null;       
        Message message = new Message();
         
         xmppOutTransportInfo = (XMPPOutTransportInfo)msgCtx.getProperty(Constants.OUT_TRANSPORT_INFO);
         if(xmppOutTransportInfo != null){
           message.setProperty(XMPPConstants.IN_REPLY_TO, xmppOutTransportInfo.getInReplyTo());
             xmppConnection = xmppOutTransportInfo.getConnectionFactory().getXmppConnection();
               if(xmppConnection == null){
                 handleException("Connection to XMPP Server is not established.");       
               }            
         }else{
           handleException("Could not find message sender details.");
         }        
       
        //initialize the chat manager using connection
        ChatManager chatManager = xmppConnection.getChatManager();
        Chat chat = chatManager.createChat(xmppOutTransportInfo.getDestinationAccount(), null);       
        try{        
            message.setProperty(XMPPConstants.SEQUENCE_ID,
                    xmppOutTransportInfo.getSequenceID());
           message.setBody(responseMsg);   
View Full Code Here

            if (SSL) {
              xmppConf.setSecurityMode(ConnectionConfiguration.SecurityMode.enabled);
            }else {
              xmppConf.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled);
            }
            con = new XMPPConnection(xmppConf);

            // Most servers require you to login before performing other tasks
            LogLog.debug("About to login as [" + username + "/" + password + "]");
            con.login(username, password);
View Full Code Here

    }.start();
  }
  public boolean connect() {
    try {
      //XMPPConnection.DEBUG_ENABLED = true;
      connection = new XMPPConnection(host,port,StringUtils.parseServer(jid));
      presences = new HashMap<String,PresenceWrapper>();
      connection.addPacketListener(new PacketListener() {
        public void processPacket(Packet packet) {
          System.out.println("Received packet: " + packet.toXML());
          if(!(packet instanceof Presence)) return;
View Full Code Here

    {
        ConnectionConfiguration connectionConfig = new ConnectionConfiguration(host);
        // no roster required
        connectionConfig.setRosterLoadedAtLogin(false);
       
        connection = new XMPPConnection(connectionConfig);
        connection.connect();
        if (logger.isDebugEnabled())
        {
            logger.debug("connected to " + host);
        }
View Full Code Here

            connectionConfig = new ConnectionConfiguration(host, port);
        }
        // no need to load the roster (this is not an interactive app)
        connectionConfig.setRosterLoadedAtLogin(false);
       
        connection = new XMPPConnection(connectionConfig);
    }
View Full Code Here

        String password = args[2];
        try {
            // Connect to the server, without TLS encryption.
            ConnectionConfiguration config = new ConnectionConfiguration(server);
            config.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled);
            final XMPPConnection con = new XMPPConnection(config);
            System.out.print("Connecting to " + server + "... ");
            con.connect();

            con.login(username, password, "writer");
            System.out.print("success.");
            System.out.println("");

            // Get the "real" server address.
            server = con.getServiceName();

            String writerAddress = username + "@" + server + "/writer";
            final String readerAddress = username + "@" + server + "/reader";

            System.out.println("Registered as " + writerAddress);

            // Look for the reader process.
            System.out.print("Looking for " + readerAddress + "...");
            while (true) {
                IQ testIQ = new Time();
                testIQ.setType(IQ.Type.GET);
                testIQ.setTo(readerAddress);
                PacketCollector collector = con.createPacketCollector(new PacketIDFilter(testIQ.getPacketID()));
                con.sendPacket(testIQ);
                // Wait 5 seconds.
                long start = System.currentTimeMillis();
                Packet result = collector.nextResult(5000);
                collector.cancel();
                // If we got a result, continue.
                if (result != null && result.getError() == null) {
                    System.out.println(" found reader. Starting packet flood.");
                    break;
                }
                System.out.print(".");
                long end = System.currentTimeMillis();
                if (end - start < 5000) {
                    try {
                        Thread.sleep(5000 - (end-start));
                    }
                    catch (Exception e) {
                        // ignore.
                    }
                }
            }

            // Create a process to log how many packets we're writing out.
            Runnable statsRunnable = new Runnable() {

                public void run() {
                    while (!done) {
                        try {
                            Thread.sleep(5000);
                        }
                        catch (Exception e) { /* ignore */ }
                        int count = packetCount.getAndSet(0);
                        System.out.println("Packets per second: " + (count/5));
                    }
                }
            };
            Thread statsThread = new Thread(statsRunnable);
            statsThread.setDaemon(true);
            statsThread.start();

            // Now start flooding packets.
            Message testMessage = new Message(readerAddress);
            testMessage.setBody("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
            while (!done) {
                con.sendPacket(testMessage);
                packetCount.getAndIncrement();
            }
        }
        catch (Exception e) {
            System.out.println("\nError: " + e.getMessage());
View Full Code Here

   */
  public static void main(String[] args) {
    String jid = args[0];
    String password = args[1];
    XMPPConnection.DEBUG_ENABLED = true;
    XMPPConnection conn;
    try {
      conn = new XMPPConnection(StringUtils.parseServer(jid));
      conn.login(StringUtils.parseName(jid),password);
      System.out.println("Logged In.");
      conn.close();
    } catch (XMPPException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
   
View Full Code Here

  }
  public void createConnection() throws XMPPException {
    if(account.useSSL)
      conn = new SSLXMPPConnection(account.getServer(),account.getPort(),StringUtils.parseServer(account.jid));
    else
      conn = new XMPPConnection(account.getServer(),account.getPort(),StringUtils.parseServer(account.jid));
  }
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.