예제 #1
0
  public void generateXmlListOfForms(PrintWriter output, CallingContext cc)
      throws IOException, ODKDatastoreException {
    Document d = new Document();
    d.setStandalone(true);
    d.setEncoding(HtmlConsts.UTF8_ENCODE);
    Element e = d.createElement(XML_TAG_NAMESPACE, XFormsTableConsts.XFORMS_TAG);
    e.setPrefix(null, XML_TAG_NAMESPACE);
    d.addChild(0, Node.ELEMENT, e);
    int idx = 0;
    e.addChild(idx++, Node.IGNORABLE_WHITESPACE, BasicConsts.NEW_LINE);

    // build XML table of form information
    for (IForm form : forms) {
      if (!form.getDownloadEnabled()) continue;

      idx = generateFormXmlEntry(d, e, idx, form, cc);
    }

    KXmlSerializer serializer = new KXmlSerializer();
    serializer.setOutput(output);
    // setting the response content type emits the xml header.
    // just write the body here...
    d.writeChildren(serializer);
    serializer.flush();
  }
  private static void writeSubmissionManifest(
      EncryptedFormInformation formInfo, File submissionXml, List<File> mediaFiles)
      throws EncryptionException {

    Document d = new Document();
    d.setStandalone(true);
    d.setEncoding(UTF_8);
    Element e = d.createElement(XML_ENCRYPTED_TAG_NAMESPACE, DATA);
    e.setPrefix(null, XML_ENCRYPTED_TAG_NAMESPACE);
    e.setAttribute(null, ID, formInfo.formId);
    if (formInfo.formVersion != null) {
      e.setAttribute(null, VERSION, formInfo.formVersion);
    }
    e.setAttribute(null, ENCRYPTED, "yes");
    d.addChild(0, Node.ELEMENT, e);

    int idx = 0;
    Element c;
    c = d.createElement(XML_ENCRYPTED_TAG_NAMESPACE, BASE64_ENCRYPTED_KEY);
    c.addChild(0, Node.TEXT, formInfo.base64RsaEncryptedSymmetricKey);
    e.addChild(idx++, Node.ELEMENT, c);

    c = d.createElement(XML_OPENROSA_NAMESPACE, META);
    c.setPrefix("orx", XML_OPENROSA_NAMESPACE);
    {
      Element instanceTag = d.createElement(XML_OPENROSA_NAMESPACE, INSTANCE_ID);
      instanceTag.addChild(0, Node.TEXT, formInfo.instanceMetadata.instanceId);
      c.addChild(0, Node.ELEMENT, instanceTag);
    }
    e.addChild(idx++, Node.ELEMENT, c);
    e.addChild(idx++, Node.IGNORABLE_WHITESPACE, NEW_LINE);

    if (mediaFiles != null) {
      for (File file : mediaFiles) {
        c = d.createElement(XML_ENCRYPTED_TAG_NAMESPACE, MEDIA);
        Element fileTag = d.createElement(XML_ENCRYPTED_TAG_NAMESPACE, FILE);
        fileTag.addChild(0, Node.TEXT, file.getName() + ".enc");
        c.addChild(0, Node.ELEMENT, fileTag);
        e.addChild(idx++, Node.ELEMENT, c);
        e.addChild(idx++, Node.IGNORABLE_WHITESPACE, NEW_LINE);
      }
    }

    c = d.createElement(XML_ENCRYPTED_TAG_NAMESPACE, ENCRYPTED_XML_FILE);
    c.addChild(0, Node.TEXT, submissionXml.getName() + ".enc");
    e.addChild(idx++, Node.ELEMENT, c);

    c = d.createElement(XML_ENCRYPTED_TAG_NAMESPACE, BASE64_ENCRYPTED_ELEMENT_SIGNATURE);
    c.addChild(0, Node.TEXT, formInfo.getBase64EncryptedElementSignature());
    e.addChild(idx++, Node.ELEMENT, c);

    FileOutputStream fout = null;
    OutputStreamWriter writer = null;
    try {
      fout = new FileOutputStream(submissionXml);
      writer = new OutputStreamWriter(fout, UTF_8);

      KXmlSerializer serializer = new KXmlSerializer();
      serializer.setOutput(writer);
      // setting the response content type emits the xml header.
      // just write the body here...
      d.writeChildren(serializer);
      serializer.flush();
      writer.flush();
      fout.getChannel().force(true);
      writer.close();
    } catch (Exception ex) {
      ex.printStackTrace();
      String msg =
          "Error writing submission.xml for encrypted submission: "
              + submissionXml.getParentFile().getName();
      Log.e(t, msg);
      throw new EncryptionException(msg, ex);
    } finally {
      IOUtils.closeQuietly(writer);
      IOUtils.closeQuietly(fout);
    }
  }