From 9fcc97836bf5bb8fb788dc44675bf4e5f50e6f25 Mon Sep 17 00:00:00 2001 From: Aditya Borikar Date: Tue, 18 Aug 2020 10:35:22 +0530 Subject: [PATCH] Introduce AbstractStreamOpen and AbstractStreamClose - Inherit StreamOpen and StreamClose from AbstractStream classes --- .../smack/packet/AbstractStreamClose.java | 20 +++++ .../smack/packet/AbstractStreamOpen.java | 79 +++++++++++++++++++ .../smack/packet/StreamClose.java | 2 +- .../jivesoftware/smack/packet/StreamOpen.java | 52 +----------- 4 files changed, 102 insertions(+), 51 deletions(-) create mode 100644 smack-core/src/main/java/org/jivesoftware/smack/packet/AbstractStreamClose.java create mode 100644 smack-core/src/main/java/org/jivesoftware/smack/packet/AbstractStreamOpen.java diff --git a/smack-core/src/main/java/org/jivesoftware/smack/packet/AbstractStreamClose.java b/smack-core/src/main/java/org/jivesoftware/smack/packet/AbstractStreamClose.java new file mode 100644 index 000000000..c938c7e9e --- /dev/null +++ b/smack-core/src/main/java/org/jivesoftware/smack/packet/AbstractStreamClose.java @@ -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 { +} diff --git a/smack-core/src/main/java/org/jivesoftware/smack/packet/AbstractStreamOpen.java b/smack-core/src/main/java/org/jivesoftware/smack/packet/AbstractStreamOpen.java new file mode 100644 index 000000000..36f0dc760 --- /dev/null +++ b/smack-core/src/main/java/org/jivesoftware/smack/packet/AbstractStreamOpen.java @@ -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(); + } + } +} diff --git a/smack-core/src/main/java/org/jivesoftware/smack/packet/StreamClose.java b/smack-core/src/main/java/org/jivesoftware/smack/packet/StreamClose.java index 377e6c616..fd284a2e5 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/packet/StreamClose.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/packet/StreamClose.java @@ -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(); diff --git a/smack-core/src/main/java/org/jivesoftware/smack/packet/StreamOpen.java b/smack-core/src/main/java/org/jivesoftware/smack/packet/StreamOpen.java index 6ef368b63..f4b59db4b 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/packet/StreamOpen.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/packet/StreamOpen.java @@ -17,49 +17,14 @@ package org.jivesoftware.smack.packet; -import org.jivesoftware.smack.util.StringUtils; import org.jivesoftware.smack.util.XmlStringBuilder; /** * The stream open tag. */ -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