mirror of
https://github.com/vanitasvitae/Smack.git
synced 2024-12-22 10:37:59 +01:00
Rework Bits of Binary implementation
This commit is contained in:
parent
c688acaa0f
commit
9328182912
12 changed files with 166 additions and 300 deletions
|
@ -128,8 +128,9 @@ public class PacketParserUtils {
|
|||
return parser;
|
||||
}
|
||||
|
||||
public static Stanza parseStanza(String stanza) throws Exception {
|
||||
return parseStanza(getParserFor(stanza));
|
||||
@SuppressWarnings("unchecked")
|
||||
public static <S extends Stanza> S parseStanza(String stanza) throws Exception {
|
||||
return (S) parseStanza(getParserFor(stanza));
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
/**
|
||||
*
|
||||
* Copyright 2016 Fernando Ramirez
|
||||
* Copyright 2016-2017 Fernando Ramirez, Florian Schmaus
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
@ -16,34 +16,50 @@
|
|||
*/
|
||||
package org.jivesoftware.smackx.bob;
|
||||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
|
||||
import org.jivesoftware.smack.util.StringUtils;
|
||||
import org.jivesoftware.smack.util.stringencoder.Base64;
|
||||
|
||||
/**
|
||||
* Bits of Binary data class.
|
||||
*
|
||||
* @author Fernando Ramirez
|
||||
* @author Florian Schmaus
|
||||
* @see <a href="http://xmpp.org/extensions/xep-0231.html">XEP-0231: Bits of
|
||||
* Binary</a>
|
||||
*/
|
||||
public class BoBData {
|
||||
|
||||
private final long maxAge;
|
||||
private final int maxAge;
|
||||
private final String type;
|
||||
private final byte[] content;
|
||||
|
||||
private byte[] contentBinary;
|
||||
private String contentString;
|
||||
|
||||
public BoBData(String type, byte[] content) {
|
||||
this(type, content, -1);
|
||||
}
|
||||
|
||||
/**
|
||||
* BoB data constructor.
|
||||
*
|
||||
* @param maxAge
|
||||
* @param type
|
||||
* @param content
|
||||
* @param maxAge
|
||||
*/
|
||||
public BoBData(long maxAge, String type, byte[] content) {
|
||||
this.maxAge = maxAge;
|
||||
public BoBData(String type, byte[] content, int maxAge) {
|
||||
this.type = type;
|
||||
this.content = content;
|
||||
this.contentBinary = content;
|
||||
this.maxAge = maxAge;
|
||||
}
|
||||
|
||||
public BoBData(String type, String content) {
|
||||
this(type, content, -1);
|
||||
}
|
||||
|
||||
public BoBData(String type, String content, int maxAge) {
|
||||
this.type = type;
|
||||
this.contentString = content;
|
||||
this.maxAge = maxAge;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -51,7 +67,7 @@ public class BoBData {
|
|||
*
|
||||
* @return the max age
|
||||
*/
|
||||
public long getMaxAge() {
|
||||
public int getMaxAge() {
|
||||
return maxAge;
|
||||
}
|
||||
|
||||
|
@ -64,13 +80,21 @@ public class BoBData {
|
|||
return type;
|
||||
}
|
||||
|
||||
private void setContentBinaryIfRequired() {
|
||||
if (contentBinary == null) {
|
||||
assert(StringUtils.isNotEmpty(contentString));
|
||||
contentBinary = Base64.decode(contentString);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the content.
|
||||
*
|
||||
* @return the content
|
||||
*/
|
||||
public byte[] getContent() {
|
||||
return content;
|
||||
setContentBinaryIfRequired();
|
||||
return contentBinary.clone();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -78,13 +102,20 @@ public class BoBData {
|
|||
*
|
||||
* @return the content in a Base64 encoded String
|
||||
*/
|
||||
public String getBase64Encoded() {
|
||||
byte[] bytes = Base64.encode(content);
|
||||
try {
|
||||
return new String(bytes, "UTF-8");
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
throw new IllegalStateException("UTF-8 is not supported.");
|
||||
public String getContentBase64Encoded() {
|
||||
if (contentString == null) {
|
||||
contentString = Base64.encodeToString(getContent());
|
||||
}
|
||||
return contentString;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the data is of reasonable size. XEP-0231 suggest that the size should not be more than 8 KiB.
|
||||
*
|
||||
* @return true if the data if of reasonable size.
|
||||
*/
|
||||
public boolean isOfReasonableSize() {
|
||||
setContentBinaryIfRequired();
|
||||
return contentBinary.length <= 8 * 1024;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
/**
|
||||
*
|
||||
* Copyright 2016 Fernando Ramirez
|
||||
* Copyright 2016-2017 Fernando Ramirez, Florian Schmaus
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
@ -16,10 +16,13 @@
|
|||
*/
|
||||
package org.jivesoftware.smackx.bob;
|
||||
|
||||
import org.jivesoftware.smack.util.StringUtils;
|
||||
|
||||
/**
|
||||
* Bits of Binary hash class.
|
||||
*
|
||||
* @author Fernando Ramirez
|
||||
* @author Florian Schmaus
|
||||
* @see <a href="http://xmpp.org/extensions/xep-0231.html">XEP-0231: Bits of
|
||||
* Binary</a>
|
||||
*/
|
||||
|
@ -27,6 +30,7 @@ public class BoBHash {
|
|||
|
||||
private final String hash;
|
||||
private final String hashType;
|
||||
private final String cid;
|
||||
|
||||
/**
|
||||
* BoB hash constructor.
|
||||
|
@ -35,8 +39,9 @@ public class BoBHash {
|
|||
* @param hashType
|
||||
*/
|
||||
public BoBHash(String hash, String hashType) {
|
||||
this.hash = hash;
|
||||
this.hashType = hashType;
|
||||
this.hash = StringUtils.requireNotNullOrEmpty(hash, "hash must not be null or empty");
|
||||
this.hashType = StringUtils.requireNotNullOrEmpty(hashType, "hashType must not be null or empty");
|
||||
this.cid = this.hashType + '+' + this.hash + "@bob.xmpp.org";
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -63,7 +68,7 @@ public class BoBHash {
|
|||
* @return src attribute string
|
||||
*/
|
||||
public String toSrc() {
|
||||
return "cid:" + toCid();
|
||||
return "cid:" + getCid();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -71,8 +76,22 @@ public class BoBHash {
|
|||
*
|
||||
* @return cid attribute string
|
||||
*/
|
||||
public String toCid() {
|
||||
return this.hashType + "+" + this.hash + "@bob.xmpp.org";
|
||||
public String getCid() {
|
||||
return cid;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object other) {
|
||||
if (other instanceof BoBHash) {
|
||||
BoBHash otherBob = (BoBHash) other;
|
||||
return cid.equals(otherBob.cid);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return cid.hashCode();
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
/**
|
||||
*
|
||||
* Copyright 2016 Fernando Ramirez
|
||||
* Copyright 2017 Florian Schmaus
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
@ -16,18 +16,23 @@
|
|||
*/
|
||||
package org.jivesoftware.smackx.bob;
|
||||
|
||||
/**
|
||||
* Bits of Binary Saver Manager interface.
|
||||
*
|
||||
* @author Fernando Ramirez
|
||||
*
|
||||
*/
|
||||
public interface BoBSaverManager {
|
||||
import java.util.Set;
|
||||
|
||||
public void addBoB(BoBHash bobHash, BoBData bobData);
|
||||
public class BoBInfo {
|
||||
|
||||
public void removeBoB(BoBHash bobHash);
|
||||
private final Set<BoBHash> hashes;
|
||||
private final BoBData data;
|
||||
|
||||
public BoBData getBoB(BoBHash bobHash);
|
||||
BoBInfo(Set<BoBHash> hashes, BoBData data) {
|
||||
this.hashes = hashes;
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
public Set<BoBHash> getHashes() {
|
||||
return hashes;
|
||||
}
|
||||
|
||||
public BoBData getData() {
|
||||
return data;
|
||||
}
|
||||
}
|
|
@ -1,6 +1,6 @@
|
|||
/**
|
||||
*
|
||||
* Copyright 2016 Fernando Ramirez
|
||||
* Copyright 2016-2017 Fernando Ramirez, Florian Schmaus
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
@ -16,8 +16,11 @@
|
|||
*/
|
||||
package org.jivesoftware.smackx.bob;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.WeakHashMap;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
import org.jivesoftware.smack.ConnectionCreationListener;
|
||||
import org.jivesoftware.smack.Manager;
|
||||
|
@ -31,27 +34,29 @@ import org.jivesoftware.smack.iqrequest.AbstractIqRequestHandler;
|
|||
import org.jivesoftware.smack.iqrequest.IQRequestHandler.Mode;
|
||||
import org.jivesoftware.smack.packet.IQ;
|
||||
import org.jivesoftware.smack.packet.IQ.Type;
|
||||
import org.jivesoftware.smack.util.SHA1;
|
||||
import org.jivesoftware.smackx.bob.element.BoBIQ;
|
||||
import org.jivesoftware.smackx.disco.ServiceDiscoveryManager;
|
||||
import org.jxmpp.jid.Jid;
|
||||
import org.jxmpp.util.cache.LruCache;
|
||||
|
||||
/**
|
||||
* Bits of Binary manager class.
|
||||
*
|
||||
* @author Fernando Ramirez
|
||||
* @author Florian Schmaus
|
||||
* @see <a href="http://xmpp.org/extensions/xep-0231.html">XEP-0231: Bits of
|
||||
* Binary</a>
|
||||
*/
|
||||
public final class BoBManager extends Manager {
|
||||
|
||||
public static final String NAMESPACE = "urn:xmpp:bob";
|
||||
public static BoBSaverManager bobSaverManager;
|
||||
|
||||
static {
|
||||
XMPPConnectionRegistry.addConnectionCreationListener(new ConnectionCreationListener() {
|
||||
@Override
|
||||
public void connectionCreated(XMPPConnection connection) {
|
||||
getInstanceFor(connection, bobSaverManager);
|
||||
getInstanceFor(connection);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
@ -62,16 +67,9 @@ public final class BoBManager extends Manager {
|
|||
* Get the singleton instance of BoBManager.
|
||||
*
|
||||
* @param connection
|
||||
* @param saverManager
|
||||
* @return the instance of BoBManager
|
||||
*/
|
||||
public static synchronized BoBManager getInstanceFor(XMPPConnection connection, BoBSaverManager saverManager) {
|
||||
if (saverManager == null) {
|
||||
bobSaverManager = new DefaultBoBSaverManager();
|
||||
} else {
|
||||
bobSaverManager = saverManager;
|
||||
}
|
||||
|
||||
public static synchronized BoBManager getInstanceFor(XMPPConnection connection) {
|
||||
BoBManager bobManager = INSTANCES.get(connection);
|
||||
if (bobManager == null) {
|
||||
bobManager = new BoBManager(connection);
|
||||
|
@ -81,37 +79,34 @@ public final class BoBManager extends Manager {
|
|||
return bobManager;
|
||||
}
|
||||
|
||||
private static final LruCache<BoBHash, BoBData> BOB_CACHE = new LruCache<>(128);
|
||||
|
||||
private final Map<BoBHash, BoBInfo> bobs = new ConcurrentHashMap<>();
|
||||
|
||||
private BoBManager(XMPPConnection connection) {
|
||||
super(connection);
|
||||
ServiceDiscoveryManager serviceDiscoveryManager = ServiceDiscoveryManager.getInstanceFor(connection);
|
||||
serviceDiscoveryManager.addFeature(NAMESPACE);
|
||||
|
||||
connection.registerIQRequestHandler(
|
||||
new AbstractIqRequestHandler(BoBIQ.ELEMENT, BoBIQ.NAMESPACE, Type.get, Mode.sync) {
|
||||
new AbstractIqRequestHandler(BoBIQ.ELEMENT, BoBIQ.NAMESPACE, Type.get, Mode.async) {
|
||||
@Override
|
||||
public IQ handleIQRequest(IQ iqRequest) {
|
||||
BoBIQ getBoBIQ = (BoBIQ) iqRequest;
|
||||
BoBIQ bobIQRequest = (BoBIQ) iqRequest;
|
||||
|
||||
BoBData bobData = bobSaverManager.getBoB(getBoBIQ.getBoBHash());
|
||||
BoBIQ responseBoBIQ = null;
|
||||
try {
|
||||
responseBoBIQ = responseBoB(getBoBIQ, bobData);
|
||||
} catch (NotConnectedException | NotLoggedInException | InterruptedException e) {
|
||||
BoBInfo bobInfo = bobs.get(bobIQRequest.getBoBHash());
|
||||
if (bobInfo == null) {
|
||||
// TODO return item-not-found
|
||||
return null;
|
||||
}
|
||||
|
||||
BoBData bobData = bobInfo.getData();
|
||||
BoBIQ responseBoBIQ = new BoBIQ(bobIQRequest.getBoBHash(), bobData);
|
||||
responseBoBIQ.setType(Type.result);
|
||||
responseBoBIQ.setTo(bobIQRequest.getFrom());
|
||||
return responseBoBIQ;
|
||||
}
|
||||
});
|
||||
|
||||
connection.registerIQRequestHandler(
|
||||
new AbstractIqRequestHandler(BoBIQ.ELEMENT, BoBIQ.NAMESPACE, Type.result, Mode.sync) {
|
||||
@Override
|
||||
public IQ handleIQRequest(IQ iqRequest) {
|
||||
BoBIQ resultBoBIQ = (BoBIQ) iqRequest;
|
||||
bobSaverManager.addBoB(resultBoBIQ.getBoBHash(), resultBoBIQ.getBoBData());
|
||||
return null;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -142,21 +137,46 @@ public final class BoBManager extends Manager {
|
|||
*/
|
||||
public BoBData requestBoB(Jid to, BoBHash bobHash) throws NotLoggedInException, NoResponseException,
|
||||
XMPPErrorException, NotConnectedException, InterruptedException {
|
||||
BoBData bobData = BOB_CACHE.lookup(bobHash);
|
||||
if (bobData != null) {
|
||||
return bobData;
|
||||
}
|
||||
|
||||
BoBIQ requestBoBIQ = new BoBIQ(bobHash);
|
||||
requestBoBIQ.setType(Type.get);
|
||||
requestBoBIQ.setTo(to);
|
||||
|
||||
XMPPConnection connection = getAuthenticatedConnectionOrThrow();
|
||||
BoBIQ responseBoBIQ = connection.createPacketCollectorAndSend(requestBoBIQ).nextResultOrThrow();
|
||||
return responseBoBIQ.getBoBData();
|
||||
|
||||
bobData = responseBoBIQ.getBoBData();
|
||||
BOB_CACHE.put(bobHash, bobData);
|
||||
|
||||
return bobData;
|
||||
}
|
||||
|
||||
private BoBIQ responseBoB(BoBIQ requestBoBIQ, BoBData bobData)
|
||||
throws NotConnectedException, InterruptedException, NotLoggedInException {
|
||||
BoBIQ responseBoBIQ = new BoBIQ(requestBoBIQ.getBoBHash(), bobData);
|
||||
responseBoBIQ.setType(Type.result);
|
||||
responseBoBIQ.setTo(requestBoBIQ.getFrom());
|
||||
return responseBoBIQ;
|
||||
public BoBInfo addBoB(BoBData bobData) {
|
||||
// We only support SHA-1 for now.
|
||||
BoBHash bobHash = new BoBHash("sha1", SHA1.hex(bobData.getContent()));
|
||||
|
||||
Set<BoBHash> bobHashes = Collections.singleton(bobHash);
|
||||
bobHashes = Collections.unmodifiableSet(bobHashes);
|
||||
|
||||
BoBInfo bobInfo = new BoBInfo(bobHashes, bobData);
|
||||
|
||||
bobs.put(bobHash, bobInfo);
|
||||
|
||||
return bobInfo;
|
||||
}
|
||||
|
||||
public BoBInfo removeBoB(BoBHash bobHash) {
|
||||
BoBInfo bobInfo = bobs.remove(bobHash);
|
||||
if (bobInfo == null) {
|
||||
return null;
|
||||
}
|
||||
for (BoBHash otherBobHash : bobInfo.getHashes()) {
|
||||
bobs.remove(otherBobHash);
|
||||
}
|
||||
return bobInfo;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,46 +0,0 @@
|
|||
/**
|
||||
*
|
||||
* Copyright 2016 Fernando Ramirez
|
||||
*
|
||||
* 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.bob;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
/**
|
||||
* Default Bits of Binary Saver Manager class.
|
||||
*
|
||||
* @author Fernando Ramirez
|
||||
*
|
||||
*/
|
||||
public class DefaultBoBSaverManager implements BoBSaverManager {
|
||||
|
||||
HashMap<BoBHash, BoBData> bobs = new HashMap<>();
|
||||
|
||||
@Override
|
||||
public void addBoB(BoBHash bobHash, BoBData bobData) {
|
||||
bobs.put(bobHash, bobData);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeBoB(BoBHash bobHash) {
|
||||
bobs.remove(bobHash);
|
||||
}
|
||||
|
||||
@Override
|
||||
public BoBData getBoB(BoBHash bobHash) {
|
||||
return bobs.get(bobHash);
|
||||
}
|
||||
|
||||
}
|
|
@ -84,13 +84,13 @@ public class BoBIQ extends IQ {
|
|||
|
||||
@Override
|
||||
protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder xml) {
|
||||
xml.attribute("cid", bobHash.toCid());
|
||||
xml.attribute("cid", bobHash.getCid());
|
||||
|
||||
if (bobData != null) {
|
||||
xml.attribute("max-age", String.valueOf(bobData.getMaxAge()));
|
||||
xml.optIntAttribute("max_age", bobData.getMaxAge());
|
||||
xml.attribute("type", bobData.getType());
|
||||
xml.rightAngleBracket();
|
||||
xml.escape(bobData.getBase64Encoded());
|
||||
xml.escape(bobData.getContentBase64Encoded());
|
||||
} else {
|
||||
xml.setEmptyElement();
|
||||
}
|
||||
|
|
|
@ -1,65 +0,0 @@
|
|||
/**
|
||||
*
|
||||
* Copyright 2016 Fernando Ramirez
|
||||
*
|
||||
* 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.bob.provider;
|
||||
|
||||
import org.jivesoftware.smack.provider.ExtensionElementProvider;
|
||||
import org.jivesoftware.smackx.bob.BoBHash;
|
||||
import org.jivesoftware.smackx.bob.element.BoBExtension;
|
||||
import org.jivesoftware.smackx.xhtmlim.XHTMLText;
|
||||
import org.xmlpull.v1.XmlPullParser;
|
||||
|
||||
/**
|
||||
* Bits of Binary extension provider class.
|
||||
*
|
||||
* @author Fernando Ramirez
|
||||
* @see <a href="http://xmpp.org/extensions/xep-0231.html">XEP-0231: Bits of
|
||||
* Binary</a>
|
||||
*/
|
||||
public class BoBExtensionProvider extends ExtensionElementProvider<BoBExtension> {
|
||||
|
||||
@Override
|
||||
public BoBExtension parse(XmlPullParser parser, int initialDepth) throws Exception {
|
||||
BoBHash bobHash = null;
|
||||
String alt = null;
|
||||
|
||||
outerloop: while (true) {
|
||||
int eventType = parser.next();
|
||||
|
||||
switch (eventType) {
|
||||
|
||||
case XmlPullParser.START_TAG:
|
||||
if (parser.getName().equals(XHTMLText.IMG)) {
|
||||
alt = parser.getAttributeValue("", "alt");
|
||||
|
||||
String src = parser.getAttributeValue("", "src");
|
||||
bobHash = BoBHash.fromSrc(src);
|
||||
}
|
||||
break;
|
||||
|
||||
case XmlPullParser.END_TAG:
|
||||
if (parser.getDepth() == initialDepth) {
|
||||
break outerloop;
|
||||
}
|
||||
break;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
return new BoBExtension(bobHash, alt, null);
|
||||
}
|
||||
|
||||
}
|
|
@ -1,6 +1,6 @@
|
|||
/**
|
||||
*
|
||||
* Copyright 2016 Fernando Ramirez
|
||||
* Copyright 2017 Florian Schmaus
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
@ -17,7 +17,7 @@
|
|||
package org.jivesoftware.smackx.bob.provider;
|
||||
|
||||
import org.jivesoftware.smack.provider.IQProvider;
|
||||
import org.jivesoftware.smack.util.stringencoder.Base64;
|
||||
import org.jivesoftware.smack.util.ParserUtils;
|
||||
import org.jivesoftware.smackx.bob.BoBData;
|
||||
import org.jivesoftware.smackx.bob.BoBHash;
|
||||
import org.jivesoftware.smackx.bob.element.BoBIQ;
|
||||
|
@ -26,7 +26,7 @@ import org.xmlpull.v1.XmlPullParser;
|
|||
/**
|
||||
* Bits of Binary IQ provider class.
|
||||
*
|
||||
* @author Fernando Ramirez
|
||||
* @author Florian Schmaus
|
||||
* @see <a href="http://xmpp.org/extensions/xep-0231.html">XEP-0231: Bits of
|
||||
* Binary</a>
|
||||
*/
|
||||
|
@ -34,34 +34,19 @@ public class BoBIQProvider extends IQProvider<BoBIQ> {
|
|||
|
||||
@Override
|
||||
public BoBIQ parse(XmlPullParser parser, int initialDepth) throws Exception {
|
||||
BoBHash bobHash = null;
|
||||
BoBData bobData = null;
|
||||
String cid = parser.getAttributeValue("", "cid");
|
||||
BoBHash bobHash = BoBHash.fromCid(cid);
|
||||
|
||||
parser.next();
|
||||
String dataType = parser.getAttributeValue("", "type");
|
||||
int maxAge = ParserUtils.getIntegerAttribute(parser, "max-age", -1);
|
||||
|
||||
if (parser.getName().equals(BoBIQ.ELEMENT)) {
|
||||
String cid = parser.getAttributeValue("", "cid");
|
||||
bobHash = BoBHash.fromCid(cid);
|
||||
|
||||
String dataType = parser.getAttributeValue("", "type");
|
||||
String maxAgeString = parser.getAttributeValue("", "max-age");
|
||||
|
||||
long maxAge = 0;
|
||||
if (maxAgeString != null) {
|
||||
maxAge = Long.parseLong(maxAgeString);
|
||||
}
|
||||
|
||||
String base64EncodedData = null;
|
||||
try {
|
||||
base64EncodedData = parser.nextText();
|
||||
} catch (Exception e) {
|
||||
}
|
||||
|
||||
if (base64EncodedData != null && dataType != null) {
|
||||
byte[] base64EncodedDataBytes = base64EncodedData.getBytes();
|
||||
bobData = new BoBData(maxAge, dataType, Base64.decode(base64EncodedDataBytes));
|
||||
}
|
||||
String base64EncodedData = parser.nextText();
|
||||
|
||||
BoBData bobData;
|
||||
if (dataType != null) {
|
||||
bobData = new BoBData(dataType, base64EncodedData, maxAge);
|
||||
} else {
|
||||
bobData = null;
|
||||
}
|
||||
|
||||
return new BoBIQ(bobHash, bobData);
|
||||
|
|
|
@ -538,11 +538,6 @@
|
|||
</extensionProvider>
|
||||
|
||||
<!-- XEP-0231: Bits of Binary -->
|
||||
<extensionProvider>
|
||||
<elementName>html</elementName>
|
||||
<namespace>http://jabber.org/protocol/xhtml-im</namespace>
|
||||
<className>org.jivesoftware.smackx.bob.provider.BoBExtensionProvider</className>
|
||||
</extensionProvider>
|
||||
<iqProvider>
|
||||
<elementName>data</elementName>
|
||||
<namespace>urn:xmpp:bob</namespace>
|
||||
|
|
|
@ -1,67 +0,0 @@
|
|||
/**
|
||||
*
|
||||
* Copyright 2016 Fernando Ramirez
|
||||
*
|
||||
* 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.bob;
|
||||
|
||||
import org.jivesoftware.smack.packet.Message;
|
||||
import org.jivesoftware.smack.packet.Message.Type;
|
||||
import org.jivesoftware.smack.util.PacketParserUtils;
|
||||
import org.jivesoftware.smackx.bob.element.BoBExtension;
|
||||
import org.jivesoftware.smackx.bob.provider.BoBExtensionProvider;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.jxmpp.jid.impl.JidCreate;
|
||||
import org.xmlpull.v1.XmlPullParser;
|
||||
|
||||
public class BoBExtensionTest {
|
||||
|
||||
String sampleMessageWithBoBExtension = "<message to='macbeth@chat.shakespeare.lit' id='sarasa' type='groupchat'>"
|
||||
+ "<body>Yet here's a spot.</body>" + "<html xmlns='http://jabber.org/protocol/xhtml-im'>"
|
||||
+ "<body xmlns='http://www.w3.org/1999/xhtml'>"
|
||||
+ "<img alt='A spot' src='cid:sha1+8f35fef110ffc5df08d579a50083ff9308fb6242@bob.xmpp.org'/>" + "</body>"
|
||||
+ "</html>" + "</message>";
|
||||
|
||||
String sampleBoBExtension = "<html xmlns='http://jabber.org/protocol/xhtml-im'>"
|
||||
+ "<body xmlns='http://www.w3.org/1999/xhtml'>"
|
||||
+ "<img alt='A spot' src='cid:sha1+8f35fef110ffc5df08d579a50083ff9308fb6242@bob.xmpp.org'/>" + "</body>"
|
||||
+ "</html>";
|
||||
|
||||
@Test
|
||||
public void checkBoBMessageExtension() throws Exception {
|
||||
Message message = (Message) PacketParserUtils.parseStanza(sampleMessageWithBoBExtension);
|
||||
|
||||
BoBHash bobHash = new BoBHash("8f35fef110ffc5df08d579a50083ff9308fb6242", "sha1");
|
||||
|
||||
Message createdMessage = new Message(JidCreate.from("macbeth@chat.shakespeare.lit"));
|
||||
createdMessage.setStanzaId("sarasa");
|
||||
createdMessage.setType(Type.groupchat);
|
||||
createdMessage.setBody("Yet here's a spot.");
|
||||
createdMessage.addExtension(new BoBExtension(bobHash, "A spot", null));
|
||||
|
||||
Assert.assertEquals(message.toXML().toString(), createdMessage.toXML().toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void checkBoBExtensionProvider() throws Exception {
|
||||
XmlPullParser parser = PacketParserUtils.getParserFor(sampleBoBExtension);
|
||||
BoBExtension bobExtension = new BoBExtensionProvider().parse(parser);
|
||||
|
||||
Assert.assertEquals("A spot", bobExtension.getAlt());
|
||||
Assert.assertEquals("sha1", bobExtension.getBoBHash().getHashType());
|
||||
Assert.assertEquals("8f35fef110ffc5df08d579a50083ff9308fb6242", bobExtension.getBoBHash().getHash());
|
||||
}
|
||||
|
||||
}
|
|
@ -20,11 +20,9 @@ import org.jivesoftware.smack.packet.IQ.Type;
|
|||
import org.jivesoftware.smack.test.util.SmackTestSuite;
|
||||
import org.jivesoftware.smack.util.PacketParserUtils;
|
||||
import org.jivesoftware.smackx.bob.element.BoBIQ;
|
||||
import org.jivesoftware.smackx.bob.provider.BoBIQProvider;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.jxmpp.jid.impl.JidCreate;
|
||||
import org.xmlpull.v1.XmlPullParser;
|
||||
|
||||
public class BoBIQTest extends SmackTestSuite {
|
||||
|
||||
|
@ -37,12 +35,6 @@ public class BoBIQTest extends SmackTestSuite {
|
|||
|
||||
@Test
|
||||
public void checkBoBIQRequest() throws Exception {
|
||||
XmlPullParser parser = PacketParserUtils.getParserFor(sampleBoBIQRequest);
|
||||
BoBIQ bobIQ = new BoBIQProvider().parse(parser);
|
||||
bobIQ.setStanzaId("sarasa");
|
||||
bobIQ.setTo(JidCreate.from("ladymacbeth@shakespeare.lit/castle"));
|
||||
bobIQ.setType(Type.get);
|
||||
|
||||
BoBHash bobHash = new BoBHash("8f35fef110ffc5df08d579a50083ff9308fb6242", "sha1");
|
||||
|
||||
BoBIQ createdBoBIQ = new BoBIQ(bobHash);
|
||||
|
@ -55,14 +47,10 @@ public class BoBIQTest extends SmackTestSuite {
|
|||
|
||||
@Test
|
||||
public void checkBoBIQResponse() throws Exception {
|
||||
XmlPullParser parser = PacketParserUtils.getParserFor(sampleBoBIQResponse);
|
||||
BoBIQ bobIQ = new BoBIQProvider().parse(parser);
|
||||
bobIQ.setStanzaId("sarasa");
|
||||
bobIQ.setTo(JidCreate.from("doctor@shakespeare.lit/pda"));
|
||||
bobIQ.setType(Type.result);
|
||||
BoBIQ bobIQ = PacketParserUtils.parseStanza(sampleBoBIQResponse);
|
||||
|
||||
BoBHash bobHash = new BoBHash("8f35fef110ffc5df08d579a50083ff9308fb6242", "sha1");
|
||||
BoBData bobData = new BoBData(86400, "image/png", "sarasade2354j2".getBytes());
|
||||
BoBData bobData = new BoBData("image/png", "sarasade2354j2".getBytes(), 86400);
|
||||
|
||||
BoBIQ createdBoBIQ = new BoBIQ(bobHash, bobData);
|
||||
createdBoBIQ.setStanzaId("sarasa");
|
||||
|
@ -73,7 +61,7 @@ public class BoBIQTest extends SmackTestSuite {
|
|||
Assert.assertEquals(bobIQ.getBoBHash().getHashType(), createdBoBIQ.getBoBHash().getHashType());
|
||||
Assert.assertEquals(bobIQ.getBoBData().getMaxAge(), createdBoBIQ.getBoBData().getMaxAge());
|
||||
Assert.assertEquals(bobIQ.getBoBData().getType(), createdBoBIQ.getBoBData().getType());
|
||||
Assert.assertEquals(bobIQ.getBoBData().getBase64Encoded(), createdBoBIQ.getBoBData().getBase64Encoded());
|
||||
Assert.assertEquals(bobIQ.getBoBData().getContentBase64Encoded(), createdBoBIQ.getBoBData().getContentBase64Encoded());
|
||||
Assert.assertEquals(bobIQ.toXML().toString(), createdBoBIQ.toXML().toString());
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue