Java Code Examples for org.xmpp.packet.IQ#setType()

The following examples show how to use org.xmpp.packet.IQ#setType() . 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: LocationHandler.java    From openfireLBS with Apache License 2.0 6 votes vote down vote up
private IQ updateLocation(IQ packet) {
	IQ reply = IQ.createResultIQ(packet);

	Element iq = packet.getChildElement();
	JID from = packet.getFrom();
	String username = from.getNode();

	Element item = iq.element("item");
	Double myLon = Double.parseDouble(item.attributeValue("lon"));
	Double myLat = Double.parseDouble(item.attributeValue("lat"));

	boolean f = insertLocation(myLon,myLat,username);
	if (f){
		// reply.setChildElement(iq);
	}else{
		reply.setType(IQ.Type.error);
		reply.setError(PacketError.Condition.internal_server_error);
	}
	
	return reply;
}
 
Example 2
Source File: LocationHandler.java    From openfireLBS with Apache License 2.0 5 votes vote down vote up
@Override
public IQ handleIQ(IQ packet) throws UnauthorizedException {
	
	System.out.println(">>>>>>>>>>>>> RECV IQ: " + packet.toXML()); // XXX
	
	// get users near me(from JID)
	if (IQ.Type.get.equals(packet.getType())) {
		return getUsersNearme(packet);
	// set from JID's location to ...
	} else if (IQ.Type.set.equals(packet.getType())) {
		JID to = packet.getTo();
		
		// send from JID's location to to JID
		if (to.getNode() != null && !to.getNode().equals("")){  
			XMPPServer.getInstance().getIQRouter().route(packet); // route to another user 
			
			return IQ.createResultIQ(packet);
		// send from JID's location to server , and update ofLocation  
		}else{ 
			return updateLocation(packet);
		}
	} else {
		IQ reply = IQ.createResultIQ(packet);
		reply.setType(IQ.Type.error);
		reply.setError(PacketError.Condition.bad_request);
		return reply;
	}
}
 
Example 3
Source File: IQRosterHandler.java    From Openfire with Apache License 2.0 4 votes vote down vote up
/**
 * The packet is a typical 'set' or 'get' update targeted at the server.
 * Notice that the set could be a roster removal in which case we have to
 * generate a local roster removal update as well as a new roster removal
 * to send to the the roster item's owner.
 *
 * @param packet The packet that triggered this update
 * @return Either a response to the roster update or null if the packet is corrupt and the session was closed down
 */
private IQ manageRoster(org.xmpp.packet.Roster packet) throws UnauthorizedException,
        UserAlreadyExistsException, SharedGroupException {

    IQ returnPacket = null;
    JID sender = packet.getFrom();
    IQ.Type type = packet.getType();

    try {
        if ((sender.getNode() == null || !RosterManager.isRosterServiceEnabled() ||
                !userManager.isRegisteredUser(sender.getNode())) &&
                IQ.Type.get == type) {
            // If anonymous user asks for his roster or roster service is disabled then
            // return an empty roster
            IQ reply = IQ.createResultIQ(packet);
            reply.setChildElement("query", "jabber:iq:roster");
            return reply;
        }
        if (!localServer.isLocal(sender)) {
            // Sender belongs to a remote server so discard this IQ request
            Log.warn("Discarding IQ roster packet of remote user: " + packet);
            return null;
        }

        Roster cachedRoster = userManager.getUser(sender.getNode()).getRoster();
        if (IQ.Type.get == type) {

            if (RosterManager.isRosterVersioningEnabled()) {
                String clientVersion = packet.getChildElement().attributeValue("ver");
                String latestVersion = String.valueOf( cachedRoster.hashCode() );
                // Whether or not the roster has been modified since the version ID enumerated by the client, ...
                if (!latestVersion.equals(clientVersion)) {
                    // ... the server MUST either return the complete roster
                    // (including a 'ver' attribute that signals the latest version)
                    returnPacket = cachedRoster.getReset();
                    returnPacket.getChildElement().addAttribute("ver", latestVersion );
                } else {
                    // ... or return an empty IQ-result
                    returnPacket = new org.xmpp.packet.IQ();
                }
            } else {
                returnPacket = cachedRoster.getReset();
            }
            returnPacket.setType(IQ.Type.result);
            returnPacket.setTo(sender);
            returnPacket.setID(packet.getID());
            // Force delivery of the response because we need to trigger
            // a presence probe from all contacts
            deliverer.deliver(returnPacket);
            returnPacket = null;
        }
        else if (IQ.Type.set == type) {
            returnPacket = IQ.createResultIQ(packet);

            // RFC 6121 2.3.3.  Error Cases:
            // The server MUST return a <bad-request/> stanza error to the client if the roster set contains any of the following violations:
            // The <query/> element contains more than one <item/> child element.
            if (packet.getItems().size() > 1) {
                returnPacket.setError(new PacketError(PacketError.Condition.bad_request, PacketError.Type.modify, "Query contains more than one item"));
            } else {
                for (org.xmpp.packet.Roster.Item item : packet.getItems()) {
                    if (item.getSubscription() == org.xmpp.packet.Roster.Subscription.remove) {
                        if (removeItem(cachedRoster, packet.getFrom(), item) == null) {
                            // RFC 6121 2.5.3.  Error Cases: If the value of the 'jid' attribute specifies an item that is not in the roster, then the server MUST return an <item-not-found/> stanza error.
                            returnPacket.setError(PacketError.Condition.item_not_found);
                        }
                    } else {
                        PacketError error = checkGroups(item.getGroups());
                        if (error != null) {
                            returnPacket.setError(error);
                        } else {
                            if (cachedRoster.isRosterItem(item.getJID())) {
                                // existing item
                                RosterItem cachedItem = cachedRoster.getRosterItem(item.getJID());
                                cachedItem.setAsCopyOf(item);
                                cachedRoster.updateRosterItem(cachedItem);
                            } else {
                                // new item
                                cachedRoster.createRosterItem(item);
                            }
                        }
                    }
                }
            }
        }
    }
    catch (UserNotFoundException e) {
        throw new UnauthorizedException(e);
    }

    return returnPacket;

}
 
Example 4
Source File: LocationHandler.java    From openfireLBS with Apache License 2.0 4 votes vote down vote up
/**
 * 
 * 
 * @param packet
 * @return
 */
private IQ getUsersNearme(IQ packet) {

	IQ reply = IQ.createResultIQ(packet);
	
	JID from = packet.getFrom();
	
	Element iq = packet.getChildElement();
	Element item = iq.element("item");
	Double myLon = Double.parseDouble(item.attributeValue("lon"));
	Double myLat = Double.parseDouble(item.attributeValue("lat"));
	
	// XXX: update user location firstly 
	insertLocation(myLon,myLat,from.getNode());
	
	// find users near me 
	PreparedStatement pstmt = null;
	ResultSet rs = null;
	try {
		pstmt = openfireConn.prepareStatement(SQL_USERS_NEARME);
		pstmt.setDouble(1, myLon);
		pstmt.setDouble(2, myLon);
		pstmt.setDouble(3, myLat);
		pstmt.setDouble(4, myLat);
		rs = pstmt.executeQuery();
		String username = null;
		double nearLon = 0;
		double nearLat = 0;
		while (rs.next()) {  
			username = rs.getString("username");
			nearLon = rs.getDouble("lon");
			nearLat = rs.getDouble("lat");
			Element e = iq.addElement("item");
			e.addAttribute("user", username);
			e.addAttribute("lon", Double.toString(nearLon));
			e.addAttribute("lat", Double.toString(nearLat));
		}
		reply.setChildElement(iq);
	} catch (SQLException e1) {
		reply.setType(IQ.Type.error);
		reply.setError(PacketError.Condition.internal_server_error);
		e1.printStackTrace();
	}
	return reply;
}