1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2025-09-09 00:59:39 +02:00

Create smack-streammanagement project and move o.j.smack.sm code there

This commit is contained in:
Florian Schmaus 2020-08-15 14:03:57 +02:00
parent 89c5895ab3
commit 317e391da5
23 changed files with 33 additions and 2 deletions

View file

@ -0,0 +1,8 @@
description = """\
Smack support for XMPP Stream Management (XEP-0198)."""
dependencies {
api project(':smack-core')
testFixturesApi(testFixtures(project(":smack-core")))
}

View file

@ -0,0 +1,55 @@
/**
*
* Copyright © 2014 Florian Schmaus
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smack.sm;
import java.math.BigInteger;
public class SMUtils {
private static long MASK_32_BIT = BigInteger.ONE.shiftLeft(32).subtract(BigInteger.ONE).longValue();
/**
* Increment the Stream Management height counter.
*
* Quoting XEP-198 4.:
* "In the unlikely case that the number of stanzas handled during a stream management session exceeds the number
* of digits that can be represented by the unsignedInt datatype as specified in XML Schema Part 2 [10]
* (i.e., 2^32), the value of 'h' SHALL be reset from 2^32-1 back to zero (rather than being incremented to 2^32)."
*
* @param height TODO javadoc me please
* @return the incremented height
*/
public static long incrementHeight(long height) {
return ++height & MASK_32_BIT;
}
/**
* Calculates the delta of the last known stanza handled count and the new
* reported stanza handled count while considering that the new value may be
* wrapped after 2^32-1.
*
* @param reportedHandledCount TODO javadoc me please
* @param lastKnownHandledCount TODO javadoc me please
* @return the delta
*/
public static long calculateDelta(long reportedHandledCount, long lastKnownHandledCount) {
if (lastKnownHandledCount > reportedHandledCount) {
throw new IllegalStateException("Illegal Stream Management State: Last known handled count (" + lastKnownHandledCount + ") is greater than reported handled count (" + reportedHandledCount + ')');
}
return (reportedHandledCount - lastKnownHandledCount) & MASK_32_BIT;
}
}

View file

@ -0,0 +1,168 @@
/**
*
* Copyright © 2014 Florian Schmaus
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smack.sm;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.BlockingQueue;
import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.packet.Element;
import org.jivesoftware.smack.packet.Stanza;
public abstract class StreamManagementException extends SmackException {
public StreamManagementException() {
}
public StreamManagementException(String message) {
super(message);
}
/**
*
*/
private static final long serialVersionUID = 3767590115788821101L;
public static class StreamManagementNotEnabledException extends StreamManagementException {
/**
*
*/
private static final long serialVersionUID = 2624821584352571307L;
}
public static class StreamIdDoesNotMatchException extends StreamManagementException {
/**
*
*/
private static final long serialVersionUID = 1191073341336559621L;
public StreamIdDoesNotMatchException(String expected, String got) {
super("Stream IDs do not match. Expected '" + expected + "', but got '" + got + "'");
}
}
public static class StreamManagementCounterError extends StreamManagementException {
/**
*
*/
private static final long serialVersionUID = 1L;
private final long handledCount;
private final long previousServerHandledCount;
private final long ackedStanzaCount;
private final int outstandingStanzasCount;
private final List<Stanza> ackedStanzas;
public StreamManagementCounterError(long handledCount, long previousServerHandlerCount,
long ackedStanzaCount,
List<Stanza> ackedStanzas) {
super(
"There was an error regarding the Stream Management counters. Server reported "
+ handledCount
+ " handled stanzas, which means that the "
+ ackedStanzaCount
+ " recently send stanzas by client are now acked by the server. But Smack had only "
+ ackedStanzas.size()
+ " to acknowledge. The stanza id of the last acked outstanding stanza is "
+ (ackedStanzas.isEmpty() ? "<no acked stanzas>"
: ackedStanzas.get(ackedStanzas.size() - 1).getStanzaId()));
this.handledCount = handledCount;
this.previousServerHandledCount = previousServerHandlerCount;
this.ackedStanzaCount = ackedStanzaCount;
this.outstandingStanzasCount = ackedStanzas.size();
this.ackedStanzas = Collections.unmodifiableList(ackedStanzas);
}
public long getHandledCount() {
return handledCount;
}
public long getPreviousServerHandledCount() {
return previousServerHandledCount;
}
public long getAckedStanzaCount() {
return ackedStanzaCount;
}
public int getOutstandingStanzasCount() {
return outstandingStanzasCount;
}
public List<Stanza> getAckedStanzas() {
return ackedStanzas;
}
}
public static final class UnacknowledgedQueueFullException extends StreamManagementException {
/**
*
*/
private static final long serialVersionUID = 1L;
private final int overflowElementNum;
private final int droppedElements;
private final List<Element> elements;
private final List<Stanza> unacknowledgesStanzas;
private UnacknowledgedQueueFullException(String message, int overflowElementNum, int droppedElements, List<Element> elements,
List<Stanza> unacknowledgesStanzas) {
super(message);
this.overflowElementNum = overflowElementNum;
this.droppedElements = droppedElements;
this.elements = elements;
this.unacknowledgesStanzas = unacknowledgesStanzas;
}
public int getOverflowElementNum() {
return overflowElementNum;
}
public int getDroppedElements() {
return droppedElements;
}
public List<Element> getElements() {
return elements;
}
public List<Stanza> getUnacknowledgesStanzas() {
return unacknowledgesStanzas;
}
public static UnacknowledgedQueueFullException newWith(int overflowElementNum, List<Element> elements,
BlockingQueue<Stanza> unacknowledgedStanzas) {
final int unacknowledgesStanzasQueueSize = unacknowledgedStanzas.size();
List<Stanza> localUnacknowledgesStanzas = new ArrayList<>(unacknowledgesStanzasQueueSize);
localUnacknowledgesStanzas.addAll(unacknowledgedStanzas);
int droppedElements = elements.size() - overflowElementNum - 1;
String message = "The queue size " + unacknowledgesStanzasQueueSize + " is not able to fit another "
+ droppedElements + " potential stanzas type top-level stream-elements.";
return new UnacknowledgedQueueFullException(message, overflowElementNum, droppedElements, elements,
localUnacknowledgesStanzas);
}
}
}

View file

@ -0,0 +1,141 @@
/**
*
* Copyright 2019-2020 Florian Schmaus
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smack.sm;
import org.jivesoftware.smack.c2s.ModularXmppClientToServerConnection.AuthenticatedAndResourceBoundStateDescriptor;
import org.jivesoftware.smack.c2s.ModularXmppClientToServerConnection.AuthenticatedButUnboundStateDescriptor;
import org.jivesoftware.smack.c2s.ModularXmppClientToServerConnection.ResourceBindingStateDescriptor;
import org.jivesoftware.smack.c2s.ModularXmppClientToServerConnectionModule;
import org.jivesoftware.smack.c2s.internal.ModularXmppClientToServerConnectionInternal;
import org.jivesoftware.smack.c2s.internal.WalkStateGraphContext;
import org.jivesoftware.smack.compression.CompressionModule.CompressionStateDescriptor;
import org.jivesoftware.smack.fsm.State;
import org.jivesoftware.smack.fsm.StateDescriptor;
import org.jivesoftware.smack.fsm.StateTransitionResult;
public class StreamManagementModule extends ModularXmppClientToServerConnectionModule<StreamManagementModuleDescriptor> {
protected StreamManagementModule(StreamManagementModuleDescriptor moduleDescriptor,
ModularXmppClientToServerConnectionInternal connectionInternal) {
super(moduleDescriptor, connectionInternal);
}
private boolean useSm = true;
private boolean useSmResumption = true;
public static final class EnableStreamManagementStateDescriptor extends StateDescriptor {
private EnableStreamManagementStateDescriptor() {
super(StreamManagementModule.EnableStreamManagementState.class, 198, StateDescriptor.Property.notImplemented);
addPredeccessor(ResourceBindingStateDescriptor.class);
addSuccessor(AuthenticatedAndResourceBoundStateDescriptor.class);
declarePrecedenceOver(AuthenticatedAndResourceBoundStateDescriptor.class);
}
@Override
protected StreamManagementModule.EnableStreamManagementState constructState(ModularXmppClientToServerConnectionInternal connectionInternal) {
// This is the trick: the module is constructed prior the states, so we get the actual state out of the module by fetching the module from the connection.
StreamManagementModule smModule = connectionInternal.connection.getConnectionModuleFor(StreamManagementModuleDescriptor.class);
return smModule.constructEnableStreamMangementState(this, connectionInternal);
}
}
private EnableStreamManagementState constructEnableStreamMangementState(
EnableStreamManagementStateDescriptor enableStreamManagementStateDescriptor,
ModularXmppClientToServerConnectionInternal connectionInternal) {
return new EnableStreamManagementState(enableStreamManagementStateDescriptor, connectionInternal);
}
private final class EnableStreamManagementState extends State {
private EnableStreamManagementState(EnableStreamManagementStateDescriptor stateDescriptor,
ModularXmppClientToServerConnectionInternal connectionInternal) {
super(stateDescriptor, connectionInternal);
}
@Override
public StateTransitionResult.TransitionImpossible isTransitionToPossible(WalkStateGraphContext walkStateGraphContext) {
if (!useSm) {
return new StateTransitionResult.TransitionImpossibleReason("Stream management not enabled");
}
return new StateTransitionResult.TransitionImpossibleBecauseNotImplemented(stateDescriptor);
}
@Override
public StateTransitionResult.AttemptResult transitionInto(WalkStateGraphContext walkStateGraphContext) {
throw new IllegalStateException("SM not implemented");
}
}
public static final class ResumeStreamStateDescriptor extends StateDescriptor {
private ResumeStreamStateDescriptor() {
super(StreamManagementModule.ResumeStreamState.class, 198, StateDescriptor.Property.notImplemented);
addPredeccessor(AuthenticatedButUnboundStateDescriptor.class);
addSuccessor(AuthenticatedAndResourceBoundStateDescriptor.class);
declarePrecedenceOver(ResourceBindingStateDescriptor.class);
declareInferiorityTo(CompressionStateDescriptor.class);
}
@Override
protected StreamManagementModule.ResumeStreamState constructState(ModularXmppClientToServerConnectionInternal connectionInternal) {
StreamManagementModule smModule = connectionInternal.connection.getConnectionModuleFor(StreamManagementModuleDescriptor.class);
return smModule.constructResumeStreamState(this, connectionInternal);
}
}
private ResumeStreamState constructResumeStreamState(
ResumeStreamStateDescriptor resumeStreamStateDescriptor,
ModularXmppClientToServerConnectionInternal connectionInternal) {
return new ResumeStreamState(resumeStreamStateDescriptor, connectionInternal);
}
private final class ResumeStreamState extends State {
private ResumeStreamState(ResumeStreamStateDescriptor stateDescriptor,
ModularXmppClientToServerConnectionInternal connectionInternal) {
super(stateDescriptor, connectionInternal);
}
@Override
public StateTransitionResult.TransitionImpossible isTransitionToPossible(WalkStateGraphContext walkStateGraphContext) {
if (!useSmResumption) {
return new StateTransitionResult.TransitionImpossibleReason("Stream resumption not enabled");
}
return new StateTransitionResult.TransitionImpossibleBecauseNotImplemented(stateDescriptor);
}
@Override
public StateTransitionResult.AttemptResult transitionInto(WalkStateGraphContext walkStateGraphContext) {
throw new IllegalStateException("Stream resumption not implemented");
}
}
public void setStreamManagementEnabled(boolean useSm) {
this.useSm = useSm;
}
public void setStreamResumptionEnabled(boolean useSmResumption) {
this.useSmResumption = useSmResumption;
}
}

View file

@ -0,0 +1,58 @@
/**
*
* Copyright 2019-2020 Florian Schmaus
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smack.sm;
import java.util.HashSet;
import java.util.Set;
import org.jivesoftware.smack.c2s.ModularXmppClientToServerConnectionConfiguration;
import org.jivesoftware.smack.c2s.ModularXmppClientToServerConnectionModuleDescriptor;
import org.jivesoftware.smack.c2s.internal.ModularXmppClientToServerConnectionInternal;
import org.jivesoftware.smack.fsm.StateDescriptor;
import org.jivesoftware.smack.sm.StreamManagementModule.EnableStreamManagementStateDescriptor;
import org.jivesoftware.smack.sm.StreamManagementModule.ResumeStreamStateDescriptor;
public class StreamManagementModuleDescriptor extends ModularXmppClientToServerConnectionModuleDescriptor {
private static final StreamManagementModuleDescriptor INSTANCE = new StreamManagementModuleDescriptor();
@Override
protected Set<Class<? extends StateDescriptor>> getStateDescriptors() {
Set<Class<? extends StateDescriptor>> res = new HashSet<>();
res.add(EnableStreamManagementStateDescriptor.class);
res.add(ResumeStreamStateDescriptor.class);
return res;
}
@Override
protected StreamManagementModule constructXmppConnectionModule(
ModularXmppClientToServerConnectionInternal connectionInternal) {
return new StreamManagementModule(this, connectionInternal);
}
public static class Builder extends ModularXmppClientToServerConnectionModuleDescriptor.Builder {
protected Builder(ModularXmppClientToServerConnectionConfiguration.Builder connectionConfigurationBuilder) {
super(connectionConfigurationBuilder);
}
@Override
protected StreamManagementModuleDescriptor build() {
return INSTANCE;
}
}
}

View file

@ -0,0 +1,21 @@
/**
*
* Copyright 2015 Florian Schmaus
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Smack's implementation of XEP-0198: Stream Management.
*/
package org.jivesoftware.smack.sm;

View file

@ -0,0 +1,365 @@
/**
*
* Copyright © 2014-2018 Florian Schmaus
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smack.sm.packet;
import java.util.Collections;
import java.util.List;
import org.jivesoftware.smack.packet.ExtensionElement;
import org.jivesoftware.smack.packet.Nonza;
import org.jivesoftware.smack.packet.StanzaError;
import org.jivesoftware.smack.packet.StanzaErrorTextElement;
import org.jivesoftware.smack.util.XmlStringBuilder;
public class StreamManagement {
public static final String NAMESPACE = "urn:xmpp:sm:3";
public static final class StreamManagementFeature implements ExtensionElement {
public static final String ELEMENT = "sm";
public static final StreamManagementFeature INSTANCE = new StreamManagementFeature();
private StreamManagementFeature() {
}
@Override
public String getElementName() {
return ELEMENT;
}
@Override
public String getNamespace() {
return NAMESPACE;
}
@Override
public CharSequence toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
XmlStringBuilder xml = new XmlStringBuilder(this, enclosingNamespace);
xml.closeEmptyElement();
return xml;
}
}
private abstract static class AbstractEnable implements Nonza {
/**
* Preferred maximum resumption time in seconds (optional).
*/
protected int max = -1;
protected boolean resume = false;
protected void maybeAddResumeAttributeTo(XmlStringBuilder xml) {
if (resume) {
// XEP 198 never mentions the case where resume='false', it's either set to true or
// not set at all. We reflect this in this code part
xml.attribute("resume", "true");
}
}
protected void maybeAddMaxAttributeTo(XmlStringBuilder xml) {
if (max > 0) {
xml.attribute("max", Integer.toString(max));
}
}
public boolean isResumeSet() {
return resume;
}
/**
* Return the max resumption time in seconds.
* @return the max resumption time in seconds
*/
public int getMaxResumptionTime() {
return max;
}
@Override
public final String getNamespace() {
return NAMESPACE;
}
}
public static class Enable extends AbstractEnable {
public static final String ELEMENT = "enable";
public static final Enable INSTANCE = new Enable();
private Enable() {
}
public Enable(boolean resume) {
this.resume = resume;
}
public Enable(boolean resume, int max) {
this(resume);
this.max = max;
}
@Override
public CharSequence toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
XmlStringBuilder xml = new XmlStringBuilder(this, enclosingNamespace);
maybeAddResumeAttributeTo(xml);
maybeAddMaxAttributeTo(xml);
xml.closeEmptyElement();
return xml;
}
@Override
public String getElementName() {
return ELEMENT;
}
}
/**
* A Stream Management 'enabled' element.
* <p>
* Here is a full example, all attributes besides 'xmlns' are optional.
* </p>
* <pre>
* {@code
* <enabled xmlns='urn:xmpp:sm:3'
* id='some-long-sm-id'
* location='[2001:41D0:1:A49b::1]:9222'
* resume='true'/>
* }
* </pre>
*/
public static class Enabled extends AbstractEnable {
public static final String ELEMENT = "enabled";
/**
* The stream id ("SM-ID")
*/
private final String id;
/**
* The location where the server prefers reconnection.
*/
private final String location;
public Enabled(String id, boolean resume) {
this(id, resume, null, -1);
}
public Enabled(String id, boolean resume, String location, int max) {
this.id = id;
this.resume = resume;
this.location = location;
this.max = max;
}
public String getId() {
return id;
}
public String getLocation() {
return location;
}
@Override
public CharSequence toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
XmlStringBuilder xml = new XmlStringBuilder(this, enclosingNamespace);
xml.optAttribute("id", id);
maybeAddResumeAttributeTo(xml);
xml.optAttribute("location", location);
maybeAddMaxAttributeTo(xml);
xml.closeEmptyElement();
return xml;
}
@Override
public String getElementName() {
return ELEMENT;
}
}
public static class Failed implements Nonza {
public static final String ELEMENT = "failed";
private final StanzaError.Condition condition;
private final List<StanzaErrorTextElement> textElements;
public Failed() {
this(null, null);
}
public Failed(StanzaError.Condition condition, List<StanzaErrorTextElement> textElements) {
this.condition = condition;
if (textElements == null) {
this.textElements = Collections.emptyList();
} else {
this.textElements = Collections.unmodifiableList(textElements);
}
}
public StanzaError.Condition getStanzaErrorCondition() {
return condition;
}
public List<StanzaErrorTextElement> getTextElements() {
return textElements;
}
@Override
public CharSequence toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
XmlStringBuilder xml = new XmlStringBuilder(this, enclosingNamespace);
if (condition == null && textElements.isEmpty()) {
xml.closeEmptyElement();
} else {
if (condition != null) {
xml.rightAngleBracket();
xml.append(condition.toString());
xml.xmlnsAttribute(StanzaError.ERROR_CONDITION_AND_TEXT_NAMESPACE);
xml.closeEmptyElement();
}
xml.append(textElements);
xml.closeElement(ELEMENT);
}
return xml;
}
@Override
public String getNamespace() {
return NAMESPACE;
}
@Override
public String getElementName() {
return ELEMENT;
}
}
private abstract static class AbstractResume implements Nonza {
private final long handledCount;
private final String previd;
private AbstractResume(long handledCount, String previd) {
this.handledCount = handledCount;
this.previd = previd;
}
public long getHandledCount() {
return handledCount;
}
public String getPrevId() {
return previd;
}
@Override
public final String getNamespace() {
return NAMESPACE;
}
@Override
public final XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
XmlStringBuilder xml = new XmlStringBuilder(this, enclosingNamespace);
xml.attribute("h", Long.toString(handledCount));
xml.attribute("previd", previd);
xml.closeEmptyElement();
return xml;
}
}
public static class Resume extends AbstractResume {
public static final String ELEMENT = "resume";
public Resume(long handledCount, String previd) {
super(handledCount, previd);
}
@Override
public String getElementName() {
return ELEMENT;
}
}
public static class Resumed extends AbstractResume {
public static final String ELEMENT = "resumed";
public Resumed(long handledCount, String previd) {
super(handledCount, previd);
}
@Override
public String getElementName() {
return ELEMENT;
}
}
public static class AckAnswer implements Nonza {
public static final String ELEMENT = "a";
private final long handledCount;
public AckAnswer(long handledCount) {
this.handledCount = handledCount;
}
public long getHandledCount() {
return handledCount;
}
@Override
public CharSequence toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
XmlStringBuilder xml = new XmlStringBuilder(this, enclosingNamespace);
xml.attribute("h", Long.toString(handledCount));
xml.closeEmptyElement();
return xml;
}
@Override
public String getNamespace() {
return NAMESPACE;
}
@Override
public String getElementName() {
return ELEMENT;
}
}
public static final class AckRequest implements Nonza {
public static final String ELEMENT = "r";
public static final AckRequest INSTANCE = new AckRequest();
private AckRequest() {
}
@Override
public CharSequence toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
return '<' + ELEMENT + " xmlns='" + NAMESPACE + "'/>";
}
@Override
public String getNamespace() {
return NAMESPACE;
}
@Override
public String getElementName() {
return ELEMENT;
}
}
}

View file

@ -0,0 +1,21 @@
/**
*
* Copyright 2015 Florian Schmaus
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Plain stream elements for XEP-0198: Stream Management.
*/
package org.jivesoftware.smack.sm.packet;

View file

@ -0,0 +1,45 @@
/**
*
* Copyright © 2014 Florian Schmaus
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smack.sm.predicates;
import org.jivesoftware.smack.filter.StanzaFilter;
import org.jivesoftware.smack.packet.Stanza;
public class AfterXStanzas implements StanzaFilter {
final int count;
int currentCount;
public AfterXStanzas(int count) {
this.count = count;
currentCount = 0;
}
@Override
public synchronized boolean accept(Stanza packet) {
currentCount++;
if (currentCount == count) {
resetCounter();
return true;
}
return false;
}
public synchronized void resetCounter() {
currentCount = 0;
}
}

View file

@ -0,0 +1,38 @@
/**
*
* Copyright © 2014 Florian Schmaus
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smack.sm.predicates;
import org.jivesoftware.smack.filter.StanzaFilter;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Stanza;
public final class ForEveryMessage implements StanzaFilter {
public static final ForEveryMessage INSTANCE = new ForEveryMessage();
private ForEveryMessage() {
}
@Override
public boolean accept(Stanza packet) {
if (packet instanceof Message) {
return true;
}
return false;
}
}

View file

@ -0,0 +1,34 @@
/**
*
* Copyright © 2014 Florian Schmaus
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smack.sm.predicates;
import org.jivesoftware.smack.filter.StanzaFilter;
import org.jivesoftware.smack.packet.Stanza;
public final class ForEveryStanza implements StanzaFilter {
public static final ForEveryStanza INSTANCE = new ForEveryStanza();
private ForEveryStanza() {
}
@Override
public boolean accept(Stanza packet) {
return true;
}
}

View file

@ -0,0 +1,40 @@
/**
*
* Copyright © 2014 Florian Schmaus
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smack.sm.predicates;
import org.jivesoftware.smack.filter.StanzaFilter;
import org.jivesoftware.smack.packet.Stanza;
public class ForMatchingPredicateOrAfterXStanzas implements StanzaFilter {
private final StanzaFilter predicate;
private final AfterXStanzas afterXStanzas;
public ForMatchingPredicateOrAfterXStanzas(StanzaFilter predicate, int count) {
this.predicate = predicate;
this.afterXStanzas = new AfterXStanzas(count);
}
@Override
public boolean accept(Stanza packet) {
if (predicate.accept(packet)) {
afterXStanzas.resetCounter();
return true;
}
return afterXStanzas.accept(packet);
}
}

View file

@ -0,0 +1,30 @@
/**
*
* Copyright © 2014 Florian Schmaus
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smack.sm.predicates;
import org.jivesoftware.smack.filter.StanzaFilter;
public class Predicate {
public static StanzaFilter forMessagesOrAfter5Stanzas() {
return new ForMatchingPredicateOrAfterXStanzas(ForEveryMessage.INSTANCE, 5);
}
public static AfterXStanzas after5Stanzas() {
return new AfterXStanzas(5);
}
}

View file

@ -0,0 +1,54 @@
/**
*
* Copyright © 2014 Florian Schmaus
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smack.sm.predicates;
import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.Set;
import org.jivesoftware.smack.filter.StanzaFilter;
import org.jivesoftware.smack.packet.Stanza;
public class ShortcutPredicates implements StanzaFilter {
private final Set<StanzaFilter> predicates = new LinkedHashSet<>();
public ShortcutPredicates() {
}
public ShortcutPredicates(Collection<? extends StanzaFilter> predicates) {
this.predicates.addAll(predicates);
}
public boolean addPredicate(StanzaFilter predicate) {
return predicates.add(predicate);
}
public boolean removePredicate(StanzaFilter prediacte) {
return predicates.remove(prediacte);
}
@Override
public boolean accept(Stanza packet) {
for (StanzaFilter predicate : predicates) {
if (predicate.accept(packet)) {
return true;
}
}
return false;
}
}

View file

@ -0,0 +1,21 @@
/**
*
* Copyright 2015 Florian Schmaus
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Predicates for requesting Stream Management acknowledgements.
*/
package org.jivesoftware.smack.sm.predicates;

View file

@ -0,0 +1,109 @@
/**
*
* Copyright © 2014-2018 Florian Schmaus
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smack.sm.provider;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.jivesoftware.smack.packet.AbstractTextElement;
import org.jivesoftware.smack.packet.StanzaError;
import org.jivesoftware.smack.packet.StanzaErrorTextElement;
import org.jivesoftware.smack.sm.packet.StreamManagement.AckAnswer;
import org.jivesoftware.smack.sm.packet.StreamManagement.AckRequest;
import org.jivesoftware.smack.sm.packet.StreamManagement.Enabled;
import org.jivesoftware.smack.sm.packet.StreamManagement.Failed;
import org.jivesoftware.smack.sm.packet.StreamManagement.Resumed;
import org.jivesoftware.smack.util.ParserUtils;
import org.jivesoftware.smack.xml.XmlPullParser;
import org.jivesoftware.smack.xml.XmlPullParserException;
public class ParseStreamManagement {
public static Enabled enabled(XmlPullParser parser) throws XmlPullParserException, IOException {
ParserUtils.assertAtStartTag(parser);
boolean resume = ParserUtils.getBooleanAttribute(parser, "resume", false);
String id = parser.getAttributeValue("", "id");
String location = parser.getAttributeValue("", "location");
int max = ParserUtils.getIntegerAttribute(parser, "max", -1);
parser.next();
ParserUtils.assertAtEndTag(parser);
return new Enabled(id, resume, location, max);
}
public static Failed failed(XmlPullParser parser) throws XmlPullParserException, IOException {
ParserUtils.assertAtStartTag(parser);
String name;
StanzaError.Condition condition = null;
List<StanzaErrorTextElement> textElements = new ArrayList<>(4);
outerloop:
while (true) {
XmlPullParser.Event event = parser.next();
switch (event) {
case START_ELEMENT:
name = parser.getName();
String namespace = parser.getNamespace();
if (StanzaError.ERROR_CONDITION_AND_TEXT_NAMESPACE.equals(namespace)) {
if (name.equals(AbstractTextElement.ELEMENT)) {
String lang = ParserUtils.getXmlLang(parser);
String text = parser.nextText();
StanzaErrorTextElement stanzaErrorTextElement = new StanzaErrorTextElement(text, lang);
textElements.add(stanzaErrorTextElement);
} else {
condition = StanzaError.Condition.fromString(name);
}
}
break;
case END_ELEMENT:
name = parser.getName();
if (Failed.ELEMENT.equals(name)) {
break outerloop;
}
break;
default:
// Catch all for incomplete switch (MissingCasesInEnumSwitch) statement.
break;
}
}
ParserUtils.assertAtEndTag(parser);
return new Failed(condition, textElements);
}
public static Resumed resumed(XmlPullParser parser) throws XmlPullParserException, IOException {
ParserUtils.assertAtStartTag(parser);
long h = ParserUtils.getLongAttribute(parser, "h");
String previd = parser.getAttributeValue("", "previd");
parser.next();
ParserUtils.assertAtEndTag(parser);
return new Resumed(h, previd);
}
public static AckAnswer ackAnswer(XmlPullParser parser) throws XmlPullParserException, IOException {
ParserUtils.assertAtStartTag(parser);
long h = ParserUtils.getLongAttribute(parser, "h");
parser.next();
ParserUtils.assertAtEndTag(parser);
return new AckAnswer(h);
}
public static AckRequest ackRequest(XmlPullParser parser) throws XmlPullParserException, IOException {
ParserUtils.assertAtStartTag(parser);
parser.next();
ParserUtils.assertAtEndTag(parser);
return AckRequest.INSTANCE;
}
}

View file

@ -0,0 +1,32 @@
/**
*
* Copyright © 2014 Florian Schmaus
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smack.sm.provider;
import org.jivesoftware.smack.packet.XmlEnvironment;
import org.jivesoftware.smack.provider.ExtensionElementProvider;
import org.jivesoftware.smack.sm.packet.StreamManagement.StreamManagementFeature;
import org.jivesoftware.smack.xml.XmlPullParser;
public class StreamManagementStreamFeatureProvider extends ExtensionElementProvider<StreamManagementFeature> {
@Override
public StreamManagementFeature parse(XmlPullParser parser,
int initialDepth, XmlEnvironment xmlEnvironment) {
return StreamManagementFeature.INSTANCE;
}
}

View file

@ -0,0 +1,21 @@
/**
*
* Copyright 2015 Florian Schmaus
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Providers for XEP-0198: Stream Management.
*/
package org.jivesoftware.smack.sm.provider;

View file

@ -0,0 +1,168 @@
/**
*
* Copyright 2014 Vyacheslav Blinov, 2017-2019 Florian Schmaus
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smack.sm.provider;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.io.IOException;
import java.util.List;
import java.util.Properties;
import org.jivesoftware.smack.packet.StanzaError;
import org.jivesoftware.smack.packet.StanzaErrorTextElement;
import org.jivesoftware.smack.sm.packet.StreamManagement;
import org.jivesoftware.smack.util.PacketParserUtils;
import org.jivesoftware.smack.xml.XmlPullParser;
import org.jivesoftware.smack.xml.XmlPullParserException;
import com.jamesmurty.utils.XMLBuilder;
import org.junit.jupiter.api.Test;
public class ParseStreamManagementTest {
private static final Properties outputProperties = initOutputProperties();
@Test
public void testParseEnabled() throws Exception {
String stanzaID = "zid615d9";
boolean resume = true;
String location = "test";
int max = 42;
String enabledStanza = XMLBuilder.create("enabled")
.a("xmlns", "urn:xmpp:sm:3")
.a("id", "zid615d9")
.a("resume", String.valueOf(resume))
.a("location", location)
.a("max", String.valueOf(max))
.asString(outputProperties);
StreamManagement.Enabled enabledPacket = ParseStreamManagement.enabled(
PacketParserUtils.getParserFor(enabledStanza));
assertNotNull(enabledPacket);
assertEquals(enabledPacket.getId(), stanzaID);
assertEquals(location, enabledPacket.getLocation());
assertEquals(resume, enabledPacket.isResumeSet());
assertEquals(max, enabledPacket.getMaxResumptionTime());
}
@Test
public void testParseEnabledInvariant() throws XmlPullParserException, IOException {
String enabledString = new StreamManagement.Enabled("stream-id", false).toXML().toString();
XmlPullParser parser = PacketParserUtils.getParserFor(enabledString);
StreamManagement.Enabled enabled = ParseStreamManagement.enabled(parser);
assertEquals(enabledString, enabled.toXML().toString());
}
@Test
public void testParseFailed() throws Exception {
String failedStanza = XMLBuilder.create("failed")
.a("xmlns", "urn:xmpp:sm:3")
.asString(outputProperties);
StreamManagement.Failed failedPacket = ParseStreamManagement.failed(
PacketParserUtils.getParserFor(failedStanza));
assertNotNull(failedPacket);
assertTrue(failedPacket.getStanzaErrorCondition() == null);
}
@Test
public void testParseFailedError() throws Exception {
StanzaError.Condition errorCondition = StanzaError.Condition.unexpected_request;
String failedStanza = XMLBuilder.create("failed")
.a("xmlns", "urn:xmpp:sm:3")
.element(errorCondition.toString(), StanzaError.ERROR_CONDITION_AND_TEXT_NAMESPACE)
.asString(outputProperties);
StreamManagement.Failed failedPacket = ParseStreamManagement.failed(
PacketParserUtils.getParserFor(failedStanza));
assertNotNull(failedPacket);
assertTrue(failedPacket.getStanzaErrorCondition() == errorCondition);
}
@Test
public void testParseFailedWithTExt() throws XmlPullParserException, IOException {
// @formatter:off
final String failedNonza = "<failed h='20' xmlns='urn:xmpp:sm:3'>"
+ "<item-not-found xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/>"
+ "<text xml:lang='en' xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'>"
+ "Previous session timed out"
+ "</text>"
+ "</failed>";
// @formatter:on
XmlPullParser parser = PacketParserUtils.getParserFor(failedNonza);
StreamManagement.Failed failed = ParseStreamManagement.failed(parser);
assertEquals(StanzaError.Condition.item_not_found, failed.getStanzaErrorCondition());
List<StanzaErrorTextElement> textElements = failed.getTextElements();
assertEquals(1, textElements.size());
StanzaErrorTextElement textElement = textElements.get(0);
assertEquals("Previous session timed out", textElement.getText());
assertEquals("en", textElement.getLanguage());
}
@Test
public void testParseResumed() throws Exception {
long handledPackets = 42;
String previousID = "zid615d9";
String resumedStanza = XMLBuilder.create("resumed")
.a("xmlns", "urn:xmpp:sm:3")
.a("h", String.valueOf(handledPackets))
.a("previd", previousID)
.asString(outputProperties);
StreamManagement.Resumed resumedPacket = ParseStreamManagement.resumed(
PacketParserUtils.getParserFor(resumedStanza));
assertNotNull(resumedPacket);
assertEquals(handledPackets, resumedPacket.getHandledCount());
assertEquals(previousID, resumedPacket.getPrevId());
}
@Test
public void testParseAckAnswer() throws Exception {
long handledPackets = 42 + 42;
String ackStanza = XMLBuilder.create("a")
.a("xmlns", "urn:xmpp:sm:3")
.a("h", String.valueOf(handledPackets))
.asString(outputProperties);
StreamManagement.AckAnswer acknowledgementPacket = ParseStreamManagement.ackAnswer(
PacketParserUtils.getParserFor(ackStanza));
assertNotNull(acknowledgementPacket);
assertEquals(handledPackets, acknowledgementPacket.getHandledCount());
}
private static Properties initOutputProperties() {
Properties properties = new Properties();
properties.put(javax.xml.transform.OutputKeys.OMIT_XML_DECLARATION, "yes");
return properties;
}
}