FileBasedOpenPgpMetadataStore.java

  1. /**
  2.  *
  3.  * Copyright 2018 Paul Schaub.
  4.  *
  5.  * Licensed under the Apache License, Version 2.0 (the "License");
  6.  * you may not use this file except in compliance with the License.
  7.  * You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.jivesoftware.smackx.ox.store.filebased;

  18. import java.io.BufferedReader;
  19. import java.io.BufferedWriter;
  20. import java.io.File;
  21. import java.io.IOException;
  22. import java.io.InputStream;
  23. import java.io.InputStreamReader;
  24. import java.io.OutputStream;
  25. import java.io.OutputStreamWriter;
  26. import java.text.ParseException;
  27. import java.util.Date;
  28. import java.util.HashMap;
  29. import java.util.Map;
  30. import java.util.logging.Level;
  31. import java.util.logging.Logger;

  32. import org.jivesoftware.smack.util.CloseableUtil;
  33. import org.jivesoftware.smack.util.FileUtils;

  34. import org.jivesoftware.smackx.ox.store.abstr.AbstractOpenPgpMetadataStore;
  35. import org.jivesoftware.smackx.ox.store.definition.OpenPgpMetadataStore;
  36. import org.jivesoftware.smackx.ox.util.Util;

  37. import org.jxmpp.jid.BareJid;
  38. import org.jxmpp.util.XmppDateTime;
  39. import org.pgpainless.key.OpenPgpV4Fingerprint;

  40. /**
  41.  * Implementation of the {@link OpenPgpMetadataStore}, which stores metadata information in a file structure.
  42.  * The information is stored in the following directory structure:
  43.  *
  44.  * <pre>
  45.  * {@code
  46.  * <basePath>/
  47.  *     <userjid@server.tld>/
  48.  *         announced.list       // list of the users announced key fingerprints and modification dates
  49.  * }
  50.  * </pre>
  51.  */
  52. public class FileBasedOpenPgpMetadataStore extends AbstractOpenPgpMetadataStore {

  53.     public static final String ANNOUNCED = "announced.list";

  54.     private static final Logger LOGGER = Logger.getLogger(FileBasedOpenPgpMetadataStore.class.getName());

  55.     private final File basePath;

  56.     public FileBasedOpenPgpMetadataStore(File basePath) {
  57.         this.basePath = basePath;
  58.     }

  59.     @Override
  60.     public Map<OpenPgpV4Fingerprint, Date> readAnnouncedFingerprintsOf(BareJid contact) throws IOException {
  61.         return readFingerprintsAndDates(getAnnouncedFingerprintsPath(contact));
  62.     }

  63.     @Override
  64.     public void writeAnnouncedFingerprintsOf(BareJid contact, Map<OpenPgpV4Fingerprint, Date> metadata)
  65.             throws IOException {
  66.         File destination = getAnnouncedFingerprintsPath(contact);
  67.         writeFingerprintsAndDates(metadata, destination);
  68.     }

  69.     static Map<OpenPgpV4Fingerprint, Date> readFingerprintsAndDates(File source) throws IOException {
  70.         // TODO: Why do we not throw a FileNotFoundException here?
  71.         if (!source.exists() || source.isDirectory()) {
  72.             return new HashMap<>();
  73.         }

  74.         BufferedReader reader = null;
  75.         try {
  76.             InputStream inputStream = FileUtils.prepareFileInputStream(source);
  77.             InputStreamReader isr = new InputStreamReader(inputStream, Util.UTF8);
  78.             reader = new BufferedReader(isr);
  79.             Map<OpenPgpV4Fingerprint, Date> fingerprintDateMap = new HashMap<>();

  80.             String line; int lineNr = 0;
  81.             while ((line = reader.readLine()) != null) {
  82.                 lineNr++;

  83.                 line = line.trim();
  84.                 String[] split = line.split(" ");
  85.                 if (split.length != 2) {
  86.                     LOGGER.log(Level.FINE, "Skipping invalid line " + lineNr + " in file " + source.getAbsolutePath());
  87.                     continue;
  88.                 }

  89.                 try {
  90.                     OpenPgpV4Fingerprint fingerprint = new OpenPgpV4Fingerprint(split[0]);
  91.                     Date date = XmppDateTime.parseXEP0082Date(split[1]);
  92.                     fingerprintDateMap.put(fingerprint, date);
  93.                 } catch (IllegalArgumentException | ParseException e) {
  94.                     LOGGER.log(Level.WARNING, "Error parsing fingerprint/date touple in line " + lineNr +
  95.                             " of file " + source.getAbsolutePath(), e);
  96.                 }
  97.             }

  98.             return fingerprintDateMap;
  99.         } finally {
  100.             CloseableUtil.maybeClose(reader, LOGGER);
  101.         }
  102.     }

  103.     static void writeFingerprintsAndDates(Map<OpenPgpV4Fingerprint, Date> data, File destination)
  104.             throws IOException {
  105.         if (data == null || data.isEmpty()) {
  106.             FileUtils.maybeDeleteFileOrThrow(destination);
  107.             return;
  108.         }

  109.         FileUtils.maybeCreateFileWithParentDirectories(destination);

  110.         BufferedWriter writer = null;
  111.         try {
  112.             OutputStream outputStream = FileUtils.prepareFileOutputStream(destination);
  113.             OutputStreamWriter osw = new OutputStreamWriter(outputStream, Util.UTF8);
  114.             writer = new BufferedWriter(osw);
  115.             for (OpenPgpV4Fingerprint fingerprint : data.keySet()) {
  116.                 Date date = data.get(fingerprint);
  117.                 String line = fingerprint.toString() + " " +
  118.                         (date != null ? XmppDateTime.formatXEP0082Date(date) : XmppDateTime.formatXEP0082Date(new Date()));
  119.                 writer.write(line);
  120.                 writer.newLine();
  121.             }
  122.         } finally {
  123.             CloseableUtil.maybeClose(writer, LOGGER);
  124.         }
  125.     }

  126.     private File getAnnouncedFingerprintsPath(BareJid contact) {
  127.         return new File(FileBasedOpenPgpStore.getContactsPath(basePath, contact), ANNOUNCED);
  128.     }
  129. }