Exemple #1
0
  public int ChangeStreamPass(
      XStorage xStorage, String sStreamName, byte[] pOldPass, byte[] pNewPass) {
    // open substream element
    XStream xSubStream = null;
    try {
      Object oSubStream =
          xStorage.openEncryptedStreamElement(
              sStreamName, ElementModes.WRITE, new String(pOldPass));
      xSubStream = UnoRuntime.queryInterface(XStream.class, oSubStream);
      if (xSubStream == null) {
        Error("Can't open substream '" + sStreamName + "'!");
        return 0;
      }
    } catch (Exception e) {
      Error("Can't open substream '" + sStreamName + "', exception : " + e + "!");
      return 0;
    }

    // change the password for the stream
    XEncryptionProtectedSource xStreamEncryption =
        UnoRuntime.queryInterface(XEncryptionProtectedSource.class, xSubStream);

    if (xStreamEncryption == null) {
      Message(
          "Optional interface XEncryptionProtectedSource is not implemented, feature can not be tested!");
      return -1;
    }

    try {
      xStreamEncryption.setEncryptionPassword(new String(pNewPass));
    } catch (Exception e) {
      Error("Can't change encryption key of the substream '" + sStreamName + "', exception:" + e);
      return 0;
    }

    // free the stream resources, garbage collector may remove the object too late
    XComponent xComponent = UnoRuntime.queryInterface(XComponent.class, xSubStream);
    if (xComponent == null) {
      Error("Can't get XComponent implementation from substream '" + sStreamName + "'!");
      return 0;
    }
    xComponent.dispose();

    return 1;
  }
Exemple #2
0
  public int ChangeStreamPassH(
      XStorage xStorage, String sPath, String sOldPass, String sNewPass, boolean bCommit) {
    // open substream element
    XHierarchicalStorageAccess xHStorage =
        (XHierarchicalStorageAccess)
            UnoRuntime.queryInterface(XHierarchicalStorageAccess.class, xStorage);
    if (xHStorage == null) {
      Error("The storage does not support hierarchical access!");
      return 0;
    }

    XStream xSubStream = null;
    try {
      Object oSubStream =
          xHStorage.openEncryptedStreamElementByHierarchicalName(
              sPath, ElementModes.WRITE, sOldPass);
      xSubStream = (XStream) UnoRuntime.queryInterface(XStream.class, oSubStream);
      if (xSubStream == null) {
        Error("Can't open encrypted substream '" + sPath + "'!");
        return 0;
      }
    } catch (Exception e) {
      Error("Can't open encrypted substream '" + sPath + "', exception : " + e + "!");
      return 0;
    }

    // change the password for the stream
    XEncryptionProtectedSource xStreamEncryption =
        (XEncryptionProtectedSource)
            UnoRuntime.queryInterface(XEncryptionProtectedSource.class, xSubStream);

    if (xStreamEncryption == null) {
      Message(
          "Optional interface XEncryptionProtectedSource is not implemented, feature can not be tested!");
      return -1;
    }

    try {
      xStreamEncryption.setEncryptionPassword(sNewPass);
    } catch (Exception e) {
      Error("Can't change encryption key of the substream '" + sPath + "', exception:" + e);
      return 0;
    }

    XTransactedObject xTransact =
        (XTransactedObject) UnoRuntime.queryInterface(XTransactedObject.class, xSubStream);
    if (xTransact == null) {
      Error("Substream '" + sPath + "', stream opened for writing must be transacted!");
      return 0;
    }

    if (bCommit) {
      try {
        xTransact.commit();
      } catch (Exception e) {
        Error(
            "Can't commit storage after substream '" + sPath + "' change, exception : " + e + "!");
        return 0;
      }
    }

    // free the stream resources, garbage collector may remove the object too late
    if (!disposeStream(xSubStream, sPath)) return 0;

    return 1;
  }