mirror of
https://codeberg.org/Mercury-IM/Smack
synced 2025-12-14 00:51:19 +01:00
Enable PacketExtensions for IQs
This is actually only part one, i.e. with this commit if the user adds a PacketExtension to an IQ it will be included in IQ.toXml(). Which was previously only the case if the IQ subclass explicitly included packet extensions. The second part of the change is to change the IQ provider, so that packet extensions are automatically parsed. Cases where PacketExtensions are used for Message and IQ are slightly changed. The IQ sublcass now only has a field with this PacketExtension (see for example bytestreams.ibb.packet.DataPacketExtension). Also changed hoxt API: Removed unnecessary indirection and made the API more Smack idiomatic.
This commit is contained in:
parent
a9c798f3bb
commit
9e797c1b17
93 changed files with 1347 additions and 1438 deletions
|
|
@ -27,6 +27,7 @@ import org.jivesoftware.smack.packet.IQ;
|
|||
import org.jivesoftware.smack.provider.IQProvider;
|
||||
import org.jivesoftware.smackx.iqprivate.packet.DefaultPrivateData;
|
||||
import org.jivesoftware.smackx.iqprivate.packet.PrivateData;
|
||||
import org.jivesoftware.smackx.iqprivate.packet.PrivateDataIQ;
|
||||
import org.jivesoftware.smackx.iqprivate.provider.PrivateDataProvider;
|
||||
import org.xmlpull.v1.XmlPullParser;
|
||||
import org.xmlpull.v1.XmlPullParserException;
|
||||
|
|
@ -156,18 +157,9 @@ public class PrivateDataManager extends Manager {
|
|||
public PrivateData getPrivateData(final String elementName, final String namespace) throws NoResponseException, XMPPErrorException, NotConnectedException
|
||||
{
|
||||
// Create an IQ packet to get the private data.
|
||||
IQ privateDataGet = new IQ() {
|
||||
public String getChildElementXML() {
|
||||
StringBuilder buf = new StringBuilder();
|
||||
buf.append("<query xmlns=\"jabber:iq:private\">");
|
||||
buf.append("<").append(elementName).append(" xmlns=\"").append(namespace).append("\"/>");
|
||||
buf.append("</query>");
|
||||
return buf.toString();
|
||||
}
|
||||
};
|
||||
privateDataGet.setType(IQ.Type.get);
|
||||
IQ privateDataGet = new PrivateDataIQ(elementName, namespace);
|
||||
|
||||
PrivateDataResult response = (PrivateDataResult) connection().createPacketCollectorAndSend(
|
||||
PrivateDataIQ response = connection().createPacketCollectorAndSend(
|
||||
privateDataGet).nextResultOrThrow();
|
||||
return response.getPrivateData();
|
||||
}
|
||||
|
|
@ -184,16 +176,7 @@ public class PrivateDataManager extends Manager {
|
|||
*/
|
||||
public void setPrivateData(final PrivateData privateData) throws NoResponseException, XMPPErrorException, NotConnectedException {
|
||||
// Create an IQ packet to set the private data.
|
||||
IQ privateDataSet = new IQ() {
|
||||
public String getChildElementXML() {
|
||||
StringBuilder buf = new StringBuilder();
|
||||
buf.append("<query xmlns=\"jabber:iq:private\">");
|
||||
buf.append(privateData.toXML());
|
||||
buf.append("</query>");
|
||||
return buf.toString();
|
||||
}
|
||||
};
|
||||
privateDataSet.setType(IQ.Type.set);
|
||||
IQ privateDataSet = new PrivateDataIQ(privateData);
|
||||
|
||||
connection().createPacketCollectorAndSend(privateDataSet).nextResultOrThrow();
|
||||
}
|
||||
|
|
@ -214,10 +197,10 @@ public class PrivateDataManager extends Manager {
|
|||
/**
|
||||
* An IQ provider to parse IQ results containing private data.
|
||||
*/
|
||||
public static class PrivateDataIQProvider extends IQProvider<PrivateDataResult> {
|
||||
public static class PrivateDataIQProvider extends IQProvider<PrivateDataIQ> {
|
||||
|
||||
@Override
|
||||
public PrivateDataResult parse(XmlPullParser parser, int initialDepth)
|
||||
public PrivateDataIQ parse(XmlPullParser parser, int initialDepth)
|
||||
throws XmlPullParserException, IOException, SmackException {
|
||||
PrivateData privateData = null;
|
||||
boolean done = false;
|
||||
|
|
@ -268,33 +251,7 @@ public class PrivateDataManager extends Manager {
|
|||
}
|
||||
}
|
||||
}
|
||||
return new PrivateDataResult(privateData);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* An IQ packet to hold PrivateData GET results.
|
||||
*/
|
||||
private static class PrivateDataResult extends IQ {
|
||||
|
||||
private PrivateData privateData;
|
||||
|
||||
PrivateDataResult(PrivateData privateData) {
|
||||
this.privateData = privateData;
|
||||
}
|
||||
|
||||
public PrivateData getPrivateData() {
|
||||
return privateData;
|
||||
}
|
||||
|
||||
public String getChildElementXML() {
|
||||
StringBuilder buf = new StringBuilder();
|
||||
buf.append("<query xmlns=\"jabber:iq:private\">");
|
||||
if (privateData != null) {
|
||||
buf.append(privateData.toXML());
|
||||
}
|
||||
buf.append("</query>");
|
||||
return buf.toString();
|
||||
return new PrivateDataIQ(privateData);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,61 @@
|
|||
/**
|
||||
*
|
||||
* 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.smackx.iqprivate.packet;
|
||||
|
||||
import org.jivesoftware.smack.packet.IQ;
|
||||
|
||||
public class PrivateDataIQ extends IQ {
|
||||
|
||||
public static final String ELEMENT = QUERY_ELEMENT;
|
||||
public static final String NAMESPACE = "jabber:iq:private";
|
||||
|
||||
private final PrivateData privateData;
|
||||
private final String getElement;
|
||||
private final String getNamespace;
|
||||
|
||||
public PrivateDataIQ(PrivateData privateData) {
|
||||
this(privateData, null, null);
|
||||
setType(Type.set);
|
||||
}
|
||||
|
||||
public PrivateDataIQ(String element, String namespace) {
|
||||
this(null, element, namespace);
|
||||
setType(Type.get);
|
||||
}
|
||||
|
||||
private PrivateDataIQ(PrivateData privateData, String getElement, String getNamespace) {
|
||||
super(ELEMENT, NAMESPACE);
|
||||
this.privateData = privateData;
|
||||
this.getElement = getElement;
|
||||
this.getNamespace = getNamespace;
|
||||
}
|
||||
|
||||
public PrivateData getPrivateData() {
|
||||
return privateData;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder xml) {
|
||||
xml.rightAngleBracket();
|
||||
if (privateData != null) {
|
||||
xml.append(privateData.toXML());
|
||||
} else {
|
||||
xml.halfOpenElement(getElement).xmlnsAttribute(getNamespace).closeEmptyElement();
|
||||
}
|
||||
return xml;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue