Search code examples
openfiresmack

Use smack#UserSearchManager cannot search user


i try use Smack#UserSearchManager to search user .but it was always return empty ReportedData.i am sure the user what i search was exists.so i use spark to search the same user.it was real existence. that is my search code.it use in smack 4.2.4.

DomainBareJid searchJID = JidCreate.domainBareFrom(Domainpart.from("search."+IMLib.getConn().getServiceName().asUnescapedString()));
UserSearchManager manager = new UserSearchManager(IMLib.getConn());
Form searchForm = manager.getSearchForm(searchJID);
Form answerForm = searchForm.createAnswerForm();
answerForm.setAnswer("Name", true);
answerForm.setAnswer("Username", true);
answerForm.setAnswer("Email", true);
answerForm.setAnswer("search", account);
ReportedData reportedData = manager.getSearchResults(answerForm, searchJID);
List<ReportedData.Row> rows = reportedData.getRows();

Solution

  • My writing is bad.
    Finally,i resolve my question by myself.
    I watch the content of the stanza which was UserSearch.In Spark client,use the UserSearch iq stanza ,the value of the boolean type was 1 or 0.But it was true or false,when we use smack 4.2.4.
    As a result,spark can search contacts but the searching use smack 4.2.4 it can't do that.By the way,i have no try the other smack version.I just know the asmack can search contacts,and the value of boolean was 0 or 1.
    So i use StanzaInterceptor to resolve this problem according to the replace keyword value in the stanza.
    Here is the solution.

    //register StanzaInterceptor
    IMLib.getConn().addStanzaInterceptor(new StanzaListener() {
            @Override
            public void processStanza(Stanza packet) throws SmackException.NotConnectedException, InterruptedException, SmackException.NotLoggedInException {
                if (packet instanceof UserSearch) {
                    List<ExtensionElement> list = packet.getExtensions();
                    if (list != null) {
                        for (ExtensionElement element : list) {
    
                            if (element.getNamespace().equals("jabber:x:data")) {
                                packet.removeExtension(element);
                                packet.addExtension(new FixedQueryXElement(element.toXML().toString()));
                            }
                        }
                    }
                }
            }
        }, new StanzaTypeFilter(IQ.class));
    //the FixedQueryXElement class
    public class FixedQueryXElement implements ExtensionElement {
    
    private String mSource;
    
    private static final String REGEX = "(<field var='[\\w]{4,8}' type='boolean'><value>true</value></field>)";
    
    public FixedQueryXElement(String source) {
        this.mSource = source;
    }
    
    @Override
    public String getNamespace() {
        return "jabber:x:data";
    }
    
    @Override
    public String getElementName() {
        return "x";
    }
    
    @Override
    public CharSequence toXML() {
        Pattern pattern = Pattern.compile(REGEX);
        Matcher matcher = pattern.matcher(mSource);
        String des = mSource;
        while (matcher.find()) {
            String group = matcher.group();
            des = des.replace(group, group.replace("<value>true</value>", "<value>1</value>"));
        }
        return des;
    }
    

    }