1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2024-11-24 04:52:05 +01:00

Improve store implementations

This commit is contained in:
Paul Schaub 2018-07-08 16:25:56 +02:00
parent 4e909abb7e
commit bf92cec618
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311
9 changed files with 182 additions and 40 deletions

View file

@ -43,6 +43,14 @@ public abstract class AbstractOpenPgpKeyStore implements OpenPgpKeyStore {
protected Map<BareJid, PGPPublicKeyRingCollection> publicKeys = new HashMap<>(); protected Map<BareJid, PGPPublicKeyRingCollection> publicKeys = new HashMap<>();
protected Map<BareJid, PGPSecretKeyRingCollection> secretKeys = new HashMap<>(); protected Map<BareJid, PGPSecretKeyRingCollection> secretKeys = new HashMap<>();
protected abstract PGPPublicKeyRingCollection readPublicKeysOf(BareJid owner) throws IOException, PGPException;
protected abstract void writePublicKeysOf(BareJid owner, PGPPublicKeyRingCollection publicKeys) throws IOException;
protected abstract PGPSecretKeyRingCollection readSecretKeysOf(BareJid owner) throws IOException, PGPException;
protected abstract void writeSecretKeysOf(BareJid owner, PGPSecretKeyRingCollection secretKeys) throws IOException;
@Override @Override
public PGPPublicKeyRingCollection getPublicKeysOf(BareJid owner) throws IOException, PGPException { public PGPPublicKeyRingCollection getPublicKeysOf(BareJid owner) throws IOException, PGPException {
PGPPublicKeyRingCollection keys = publicKeys.get(owner); PGPPublicKeyRingCollection keys = publicKeys.get(owner);

View file

@ -38,4 +38,14 @@ public abstract class AbstractOpenPgpMetadataStore implements OpenPgpMetadataSto
} }
return fingerprints; return fingerprints;
} }
@Override
public void setAnnouncedFingerprintsOf(BareJid contact, Map<OpenPgpV4Fingerprint, Date> data) throws IOException {
announcedFingerprints.put(contact, data);
writeAnnouncedFingerprintsOf(contact, data);
}
protected abstract Map<OpenPgpV4Fingerprint, Date> readAnnouncedFingerprintsOf(BareJid contact) throws IOException;
protected abstract void writeAnnouncedFingerprintsOf(BareJid contact, Map<OpenPgpV4Fingerprint, Date> metadata) throws IOException;
} }

View file

@ -0,0 +1,55 @@
package org.jivesoftware.smackx.ox.v2.store;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import org.jivesoftware.smackx.ox.OpenPgpV4Fingerprint;
import org.jxmpp.jid.BareJid;
public abstract class AbstractOpenPgpTrustStore implements OpenPgpTrustStore {
private final Map<BareJid, Map<OpenPgpV4Fingerprint, Trust>> trustCache = new HashMap<>();
protected abstract Trust readTrust(BareJid owner, OpenPgpV4Fingerprint fingerprint) throws IOException;
protected abstract void writeTrust(BareJid owner, OpenPgpV4Fingerprint fingerprint, Trust trust) throws IOException;
@Override
public Trust getTrust(BareJid owner, OpenPgpV4Fingerprint fingerprint) throws IOException {
Trust trust;
Map<OpenPgpV4Fingerprint, Trust> trustMap = trustCache.get(owner);
if (trustMap != null) {
trust = trustMap.get(fingerprint);
if (trust != null) {
return trust;
}
} else {
trustMap = new HashMap<>();
trustCache.put(owner, trustMap);
}
trust = readTrust(owner, fingerprint);
trustMap.put(fingerprint, trust);
return trust;
}
@Override
public void setTrust(BareJid owner, OpenPgpV4Fingerprint fingerprint, Trust trust) throws IOException {
Map<OpenPgpV4Fingerprint, Trust> trustMap = trustCache.get(owner);
if (trust == null) {
trustMap = new HashMap<>();
trustCache.put(owner, trustMap);
}
if (trustMap.get(fingerprint) == trust) {
return;
}
trustMap.put(fingerprint, trust);
writeTrust(owner, fingerprint, trust);
}
}

View file

@ -32,16 +32,10 @@ import org.jxmpp.jid.BareJid;
public interface OpenPgpKeyStore { public interface OpenPgpKeyStore {
PGPPublicKeyRingCollection readPublicKeysOf(BareJid owner) throws IOException, PGPException;
void writePublicKeysOf(BareJid owner, PGPPublicKeyRingCollection publicKeys) throws IOException;
PGPPublicKeyRingCollection getPublicKeysOf(BareJid owner) throws IOException, PGPException; PGPPublicKeyRingCollection getPublicKeysOf(BareJid owner) throws IOException, PGPException;
PGPSecretKeyRingCollection readSecretKeysOf(BareJid owner) throws IOException, PGPException;
void writeSecretKeysOf(BareJid owner, PGPSecretKeyRingCollection secretKeys) throws IOException;
PGPSecretKeyRingCollection getSecretKeysOf(BareJid owner) throws IOException, PGPException; PGPSecretKeyRingCollection getSecretKeysOf(BareJid owner) throws IOException, PGPException;
PGPPublicKeyRing getPublicKeyRing(BareJid owner, OpenPgpV4Fingerprint fingerprint) throws IOException, PGPException; PGPPublicKeyRing getPublicKeyRing(BareJid owner, OpenPgpV4Fingerprint fingerprint) throws IOException, PGPException;

View file

@ -21,7 +21,6 @@ import java.util.Date;
import java.util.Map; import java.util.Map;
import org.jivesoftware.smackx.ox.OpenPgpV4Fingerprint; import org.jivesoftware.smackx.ox.OpenPgpV4Fingerprint;
import org.jivesoftware.smackx.ox.element.PublicKeysListElement;
import org.jxmpp.jid.BareJid; import org.jxmpp.jid.BareJid;
@ -29,9 +28,5 @@ public interface OpenPgpMetadataStore {
Map<OpenPgpV4Fingerprint, Date> getAnnouncedFingerprintsOf(BareJid contact) throws IOException; Map<OpenPgpV4Fingerprint, Date> getAnnouncedFingerprintsOf(BareJid contact) throws IOException;
Map<OpenPgpV4Fingerprint, Date> readAnnouncedFingerprintsOf(BareJid contact) throws IOException; void setAnnouncedFingerprintsOf(BareJid contact, Map<OpenPgpV4Fingerprint, Date> data) throws IOException;
void writeAnnouncedFingerprintsOf(BareJid contact, PublicKeysListElement metadata) throws IOException;
} }

View file

@ -16,15 +16,17 @@
*/ */
package org.jivesoftware.smackx.ox.v2.store; package org.jivesoftware.smackx.ox.v2.store;
import java.io.IOException;
import org.jivesoftware.smackx.ox.OpenPgpV4Fingerprint; import org.jivesoftware.smackx.ox.OpenPgpV4Fingerprint;
import org.jxmpp.jid.BareJid; import org.jxmpp.jid.BareJid;
public interface OpenPgpTrustStore { public interface OpenPgpTrustStore {
Trust getTrust(BareJid owner, OpenPgpV4Fingerprint fingerprint); Trust getTrust(BareJid owner, OpenPgpV4Fingerprint fingerprint) throws IOException;
void setTrust(BareJid owner, OpenPgpV4Fingerprint fingerprint, Trust trust); void setTrust(BareJid owner, OpenPgpV4Fingerprint fingerprint, Trust trust) throws IOException;
enum Trust { enum Trust {
trusted, trusted,

View file

@ -21,6 +21,7 @@ import static org.jivesoftware.smackx.ox.util.FileUtils.prepareFileOutputStream;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStream; import java.io.OutputStream;
@ -48,17 +49,41 @@ public class FileBasedOpenPgpKeyStore extends AbstractOpenPgpKeyStore {
@Override @Override
public void writePublicKeysOf(BareJid owner, PGPPublicKeyRingCollection publicKeys) throws IOException { public void writePublicKeysOf(BareJid owner, PGPPublicKeyRingCollection publicKeys) throws IOException {
File file = getPublicKeyRingPath(owner); File file = getPublicKeyRingPath(owner);
OutputStream outputStream = prepareFileOutputStream(file); OutputStream outputStream = null;
publicKeys.encode(outputStream); try {
outputStream.close(); outputStream = prepareFileOutputStream(file);
publicKeys.encode(outputStream);
outputStream.close();
} catch (IOException e) {
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException ignored) {
// Don't care
}
}
throw e;
}
} }
@Override @Override
public void writeSecretKeysOf(BareJid owner, PGPSecretKeyRingCollection secretKeys) throws IOException { public void writeSecretKeysOf(BareJid owner, PGPSecretKeyRingCollection secretKeys) throws IOException {
File file = getSecretKeyRingPath(owner); File file = getSecretKeyRingPath(owner);
OutputStream outputStream = prepareFileOutputStream(file); OutputStream outputStream = null;
secretKeys.encode(outputStream); try {
outputStream.close(); outputStream = prepareFileOutputStream(file);
secretKeys.encode(outputStream);
outputStream.close();
} catch (IOException e) {
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException ignored) {
// Don't care
}
}
throw e;
}
} }
@Override @Override
@ -69,7 +94,7 @@ public class FileBasedOpenPgpKeyStore extends AbstractOpenPgpKeyStore {
FileInputStream inputStream; FileInputStream inputStream;
try { try {
inputStream = prepareFileInputStream(file); inputStream = prepareFileInputStream(file);
} catch (IOException e) { } catch (FileNotFoundException e) {
return null; return null;
} }
@ -85,7 +110,7 @@ public class FileBasedOpenPgpKeyStore extends AbstractOpenPgpKeyStore {
FileInputStream inputStream; FileInputStream inputStream;
try { try {
inputStream = prepareFileInputStream(file); inputStream = prepareFileInputStream(file);
} catch (IOException e) { } catch (FileNotFoundException e) {
return null; return null;
} }

View file

@ -29,7 +29,6 @@ import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
import org.jivesoftware.smackx.ox.OpenPgpV4Fingerprint; import org.jivesoftware.smackx.ox.OpenPgpV4Fingerprint;
import org.jivesoftware.smackx.ox.element.PublicKeysListElement;
import org.jivesoftware.smackx.ox.util.Util; import org.jivesoftware.smackx.ox.util.Util;
import org.jivesoftware.smackx.ox.v2.store.AbstractOpenPgpMetadataStore; import org.jivesoftware.smackx.ox.v2.store.AbstractOpenPgpMetadataStore;
@ -55,15 +54,10 @@ public class FileBasedOpenPgpMetadataStore extends AbstractOpenPgpMetadataStore
} }
@Override @Override
public void writeAnnouncedFingerprintsOf(BareJid contact, PublicKeysListElement metadata) public void writeAnnouncedFingerprintsOf(BareJid contact, Map<OpenPgpV4Fingerprint, Date> metadata)
throws IOException { throws IOException {
File destination = getAnnouncedFingerprintsPath(contact); File destination = getAnnouncedFingerprintsPath(contact);
Map<OpenPgpV4Fingerprint, Date> fingerprintDateMap = new HashMap<>(); writeFingerprintsAndDates(metadata, destination);
for (OpenPgpV4Fingerprint fingerprint : metadata.getMetadata().keySet()) {
fingerprintDateMap.put(fingerprint, metadata.getMetadata().get(fingerprint).getDate());
}
writeFingerprintsAndDates(fingerprintDateMap, destination);
} }
private Map<OpenPgpV4Fingerprint, Date> readFingerprintsAndDates(File source) throws IOException { private Map<OpenPgpV4Fingerprint, Date> readFingerprintsAndDates(File source) throws IOException {
@ -120,6 +114,7 @@ public class FileBasedOpenPgpMetadataStore extends AbstractOpenPgpMetadataStore
(date != null ? XmppDateTime.formatXEP0082Date(date) : XmppDateTime.formatXEP0082Date(new Date())); (date != null ? XmppDateTime.formatXEP0082Date(date) : XmppDateTime.formatXEP0082Date(new Date()));
writer.write(line); writer.write(line);
} }
writer.flush();
writer.close(); writer.close();
} catch (IOException e) { } catch (IOException e) {
if (writer != null) { if (writer != null) {

View file

@ -16,18 +16,24 @@
*/ */
package org.jivesoftware.smackx.ox.v2.store.filebased; package org.jivesoftware.smackx.ox.v2.store.filebased;
import static org.jivesoftware.smackx.ox.util.FileUtils.prepareFileInputStream; import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File; import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.nio.file.Files;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jivesoftware.smackx.ox.OpenPgpV4Fingerprint; import org.jivesoftware.smackx.ox.OpenPgpV4Fingerprint;
import org.jivesoftware.smackx.ox.v2.store.OpenPgpTrustStore; import org.jivesoftware.smackx.ox.util.Util;
import org.jivesoftware.smackx.ox.v2.store.AbstractOpenPgpTrustStore;
import org.jxmpp.jid.BareJid; import org.jxmpp.jid.BareJid;
public class FileBasedOpenPgpTrustStore implements OpenPgpTrustStore { public class FileBasedOpenPgpTrustStore extends AbstractOpenPgpTrustStore {
private static final Logger LOGGER = Logger.getLogger(FileBasedOpenPgpTrustStore.class.getName());
private final File basePath; private final File basePath;
@ -40,20 +46,72 @@ public class FileBasedOpenPgpTrustStore implements OpenPgpTrustStore {
} }
@Override @Override
public Trust getTrust(BareJid owner, OpenPgpV4Fingerprint fingerprint) { protected Trust readTrust(BareJid owner, OpenPgpV4Fingerprint fingerprint) throws IOException {
File file = getTrustPath(owner, fingerprint); File file = getTrustPath(owner, fingerprint);
BufferedReader reader = null;
try { try {
InputStream inputStream = prepareFileInputStream(file); reader = Files.newBufferedReader(file.toPath(), Util.UTF8);
return Trust.trusted;
Trust trust = null;
String line; int lineNr = 0;
while ((line = reader.readLine()) != null) {
lineNr++;
try {
trust = Trust.valueOf(line);
break;
} catch (IllegalArgumentException e) {
LOGGER.log(Level.WARNING, "Skipping invalid trust record in line " + lineNr + " of file " +
file.getAbsolutePath());
}
}
reader.close();
return trust != null ? trust : Trust.undecided;
} catch (IOException e) { } catch (IOException e) {
return Trust.undecided; if (reader != null) {
try {
reader.close();
} catch (IOException ignored) {
// Don't care
}
}
if (e instanceof FileNotFoundException) {
return Trust.undecided;
}
throw e;
} }
} }
@Override @Override
public void setTrust(BareJid owner, OpenPgpV4Fingerprint fingerprint, Trust trust) { protected void writeTrust(BareJid owner, OpenPgpV4Fingerprint fingerprint, Trust trust) throws IOException {
File file = getTrustPath(owner, fingerprint);
if (!file.exists()) {
if (!file.createNewFile()) {
throw new IOException("Cannot create file " + file.getAbsolutePath());
}
} else {
if (file.isDirectory()) {
throw new IOException("File " + file.getAbsolutePath() + " is a directory.");
}
}
BufferedWriter writer = null;
try {
writer = Files.newBufferedWriter(file.toPath(), Util.UTF8);
writer.write(trust.toString());
writer.flush();
writer.close();
} catch (IOException e) {
if (writer != null) {
try {
writer.close();
} catch (IOException ignored) {
// Don't care
}
}
throw e;
}
} }
private File getTrustPath(BareJid owner, OpenPgpV4Fingerprint fingerprint) { private File getTrustPath(BareJid owner, OpenPgpV4Fingerprint fingerprint) {