org.jivesoftware.smack.PacketCollector Java Examples

The following examples show how to use org.jivesoftware.smack.PacketCollector. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: RemoteSessionCountOverXMPP.java    From olat with Apache License 2.0 5 votes vote down vote up
/**
*/
  @Override
  public int countSessions() {
      final XMPPConnection con = adminUser.getConnection();
      if (con != null && con.isConnected()) {
          // TODO:gs may put in other thread???
          SessionCount response;
          try {
              final IQ packet = new SessionCount();
              packet.setFrom(con.getUser());
              final PacketCollector collector = con.createPacketCollector(new PacketIDFilter(packet.getPacketID()));
              con.sendPacket(packet);
              response = (SessionCount) collector.nextResult(SmackConfiguration.getPacketReplyTimeout());
              collector.cancel();
              if (response == null) {
                  log.warn("Error while trying to count sessions at IM server. Response was null!");
                  return sessionCount;
              }
              if (response.getError() != null) {
                  log.warn("Error while trying to count sessions at IM server. " + response.getError().getMessage());
                  return sessionCount;
              } else if (response.getType() == IQ.Type.ERROR) {
                  log.warn("Error while trying to count sessions at IM server");
                  return sessionCount;
              }
              sessionCount = response.getNumberOfSessions();
              if (sessionCount > 0) {
                  return sessionCount - 1;
              }
              return sessionCount;

          } catch (final Exception e) {
              log.warn("Error while trying to count sessions at IM server. Response was null!", e);
              return sessionCount;
          }

      }
      return sessionCount;
  }
 
Example #2
Source File: RemoteGroupCreationOverXMPP.java    From olat with Apache License 2.0 5 votes vote down vote up
private boolean sendPacket(final IQ packet) {
    final XMPPConnection con = adminUser.getConnection();
    try {
        packet.setFrom(con.getUser());
        final PacketCollector collector = con.createPacketCollector(new PacketIDFilter(packet.getPacketID()));
        con.sendPacket(packet);
        final IQ response = (IQ) collector.nextResult(SmackConfiguration.getPacketReplyTimeout());
        collector.cancel();

        if (response == null) {
            log.error("Error while trying to create/delete group at IM server. Response was null! packet type: " + packet.getClass());
            return false;
        }
        if (response.getError() != null) {
            if (response.getError().getCode() == 409) {
                // 409 -> conflict / group already exists
                return true;
            } else if (response.getError().getCode() == 404) {
                // 404 -> not found, does not matter when trying to delete
                return true;
            }
            log.error("Error while trying to create/delete group at IM server. " + response.getError().getMessage());
            return false;
        } else if (response.getType() == IQ.Type.ERROR) {
            log.error("Error while trying to create/delete group at IM server");
            return false;
        }
        return true;
    } catch (final RuntimeException e) {
        log.error("Error while trying to create/delete group at IM server");
        return false;
    }
}
 
Example #3
Source File: RemotePluginVersionOverXMPP.java    From olat with Apache License 2.0 5 votes vote down vote up
/**
*/
  @Override
  public String getPluginVersion() {
      final XMPPConnection con = adminUser.getConnection();
      if (con != null && con.isConnected()) {
          PluginVersion response;
          try {
              final IQ packet = new PluginVersion();
              packet.setFrom(con.getUser());
              final PacketCollector collector = con.createPacketCollector(new PacketIDFilter(packet.getPacketID()));
              // TODO:gs is sending packets over one connection thread save?
              con.sendPacket(packet);
              response = (PluginVersion) collector.nextResult(SmackConfiguration.getPacketReplyTimeout());
              collector.cancel();
              if (response == null) {
                  log.error("Error while trying to get version at IM server. Response was null!");
                  return version;
              }
              if (response.getError() != null) {
                  log.error("Error while trying to get version at IM server. " + response.getError().getMessage());
                  return version;
              } else if (response.getType() == IQ.Type.ERROR) {
                  log.error("Error while trying to get version at IM server");
                  return version;
              }
              return response.getVersion();

          } catch (final Exception e) {
              log.error("Error while trying to get version at IM server. Response was null!", e);
              return version;
          }

      }
      return version;
  }
 
Example #4
Source File: RemoteSessionCountOverXMPP.java    From olat with Apache License 2.0 5 votes vote down vote up
/**
*/
  @Override
  public int countSessions() {
      final XMPPConnection con = adminUser.getConnection();
      if (con != null && con.isConnected()) {
          // TODO:gs may put in other thread???
          SessionCount response;
          try {
              final IQ packet = new SessionCount();
              packet.setFrom(con.getUser());
              final PacketCollector collector = con.createPacketCollector(new PacketIDFilter(packet.getPacketID()));
              con.sendPacket(packet);
              response = (SessionCount) collector.nextResult(SmackConfiguration.getPacketReplyTimeout());
              collector.cancel();
              if (response == null) {
                  log.warn("Error while trying to count sessions at IM server. Response was null!");
                  return sessionCount;
              }
              if (response.getError() != null) {
                  log.warn("Error while trying to count sessions at IM server. " + response.getError().getMessage());
                  return sessionCount;
              } else if (response.getType() == IQ.Type.ERROR) {
                  log.warn("Error while trying to count sessions at IM server");
                  return sessionCount;
              }
              sessionCount = response.getNumberOfSessions();
              if (sessionCount > 0) {
                  return sessionCount - 1;
              }
              return sessionCount;

          } catch (final Exception e) {
              log.warn("Error while trying to count sessions at IM server. Response was null!", e);
              return sessionCount;
          }

      }
      return sessionCount;
  }
 
Example #5
Source File: RemoteGroupCreationOverXMPP.java    From olat with Apache License 2.0 5 votes vote down vote up
private boolean sendPacket(final IQ packet) {
    final XMPPConnection con = adminUser.getConnection();
    try {
        packet.setFrom(con.getUser());
        final PacketCollector collector = con.createPacketCollector(new PacketIDFilter(packet.getPacketID()));
        con.sendPacket(packet);
        final IQ response = (IQ) collector.nextResult(SmackConfiguration.getPacketReplyTimeout());
        collector.cancel();

        if (response == null) {
            log.error("Error while trying to create/delete group at IM server. Response was null! packet type: " + packet.getClass());
            return false;
        }
        if (response.getError() != null) {
            if (response.getError().getCode() == 409) {
                // 409 -> conflict / group already exists
                return true;
            } else if (response.getError().getCode() == 404) {
                // 404 -> not found, does not matter when trying to delete
                return true;
            }
            log.error("Error while trying to create/delete group at IM server. " + response.getError().getMessage());
            return false;
        } else if (response.getType() == IQ.Type.ERROR) {
            log.error("Error while trying to create/delete group at IM server");
            return false;
        }
        return true;
    } catch (final RuntimeException e) {
        log.error("Error while trying to create/delete group at IM server");
        return false;
    }
}
 
Example #6
Source File: RemotePluginVersionOverXMPP.java    From olat with Apache License 2.0 5 votes vote down vote up
/**
*/
  @Override
  public String getPluginVersion() {
      final XMPPConnection con = adminUser.getConnection();
      if (con != null && con.isConnected()) {
          PluginVersion response;
          try {
              final IQ packet = new PluginVersion();
              packet.setFrom(con.getUser());
              final PacketCollector collector = con.createPacketCollector(new PacketIDFilter(packet.getPacketID()));
              // TODO:gs is sending packets over one connection thread save?
              con.sendPacket(packet);
              response = (PluginVersion) collector.nextResult(SmackConfiguration.getPacketReplyTimeout());
              collector.cancel();
              if (response == null) {
                  log.error("Error while trying to get version at IM server. Response was null!");
                  return version;
              }
              if (response.getError() != null) {
                  log.error("Error while trying to get version at IM server. " + response.getError().getMessage());
                  return version;
              } else if (response.getType() == IQ.Type.ERROR) {
                  log.error("Error while trying to get version at IM server");
                  return version;
              }
              return response.getVersion();

          } catch (final Exception e) {
              log.error("Error while trying to get version at IM server. Response was null!", e);
              return version;
          }

      }
      return version;
  }
 
Example #7
Source File: XmppConnectReceiver.java    From AndroidPNClient with Apache License 2.0 5 votes vote down vote up
public boolean register(String user, String pass) {
    Log.i(LOG_TAG, "RegisterTask.run()...");

    if (xmppManager.isRegistered()) {
        Log.i(LOG_TAG, "Account registered already");
        return true;
    }

    final Registration registration = new Registration();

    PacketFilter packetFilter = new AndFilter(new PacketIDFilter(
            registration.getPacketID()), new PacketTypeFilter(
            IQ.class));

    PacketCollector collector = xmppManager.getConnection().createPacketCollector(packetFilter);
    registration.setType(IQ.Type.SET);
    registration.addAttribute("username", user);
    registration.addAttribute("password", pass);
    if (xmppManager.getConnection().isConnected()) {
        xmppManager.getConnection().sendPacket(registration);
        IQ result = (IQ) collector.nextResult(REGISTER_TIME_OUT);
        collector.cancel();
        if(result == null) {
            Log.d(LOG_TAG, "The server didn't return result after 60 seconds.");
            return false;
        } else if (result.getType() == IQ.Type.ERROR) {
            if(result.getError().toString().contains("409")) {
                return true;
            } else {
                return false;
            }
        } else if (result.getType() == IQ.Type.RESULT) {
            return true;
        }
        return false;
    } else {
        Log.d(LOG_TAG, "connection is not connected");
        return false;
    }
}
 
Example #8
Source File: XmppConnection.java    From weixin with Apache License 2.0 5 votes vote down vote up
/**
 * 注册
 * 
 * @param account 注册帐号
 * @param password 注册密码
 * @return 1、注册成功 0、服务器没有返回结果2、这个账号已经存在3、注册失败
 */
public String regist(String account, String password) {
	if (getConnection() == null)
		return "0";
	Registration reg = new Registration();
	reg.setType(IQ.Type.SET);
	reg.setTo(getConnection().getServiceName());
	// 注意这里createAccount注册时,参数是UserName,不是jid,是"@"前面的部分。
	reg.setUsername(account);
	reg.setPassword(password);
	// 这边addAttribute不能为空,否则出错。所以做个标志是android手机创建的吧!!!!!
	reg.addAttribute("android", "geolo_createUser_android");
	PacketFilter filter = new AndFilter(new PacketIDFilter(reg.getPacketID()), new PacketTypeFilter(IQ.class));
	PacketCollector collector = getConnection().createPacketCollector(filter);
	getConnection().sendPacket(reg);
	IQ result = (IQ) collector.nextResult(SmackConfiguration.getPacketReplyTimeout());
	// Stop queuing results停止请求results(是否成功的结果)
	collector.cancel();
	if (result == null) {
		Log.e("regist", "No response from server.");
		return "0";
	} else if (result.getType() == IQ.Type.RESULT) {
		Log.v("regist", "regist success.");
		return "1";
	} else { // if (result.getType() == IQ.Type.ERROR)
		if (result.getError().toString().equalsIgnoreCase("conflict(409)")) {
			Log.e("regist", "IQ.Type.ERROR: " + result.getError().toString());
			return "2";
		} else {
			Log.e("regist", "IQ.Type.ERROR: " + result.getError().toString());
			return "3";
		}
	}
}
 
Example #9
Source File: XmppManager.java    From weixin with Apache License 2.0 5 votes vote down vote up
/**
 * 注册
 * 
 * @param account 注册帐号
 * @param password 注册密码
 * @return 0、 服务器没有返回结果<br>
 *         1、注册成功 <br>
 *         2、这个帐号已经存在 <br>
 *         3、注册失败
 */
public String regist(String account, String password) {
	if (!isConnected()) {
		return "0";
	}
	Registration reg = new Registration();
	reg.setType(IQ.Type.SET);
	reg.setTo(getConnection().getServiceName());
	reg.setUsername(account);
	reg.setPassword(password);
	reg.addAttribute("android", "geolo_createUser_android");

	//数据包过滤器
	PacketFilter filter = new AndFilter(new PacketIDFilter(reg.getPacketID()), new PacketTypeFilter(IQ.class));
	PacketCollector collector = getConnection().createPacketCollector(filter);
	getConnection().sendPacket(reg);

	IQ result = (IQ) collector.nextResult(SmackConfiguration.getPacketReplyTimeout());
	//停止请求result(是否成功的结果)
	collector.cancel();
	if (result == null) {
		L.e(LOGTAG, "No response from server.");
		return "0";
	} else if (result.getType() == IQ.Type.RESULT) {
		return "1";
	} else {
		if (result.getError().toString().equalsIgnoreCase("conflict(409)")) {
			L.e(LOGTAG, "IQ.Type.ERROR: " + result.getError().toString());
			return "2";
		} else {
			L.e(LOGTAG, "IQ.Type.ERROR: " + result.getError().toString());
			return "3";
		}
	}
}
 
Example #10
Source File: XSCHelper.java    From PracticeCode with Apache License 2.0 4 votes vote down vote up
/**
 * 注册。网络操作不可以在主线程中进行。多线程无法等待return,所以必须使用Handler
 *
 * @param account  登陆账户
 * @param password 登录密码
 * @param handler  处理信息的Handler
 */
public void regist(final String account, final String password, final Handler handler) {
    new Thread(){
        @Override
        public void run() {
            //如果无法连接服务器,Handle Error
            if (getConnection() == null) {
                Log.e("------->", "ERROR");
                handler.sendEmptyMessage(ERROR);
                return;
            }

            //如果已经是登录状态,Handle Failure
            if (getConnection().isAuthenticated()) {
                Log.e("------->", "hasAuthenticated");
                handler.sendEmptyMessage(FAILURE);
                return;
            }

            //如果状态正常则配置注册信息
            Registration reg = new Registration();
            reg.setType(IQ.Type.SET);
            reg.setTo(getConnection().getServiceName());
            reg.setUsername(account);
            reg.setPassword(password);
            reg.addAttribute("H-S-A-2015", "ThronBird");

            //配置Filter过滤器,注明包ID信息
            PacketFilter filter = new AndFilter(
                    new PacketIDFilter(reg.getPacketID()),
                    new PacketTypeFilter(IQ.class));

            //用Filter过滤器配置Collector接受器以便接受信息,并发送封包
            PacketCollector collector =
                    getConnection().createPacketCollector(filter);
            getConnection().sendPacket(reg);

            //等待信息,注明超时时间
            IQ result = (IQ) collector.nextResult(
                    SmackConfiguration.getPacketReplyTimeout());

            //接收到信息就可以取消掉Collector了,处理接受的信息
            collector.cancel();
            if(result == null) {
                handler.sendEmptyMessage(ERROR);
                return;
            }

            if(result.getType() == IQ.Type.RESULT) {
                handler.sendEmptyMessage(SUCCESS);
                return;
            }

            //以上是超时或者成功的情况,一下是失败的情况(已有/失败)
            if(result.getError().toString().equalsIgnoreCase("conflict(409)")) {
                handler.sendEmptyMessage(EXIST);
                return;
            }

            handler.sendEmptyMessage(FAILURE);

        }
    }.run();
}
 
Example #11
Source File: RemoteAccountCreationOverXMPP.java    From olat with Apache License 2.0 4 votes vote down vote up
private boolean sendPacket(final IQ packet) {
    final XMPPConnection con = adminUser.getConnection();
    try {
        packet.setFrom(con.getUser());
        final PacketCollector collector = con.createPacketCollector(new PacketIDFilter(packet.getPacketID()));
        con.sendPacket(packet);
        final IQ response = (IQ) collector.nextResult(SmackConfiguration.getPacketReplyTimeout());
        collector.cancel();

        if (response == null) {
            // OLAT-5384: error happens frequently, lowering to WARN
            log.warn("Error while trying to create/delete user at IM server. Response was null!");
            return false;
        }
        if (response.getError() != null) {
            if (response.getError().getCode() == 503) {
                // 503 code means service not available, IM server plugin may not installed
                log.error("Openfire and OLAT talk over an custom Openfire plugin. Please make sure you have it installed! "
                        + "Download it under http://www.olat.org/downloads/stable/olatUserAndGroupService.jar");
            } else if (response.getError().getCode() == 407 || response.getError().getCode() == 409) {
                // 407 or 409 -> conflict / user already exists
                return true;
            } else if (response.getError().getCode() == 404) {
                // 404 -> user not found, ok when trying to delete
                return true;
            }
            log.warn("Error while trying to create/delete user at IM server. Errorcode: " + response.getError().getCode());
            return false;
        } else if (response.getType() == IQ.Type.ERROR) {
            log.error("Error while trying to create/delete user at IM server. Response type error");
            return false;
        }
        if (response instanceof UserCheck) {
            final UserCheck check = (UserCheck) response;
            return check.hasAccount();
        }
        return true;
    } catch (final Exception e) {
        log.error("Error while trying to create/delete user at IM server", e);
        return false;
    }
}
 
Example #12
Source File: RemoteAccountCreationOverXMPP.java    From olat with Apache License 2.0 4 votes vote down vote up
private boolean sendPacket(final IQ packet) {
    final XMPPConnection con = adminUser.getConnection();
    try {
        packet.setFrom(con.getUser());
        final PacketCollector collector = con.createPacketCollector(new PacketIDFilter(packet.getPacketID()));
        con.sendPacket(packet);
        final IQ response = (IQ) collector.nextResult(SmackConfiguration.getPacketReplyTimeout());
        collector.cancel();

        if (response == null) {
            // OLAT-5384: error happens frequently, lowering to WARN
            log.warn("Error while trying to create/delete user at IM server. Response was null!");
            return false;
        }
        if (response.getError() != null) {
            if (response.getError().getCode() == 503) {
                // 503 code means service not available, IM server plugin may not installed
                log.error("Openfire and OLAT talk over an custom Openfire plugin. Please make sure you have it installed! "
                        + "Download it under http://www.olat.org/downloads/stable/olatUserAndGroupService.jar");
            } else if (response.getError().getCode() == 407 || response.getError().getCode() == 409) {
                // 407 or 409 -> conflict / user already exists
                return true;
            } else if (response.getError().getCode() == 404) {
                // 404 -> user not found, ok when trying to delete
                return true;
            }
            log.warn("Error while trying to create/delete user at IM server. Errorcode: " + response.getError().getCode());
            return false;
        } else if (response.getType() == IQ.Type.ERROR) {
            log.error("Error while trying to create/delete user at IM server. Response type error");
            return false;
        }
        if (response instanceof UserCheck) {
            final UserCheck check = (UserCheck) response;
            return check.hasAccount();
        }
        return true;
    } catch (final Exception e) {
        log.error("Error while trying to create/delete user at IM server", e);
        return false;
    }
}