public static AttachmentProxy extractZipAttachmentToFileProxy(
     MartusCrypto verifier, ZipFile zip, AttachmentProxy attachment) throws IOException {
   String localId = attachment.getUniversalId().getLocalId();
   ZipEntry attachmentEntry = zip.getEntry(localId);
   if (attachmentEntry == null) throw new IOException("Attachment packet not found: " + localId);
   InputStreamWithSeek attachmentIn = new ZipEntryInputStreamWithSeek(zip, attachmentEntry);
   try {
     return AttachmentProxy.createFileProxyFromAttachmentPacket(
         attachmentIn, attachment, verifier);
   } catch (Exception e) {
     throw new IOException(e.getMessage());
   } finally {
     attachmentIn.close();
   }
 }
Example #2
0
  public AttachmentProxy getAsFileProxy(
      AttachmentProxy ap, ReadableDatabase otherDatabase, String status)
      throws IOException, CryptoException, InvalidPacketException, SignatureVerificationException,
          WrongPacketTypeException, InvalidBase64Exception {
    if (ap.getFile() != null) return ap;
    if (otherDatabase == null) return ap;

    DatabaseKey key = DatabaseKey.createKey(ap.getUniversalId(), status);
    InputStreamWithSeek packetIn = otherDatabase.openInputStream(key, security);
    if (packetIn == null) return ap;

    try {
      return AttachmentProxy.createFileProxyFromAttachmentPacket(packetIn, ap, security);
    } finally {
      packetIn.close();
    }
  }
  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();
    }
  }