Search in sources :

Example 31 with Journal

use of net.viperfish.journal.framework.Journal in project vsDiaryWriter by shilongdai.

the class ImportEntriesOperation method execute.

@Override
public void execute() {
    // load the file
    try {
        String json = importFile.read(StandardCharsets.UTF_16);
        // de-serialize and add
        Journal[] result = generator.fromJson(Journal[].class, json);
        for (Journal i : result) {
            db().addEntry(i);
            indexer().add(i);
        }
    } catch (IOException e) {
        FailToImportEntriesException f = new FailToImportEntriesException("Cannot import from:" + importFile.getFile() + " message:" + e.getMessage());
        f.initCause(e);
        throw new RuntimeException(f);
    }
}
Also used : FailToImportEntriesException(net.viperfish.journal.framework.errors.FailToImportEntriesException) Journal(net.viperfish.journal.framework.Journal) IOException(java.io.IOException)

Example 32 with Journal

use of net.viperfish.journal.framework.Journal in project vsDiaryWriter by shilongdai.

the class SerializationDBTest method testPersistence.

@Test
public void testPersistence() {
    JavaSerializationEntryDatabase database = new JavaSerializationEntryDatabase();
    for (int i = 0; i < 10; ++i) {
        Journal j = new Journal();
        j.setSubject("testPersist");
        j.setContent("testPersist");
        database.addEntry(j);
    }
    JavaSerializationEntryDatabase.serialize(new File("testPersist"), database);
    database = JavaSerializationEntryDatabase.deSerialize(new File("testPersist"));
    int count = 0;
    for (Journal i : database.getAll()) {
        Assert.assertEquals("testPersist", i.getContent());
        Assert.assertEquals("testPersist", i.getSubject());
        count++;
    }
    Assert.assertEquals(10, count);
    CommonFunctions.delete(new File("testPersist"));
}
Also used : Journal(net.viperfish.journal.framework.Journal) File(java.io.File) Test(org.junit.Test) DatabaseTest(net.viperfish.journal.framework.DatabaseTest)

Example 33 with Journal

use of net.viperfish.journal.framework.Journal in project vsDiaryWriter by shilongdai.

the class CompressMacTransformer method encryptJournal.

@Override
public Journal encryptJournal(Journal j) throws CipherException {
    ByteBuffer dateBuffer = ByteBuffer.allocate(8);
    dateBuffer.putLong(j.getDate().getTime());
    String encrytSubject = encrypt_format(j.getSubject(), new byte[0]);
    String encryptContent = encrypt_format(j.getContent(), dateBuffer.array());
    Journal result = new Journal();
    result.setSubject(encrytSubject);
    result.setContent(encryptContent);
    result.setDate(j.getDate());
    result.setId(j.getId());
    return result;
}
Also used : Journal(net.viperfish.journal.framework.Journal) ByteBuffer(java.nio.ByteBuffer)

Example 34 with Journal

use of net.viperfish.journal.framework.Journal in project vsDiaryWriter by shilongdai.

the class CompressMacTransformer method decryptJournal.

@Override
public Journal decryptJournal(Journal j) throws CipherException, CompromisedDataException {
    String decSubject = decrypt_format(j.getSubject(), new byte[0]);
    String decContent = decrypt_format(j.getContent(), ByteBuffer.allocate(8).putLong(j.getDate().getTime()).array());
    Journal result = new Journal();
    result.setSubject(decSubject);
    result.setContent(decContent);
    result.setDate(j.getDate());
    result.setId(j.getId());
    return result;
}
Also used : Journal(net.viperfish.journal.framework.Journal)

Example 35 with Journal

use of net.viperfish.journal.framework.Journal in project vsDiaryWriter by shilongdai.

the class CompressMacTest method testWrapper.

protected void testWrapper() {
    JournalTransformer wrapper;
    try {
        CommonFunctions.initDir(testDir);
        wrapper = createTransformer(new File(testDir.getCanonicalPath() + "/salt"));
    } catch (IOException e) {
        System.err.println("cannot load salt");
        throw new RuntimeException(e);
    }
    try {
        wrapper.setPassword("password");
    } catch (FailToSyncCipherDataException e1) {
        throw new RuntimeException(e1);
    }
    Journal j = new Journal();
    j.setSubject("test get");
    j.setContent("test get content");
    try {
        Journal result = wrapper.encryptJournal(j);
        result = wrapper.decryptJournal(result);
        String plainContent = result.getContent();
        Assert.assertEquals("test get content", plainContent);
        Assert.assertEquals("test get", result.getSubject());
    } catch (CipherException | CompromisedDataException e) {
        throw new RuntimeException(e);
    }
}
Also used : FailToSyncCipherDataException(net.viperfish.journal.framework.errors.FailToSyncCipherDataException) JournalTransformer(net.viperfish.journal.framework.JournalTransformer) CompromisedDataException(net.viperfish.journal.framework.errors.CompromisedDataException) CipherException(net.viperfish.journal.framework.errors.CipherException) Journal(net.viperfish.journal.framework.Journal) IOException(java.io.IOException) File(java.io.File)

Aggregations

Journal (net.viperfish.journal.framework.Journal)35 Test (org.junit.Test)13 Date (java.util.Date)8 File (java.io.File)7 JournalPointer (net.viperfish.journal.framework.JournalPointer)6 IOException (java.io.IOException)4 FailToSyncEntryException (net.viperfish.journal.framework.errors.FailToSyncEntryException)4 CipherException (net.viperfish.journal.framework.errors.CipherException)3 OperationErrorException (net.viperfish.journal.framework.errors.OperationErrorException)3 LinkedList (java.util.LinkedList)2 TreeSet (java.util.TreeSet)2 IOFile (net.viperfish.framework.file.IOFile)2 TextIOStreamHandler (net.viperfish.framework.file.TextIOStreamHandler)2 DatabaseTest (net.viperfish.journal.framework.DatabaseTest)2 FailToStoreCredentialException (net.viperfish.journal.framework.errors.FailToStoreCredentialException)2 ByteBuffer (java.nio.ByteBuffer)1 DateFormat (java.text.DateFormat)1 Calendar (java.util.Calendar)1 HashMap (java.util.HashMap)1 JsonGenerator (net.viperfish.framework.serialization.JsonGenerator)1