/** * Creates an <code>Email</code> from a file containing an <strong>uncompressed</strong> MIME email * and another file containing metadata. If the metadata file doesn't exist, the metadata will be * empty. * @param emailStream * @param metadataStream * @param passwordHolder * @throws MessagingException * @throws IOException * @throws PasswordException * @throws GeneralSecurityException */ public Email(InputStream emailStream, InputStream metadataStream, PasswordHolder passwordHolder) throws MessagingException, IOException, PasswordException, GeneralSecurityException { this(emailStream, false); if (metadataStream != null) metadata = new EmailMetadata(metadataStream); else metadata = new EmailMetadata(); }
public boolean isDelivered() { return getNumUndeliveredRecipients() == 0; }
/** @see EmailMetadata#getCreateTime() */ public Date getCreateTime() { return metadata.getCreateTime(); }
public int getDeliveryPercentage() { int numPackets = getNumPackets(); if (numPackets == 0) return 0; else return 100 * getNumDeliveredPackets() / numPackets; }
public Hash getDeleteVerificationHash(EmailDestination destination, Hash dhtKey) { int destIndex = getDestinationIndex(destination); int packetIndex = getPacketIndex(destIndex, dhtKey); String hashStr = getProperty(PROPERTY_DESTINATION + destIndex + "." + PACKET + packetIndex + "." + DELETE_VERIFICATION_HASH); try { return Util.createHash(hashStr); } catch (DataFormatException e) { log.error("Invalid delete verification hash: <" + hashStr + ">", e); return null; } }
/** * Adds metadata about an email packet that has been stored in the DHT * and is waiting to be picked up and deleted. * @param destination * @param dhtKey * @param delVerificationHash */ public void addPacketInfo(EmailDestination destination, Hash dhtKey, Hash delVerificationHash) { int destIndex = getDestinationIndex(destination); int packetIndex = getPacketIndex(destIndex, dhtKey); setProperty(PROPERTY_DESTINATION + destIndex, destination.toBase64()); String packetProperty = PROPERTY_DESTINATION + destIndex + "." + PACKET + packetIndex + "."; setProperty(packetProperty + DHT_KEY, dhtKey.toBase64()); setProperty(packetProperty + DELETE_VERIFICATION_HASH, delVerificationHash.toBase64()); setProperty(packetProperty + DELETED_FROM_DHT, "false"); }
EmailMetadata metadata = new EmailMetadata(); assertEquals(null, metadata.getDeleteVerificationHash(destination1, dhtKey1)); assertEquals(null, metadata.getDeleteVerificationHash(destination1, dhtKey2)); assertEquals(null, metadata.getDeleteVerificationHash(destination1, dhtKey3)); assertEquals(null, metadata.getDeleteVerificationHash(destination2, dhtKey4)); metadata.addPacketInfo(destination1, dhtKey1, delVerificationHash1); metadata.addPacketInfo(destination1, dhtKey2, delVerificationHash2); metadata.addPacketInfo(destination1, dhtKey3, delVerificationHash3); metadata.addPacketInfo(destination2, dhtKey4, delVerificationHash4); assertEquals(2, metadata.getNumUndeliveredRecipients()); assertEquals(delVerificationHash1, metadata.getDeleteVerificationHash(destination1, dhtKey1)); assertEquals(delVerificationHash2, metadata.getDeleteVerificationHash(destination1, dhtKey2)); assertEquals(delVerificationHash3, metadata.getDeleteVerificationHash(destination1, dhtKey3)); assertEquals(delVerificationHash4, metadata.getDeleteVerificationHash(destination2, dhtKey4)); assertEquals(null, metadata.getDeleteVerificationHash(destination1, dhtKey4)); assertEquals(null, metadata.getDeleteVerificationHash(destination2, dhtKey3)); assertEquals(2, metadata.getNumUndeliveredRecipients()); metadata.setPacketDelivered(dhtKey1, true); assertEquals(2, metadata.getNumUndeliveredRecipients()); metadata.setPacketDelivered(dhtKey2, true); assertEquals(2, metadata.getNumUndeliveredRecipients()); metadata.setPacketDelivered(dhtKey3, true); assertEquals(1, metadata.getNumUndeliveredRecipients()); metadata.setPacketDelivered(dhtKey4, true); assertEquals(0, metadata.getNumUndeliveredRecipients());
/** * Checks the DHT for all undelivered packets belonging to a given email. * @param email * @throws InterruptedException */ private void checkDelivery(Email email) throws InterruptedException { EmailMetadata metadata = email.getMetadata(); Collection<PacketInfo> packets = metadata.getUndeliveredPacketKeys(); synchronized(sentFolder) { boolean updateMetadata = false; for (PacketInfo packet: packets) { UniqueId delAuth = dht.findDeleteAuthorizationKey(packet.dhtKey, packet.delVerificationHash); if (delAuth != null) { metadata.setPacketDelivered(packet.dhtKey, true); updateMetadata = true; log.debug("Delivery of email packet with DHT key " + packet.dhtKey + " confirmed."); } } if (updateMetadata) try { sentFolder.saveMetadata(email); } catch (Exception e) { log.error("Can't save email metadata.", e); } } } }
send(emailPacket, hops, minDelay, maxDelay, relayRedundancy); indexPacket.put(emailPacket); metadata.addPacketInfo(recipientDest, emailPacket.getDhtKey(), emailPacket.getDeleteVerificationHash());
/** @see EmailMetadata#getDeliveryPercentage() */ public int getDeliveryPercentage() { return metadata.getDeliveryPercentage(); }
/** * Creates an <code>Email</code> from a byte array containing a <strong>compressed</strong> MIME email. * @param bytes * @throws MessagingException * @throws IOException */ public Email(byte[] bytes) throws MessagingException, IOException { this(new ByteArrayInputStream(bytes), true); messageId = new UniqueId(); metadata = new EmailMetadata(); }
/** * @param includeSendTime Whether to add a "Date" header. */ public Email(boolean includeSendTime) { super(Session.getDefaultInstance(new Properties())); messageId = new UniqueId(); metadata = new EmailMetadata(); this.includeSendTime = includeSendTime; }
private EmailMetadata getMetadata(File file) throws IOException, GeneralSecurityException, PasswordException { InputStream metadataStream = new BufferedInputStream(new EncryptedInputStream(new FileInputStream(file), passwordHolder)); EmailMetadata metadata = new EmailMetadata(metadataStream); return metadata; }
/** * Returns the metadata for an email. If the metadata file doesn't exist or cannot be read, * an empty {@link EmailMetadata} object is returned. This method never returns <code>null</code>.<br/> * The returned metadata object is not connected to the * @throws PasswordException * @throws GeneralSecurityException */ private EmailMetadata getMetadata(String messageId) throws GeneralSecurityException, PasswordException { File file = getMetadataFile(messageId); if (!file.exists()) return new EmailMetadata(); try { return getMetadata(file); } catch (IOException e) { log.error("Can't read metadata file: <" + file.getAbsolutePath() + ">", e); return new EmailMetadata(); } }
/** * Creates an <code>Email</code> from an InputStream containing a compressed or uncompressed MIME email. * @param inputStream * @param compressed <code>true</code> if the stream contains compressed data * @throws MessagingException * @throws IOException */ public Email(InputStream inputStream, boolean compressed) throws MessagingException, IOException { super(Session.getDefaultInstance(new Properties()), compressed?Email.decompress(inputStream):inputStream); messageId = new UniqueId(); includeSendTime = getSentDate() != null; metadata = new EmailMetadata(); }