Example #1
0
  /* (non-Javadoc)
   * @see org.exist.xquery.BasicFunction#eval(org.exist.xquery.value.Sequence[], org.exist.xquery.value.Sequence)
   */
  public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
    final String userName = args[0].getStringValue();

    Collection collection = null;
    try {
      collection =
          new LocalCollection(
              context.getSubject(),
              context.getBroker().getBrokerPool(),
              XmldbURI.ROOT_COLLECTION_URI,
              context.getAccessContext());
      final UserManagementService ums =
          (UserManagementService) collection.getService("UserManagementService", "1.0");
      final Account user = ums.getAccount(userName);

      if (user == null)
      // todo - why not just return false()? /ljo
      {
        return Sequence.EMPTY_SEQUENCE;
      }
      return user.hasDbaRole() ? BooleanValue.TRUE : BooleanValue.FALSE;
    } catch (final XMLDBException xe) {
      logger.error("Failed to access user " + userName);
      throw new XPathException(this, "Failed to access user " + userName, xe);
    } finally {
      if (null != collection)
        try {
          collection.close();
        } catch (final XMLDBException e) {
          /* ignore */
        }
    }
  }
  /**
   * Main method of the example class.
   *
   * @param args (ignored) command-line arguments
   * @throws Exception exception
   */
  public static void main(final String[] args) throws Exception {

    System.out.println("=== XMLDBInsert ===");

    // Collection instance
    Collection col = null;

    try {
      // Register the database
      Class<?> c = Class.forName(DRIVER);
      Database db = (Database) c.newInstance();
      DatabaseManager.registerDatabase(db);

      System.out.println("\n* Get collection.");

      // Receive the collection
      col = DatabaseManager.getCollection(DBNAME);

      // ID for the new document
      String id = "world";

      // Content of the new document
      String doc = "<xml>Hello World!</xml>";

      System.out.println("\n* Create new resource.");

      // Create a new XML resource with the specified ID
      XMLResource res = (XMLResource) col.createResource(id, XMLResource.RESOURCE_TYPE);

      // Set the content of the XML resource as the document
      res.setContent(doc);

      System.out.println("\n* Store new resource.");

      // Store the resource into the database
      col.storeResource(res);

    } catch (final XMLDBException ex) {
      // Handle exceptions
      System.err.println("XML:DB Exception occurred " + ex.errorCode);
      ex.printStackTrace();
    } finally {
      // Close the collection
      if (col != null) col.close();
    }
  }
  /**
   * Main method of the example class.
   *
   * @param args (ignored) command-line arguments
   * @throws Exception exception
   */
  public static void main(final String[] args) throws Exception {

    System.out.println("=== XMLDBQuery ===\n");

    System.out.println("* Run query via XML:DB:");

    // Collection instance
    Collection coll = null;

    try {
      // Register the database
      Class<?> c = Class.forName(DRIVER);
      Database db = (Database) c.newInstance();
      DatabaseManager.registerDatabase(db);

      // Receive the database
      coll = DatabaseManager.getCollection(DBNAME);

      // Receive the XPath query service
      XPathQueryService service = (XPathQueryService) coll.getService("XPathQueryService", "1.0");

      // Execute the query and receives all results
      ResourceSet set = service.query(QUERY);

      // Create a result iterator
      ResourceIterator iter = set.getIterator();

      // Loop through all result items
      while (iter.hasMoreResources()) {
        // Receive the next results
        Resource res = iter.nextResource();

        // Write the result to the console
        System.out.println(res.getContent());
      }
    } catch (final XMLDBException ex) {
      // Handle exceptions
      System.err.println("XML:DB Exception occured " + ex.errorCode);
    } finally {
      // Close the collection
      if (coll != null) coll.close();
    }
  }
Example #4
0
  /*
   * (non-Javadoc)
   *
   * @see org.exist.xquery.Expression#eval(org.exist.dom.DocumentSet,
   * org.exist.xquery.value.Sequence, org.exist.xquery.value.Item)
   */
  public Sequence eval(Sequence args[], Sequence contextSequence) throws XPathException {

    if (!context.getSubject().hasDbaRole()) {
      final XPathException xPathException =
          new XPathException(
              this,
              "Permission denied, calling user '"
                  + context.getSubject().getName()
                  + "' must be a DBA to call this function.");
      logger.error("Invalid user", xPathException);
      throw xPathException;
    }

    final String user = args[0].getStringValue();
    final String pass = args[1].getStringValue();

    logger.info("Attempting to create user " + user);

    final UserAider userObj = new UserAider(user);
    userObj.setPassword(pass);

    // changed by wolf: the first group is always the primary group, so we
    // don't need
    // an additional argument
    final Sequence groups = args[2];
    final int len = groups.getItemCount();
    for (int x = 0; x < len; x++) {
      userObj.addGroup(groups.itemAt(x).getStringValue());
    }

    Collection collection = null;
    try {
      collection =
          new LocalCollection(
              context.getSubject(),
              context.getBroker().getBrokerPool(),
              XmldbURI.ROOT_COLLECTION_URI,
              context.getAccessContext());
      final UserManagementService ums =
          (UserManagementService) collection.getService("UserManagementService", "1.0");
      ums.addAccount(userObj);

    } catch (final XMLDBException xe) {
      logger.error("Failed to create user: "******"Failed to create user: "******"Failed to create new user '" + user + "' by " + context.getSubject().getName(),
          xe);
    } finally {
      if (null != collection)
        try {
          collection.close();
        } catch (final XMLDBException e) {
          /* ignore */
        }
    }
    return Sequence.EMPTY_SEQUENCE;
  }