예제 #1
0
  /**
   * Adds a assignment relationship of a LinkContract to a target assignee node
   *
   * @param assignee The context node to whom this LinkContract is being assigned to
   * @return NONE
   */
  public boolean addAssignee(ContextNode assignee) {
    boolean status = false;
    if (assignee == null) {
      // TBD
      // write error in debug log
      return status;
    }

    // one cannot add this Link Contract node as assignee to itself

    if (assignee.equals(contextNode)) {
      return false;
    }

    try {
      if (contextNode.createRelation(XDILinkContractConstants.XRI_S_IS_DO, assignee) != null) {

        status = true;
      } else {
        // TBD
        // write error in debug log

      }
    } catch (Xdi2GraphException relationExists) {

    }
    return status;
  }
예제 #2
0
  /**
   * Removes an assignment relationship of a LinkContract from an assignee node
   *
   * @param assignee The context node who will not longer have access to this LinkContract
   * @return NONE
   */
  public boolean removeAssignee(ContextNode assignee) {
    boolean status = false;
    if (assignee == null) {
      // TBD
      // write error in debug log
      return status;
    }
    Iterator<Relation> allRelations = contextNode.getRelations();
    Relation r = null;
    for (; allRelations.hasNext(); ) {
      r = allRelations.next();
      ContextNode target = r.follow();
      if (target.equals(assignee)) {

        // write debug log with information about the relation XRI that
        // is removed
        // TBD
        status = true;
        break;
      }
    }
    if (status) {
      contextNode.deleteRelation(XDILinkContractConstants.XRI_S_IS_DO, r.getTargetContextNodeXri());
    }
    return status;
  }
예제 #3
0
  public boolean getLiteral(
      XRI3Segment[] contributorXris,
      XRI3Segment relativeContextNodeXri,
      XRI3Segment contextNodeXri,
      String literalData,
      GetOperation operation,
      MessageResult messageResult,
      ExecutionContext executionContext)
      throws Xdi2MessagingException {

    MessageResult tempMessageResult = new MessageResult();

    this.getContext(
        contributorXris,
        relativeContextNodeXri,
        contextNodeXri,
        operation,
        tempMessageResult,
        executionContext);

    ContextNode tempContextNode =
        tempMessageResult.getGraph().findContextNode(contextNodeXri, false);
    if (tempContextNode == null) return false;

    Literal tempLiteral = tempContextNode.getLiteral();
    if (tempLiteral == null) return false;

    if (literalData.isEmpty() || literalData.equals(tempLiteral.getLiteralData())) {

      CopyUtil.copyLiteral(tempLiteral, messageResult.getGraph(), null);
    }

    return false;
  }
예제 #4
0
  public ContextNode getBaseContextNode() {

    ContextNode contextNode =
        (this.getXdiAttribute() instanceof XdiAttributeInstance)
            ? this.getXdiAttribute().getContextNode().getContextNode()
            : this.getXdiAttribute().getContextNode();
    contextNode = contextNode.getContextNode();

    return contextNode;
  }
  public static void storeAccessToken(Graph graph, XDI3Segment userXri, String accessToken) {

    XDI3Segment contextNodeXri =
        new XDI3Segment(
            ""
                + GoogleCalendarMapping.XRI_S_GOOGLE_CALENDAR_CONTEXT
                + userXri
                + XDIMessagingConstants.XRI_S_OAUTH_TOKEN);

    ContextNode contextNode = graph.findContextNode(contextNodeXri, true);

    if (contextNode.containsLiteral()) contextNode.getLiteral().setLiteralData(accessToken);
    else contextNode.createLiteral(accessToken);
  }
  public static void removeAccessToken(Graph graph, XDI3Segment userXri) {

    XDI3Segment contextNodeXri =
        new XDI3Segment(
            ""
                + GoogleCalendarMapping.XRI_S_GOOGLE_CALENDAR_CONTEXT
                + userXri
                + XDIMessagingConstants.XRI_S_OAUTH_TOKEN);

    ContextNode contextNode = graph.findContextNode(contextNodeXri, false);
    if (contextNode == null) return;

    contextNode.delete();
  }
예제 #7
0
  public boolean getRelation(
      XRI3Segment[] contributorXris,
      XRI3Segment relativeContextNodeXri,
      XRI3Segment arcXri,
      XRI3Segment targetContextNodeXri,
      XRI3Segment contextNodeXri,
      GetOperation operation,
      MessageResult messageResult,
      ExecutionContext executionContext)
      throws Xdi2MessagingException {

    MessageResult tempMessageResult = new MessageResult();

    this.getContext(
        contributorXris,
        relativeContextNodeXri,
        contextNodeXri,
        operation,
        tempMessageResult,
        executionContext);

    ContextNode tempContextNode =
        tempMessageResult.getGraph().findContextNode(contextNodeXri, false);
    if (tempContextNode == null) return false;

    if (Variables.isVariableSingle(targetContextNodeXri)) {

      Iterator<Relation> relations;

      if (Variables.isVariableSingle(arcXri)) {

        relations = tempContextNode.getRelations();
      } else {

        relations = tempContextNode.getRelations(arcXri);
      }

      while (relations.hasNext())
        CopyUtil.copyRelation(relations.next(), messageResult.getGraph(), null);
    } else {

      Relation relation = tempContextNode.getRelation(arcXri, targetContextNodeXri);
      if (relation == null) return false;

      CopyUtil.copyRelation(relation, messageResult.getGraph(), null);
    }

    return false;
  }
예제 #8
0
  /**
   * Gets all assignees of this Link Contract node
   *
   * @return an iterator over the list of assignees
   */
  public Iterator<ContextNode> getAssignees() {

    List<ContextNode> assignees = new ArrayList<ContextNode>();
    Iterator<Relation> allRelations = contextNode.getRelations();
    for (; allRelations.hasNext(); ) {
      Relation r = allRelations.next();

      if (r.getArcXri().equals(XDILinkContractConstants.XRI_S_IS_DO)) {
        ContextNode assignee = r.follow();
        assignees.add(assignee);
      }
    }
    if (assignees.isEmpty()) {
      return new EmptyIterator<ContextNode>();
    } else {
      return new SelectingIterator<ContextNode>(assignees.iterator()) {

        @Override
        public boolean select(ContextNode contextNode) {

          return true;
        }
      };
    }
  }
  public static String retrieveAccessToken(Graph graph, XDI3Segment userXri) {

    XDI3Segment contextNodeXri =
        new XDI3Segment(
            ""
                + GoogleCalendarMapping.XRI_S_GOOGLE_CALENDAR_CONTEXT
                + userXri
                + XDIMessagingConstants.XRI_S_OAUTH_TOKEN);

    ContextNode contextNode = graph.findContextNode(contextNodeXri, false);
    if (contextNode == null) return null;

    Literal literal = contextNode.getLiteral();
    if (literal == null) return null;

    return literal.getLiteralData();
  }
예제 #10
0
  public boolean removePermission(XDILinkContractPermission permission, ContextNode targetNode) {
    boolean status = false;
    XRI3Segment perm = null;
    switch (permission) {
      case LC_OP_GET:
        perm = XDILinkContractConstants.XRI_S_GET;
        break;
      case LC_OP_ADD:
        perm = XDILinkContractConstants.XRI_S_ADD;
        break;
      case LC_OP_MOD:
        perm = XDILinkContractConstants.XRI_S_MOD;
        break;
      case LC_OP_DEL:
        perm = XDILinkContractConstants.XRI_S_DEL;
        break;
      case LC_OP_ALL:
        perm = XDILinkContractConstants.XRI_S_ALL;
        break;

      default:
        // TBD
        // debug log
        break;
    }
    if (null == perm) {
      return status;
    }
    Iterator<Relation> allRelations = contextNode.getRelations();
    Relation r = null;
    for (; allRelations.hasNext(); ) {
      r = allRelations.next();
      if (r.getArcXri().equals(perm)) {
        ContextNode nodeWithPermission = r.follow();
        if (nodeWithPermission.equals(targetNode)) {
          status = true;
          break;
        }
      }
    }
    if (status) {
      contextNode.deleteRelation(r.getArcXri(), r.getTargetContextNodeXri());
    }
    return status;
  }
예제 #11
0
  private static void addContextNodeToBuffer(StringBuffer buffer, ContextNode contextNode) {

    buffer.append("{\n");
    buffer.append("type: \"context\",\n");
    buffer.append("name: \"" + contextNode.getXDIAddress() + "\",\n");
    buffer.append(
        "arc: \"" + (contextNode.isRootContextNode() ? "" : contextNode.getXDIArc()) + "\",\n");
    buffer.append("root: " + XdiAbstractRoot.isValid(contextNode) + ",\n");
    buffer.append("contents: [\n");

    for (Iterator<ContextNode> innerContextNodes = contextNode.getContextNodes();
        innerContextNodes.hasNext(); ) {

      ContextNode innerContextNode = innerContextNodes.next();
      addContextNodeToBuffer(buffer, innerContextNode);
      if (innerContextNodes.hasNext() || contextNode.containsLiteralNode()) buffer.append(",");
      buffer.append("\n");
    }

    if (contextNode.containsLiteralNode()) {

      buffer.append("{\n");
      buffer.append("type: \"literal\",\n");
      buffer.append("name: \"\\\"" + contextNode.getLiteralNode().getLiteralData() + "\\\"\",\n");
      buffer.append("arc: \"&\"\n");
      buffer.append("}\n");
    }

    buffer.append("],\n");

    buffer.append("rel: [");
    for (Iterator<Relation> relations = contextNode.getRelations(); relations.hasNext(); ) {

      Relation relation = relations.next();

      buffer.append("{\n");
      buffer.append("type: \"relation\",\n");
      buffer.append("arc: \"" + relation.getXDIAddress() + "\",\n");
      buffer.append("target: \"" + relation.getTargetXDIAddress() + "\"\n");
      buffer.append("}");
      if (relations.hasNext()) buffer.append(",");
      buffer.append("\n");
    }
    buffer.append("]");

    buffer.append("}\n");
  }
예제 #12
0
  @Override
  public Node follow() {

    XDIAddress targetXDIAddress = this.getTargetXDIAddress();

    ContextNode targetContextNode =
        (ContextNode)
            this.getGraph().getDeepNode(targetXDIAddress.getContextNodeXDIAddress(), false);

    if (targetContextNode == null) {

      if (log.isWarnEnabled())
        log.warn("Relation points to non-existent target node " + targetXDIAddress);
      return null;
    }

    if (targetXDIAddress.isLiteralNodeXDIAddress()) {

      return targetContextNode.getLiteralNode();
    } else {

      return targetContextNode;
    }
  }
예제 #13
0
  public Iterator<ContextNode> getNodesWithPermission(XDILinkContractPermission permission) {

    List<ContextNode> nodesWithPermission = new ArrayList<ContextNode>();
    XRI3Segment perm = null;
    switch (permission) {
      case LC_OP_GET:
        perm = XDILinkContractConstants.XRI_S_GET;
        break;
      case LC_OP_ADD:
        perm = XDILinkContractConstants.XRI_S_ADD;
        break;
      case LC_OP_MOD:
        perm = XDILinkContractConstants.XRI_S_MOD;
        break;
      case LC_OP_DEL:
        perm = XDILinkContractConstants.XRI_S_DEL;
        break;
      case LC_OP_ALL:
        perm = XDILinkContractConstants.XRI_S_ALL;
        break;

      default:
        // TBD
        // debug log
        break;
    }
    if (null == perm) {
      return new EmptyIterator<ContextNode>();
    }
    Iterator<Relation> allRelations = contextNode.getRelations();

    for (; allRelations.hasNext(); ) {
      Relation r = allRelations.next();
      if (r.getArcXri().equals(perm)) {
        ContextNode nodeWithPermission = r.follow();
        nodesWithPermission.add(nodeWithPermission);
      }
    }
    return new SelectingIterator<ContextNode>(nodesWithPermission.iterator()) {

      @Override
      public boolean select(ContextNode contextNode) {

        return true;
      }
    };
  }
예제 #14
0
  @Override
  public boolean evaluateInternal(PolicyEvaluationContext policyEvaluationContext) {

    ContextNode subject =
        policyEvaluationContext.getContextNode(this.getXDIStatement().getSubject());
    ContextNode object =
        policyEvaluationContext.getContextNode((XDIAddress) this.getXDIStatement().getObject());

    if (subject == null || object == null) return false;

    if (subject.containsLiteralNode()) {

      if (!(object.containsLiteralNode())) return false;

      String subjectLiteralData = subject.getLiteralNode().getLiteralDataString();
      String objectLiteralData = object.getLiteralNode().getLiteralDataString();

      if (subjectLiteralData == null || objectLiteralData == null) return false;

      return Pattern.matches(objectLiteralData, subjectLiteralData);
    }

    return false;
  }
예제 #15
0
  /**
   * Checks if a context node is a valid XDI link contract.
   *
   * @param contextNode The context node to check.
   * @return True if the context node is a valid XDI link contract.
   */
  public static boolean isValid(ContextNode contextNode) {

    return XDILinkContractConstants.XRI_SS_DO.equals(contextNode.getArcXri());
  }
예제 #16
0
  /**
   * Checks if a context node is a valid XDI attribute singleton.
   *
   * @param contextNode The context node to check.
   * @return True if the context node is a valid XDI attribute singleton.
   */
  public static boolean isValid(ContextNode contextNode) {

    if (contextNode == null) return false;

    return isValidArcXri(contextNode.getArcXri());
  }
예제 #17
0
  /**
   * Adds a permission (one of GET, ADD, MOD, DEL, COPY , MOVE , ALL) from this Link Contract node
   * to a target node
   *
   * @param permission The enum value of permission
   * @param targetNode The target node where the permission relation arc will terminate
   * @return NONE
   */
  public boolean addPermission(XDILinkContractPermission permission, ContextNode targetNode) {
    boolean status = false;
    XRI3Segment perm = null;

    // one cannot add an authorization permission to the Link Contract Node
    // itself

    if (targetNode.equals(contextNode)) {
      return false;
    }

    // if the same permission arc exists for the same target node, then a
    // new arc should not be added

    for (Iterator<ContextNode> iter = this.getNodesWithPermission(permission); iter.hasNext(); ) {
      ContextNode t = iter.next();
      if (t.equals(targetNode)) {
        return true;
      }
    }

    // if an arc to the given target node exists with $all, then no other
    // permission arc should be allowed

    for (Iterator<ContextNode> iter =
            this.getNodesWithPermission(XDILinkContractPermission.LC_OP_ALL);
        iter.hasNext(); ) {
      ContextNode t = iter.next();
      if (t.equals(targetNode)) {
        return true;
      }
    }
    // if a $all permission is added to the target node then all other
    // permission arcs should be deleted
    if (permission == XDILinkContractPermission.LC_OP_ALL) {
      this.removePermission(XDILinkContractPermission.LC_OP_GET, targetNode);
      this.removePermission(XDILinkContractPermission.LC_OP_ADD, targetNode);
      this.removePermission(XDILinkContractPermission.LC_OP_MOD, targetNode);
      this.removePermission(XDILinkContractPermission.LC_OP_DEL, targetNode);
    }

    switch (permission) {
      case LC_OP_GET:
        perm = XDILinkContractConstants.XRI_S_GET;
        break;
      case LC_OP_ADD:
        perm = XDILinkContractConstants.XRI_S_ADD;
        break;
      case LC_OP_MOD:
        perm = XDILinkContractConstants.XRI_S_MOD;
        break;
      case LC_OP_DEL:
        perm = XDILinkContractConstants.XRI_S_DEL;
        break;
      case LC_OP_ALL:
        perm = XDILinkContractConstants.XRI_S_ALL;
        break;

      default:
        // TBD
        // debug log
        break;
    }
    if (null != perm) {
      try {
        if (null != contextNode.createRelation(perm, targetNode)) {
          status = true;
        }
      } catch (Xdi2GraphException relationExists) {

      }
    }
    return status;
  }