|
| 1 | +package org.buddycloud.channelserver.packetprocessor.iq.namespace.search; |
| 2 | + |
| 3 | +import java.util.concurrent.BlockingQueue; |
| 4 | + |
| 5 | +import org.buddycloud.channelserver.channel.ChannelManager; |
| 6 | +import org.buddycloud.channelserver.packetprocessor.PacketProcessor; |
| 7 | +import org.dom4j.Element; |
| 8 | +import org.xmpp.forms.DataForm; |
| 9 | +import org.xmpp.packet.IQ; |
| 10 | +import org.xmpp.packet.Packet; |
| 11 | +import org.xmpp.packet.PacketError; |
| 12 | +import org.xmpp.packet.PacketError.Type; |
| 13 | + |
| 14 | +public class SearchGet implements PacketProcessor<IQ> { |
| 15 | + |
| 16 | + public static final String INSTRUCTIONS = "Search for content/hashtags/mentions"; |
| 17 | + |
| 18 | + public static final String TITLE = "Please populate one or more of the following fields"; |
| 19 | + public static final String CONTENT_FIELD_LABEL = "Content search"; |
| 20 | + public static final String AUTHOR_FIELD_LABEL = "Author"; |
| 21 | + public static final String RPP_FIELD_LABEL = "Results per page"; |
| 22 | + public static final String PAGE_FIELD_LABEL = "Page"; |
| 23 | + |
| 24 | + private ChannelManager channelManager; |
| 25 | + private BlockingQueue<Packet> outQueue; |
| 26 | + private IQ response; |
| 27 | + |
| 28 | + private Element x; |
| 29 | + |
| 30 | + public SearchGet(BlockingQueue<Packet> outQueue, |
| 31 | + ChannelManager channelManager) { |
| 32 | + this.channelManager = channelManager; |
| 33 | + this.outQueue = outQueue; |
| 34 | + } |
| 35 | + |
| 36 | + @Override |
| 37 | + public void process(IQ request) throws Exception { |
| 38 | + response = IQ.createResultIQ(request); |
| 39 | + |
| 40 | + if (false == channelManager.isLocalJID(request.getFrom())) { |
| 41 | + sendErrorResponse(PacketError.Type.cancel, |
| 42 | + PacketError.Condition.not_allowed); |
| 43 | + return; |
| 44 | + } |
| 45 | + |
| 46 | + Element query = response.getElement().addElement("query"); |
| 47 | + query.addAttribute("xmlns", Search.NAMESPACE_URI); |
| 48 | + query.addElement("instructions").addText(INSTRUCTIONS); |
| 49 | + x = query.addElement("x"); |
| 50 | + addFields(); |
| 51 | + outQueue.put(response); |
| 52 | + } |
| 53 | + |
| 54 | + private void addFields() { |
| 55 | + x.addAttribute("xmlns", DataForm.NAMESPACE); |
| 56 | + x.addElement("title").addText(TITLE); |
| 57 | + x.addElement("instructions").addText(INSTRUCTIONS); |
| 58 | + |
| 59 | + Element formType = x.addElement("field"); |
| 60 | + formType.addAttribute("type", "hidden"); |
| 61 | + formType.addAttribute("var", "FORM_TYPE"); |
| 62 | + formType.addElement("value").addText(Search.NAMESPACE_URI); |
| 63 | + |
| 64 | + Element content = x.addElement("field"); |
| 65 | + content.addAttribute("type", "text-multi"); |
| 66 | + content.addAttribute("var", "content"); |
| 67 | + content.addAttribute("label", CONTENT_FIELD_LABEL); |
| 68 | + |
| 69 | + Element author = x.addElement("field"); |
| 70 | + author.addAttribute("type", "jid-single"); |
| 71 | + author.addAttribute("var", "author"); |
| 72 | + author.addAttribute("label", AUTHOR_FIELD_LABEL); |
| 73 | + |
| 74 | + Element rpp = x.addElement("field"); |
| 75 | + rpp.addAttribute("type", "fixed"); |
| 76 | + rpp.addAttribute("var", "rpp"); |
| 77 | + rpp.addAttribute("label", RPP_FIELD_LABEL); |
| 78 | + |
| 79 | + Element page = x.addElement("field"); |
| 80 | + page.addAttribute("type", "fixed"); |
| 81 | + page.addAttribute("var", "page"); |
| 82 | + page.addAttribute("label", PAGE_FIELD_LABEL); |
| 83 | + } |
| 84 | + |
| 85 | + private void sendErrorResponse(PacketError.Type type, |
| 86 | + PacketError.Condition condition) throws InterruptedException { |
| 87 | + response.setType(IQ.Type.error); |
| 88 | + PacketError error = new PacketError(PacketError.Condition.not_allowed, |
| 89 | + PacketError.Type.cancel); |
| 90 | + response.setError(error); |
| 91 | + outQueue.put(response); |
| 92 | + } |
| 93 | + |
| 94 | +} |
0 commit comments