use of org.xmpp.packet.JID in project Openfire by igniterealtime.
the class UserCreationPlugin method generateMessages.
public void generateMessages() {
JiveGlobals.setProperty("conversation.maxTimeDebug", String.valueOf(DEFAULT_MAX_TIME_DEBUG));
XMPPServer server = XMPPServer.getInstance();
ExecutorService taskExecutor = Executors.newFixedThreadPool(8);
for (User user : UserManager.getInstance().getUsers()) {
final JID userJid = server.createJID(user.getUsername(), null);
System.out.println("Creating messages for user: " + userJid.getNode());
for (RosterItem ri : user.getRoster().getRosterItems()) {
final JID rosterItemJid = ri.getJid();
taskExecutor.execute(new Runnable() {
@Override
public void run() {
for (int j = 0; j < NUMBER_CONVERSATION; j++) {
String thread = RandomStringUtils.randomAlphanumeric(6);
for (int i = 0; i < NUMBER_MESSAGES; i++) {
if (i % 2 == 0) {
Message msg = new Message();
msg.setBody("Hello to " + rosterItemJid.getNode() + " from " + userJid.getNode() + ", conversation number " + j + " of " + NUMBER_CONVERSATION + ", message " + i + " of " + NUMBER_MESSAGES + " thread " + thread);
msg.setType(Message.Type.chat);
msg.setFrom(userJid);
msg.setTo(rosterItemJid);
msg.setThread(thread);
XMPPServer.getInstance().getMessageRouter().route(msg);
try {
/* otherwise monitoring plugin stores messages out of order */
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
} else {
Message msg = new Message();
msg.setBody("Hello to " + userJid.getNode() + " from " + rosterItemJid.getNode() + ", conversation number " + j + " of " + NUMBER_CONVERSATION + ", message " + i + " of " + NUMBER_MESSAGES + " thread " + thread);
msg.setType(Message.Type.chat);
msg.setFrom(rosterItemJid);
msg.setTo(userJid);
msg.setThread(thread);
XMPPServer.getInstance().getMessageRouter().route(msg);
try {
/* otherwise monitoring plugin stores messages out of order */
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
try {
Thread.sleep(DEFAULT_MAX_TIME_DEBUG);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
}
}
taskExecutor.shutdown();
try {
taskExecutor.awaitTermination(2, TimeUnit.HOURS);
System.out.println("Conversation generation finished");
} catch (InterruptedException e) {
e.printStackTrace();
}
JiveGlobals.deleteProperty("conversation.maxTimeDebug");
}
use of org.xmpp.packet.JID in project Openfire by igniterealtime.
the class GroupJIDTest method testParseGroupJIDFromString.
@Test
public void testParseGroupJIDFromString() {
String testGroupName = "Test Group (2); - now with *special* =characters= too!";
JID testJid = new JID(StringUtils.encodeBase32(testGroupName), "localhost", StringUtils.hash(testGroupName));
assertTrue(GroupJID.isGroup(testJid));
assertEquals(testGroupName, ((GroupJID) GroupJID.fromString(testJid.toString())).getGroupName());
}
use of org.xmpp.packet.JID in project Openfire by igniterealtime.
the class MessageRouter method routingFailed.
/**
* Notification message indicating that a packet has failed to be routed to the recipient.
*
* @param recipient address of the entity that failed to receive the packet.
* @param packet Message packet that failed to be sent to the recipient.
*/
public void routingFailed(JID recipient, Packet packet) {
log.debug("Message sent to unreachable address: " + packet.toXML());
final Message msg = (Message) packet;
boolean storeOffline = true;
if (msg.getType().equals(Message.Type.chat) && serverName.equals(recipient.getDomain()) && recipient.getResource() != null) {
// Find an existing AVAILABLE session with non-negative priority.
for (JID address : routingTable.getRoutes(recipient.asBareJID(), packet.getFrom())) {
ClientSession session = routingTable.getClientRoute(address);
if (session != null && session.isInitialized()) {
if (session.getPresence().getPriority() >= 1) {
storeOffline = false;
}
}
}
}
if (!storeOffline) {
// If message was sent to an unavailable full JID of a user then retry using the bare JID.
routingTable.routePacket(recipient.asBareJID(), packet, false);
} else {
// Delegate to offline message strategy, which will either bounce or ignore the message depending on user settings.
messageStrategy.storeOffline((Message) packet);
}
}
use of org.xmpp.packet.JID in project Openfire by igniterealtime.
the class MessageRouter method route.
/**
* <p>Performs the actual packet routing.</p>
* <p>You routing is considered 'quick' and implementations may not take
* excessive amounts of time to complete the routing. If routing will take
* a long amount of time, the actual routing should be done in another thread
* so this method returns quickly.</p>
* <h2>Warning</h2>
* <p>Be careful to enforce concurrency DbC of concurrent by synchronizing
* any accesses to class resources.</p>
*
* @param packet The packet to route
* @throws NullPointerException If the packet is null
*/
public void route(Message packet) {
if (packet == null) {
throw new NullPointerException();
}
ClientSession session = sessionManager.getSession(packet.getFrom());
try {
// Invoke the interceptors before we process the read packet
InterceptorManager.getInstance().invokeInterceptors(packet, session, true, false);
if (session == null || session.getStatus() == Session.STATUS_AUTHENTICATED) {
JID recipientJID = packet.getTo();
// If the server receives a message stanza with no 'to' attribute, it MUST treat the message as if the 'to' address were the bare JID <localpart@domainpart> of the sending entity.
if (recipientJID == null) {
recipientJID = packet.getFrom().asBareJID();
}
// Check if the message was sent to the server hostname
if (recipientJID.getNode() == null && recipientJID.getResource() == null && serverName.equals(recipientJID.getDomain())) {
if (packet.getElement().element("addresses") != null) {
// Message includes multicast processing instructions. Ask the multicastRouter
// to route this packet
multicastRouter.route(packet);
} else {
// Message was sent to the server hostname so forward it to a configurable
// set of JID's (probably admin users)
sendMessageToAdmins(packet);
}
return;
}
boolean isAcceptable = true;
if (session instanceof LocalClientSession) {
// Check if we could process messages from the recipient.
// If not, return a not-acceptable error as per XEP-0016:
// If the user attempts to send an outbound stanza to a contact and that stanza type is blocked, the user's server MUST NOT route the stanza to the contact but instead MUST return a <not-acceptable/> error
Message dummyMessage = packet.createCopy();
dummyMessage.setFrom(packet.getTo());
dummyMessage.setTo(packet.getFrom());
if (!((LocalClientSession) session).canProcess(dummyMessage)) {
packet.setTo(session.getAddress());
packet.setFrom((JID) null);
packet.setError(PacketError.Condition.not_acceptable);
session.process(packet);
isAcceptable = false;
}
}
if (isAcceptable) {
boolean isPrivate = packet.getElement().element(QName.get("private", "urn:xmpp:carbons:2")) != null;
try {
// Deliver stanza to requested route
routingTable.routePacket(recipientJID, packet, false);
} catch (Exception e) {
log.error("Failed to route packet: " + packet.toXML(), e);
routingFailed(recipientJID, packet);
}
// When a client sends a <message/> of type "chat"
if (packet.getType() == Message.Type.chat && !isPrivate && session != null) {
// && session.isMessageCarbonsEnabled() ??? // must the own session also be carbon enabled?
List<JID> routes = routingTable.getRoutes(packet.getFrom().asBareJID(), null);
for (JID route : routes) {
// The sending server SHOULD NOT send a forwarded copy to the sending full JID if it is a Carbons-enabled resource.
if (!route.equals(session.getAddress())) {
ClientSession clientSession = sessionManager.getSession(route);
if (clientSession != null && clientSession.isMessageCarbonsEnabled()) {
Message message = new Message();
// The wrapping message SHOULD maintain the same 'type' attribute value
message.setType(packet.getType());
// the 'from' attribute MUST be the Carbons-enabled user's bare JID
message.setFrom(packet.getFrom().asBareJID());
// and the 'to' attribute SHOULD be the full JID of the resource receiving the copy
message.setTo(route);
// The content of the wrapping message MUST contain a <sent/> element qualified by the namespace "urn:xmpp:carbons:2", which itself contains a <forwarded/> qualified by the namespace "urn:xmpp:forward:0" that contains the original <message/> stanza.
message.addExtension(new Sent(new Forwarded(packet)));
clientSession.process(message);
}
}
}
}
}
} else {
packet.setTo(session.getAddress());
packet.setFrom((JID) null);
packet.setError(PacketError.Condition.not_authorized);
session.process(packet);
}
// Invoke the interceptors after we have processed the read packet
InterceptorManager.getInstance().invokeInterceptors(packet, session, true, true);
} catch (PacketRejectedException e) {
// An interceptor rejected this packet
if (session != null && e.getRejectionMessage() != null && e.getRejectionMessage().trim().length() > 0) {
// A message for the rejection will be sent to the sender of the rejected packet
Message reply = new Message();
reply.setID(packet.getID());
reply.setTo(session.getAddress());
reply.setFrom(packet.getTo());
reply.setType(packet.getType());
reply.setThread(packet.getThread());
reply.setBody(e.getRejectionMessage());
session.process(reply);
}
}
}
use of org.xmpp.packet.JID in project Openfire by igniterealtime.
the class MulticastRouter method route.
public void route(Packet packet) {
Set<String> remoteServers = new HashSet<>();
List<String> targets = new ArrayList<>();
Packet localBroadcast = packet.createCopy();
Element addresses = getAddresses(localBroadcast);
String localDomain = "@" + server.getServerInfo().getXMPPDomain();
// remote domains that should receive the packet too
for (Iterator it = addresses.elementIterator("address"); it.hasNext(); ) {
Element address = (Element) it.next();
// Skip addresses of type noreply since they don't have any address
if (Type.noreply.toString().equals(address.attributeValue("type"))) {
continue;
}
String jid = address.attributeValue("jid");
// Only send to local users and if packet has not already been delivered
if (jid.contains(localDomain) && address.attributeValue("delivered") == null) {
targets.add(jid);
} else if (!jid.contains(localDomain)) {
remoteServers.add(new JID(jid).getDomain());
}
// Set as delivered
address.addAttribute("delivered", "true");
// Remove bcc addresses
if (Type.bcc.toString().equals(address.attributeValue("type"))) {
it.remove();
}
}
// Send the packet to local target users
for (String jid : targets) {
localBroadcast.setTo(jid);
packetRouter.route(localBroadcast);
}
// Keep a registry of packets that should be sent to remote domains.
for (String domain : remoteServers) {
boolean shouldDiscover = false;
synchronized (domain.intern()) {
Collection<Packet> packets = remotePackets.get(domain);
if (packets == null) {
packets = new ArrayList<>();
remotePackets.put(domain, packets);
shouldDiscover = true;
}
// Add that this packet should be sent to the requested remote server
packets.add(packet);
}
if (shouldDiscover) {
// First time a packet is sent to this remote server so start the extra work
// of discovering if remote server supports multicast service and actually send
// the packet to the remote server
sendToRemoteEntity(domain);
}
}
// TODO Add thread that checks every 5 minutes if packets to remote servers were not
// TODO sent because no disco response was received. So assume that remote server does
// TODO not support JEP-33 and send pending packets
}
Aggregations