Merge pull request #278 from vanitasvitae/omemoFileBasedStoreUrlEncode

Use new BareJidEncoder class in FileBasedOmemoStore
This commit is contained in:
Florian Schmaus 2018-11-08 07:38:43 +01:00 committed by GitHub
commit 0adcf889bf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 74 additions and 3 deletions

View File

@ -0,0 +1,59 @@
/**
*
* Copyright 2018 Paul Schaub
*
* 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.util.stringencoder;
import org.jxmpp.jid.BareJid;
import org.jxmpp.jid.impl.JidCreate;
import org.jxmpp.stringprep.XmppStringprepException;
public abstract class BareJidEncoder implements StringEncoder<BareJid> {
@Deprecated
public static class LegacyEncoder extends BareJidEncoder {
@Override
public String encode(BareJid jid) {
return jid.toString();
}
@Override
public BareJid decode(String string) {
try {
return JidCreate.bareFrom(string);
} catch (XmppStringprepException e) {
throw new IllegalArgumentException("BareJid cannot be decoded.", e);
}
}
}
public static class UrlSafeEncoder extends BareJidEncoder {
@Override
public String encode(BareJid jid) {
return jid.asUrlEncodedString();
}
@Override
public BareJid decode(String string) {
try {
return JidCreate.bareFromUrlEncoded(string);
} catch (XmppStringprepException e) {
throw new IllegalArgumentException("BareJid cannot be decoded.", e);
}
}
}
}

View File

@ -37,7 +37,7 @@ import java.util.logging.Level;
import java.util.logging.Logger;
import org.jivesoftware.smack.util.CloseableUtil;
import org.jivesoftware.smack.util.stringencoder.BareJidEncoder;
import org.jivesoftware.smackx.omemo.exceptions.CorruptedOmemoKeyException;
import org.jivesoftware.smackx.omemo.internal.OmemoCachedDeviceList;
import org.jivesoftware.smackx.omemo.internal.OmemoDevice;
@ -54,6 +54,7 @@ public abstract class FileBasedOmemoStore<T_IdKeyPair, T_IdKey, T_PreKey, T_SigP
private final FileHierarchy hierarchy;
private static final Logger LOGGER = Logger.getLogger(FileBasedOmemoStore.class.getName());
private static BareJidEncoder bareJidEncoder = new BareJidEncoder.UrlSafeEncoder();
public FileBasedOmemoStore(File basePath) {
super();
@ -769,7 +770,7 @@ public abstract class FileBasedOmemoStore<T_IdKeyPair, T_IdKey, T_PreKey, T_SigP
}
File getUserDirectory(BareJid bareJid) {
return createDirectory(getStoreDirectory(), bareJid.toString());
return createDirectory(getStoreDirectory(), bareJidEncoder.encode(bareJid));
}
File getUserDeviceDirectory(OmemoDevice userDevice) {
@ -782,7 +783,7 @@ public abstract class FileBasedOmemoStore<T_IdKeyPair, T_IdKey, T_PreKey, T_SigP
}
File getContactsDir(OmemoDevice userDevice, BareJid contact) {
return createDirectory(getContactsDir(userDevice), contact.toString());
return createDirectory(getContactsDir(userDevice), bareJidEncoder.encode(contact));
}
File getContactsDir(OmemoDevice userDevice, OmemoDevice contactsDevice) {
@ -861,4 +862,15 @@ public abstract class FileBasedOmemoStore<T_IdKeyPair, T_IdKey, T_PreKey, T_SigP
return f;
}
}
/**
* Convert {@link BareJid BareJids} to Strings using the legacy {@link BareJid#toString()} method instead of the
* proper, url safe {@link BareJid#asUrlEncodedString()} method.
* While it is highly advised to use the new format, you can use this method to stay backwards compatible to data
* sets created by the old implementation.
*/
@SuppressWarnings("deprecation")
public static void useLegacyBareJidEncoding() {
bareJidEncoder = new BareJidEncoder.LegacyEncoder();
}
}