Пример #1
0
  public boolean execute(Object... objects) throws Exception {
    Map<String, Object> inputMap = (Map<String, Object>) objects[0];
    Map<String, Object> data = (Map<String, Object>) inputMap.get("data");
    Map<String, Object> payload = (Map<String, Object>) inputMap.get("payload");
    Map<String, Object> user = (Map<String, Object>) payload.get("user");
    String rid = (String) data.get("@rid");
    String userId = (String) data.get("userId");
    String error = null;

    String host = (String) user.get("host");
    if (host != null && !host.equals(data.get("host"))) {
      error = "You can only delete user from host: " + host;
      inputMap.put("responseCode", 403);
    } else {
      if (rid == null && userId == null) {
        error = "rid or userId is required";
        inputMap.put("responseCode", 400);
      } else {
        OrientGraph graph = ServiceLocator.getInstance().getGraph();
        Vertex deleteUser = null;
        try {
          if (rid != null) {
            deleteUser = DbService.getVertexByRid(graph, rid);
            if (deleteUser == null) {
              error = "User with rid " + rid + " cannot be found.";
              inputMap.put("responseCode", 404);
            }
          } else {
            deleteUser = getUserByUserId(graph, userId);
            if (deleteUser == null) {
              error = "User with userId " + userId + " cannot be found.";
              inputMap.put("responseCode", 404);
            }
          }
          if (deleteUser != null) {
            Map eventMap = getEventMap(inputMap);
            Map<String, Object> eventData = (Map<String, Object>) eventMap.get("data");
            inputMap.put("eventMap", eventMap);
            eventData.put("userId", deleteUser.getProperty("userId").toString());
          }
        } catch (Exception e) {
          logger.error("Exception:", e);
          throw e;
        } finally {
          graph.shutdown();
        }
      }
    }
    if (error != null) {
      inputMap.put("result", error);
      return false;
    } else {
      return true;
    }
  }
Пример #2
0
 public boolean execute(Object... objects) throws Exception {
   Map<String, Object> inputMap = (Map<String, Object>) objects[0];
   HttpServerExchange exchange = (HttpServerExchange) inputMap.get("exchange");
   Map<String, Object> data = (Map<String, Object>) inputMap.get("data");
   Map<String, Object> payload = (Map<String, Object>) inputMap.get("payload");
   Map<String, Object> user = (Map<String, Object>) payload.get("user");
   String rid = (String) data.get("@rid");
   String host = (String) data.get("host");
   String error = null;
   String userHost = (String) user.get("host");
   if (userHost != null && !userHost.equals(host)) {
     error = "You can only delete page from host: " + host;
     inputMap.put("responseCode", 403);
   } else {
     OrientGraph graph = ServiceLocator.getInstance().getGraph();
     try {
       Vertex page = DbService.getVertexByRid(graph, rid);
       if (page != null) {
         int inputVersion = (int) data.get("@version");
         int storedVersion = page.getProperty("@version");
         if (inputVersion != storedVersion) {
           inputMap.put("responseCode", 400);
           error =
               "Deleting version "
                   + inputVersion
                   + " doesn't match stored version "
                   + storedVersion;
         } else {
           Map eventMap = getEventMap(inputMap);
           Map<String, Object> eventData = (Map<String, Object>) eventMap.get("data");
           inputMap.put("eventMap", eventMap);
           eventData.put("pageId", page.getProperty("pageId"));
         }
       } else {
         error = "Page with @rid " + rid + " doesn't exist";
         inputMap.put("responseCode", 400);
       }
     } catch (Exception e) {
       logger.error("Exception:", e);
       throw e;
     } finally {
       graph.shutdown();
     }
   }
   if (error != null) {
     inputMap.put("result", error);
     return false;
   } else {
     return true;
   }
 }
Пример #3
0
  private void createEdge(List<ODocument> documents, OClass edgeClass) {
    OrientGraph tx = null;
    try {
      tx = new OrientGraphFactory(getDatabase().getURL()).getTx();
      for (ODocument createTo : documents) {
        tx.addEdge(
            null,
            tx.getVertex(documentModel.getObject().getIdentity()),
            tx.getVertex(createTo.getIdentity()),
            edgeClass.getName());
      }

    } finally {
      if (tx != null) tx.shutdown();
    }
  }
Пример #4
0
  @Override
  protected void addControls() {
    StringValue idParam = getPageParameters().get(0);
    if (!idParam.isNull() && !idParam.isEmpty()) {
      String templateId = idParam.toString();

      OrientGraph graph = DatabaseHelper.getGraph();
      try {
        Template template = null;
        try {
          template = Template.getByTemplateId(graph, templateId);
        } catch (NonUniqueResultException e) {
          log.error("Duplicate template representation: " + templateId, e);
        }
        if (template != null) {
          // get associated user
          Vertex v = template.getV();
          Iterator<Vertex> owners = v.getVertices(Direction.OUT, "owner").iterator();
          if (UserUtil.isAdmin() // user is admin
              // or user is owner
              || (owners.hasNext()
                  && UserUtil.getLogin().equals(new User(owners.next(), graph).getLogin()))) {
            add(new TemplateForm("edit-form", false, templateId));
          } else {
            throw new AbortWithHttpErrorCodeException(HttpServletResponse.SC_FORBIDDEN);
          }
        } else {
          throw new AbortWithHttpErrorCodeException(
              HttpServletResponse.SC_NOT_FOUND, "Template not found.");
        }
      } finally {
        graph.shutdown();
      }
    } else
      throw new AbortWithHttpErrorCodeException(
          HttpServletResponse.SC_BAD_REQUEST, "Template identifier must be specified.");
  }
Пример #5
0
 public boolean execute(Object... objects) throws Exception {
   Map<String, Object> inputMap = (Map<String, Object>) objects[0];
   Map<String, Object> data = (Map<String, Object>) inputMap.get("data");
   String host = (String) data.get("host");
   String source = (String) data.get("source");
   String dest = (String) data.get("desc");
   String error = null;
   Map<String, Object> payload = (Map<String, Object>) inputMap.get("payload");
   Map<String, Object> user = (Map<String, Object>) payload.get("user");
   String userHost = (String) user.get("host");
   OrientGraph graph = ServiceLocator.getInstance().getGraph();
   try {
     Vertex sourceRule = DbService.getVertexByRid(graph, source);
     Vertex destRule = DbService.getVertexByRid(graph, dest);
     if (sourceRule == null || destRule == null) {
       error = "source rule or destination rule doesn't exist";
       inputMap.put("responseCode", 400);
     } else {
       String sourceRuleClass = sourceRule.getProperty("ruleClass");
       String destRuleClass = destRule.getProperty("ruleClass");
       if (userHost != null) {
         if (!userHost.equals(host)) {
           error = "You can only add dependency from host: " + host;
           inputMap.put("responseCode", 403);
         } else {
           // make sure dest ruleClass contains host.
           if (!destRuleClass.contains(host)) {
             error = "Destination rule doesn't belong to the host " + host;
             inputMap.put("responseCode", 403);
           } else {
             // check if there is an depend edge from source to dest
             boolean hasEdge = false;
             for (Edge edge : (Iterable<Edge>) sourceRule.getEdges(Direction.OUT, "Own")) {
               if (edge.getVertex(Direction.IN) == destRule) hasEdge = true;
             }
             if (hasEdge) {
               error = "There is depend edge between source rule and dest rule";
               inputMap.put("responseCode", 400);
             } else {
               Map eventMap = getEventMap(inputMap);
               Map<String, Object> eventData = (Map<String, Object>) eventMap.get("data");
               inputMap.put("eventMap", eventMap);
               eventData.put("sourceRuleClass", sourceRuleClass);
               eventData.put("destRuleClass", destRuleClass);
               eventData.put("content", data.get("content"));
               eventData.put("createDate", new java.util.Date());
               eventData.put("createUserId", user.get("userId"));
             }
           }
         }
       }
     }
   } catch (Exception e) {
     logger.error("Exception:", e);
     throw e;
   } finally {
     graph.shutdown();
   }
   if (error != null) {
     inputMap.put("result", error);
     return false;
   } else {
     return true;
   }
 }
Пример #6
0
  public boolean execute(Object... objects) throws Exception {
    Map<String, Object> inputMap = (Map<String, Object>) objects[0];
    Map<String, Object> data = (Map<String, Object>) inputMap.get("data");
    Map<String, Object> payload = (Map<String, Object>) inputMap.get("payload");
    int inputVersion = (int) data.get("@version");
    String rid = (String) data.get("@rid");
    String ruleClass = (String) data.get("ruleClass");
    String error = null;
    if (payload == null) {
      error = "Login is required";
      inputMap.put("responseCode", 401);
    } else {
      Map<String, Object> user = (Map<String, Object>) payload.get("user");
      List roles = (List) user.get("roles");
      if (!roles.contains("owner") && !roles.contains("admin") && !roles.contains("ruleAdmin")) {
        error = "Role owner or admin or ruleAdmin is required to update access control";
        inputMap.put("responseCode", 403);
      } else {
        String host = (String) user.get("host");
        if (host != null) {
          if (!host.equals(data.get("host"))) {
            error = "You can only update access control for host: " + host;
            inputMap.put("responseCode", 403);
          } else {
            // make sure the ruleClass contains the host.
            if (!ruleClass.contains(host)) {
              // you are not allowed to update access control to the rule as it is not owned by the
              // host.
              error = "ruleClass is not owned by the host: " + host;
              inputMap.put("responseCode", 403);
            }
          }
        } else {
          OrientGraph graph = ServiceLocator.getInstance().getGraph();
          try {
            Vertex access = DbService.getVertexByRid(graph, rid);
            if (access == null) {
              error = "Access control with @rid " + rid + " cannot be found";
              inputMap.put("responseCode", 404);
            } else {
              int storedVersion = access.getProperty("@version");
              if (inputVersion != storedVersion) {
                error =
                    "Updating version "
                        + inputVersion
                        + " doesn't match stored version "
                        + storedVersion;
                inputMap.put("responseCode", 400);
              } else {
                Map eventMap = getEventMap(inputMap);
                Map<String, Object> eventData = (Map<String, Object>) eventMap.get("data");
                inputMap.put("eventMap", eventMap);
                eventData.put("ruleClass", ruleClass);
                String accessLevel = (String) data.get("accessLevel");
                eventData.put("accessLevel", accessLevel);
                List clients = (List) data.get("clients");
                roles = (List) data.get("roles");
                List users = (List) data.get("users");
                switch (accessLevel) {
                  case "A":
                    // Access by anyone, ignore clients, roles and users
                    break;
                  case "N":
                    // Not accessible, ignore clients, roles and users.
                    break;
                  case "C":
                    // client id is in the jwt token like userId and roles.
                    if (clients == null || clients.size() == 0) {
                      error = "Clients are empty for client based access control";
                      inputMap.put("responseCode", 400);
                    } else {
                      eventData.put("clients", clients);
                    }
                    break;
                  case "R":
                    // role only
                    if (roles == null || roles.size() == 0) {
                      error = "Roles are empty for role based access control";
                      inputMap.put("responseCode", 400);
                    } else {
                      eventData.put("roles", roles);
                    }
                    break;
                  case "U":
                    // user only
                    if (users == null || users.size() == 0) {
                      error = "Users are empty for user based access control";
                      inputMap.put("responseCode", 400);
                    } else {
                      eventData.put("users", users);
                    }
                    break;
                  case "CR":
                    // client and role
                    if (clients == null
                        || clients.size() == 0
                        || roles == null
                        || roles.size() == 0) {
                      error = "Clients or roles are empty for client and role based access control";
                      inputMap.put("responseCode", 400);
                    } else {
                      eventData.put("clients", clients);
                      eventData.put("roles", roles);
                    }
                    break;
                  case "CU":
                    // client and user
                    if (clients == null
                        || clients.size() == 0
                        || users == null
                        || users.size() == 0) {
                      error = "Clients or users are empty for client and user based access control";
                      inputMap.put("responseCode", 400);
                    } else {
                      eventData.put("clients", clients);
                      eventData.put("users", users);
                    }
                    break;
                  case "RU":
                    // role and user
                    if (roles == null || roles.size() == 0 || users == null || users.size() == 0) {
                      error = "Roles or users are empty for role and user based access control";
                      inputMap.put("responseCode", 400);
                    } else {
                      eventData.put("roles", roles);
                      eventData.put("users", users);
                    }
                    break;
                  case "CRU":
                    // client, role and user
                    if (clients == null
                        || clients.size() == 0
                        || roles == null
                        || roles.size() == 0
                        || users == null
                        || users.size() == 0) {
                      error =
                          "Clients, roles or users are empty for client, role and user based access control";
                      inputMap.put("responseCode", 400);
                    } else {
                      eventData.put("clients", clients);
                      eventData.put("roles", roles);
                      eventData.put("users", users);
                    }
                    break;
                  default:
                    logger.error("Invalid Access Level: " + accessLevel);
                }
                eventData.put("updateDate", new java.util.Date());
                eventData.put("updateUserId", user.get("userId"));
              }
            }

          } catch (Exception e) {
            logger.error("Exception:", e);
            throw e;
          } finally {
            graph.shutdown();
          }
        }
      }
    }
    if (error != null) {
      inputMap.put("result", error);
      return false;
    } else {
      return true;
    }
  }
  public static void createGraphDatabase() {
    OrientGraph graphDB = null;
    try {

      OServerAdmin admin = new OServerAdmin(URL_DB);
      admin.connect(ADMINISTRATOR_NAME, ADMINISTRATOR_PWD);
      if (admin.existsDatabase()) admin.dropDatabase();
      admin.createDatabase("graph", "local");
      graphDB = new OrientGraph(URL_DB, USERNAME, PASSWORD);

      graphDB.startTransaction();

      Vertex root = graphDB.addVertex(null);
      root.setProperty(NAME, "Plant");
      root.setProperty(DESCRIPTION, "This is the Plant");

      Vertex cell = graphDB.addVertex(null);
      cell.setProperty(NAME, "Cell 1");
      cell.setProperty(DESCRIPTION, "This is the Production Cell 1");
      graphDB.addEdge(null, root, cell, CONTAINMENT_EDGE);

      Vertex cellComponent = graphDB.addVertex(null);
      cellComponent.setProperty(NAME, "Cell Element A1");
      cellComponent.setProperty(DESCRIPTION, "This is an element of the production cell 1");
      graphDB.addEdge(null, cell, cellComponent, COMPONENT_EDGE);

      cell = graphDB.addVertex(null);
      cell.setProperty(NAME, "Cell 2");
      cell.setProperty(DESCRIPTION, "This is the Production Cell 2");
      graphDB.addEdge(null, root, cell, CONTAINMENT_EDGE);

      cellComponent = graphDB.addVertex(null);
      cellComponent.setProperty(NAME, "Cell Element B1");
      cellComponent.setProperty(DESCRIPTION, "This is an element of the production cell 2");
      graphDB.addEdge(null, cell, cellComponent, COMPONENT_EDGE);

      String filePath = "./src/test/resources/file.pdf";

      Vertex binaryVertex = graphDB.addVertex(null);
      binaryVertex.setProperty(NAME, "NoSQL Definition (single binary record)");
      binaryVertex.setProperty(BINARY_DATA, loadFile(graphDB.getRawGraph(), filePath));

      binaryVertex = graphDB.addVertex(null);
      binaryVertex.setProperty(NAME, "NoSQL Definition (multiple binary record)");
      binaryVertex.setProperty(BINARY_DATA, loadFile(graphDB.getRawGraph(), filePath, 50000));

      root = graphDB.addVertex(null);
      root.setProperty(NAME, "SimpleVertex");

      Vertex prop = graphDB.addVertex(null);
      prop.setProperty(VALUE, new Date());
      graphDB.addEdge(null, root, prop, CREATION_DATE_EDGE);

      prop = graphDB.addVertex(null);
      prop.setProperty(VALUE, loadFile(graphDB.getRawGraph(), filePath, 50000));
      graphDB.addEdge(null, root, prop, FILE_EDGE);

      prop = graphDB.addVertex(null);
      prop.setProperty(
          VALUE,
          "This is a simple vertex with three properties: this description, a file and a date");
      graphDB.addEdge(null, root, prop, DESCRIPTION);

      graphDB.stopTransaction(Conclusion.SUCCESS);
      graphDB.shutdown();
    } catch (Exception e) {
      System.err.println(
          "An error occured during the creation of the database " + URL_DB + ": " + e.getMessage());
      e.printStackTrace();
      if (graphDB != null) graphDB.stopTransaction(Conclusion.FAILURE);
    } finally {
      if (graphDB != null) graphDB.shutdown();
    }
  }
    @Override
    public Void call() throws Exception {
      String name = Integer.toString(serverId);

      for (int i = 0; i < count; i += 2) {
        final OrientGraph graph = factory.getTx();

        final OrientVertex localVertex = graph.getVertex(v);

        try {
          if ((i + 1) % 100 == 0)
            System.out.println(
                "\nWriter "
                    + databaseUrl
                    + " id="
                    + Thread.currentThread().getId()
                    + " managed "
                    + (i + 1)
                    + "/"
                    + count
                    + " vertices so far");

          int retry = 0;
          boolean success = false;
          for (; retry < 200; ++retry) {
            try {
              updateVertex(localVertex);
              graph.commit();
              OLogManager.instance().debug(this, "Success count %d retry %d", i, retry);
              success = true;
              break;

            } catch (ODistributedRecordLockedException e) {
              lockExceptions.incrementAndGet();
              OLogManager.instance()
                  .info(this, "increment lockExceptions %d", lockExceptions.get());

            } catch (ONeedRetryException e) {
              OLogManager.instance().info(this, "Concurrent Exceptions " + e);

            } catch (Exception e) {
              graph.rollback();
              throw e;
            }

            Thread.sleep(10 + new Random().nextInt(500));

            localVertex.reload();

            OLogManager.instance()
                .info(
                    this,
                    "Retry %d with reloaded vertex v=%d",
                    retry,
                    localVertex.getRecord().getVersion());
          }

          Assert.assertTrue(
              "Unable to complete the transaction (last="
                  + i
                  + "/"
                  + count
                  + "), even after "
                  + retry
                  + " retries",
              success);

        } catch (InterruptedException e) {
          System.out.println("Writer received interrupt (db=" + databaseUrl);
          Thread.currentThread().interrupt();
          break;
        } catch (Exception e) {
          System.out.println("Writer received exception (db=" + databaseUrl);
          e.printStackTrace();
          break;
        } finally {
          graph.shutdown();
        }
      }

      System.out.println(
          "\nWriter " + name + " END. count = " + count + " lockExceptions: " + lockExceptions);
      return null;
    }