001/** 002 * 003 * Copyright © 2014-2020 Florian Schmaus 004 * 005 * Licensed under the Apache License, Version 2.0 (the "License"); 006 * you may not use this file except in compliance with the License. 007 * You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.jivesoftware.smack.sm.predicates.tcp; 018 019import org.jivesoftware.smack.filter.StanzaFilter; 020import org.jivesoftware.smack.packet.Stanza; 021import org.jivesoftware.smack.tcp.XMPPTCPConnection; 022import org.jivesoftware.smack.util.StringUtils; 023 024public final class OnceForThisStanza implements StanzaFilter { 025 026 private final String id; 027 private final XMPPTCPConnection connection; 028 029 public static void setup(XMPPTCPConnection connection, Stanza packet) { 030 StanzaFilter packetFilter = new OnceForThisStanza(connection, packet); 031 connection.addRequestAckPredicate(packetFilter); 032 } 033 034 private OnceForThisStanza(XMPPTCPConnection connection, Stanza packet) { 035 this.connection = connection; 036 this.id = packet.getStanzaId(); 037 if (StringUtils.isNullOrEmpty(id)) { 038 throw new IllegalArgumentException("Stanza ID must be set"); 039 } 040 } 041 042 @Override 043 public boolean accept(Stanza packet) { 044 String otherId = packet.getStanzaId(); 045 if (StringUtils.isNullOrEmpty(otherId)) { 046 return false; 047 } 048 if (id.equals(otherId)) { 049 connection.removeRequestAckPredicate(this); 050 return true; 051 } 052 return false; 053 } 054 055}