1
0
Fork 0
mirror of https://codeberg.org/Mercury-IM/Smack synced 2024-11-21 22:02:06 +01:00

Merge pull request #420 from adiaholic/streamFactory

Introduce StreamOpenFactory for modular architecture
This commit is contained in:
Florian Schmaus 2020-08-18 16:22:27 +02:00 committed by GitHub
commit 9cec02b5e3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 171 additions and 52 deletions

View file

@ -2243,7 +2243,10 @@ public abstract class AbstractXMPPConnection implements XMPPConnection {
StreamOpen streamOpen = new StreamOpen(to, from, id, config.getXmlLang(), StreamOpen.StreamContentNamespace.client);
sendNonza(streamOpen);
updateOutgoingStreamXmlEnvironmentOnStreamOpen(streamOpen);
}
protected void updateOutgoingStreamXmlEnvironmentOnStreamOpen(StreamOpen streamOpen) {
XmlEnvironment.Builder xmlEnvironmentBuilder = XmlEnvironment.builder();
xmlEnvironmentBuilder.with(streamOpen);
outgoingStreamXmlEnvironment = xmlEnvironmentBuilder.build();

View file

@ -68,6 +68,7 @@ import org.jivesoftware.smack.packet.Presence;
import org.jivesoftware.smack.packet.Stanza;
import org.jivesoftware.smack.packet.StreamClose;
import org.jivesoftware.smack.packet.StreamError;
import org.jivesoftware.smack.packet.StreamOpen;
import org.jivesoftware.smack.packet.TopLevelStreamElement;
import org.jivesoftware.smack.packet.XmlEnvironment;
import org.jivesoftware.smack.parsing.SmackParsingException;
@ -81,7 +82,9 @@ import org.jivesoftware.smack.util.Supplier;
import org.jivesoftware.smack.xml.XmlPullParser;
import org.jivesoftware.smack.xml.XmlPullParserException;
import org.jxmpp.jid.DomainBareJid;
import org.jxmpp.jid.parts.Resourcepart;
import org.jxmpp.util.XmppStringUtils;
public final class ModularXmppClientToServerConnection extends AbstractXMPPConnection {
@ -560,10 +563,26 @@ public final class ModularXmppClientToServerConnection extends AbstractXMPPConne
protected void newStreamOpenWaitForFeaturesSequence(String waitFor) throws InterruptedException,
SmackException, XMPPException {
prepareToWaitForFeaturesReceived();
sendStreamOpen();
// Create StreamOpen from StreamOpenAndCloseFactory via underlying transport.
StreamOpenAndCloseFactory streamOpenAndCloseFactory = activeTransport.getStreamOpenAndCloseFactory();
CharSequence from = null;
CharSequence localpart = connectionInternal.connection.getConfiguration().getUsername();
DomainBareJid xmppServiceDomain = getXMPPServiceDomain();
if (localpart != null) {
from = XmppStringUtils.completeJidFrom(localpart, xmppServiceDomain);
}
StreamOpen streamOpen = streamOpenAndCloseFactory.createStreamOpen(xmppServiceDomain, from, getStreamId(), getConfiguration().getXmlLang());
sendStreamOpen(streamOpen);
waitForFeaturesReceived(waitFor);
}
private void sendStreamOpen(StreamOpen streamOpen) throws NotConnectedException, InterruptedException {
sendNonza(streamOpen);
updateOutgoingStreamXmlEnvironmentOnStreamOpen(streamOpen);
}
public static class DisconnectedStateDescriptor extends StateDescriptor {
protected DisconnectedStateDescriptor() {
super(DisconnectedState.class, StateDescriptor.Property.finalState);

View file

@ -0,0 +1,26 @@
/**
*
* Copyright 2020 Aditya Borikar.
*
* 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.c2s;
import org.jivesoftware.smack.packet.StreamClose;
import org.jivesoftware.smack.packet.StreamOpen;
public interface StreamOpenAndCloseFactory {
StreamOpen createStreamOpen(CharSequence to, CharSequence from, String id, String lang);
StreamClose createStreamClose();
}

View file

@ -58,6 +58,8 @@ public abstract class XmppClientToServerTransport {
return getSslSession() != null;
}
public abstract StreamOpenAndCloseFactory getStreamOpenAndCloseFactory();
public abstract Stats getStats();
public abstract static class Stats {

View file

@ -0,0 +1,20 @@
/**
*
* Copyright 2020 Aditya Borikar
*
* 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.packet;
public abstract class AbstractStreamClose implements Nonza {
}

View file

@ -0,0 +1,79 @@
/**
*
* Copyright 2020 Aditya Borikar
*
* 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.packet;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.packet.StreamOpen.StreamContentNamespace;
import org.jivesoftware.smack.util.StringUtils;
/**
* AbstractStreamOpen is actually a {@link TopLevelStreamElement}, however we
* implement {@link Nonza} here. This is because, {@link XMPPConnection} doesn't
* yet support sending {@link TopLevelStreamElement} directly and the same can only
* be achieved through {@link XMPPConnection#sendNonza(Nonza)}.
*/
public abstract class AbstractStreamOpen implements Nonza {
public static final String CLIENT_NAMESPACE = "jabber:client";
public static final String SERVER_NAMESPACE = "jabber:server";
/**
* RFC 6120 § 4.7.5.
*/
public static final String VERSION = "1.0";
/**
* RFC 6120 § 4.7.1.
*/
protected final String from;
/**
* RFC 6120 § 4.7.2.
*/
protected final String to;
/**
* RFC 6120 § 4.7.3.
*/
protected final String id;
/**
* RFC 6120 § 4.7.4.
*/
protected final String lang;
/**
* RFC 6120 § 4.8.2.
*/
protected final String contentNamespace;
public AbstractStreamOpen(CharSequence to, CharSequence from, String id, String lang, StreamContentNamespace ns) {
this.to = StringUtils.maybeToString(to);
this.from = StringUtils.maybeToString(from);
this.id = id;
this.lang = lang;
switch (ns) {
case client:
this.contentNamespace = CLIENT_NAMESPACE;
break;
case server:
this.contentNamespace = SERVER_NAMESPACE;
break;
default:
throw new IllegalStateException();
}
}
}

View file

@ -16,7 +16,7 @@
*/
package org.jivesoftware.smack.packet;
public final class StreamClose implements Nonza {
public final class StreamClose extends AbstractStreamClose {
public static final StreamClose INSTANCE = new StreamClose();

View file

@ -17,49 +17,14 @@
package org.jivesoftware.smack.packet;
import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.smack.util.XmlStringBuilder;
/**
* The stream open <b>tag</b>.
*/
public class StreamOpen implements Nonza {
public final class StreamOpen extends AbstractStreamOpen {
public static final String ELEMENT = "stream:stream";
public static final String CLIENT_NAMESPACE = "jabber:client";
public static final String SERVER_NAMESPACE = "jabber:server";
/**
* RFC 6120 § 4.7.5.
*/
public static final String VERSION = "1.0";
/**
* RFC 6120 § 4.7.1.
*/
private final String from;
/**
* RFC 6120 § 4.7.2.
*/
private final String to;
/**
* RFC 6120 § 4.7.3.
*/
private final String id;
/**
* RFC 6120 § 4.7.4.
*/
private final String lang;
/**
* RFC 6120 § 4.8.2.
*/
private final String contentNamespace;
public StreamOpen(CharSequence to) {
this(to, null, null, null, StreamContentNamespace.client);
}
@ -69,20 +34,7 @@ public class StreamOpen implements Nonza {
}
public StreamOpen(CharSequence to, CharSequence from, String id, String lang, StreamContentNamespace ns) {
this.to = StringUtils.maybeToString(to);
this.from = StringUtils.maybeToString(from);
this.id = id;
this.lang = lang;
switch (ns) {
case client:
this.contentNamespace = CLIENT_NAMESPACE;
break;
case server:
this.contentNamespace = SERVER_NAMESPACE;
break;
default:
throw new IllegalStateException();
}
super(to, from, id, lang, ns);
}
@Override

View file

@ -58,6 +58,7 @@ import org.jivesoftware.smack.XmppInputOutputFilter;
import org.jivesoftware.smack.c2s.ModularXmppClientToServerConnection.ConnectedButUnauthenticatedStateDescriptor;
import org.jivesoftware.smack.c2s.ModularXmppClientToServerConnection.LookupRemoteConnectionEndpointsStateDescriptor;
import org.jivesoftware.smack.c2s.ModularXmppClientToServerConnectionModule;
import org.jivesoftware.smack.c2s.StreamOpenAndCloseFactory;
import org.jivesoftware.smack.c2s.XmppClientToServerTransport;
import org.jivesoftware.smack.c2s.internal.ModularXmppClientToServerConnectionInternal;
import org.jivesoftware.smack.c2s.internal.WalkStateGraphContext;
@ -68,6 +69,7 @@ import org.jivesoftware.smack.fsm.StateTransitionResult;
import org.jivesoftware.smack.internal.SmackTlsContext;
import org.jivesoftware.smack.packet.Stanza;
import org.jivesoftware.smack.packet.StartTls;
import org.jivesoftware.smack.packet.StreamClose;
import org.jivesoftware.smack.packet.StreamOpen;
import org.jivesoftware.smack.packet.TlsFailure;
import org.jivesoftware.smack.packet.TlsProceed;
@ -580,6 +582,22 @@ public class XmppTcpTransportModule extends ModularXmppClientToServerConnectionM
super(connectionInternal);
}
@Override
public StreamOpenAndCloseFactory getStreamOpenAndCloseFactory() {
return new StreamOpenAndCloseFactory() {
@Override
public StreamOpen createStreamOpen(CharSequence to, CharSequence from, String id, String lang) {
String xmlLang = connectionInternal.connection.getConfiguration().getXmlLang();
StreamOpen streamOpen = new StreamOpen(to, from, id, xmlLang, StreamOpen.StreamContentNamespace.client);
return streamOpen;
}
@Override
public StreamClose createStreamClose() {
return StreamClose.INSTANCE;
}
};
}
@Override
protected void resetDiscoveredConnectionEndpoints() {
discoveredTcpEndpoints = null;