List of usage examples for org.dom4j DocumentHelper createElement
public static Element createElement(String name)
From source file:net.sf.kraken.BaseTransport.java
License:Open Source License
/** * Handle service discovery info request. * * @param packet An IQ packet in the disco info namespace. * @return A list of IQ packets to be returned to the user. *//*from w ww . j a v a2s .co m*/ private List<Packet> handleDiscoInfo(IQ packet) { // TODO: why return a list? we're sure to return always exactly one result. List<Packet> reply = new ArrayList<Packet>(); JID from = packet.getFrom(); IQ result = IQ.createResultIQ(packet); if (packet.getTo().getNode() == null) { // Requested info from transport itself. if (from.getNode() == null || RegistrationManager.getInstance().isRegistered(from, this.transportType) || permissionManager.hasAccess(from)) { Element response = DocumentHelper.createElement(QName.get("query", NameSpace.DISCO_INFO)); response.addElement("identity").addAttribute("category", "gateway") .addAttribute("type", this.transportType.discoIdentity()) .addAttribute("name", this.description); response.addElement("feature").addAttribute("var", NameSpace.DISCO_INFO); response.addElement("feature").addAttribute("var", NameSpace.DISCO_ITEMS); response.addElement("feature").addAttribute("var", NameSpace.IQ_GATEWAY); response.addElement("feature").addAttribute("var", NameSpace.IQ_REGISTER); response.addElement("feature").addAttribute("var", NameSpace.IQ_VERSION); response.addElement("feature").addAttribute("var", NameSpace.IQ_LAST); response.addElement("feature").addAttribute("var", NameSpace.VCARD_TEMP); if (RegistrationManager.getInstance().isRegistered(from, this.transportType)) { response.addElement("feature").addAttribute("var", NameSpace.IQ_REGISTERED); } result.setChildElement(response); } else { result.setError(Condition.forbidden); } } else { // Requested info from a gateway user. final TransportSession<B> session; try { session = sessionManager.getSession(packet.getFrom()); if ((from.getNode() == null || permissionManager.hasAccess(from)) && session != null) { final Element response = DocumentHelper.createElement(QName.get("query", NameSpace.DISCO_INFO)); response.addElement("identity").addAttribute("category", "client").addAttribute("type", "pc"); response.addElement("feature").addAttribute("var", NameSpace.DISCO_INFO); for (final SupportedFeature feature : session.supportedFeatures) { response.addElement("feature").addAttribute("var", feature.getVar()); } result.setChildElement(response); } else { result.setError(Condition.forbidden); } } catch (NotFoundException ex) { result.setError(Condition.item_not_found); } } reply.add(result); return reply; }
From source file:net.sf.kraken.BaseTransport.java
License:Open Source License
/** * Handle service discovery items request. * * @param packet An IQ packet in the disco items namespace. * @return A list of IQ packets to be returned to the user. */// w w w . j a v a2 s.c o m private List<Packet> handleDiscoItems(IQ packet) { List<Packet> reply = new ArrayList<Packet>(); IQ result = IQ.createResultIQ(packet); Element response = DocumentHelper.createElement(QName.get("query", NameSpace.DISCO_ITEMS)); result.setChildElement(response); reply.add(result); return reply; }
From source file:net.sf.kraken.BaseTransport.java
License:Open Source License
/** * Handle gateway translation service request. * * @param packet An IQ packet in the iq gateway namespace. * @return A list of IQ packets to be returned to the user. *//*from w w w. j a va2 s . co m*/ private List<Packet> handleIQGateway(IQ packet) { List<Packet> reply = new ArrayList<Packet>(); if (packet.getType() == IQ.Type.get) { IQ result = IQ.createResultIQ(packet); Element query = DocumentHelper.createElement(QName.get("query", NameSpace.IQ_GATEWAY)); query.addElement("desc").addText(LocaleUtils.getLocalizedString("gateway.base.enterusername", "kraken", Arrays.asList(transportType.toString().toUpperCase()))); query.addElement("prompt"); result.setChildElement(query); reply.add(result); } else if (packet.getType() == IQ.Type.set) { IQ result = IQ.createResultIQ(packet); String prompt = null; Element promptEl = packet.getChildElement().element("prompt"); if (promptEl != null) { prompt = promptEl.getTextTrim(); } if (prompt == null) { result.setError(Condition.bad_request); } else { JID jid = this.convertIDToJID(prompt); Element query = DocumentHelper.createElement(QName.get("query", NameSpace.IQ_GATEWAY)); // This is what Psi expects query.addElement("prompt").addText(jid.toString()); // This is JEP complient query.addElement("jid").addText(jid.toString()); result.setChildElement(query); } reply.add(result); } return reply; }
From source file:net.sf.kraken.BaseTransport.java
License:Open Source License
/** * Handle last request.//from w w w.j a v a 2 s .co m * * @param packet An IQ packet in the jabber:iq:last namespace. * @return A list of IQ packets to be returned to the user. */ private List<Packet> handleIQLast(IQ packet) { List<Packet> reply = new ArrayList<Packet>(); JID from = packet.getFrom(); JID to = packet.getTo(); if (packet.getType() == IQ.Type.get) { IQ result = IQ.createResultIQ(packet); if (from.getNode() != null) { try { TransportSession<B> session = sessionManager.getSession(from); Element response = DocumentHelper.createElement(QName.get("query", NameSpace.IQ_LAST)); Long timestamp = session.getBuddyManager().getBuddy(to).getLastActivityTimestamp(); String lastevent = session.getBuddyManager().getBuddy(to).getLastActivityEvent(); response.addAttribute("seconds", new Long(new Date().getTime() - timestamp).toString()); if (lastevent != null) { response.addCDATA(lastevent); } result.setChildElement(response); } catch (NotFoundException e) { Log.debug("Contact not found while retrieving last activity for: " + from); result.setError(Condition.item_not_found); } } else { result.setError(Condition.feature_not_implemented); } reply.add(result); } else if (packet.getType() == IQ.Type.set) { IQ result = IQ.createResultIQ(packet); result.setError(Condition.forbidden); reply.add(result); } return reply; }
From source file:net.sf.kraken.BaseTransport.java
License:Open Source License
/** * Handle version request./* w w w.j a v a 2 s . c o m*/ * * @param packet An IQ packet in the iq version namespace. * @return A list of IQ packets to be returned to the user. */ private List<Packet> handleIQVersion(IQ packet) { List<Packet> reply = new ArrayList<Packet>(); if (packet.getType() == IQ.Type.get) { IQ result = IQ.createResultIQ(packet); Element query = DocumentHelper.createElement(QName.get("query", NameSpace.IQ_VERSION)); query.addElement("name").addText("Openfire " + this.getDescription()); query.addElement("version") .addText(XMPPServer.getInstance().getServerInfo().getVersion().getVersionString() + " - " + this.getVersionString()); query.addElement("os").addText(System.getProperty("os.name")); result.setChildElement(query); reply.add(result); } return reply; }
From source file:net.sf.kraken.muc.BaseMUCTransport.java
License:Open Source License
/** * Handle service discovery info request. * * @param packet An IQ packet in the disco info namespace. * @return A list of IQ packets to be returned to the user. *//* www . j a v a2 s.co m*/ private List<Packet> handleDiscoInfo(IQ packet) { List<Packet> reply = new ArrayList<Packet>(); JID from = packet.getFrom(); JID to = packet.getTo(); if (packet.getTo().getNode() == null) { // Requested info from transport itself. IQ result = IQ.createResultIQ(packet); if (from.getNode() == null || getTransport().permissionManager.hasAccess(from)) { Element response = DocumentHelper.createElement(QName.get("query", NameSpace.DISCO_INFO)); response.addElement("identity").addAttribute("category", "conference").addAttribute("type", "text") .addAttribute("name", this.getDescription()); response.addElement("feature").addAttribute("var", NameSpace.DISCO_INFO); response.addElement("feature").addAttribute("var", NameSpace.DISCO_ITEMS); response.addElement("feature").addAttribute("var", NameSpace.MUC); result.setChildElement(response); } else { result.setError(PacketError.Condition.forbidden); } reply.add(result); } else { // Ah, a request for information about a room. IQ result = IQ.createResultIQ(packet); try { TransportSession<B> session = getTransport().getSessionManager().getSession(from); if (session.isLoggedIn()) { storePendingRequest(packet); session.getRoomInfo(getTransport().convertJIDToID(to)); } else { // Not logged in? Not logged in then. result.setError(PacketError.Condition.forbidden); reply.add(result); } } catch (NotFoundException e) { // Not found? No active session then. result.setError(PacketError.Condition.forbidden); reply.add(result); } } return reply; }
From source file:net.sf.kraken.muc.BaseMUCTransport.java
License:Open Source License
/** * Sends a list of rooms as a response to a service discovery request. * * @param to JID we will be sending the response to. * @param rooms List of MUCTransportRoom objects to send as a response. *///from ww w. j a va2 s .c o m public void sendRooms(JID to, Collection<MUCTransportRoom> rooms) { IQ request = getPendingRequest(to, this.getJID(), NameSpace.DISCO_ITEMS); if (request != null) { IQ result = IQ.createResultIQ(request); Element response = DocumentHelper.createElement(QName.get("query", NameSpace.DISCO_ITEMS)); for (MUCTransportRoom room : rooms) { Element item = response.addElement("item"); item.addAttribute("jid", room.getJid().toBareJID()); item.addAttribute("name", room.getName()); } result.setChildElement(response); this.sendPacket(result); } }
From source file:net.sf.kraken.muc.BaseMUCTransport.java
License:Open Source License
/** * Sends information about a room as a response to a service discovery request. * * @param to JID we will be sending the response to. * @param roomjid JID of the room info was requested about. * @param room A MUCTransportRoom object containing information to return as a response. *//*from ww w .j a v a2 s. c om*/ public void sendRoomInfo(JID to, JID roomjid, MUCTransportRoom room) { IQ request = getPendingRequest(to, roomjid, NameSpace.DISCO_INFO); if (request != null) { IQ result = IQ.createResultIQ(request); Element response = DocumentHelper.createElement(QName.get("query", NameSpace.DISCO_INFO)); response.addElement("identity").addAttribute("category", "conference").addAttribute("type", "text") .addAttribute("name", room.getName()); response.addElement("feature").addAttribute("var", NameSpace.MUC); response.addElement("feature").addAttribute("var", NameSpace.DISCO_INFO); response.addElement("feature").addAttribute("var", NameSpace.DISCO_ITEMS); if (room.getPassword_protected()) { response.addElement("feature").addAttribute("var", "muc_passwordprotected"); } if (room.getHidden()) { response.addElement("feature").addAttribute("var", "muc_hidden"); } if (room.getTemporary()) { response.addElement("feature").addAttribute("var", "muc_temporary"); } if (room.getOpen()) { response.addElement("feature").addAttribute("var", "muc_open"); } if (!room.getModerated()) { response.addElement("feature").addAttribute("var", "muc_unmoderated"); } if (!room.getAnonymous()) { response.addElement("feature").addAttribute("var", "muc_nonanonymous"); } Element form = DocumentHelper.createElement(QName.get("x", NameSpace.XDATA)); form.addAttribute("type", "result"); form.addElement("field").addAttribute("var", "FORM_TYPE").addAttribute("type", "hidden") .addElement("value").addCDATA("http://jabber.org/protocol/muc#roominfo"); if (room.getContact() != null) { form.addElement("field").addAttribute("var", "muc#roominfo_contactjid") .addAttribute("label", "Contact Addresses").addElement("value") .addCDATA(room.getContact().toString()); } if (room.getName() != null) { form.addElement("field").addAttribute("var", "muc#roominfo_description") .addAttribute("label", "Short Description of Room").addElement("value") .addCDATA(room.getName()); } if (room.getLanguage() != null) { form.addElement("field").addAttribute("var", "muc#roominfo_lang") .addAttribute("label", "Natural Language for Room Discussions").addElement("value") .addCDATA(room.getLanguage()); } if (room.getLog_location() != null) { form.addElement("field").addAttribute("var", "muc#roominfo_logs") .addAttribute("label", "URL for Archived Discussion Logs").addElement("value") .addCDATA(room.getLog_location()); } if (room.getOccupant_count() != null) { form.addElement("field").addAttribute("var", "muc#roominfo_occupants") .addAttribute("label", "Current Number of Occupants in Room").addElement("value") .addCDATA(room.getOccupant_count().toString()); } if (room.getTopic() != null) { form.addElement("field").addAttribute("var", "muc#roominfo_subject") .addAttribute("label", "Current Subject or Discussion Topic in Room").addElement("value") .addCDATA(room.getTopic()); } response.add(form); result.setChildElement(response); this.sendPacket(result); } }
From source file:net.sf.kraken.muc.BaseMUCTransport.java
License:Open Source License
/** * Sends a list of rooms as a response to a service discovery request. * * @param to JID we will be sending the response to. * @param roomjid JID of the room info was requested about. * @param members List of MUCTransportRoomMember objects to send as a response. *//* w w w. j a v a 2s.c o m*/ public void sendRoomMembers(JID to, JID roomjid, List<MUCTransportRoomMember> members) { IQ request = getPendingRequest(to, roomjid, NameSpace.DISCO_ITEMS); if (request != null) { IQ result = IQ.createResultIQ(request); Element response = DocumentHelper.createElement(QName.get("query", NameSpace.DISCO_ITEMS)); for (MUCTransportRoomMember member : members) { Element item = response.addElement("item"); item.addAttribute("jid", member.getJid().toBareJID()); } result.setChildElement(response); this.sendPacket(result); } }
From source file:net.sf.kraken.registration.RegistrationHandler.java
License:Open Source License
/** * Handles a IQ-register 'get' request, which is to be interpreted as a * request for a registration form template. The template will be prefilled * with data, if the requestee has a current registration with the gateway. * * @param packet the IQ-register 'get' stanza. * @throws UnauthorizedException if the user is not allowed to make use of the gateway. *///from w w w . ja v a 2 s . c o m private void getRegistrationForm(IQ packet) throws UnauthorizedException { final JID from = packet.getFrom(); final IQ result = IQ.createResultIQ(packet); // search for existing registrations String curUsername = null; String curPassword = null; String curNickname = null; Boolean registered = false; final Collection<Registration> registrations = RegistrationManager.getInstance().getRegistrations(from, parent.transportType); if (registrations.iterator().hasNext()) { Registration registration = registrations.iterator().next(); curUsername = registration.getUsername(); curPassword = registration.getPassword(); curNickname = registration.getNickname(); registered = true; } // Verify that the user is allowed to make use of the gateway. if (!registered && !parent.permissionManager.hasAccess(from)) { // User does not have permission to register with transport. // We want to allow them to change settings if they are already // registered. throw new UnauthorizedException( LocaleUtils.getLocalizedString("gateway.base.registrationdeniedbyacls", "kraken")); } // generate a template registration form. final Element response = DocumentHelper.createElement(QName.get("query", NameSpace.IQ_REGISTER)); final DataForm form = new DataForm(DataForm.Type.form); form.addInstruction(parent.getTerminologyRegistration()); final FormField usernameField = form.addField(); usernameField.setLabel(parent.getTerminologyUsername()); usernameField.setVariable("username"); usernameField.setType(FormField.Type.text_single); if (curUsername != null) { usernameField.addValue(curUsername); } final FormField passwordField = form.addField(); passwordField.setLabel(parent.getTerminologyPassword()); passwordField.setVariable("password"); passwordField.setType(FormField.Type.text_private); if (curPassword != null) { passwordField.addValue(curPassword); } final String nicknameTerm = parent.getTerminologyNickname(); if (nicknameTerm != null) { FormField nicknameField = form.addField(); nicknameField.setLabel(nicknameTerm); nicknameField.setVariable("nick"); nicknameField.setType(FormField.Type.text_single); if (curNickname != null) { nicknameField.addValue(curNickname); } } response.add(form.getElement()); response.addElement("instructions").addText(parent.getTerminologyRegistration()); // prefill the template with existing data if a registration already // exists. if (registered) { response.addElement("registered"); response.addElement("username").addText(curUsername); if (curPassword == null) { response.addElement("password"); } else { response.addElement("password").addText(curPassword); } if (nicknameTerm != null) { if (curNickname == null) { response.addElement("nick"); } else { response.addElement("nick").addText(curNickname); } } } else { response.addElement("username"); response.addElement("password"); if (nicknameTerm != null) { response.addElement("nick"); } } // Add special indicator for rosterless gateway handling. response.addElement("x").addNamespace("", NameSpace.IQ_GATEWAY_REGISTER); result.setChildElement(response); parent.sendPacket(result); }