public static void saveToFile(
      ReadableDatabase db, Bulletin b, File destFile, MartusCrypto security)
      throws IOException, MartusCrypto.CryptoException {
    BulletinHeaderPacket header = b.getBulletinHeaderPacket();

    FieldDataPacket publicDataPacket = b.getFieldDataPacket();
    boolean shouldEncryptPublicData = (b.isMutable() || b.isAllPrivate());
    publicDataPacket.setEncrypted(shouldEncryptPublicData);

    OutputStream outputStream = new BufferedOutputStream(new FileOutputStream(destFile));
    ZipOutputStream zipOut = new ZipOutputStream(outputStream);
    try {
      byte[] dataSig = writePacketToZip(b, zipOut, b.getFieldDataPacket(), security);
      header.setFieldDataSignature(dataSig);

      byte[] privateDataSig = writePacketToZip(b, zipOut, b.getPrivateFieldDataPacket(), security);
      header.setPrivateFieldDataSignature(privateDataSig);

      writeAttachmentsToZip(db, b, zipOut, b.getPublicAttachments(), security);
      writeAttachmentsToZip(db, b, zipOut, b.getPrivateAttachments(), security);

      writePacketToZip(b, zipOut, header, security);
    } finally {
      zipOut.close();
    }
  }
예제 #2
0
  public boolean isAllPrivate() {

    BulletinHeaderPacket bhp = getBulletinHeaderPacket();
    if (!bhp.hasAllPrivateFlag()) {
      FieldDataPacket fdp = getFieldDataPacket();
      bhp.setAllPrivate(fdp.isEncrypted());
    }
    return bhp.isAllPrivate();
  }
예제 #3
0
  public boolean hasUnknownCustomField() {
    FieldDataPacket fdp = getFieldDataPacket();
    FieldSpec[] specs = fdp.getFieldSpecs().asArray();
    for (int i = 0; i < specs.length; ++i) {
      if (specs[i].hasUnknownStuff()) return true;
    }

    return false;
  }
예제 #4
0
 public Bulletin(
     MartusCrypto securityToUse,
     FieldSpecCollection publicFieldSpecs,
     FieldSpecCollection privateFieldSpecs)
     throws Exception {
   this(
       securityToUse,
       BulletinHeaderPacket.createUniversalId(securityToUse),
       FieldDataPacket.createUniversalId(securityToUse),
       FieldDataPacket.createUniversalId(securityToUse),
       publicFieldSpecs,
       privateFieldSpecs);
 }
예제 #5
0
 private PendingAttachmentList getPendingAttachments(FieldDataPacket fdp) {
   AttachmentProxy[] proxies = fdp.getAttachments();
   PendingAttachmentList pending = new PendingAttachmentList();
   for (int i = 0; i < proxies.length; ++i) {
     AttachmentPacket packet = proxies[i].getPendingPacket();
     if (packet != null) pending.add(packet);
   }
   return pending;
 }
예제 #6
0
 private boolean doesSectionContain(
     FieldDataPacket section, String lookFor, MiniLocalization localization) {
   FieldSpec fields[] = section.getFieldSpecs().asArray();
   for (int f = 0; f < fields.length; ++f) {
     MartusField field = getField(fields[f].getTag());
     if (field.contains(lookFor, localization)) return true;
   }
   return false;
 }
  protected void setUp() throws Exception {
    super.setUp();
    logger = new LoggerToNull();
    MockMartusSecurity serverSecurity = MockMartusSecurity.createServer();
    coreServer = new MockMartusServer(this);
    coreServer.setSecurity(serverSecurity);
    server = new ServerForMirroring(coreServer, logger);

    clientSecurity1 = MockMartusSecurity.createClient();
    clientSecurity2 = MockMartusSecurity.createOtherClient();

    Database db = coreServer.getWriteableDatabase();

    bhp1 = new BulletinHeaderPacket(clientSecurity1);
    bhp1.setStatus(BulletinConstants.STATUSIMMUTABLE);
    DatabaseKey key1 = bhp1.createKeyWithHeaderStatus(bhp1.getUniversalId());
    bhp1.writeXmlToDatabase(db, key1, false, clientSecurity1);

    bhp2 = new BulletinHeaderPacket(clientSecurity1);
    bhp2.setStatus(BulletinConstants.STATUSIMMUTABLE);
    DatabaseKey key2 = bhp2.createKeyWithHeaderStatus(bhp2.getUniversalId());
    bhp2.writeXmlToDatabase(db, key2, false, clientSecurity1);

    bhp3 = new BulletinHeaderPacket(clientSecurity2);
    bhp3.setStatus(BulletinConstants.STATUSIMMUTABLE);
    DatabaseKey key3 = bhp3.createKeyWithHeaderStatus(bhp3.getUniversalId());
    bhp3.writeXmlToDatabase(db, key3, false, clientSecurity2);

    bhp4 = new BulletinHeaderPacket(clientSecurity2);
    bhp4.setStatus(BulletinConstants.STATUSMUTABLE);
    DatabaseKey key4 = bhp4.createKeyWithHeaderStatus(bhp4.getUniversalId());
    bhp4.writeXmlToDatabase(db, key4, false, clientSecurity2);

    UniversalId fdpUid = FieldDataPacket.createUniversalId(clientSecurity1);
    FieldSpec[] tags = {LegacyCustomFields.createFromLegacy("whatever")};
    FieldDataPacket fdp1 = new FieldDataPacket(fdpUid, new FieldSpecCollection(tags));
    fdp1.writeXmlToClientDatabase(db, false, clientSecurity1);

    UniversalId otherPacketId =
        UniversalIdForTesting.createFromAccountAndPrefix(clientSecurity2.getPublicKeyString(), "X");
    DatabaseKey key = DatabaseKey.createImmutableKey(otherPacketId);
    db.writeRecord(key, "Not a valid packet");
  }
  private Bulletin createBulletin(
      MartusCrypto signatureGenerator,
      FormEntryController formEntryController,
      FieldSpecCollection fieldsFromXForms)
      throws Exception {
    FieldSpecCollection allFields = new FieldSpecCollection();
    FieldSpecCollection nonEmptytopSectionFieldSpecsFrom = getNonEmptyTopFieldSpecs();
    allFields.addAll(nonEmptytopSectionFieldSpecsFrom);
    allFields.addAll(fieldsFromXForms);

    Bulletin bulletinLoadedFromXForms =
        new Bulletin(signatureGenerator, allFields, new FieldSpecCollection());
    transferAllStandardFields(bulletinLoadedFromXForms, nonEmptytopSectionFieldSpecsFrom);

    resetFormEntryControllerIndex(formEntryController);
    int event;
    while ((event = formEntryController.stepToNextEvent())
        != FormEntryController.EVENT_END_OF_FORM) {
      if (event == FormEntryController.EVENT_REPEAT)
        convertXFormRepeatToGridData(
            formEntryController, fieldsFromXForms, bulletinLoadedFromXForms);

      if (event != FormEntryController.EVENT_QUESTION) continue;

      FormEntryPrompt questionPrompt = formEntryController.getModel().getQuestionPrompt();
      IAnswerData answer = questionPrompt.getAnswerValue();
      if (answer == null) continue;

      QuestionDef question = questionPrompt.getQuestion();
      final int dataType = questionPrompt.getDataType();
      TreeReference reference = (TreeReference) question.getBind().getReference();
      FieldDataPacket fieldDataPacket = bulletinLoadedFromXForms.getFieldDataPacket();
      String xFormsFieldTag = reference.getNameLast();
      String answerAsString = getMartusAnswerStringFromQuestion(answer, question, dataType);
      fieldDataPacket.set(xFormsFieldTag, answerAsString);
    }

    copyPrivateAttachmentProxies(bulletinLoadedFromXForms);
    copyPublicAttachmentProxies(bulletinLoadedFromXForms);

    return bulletinLoadedFromXForms;
  }
  public static void loadFromFile(Bulletin b, File inputFile, MartusCrypto verifier)
      throws IOException {
    b.clearAllUserData();
    ZipFile zip = new ZipFile(inputFile);
    try {

      BulletinHeaderPacket header = b.getBulletinHeaderPacket();

      ZipEntry headerEntry = null;
      Enumeration entries = zip.entries();
      while (entries.hasMoreElements()) headerEntry = (ZipEntry) entries.nextElement();
      InputStreamWithSeek headerIn = new ZipEntryInputStreamWithSeek(zip, headerEntry);
      try {
        header.loadFromXml(headerIn, verifier);
        if (!header.getLocalId().equals(headerEntry.getName()))
          throw new IOException("Misnamed header entry");
      } catch (Exception e) {
        throw new IOException(e.getMessage());
      } finally {
        headerIn.close();
      }

      FieldDataPacket data = b.getFieldDataPacket();

      entries = zip.entries();
      ZipEntry dataEntry = zip.getEntry(header.getFieldDataPacketId());
      if (dataEntry == null) throw new IOException("Data packet not found");
      InputStreamWithSeek dataIn = new ZipEntryInputStreamWithSeek(zip, dataEntry);
      try {
        data.loadFromXml(dataIn, header.getFieldDataSignature(), verifier);
      } catch (MartusCrypto.DecryptionException e) {
        // TODO mark bulletin as not complete
      } catch (Exception e) {
        throw new IOException(e.getMessage());
      } finally {
        dataIn.close();
      }

      FieldDataPacket privateData = b.getPrivateFieldDataPacket();

      entries = zip.entries();
      ZipEntry privateDataEntry = zip.getEntry(header.getPrivateFieldDataPacketId());
      if (privateDataEntry == null) throw new IOException("Private data packet not found");
      InputStreamWithSeek privateDataIn = new ZipEntryInputStreamWithSeek(zip, privateDataEntry);
      try {
        privateData.loadFromXml(privateDataIn, header.getPrivateFieldDataSignature(), verifier);
      } catch (MartusCrypto.DecryptionException e) {
        // TODO Mark bulletin as not complete
      } catch (Exception e) {
        System.out.println(e);
        e.printStackTrace();
        throw new IOException(e.getMessage());
      } finally {
        privateDataIn.close();
      }

      AttachmentProxy[] attachments = b.getPublicAttachments();
      b.clearPublicAttachments();
      for (int i = 0; i < attachments.length; ++i) {
        final AttachmentProxy ap =
            BulletinZipImporter.extractZipAttachmentToFileProxy(verifier, zip, attachments[i]);
        b.addPublicAttachment(ap);
      }

      AttachmentProxy[] attachmentsPrivate = b.getPrivateAttachments();
      b.clearPrivateAttachments();
      for (int i = 0; i < attachmentsPrivate.length; ++i) {
        final AttachmentProxy ap =
            BulletinZipImporter.extractZipAttachmentToFileProxy(
                verifier, zip, attachmentsPrivate[i]);
        b.addPrivateAttachment(ap);
      }

      b.setAuthorizedToReadKeys(header.getAuthorizedToReadKeys());
    } catch (Exception e) {
      throw new IOException(e.getMessage());
    } finally {
      zip.close();
    }
  }