Package org.structr.core.app

Examples of org.structr.core.app.App


      ArchiveStreamFactory.TAR,
      ArchiveStreamFactory.ZIP
    }));

    final SecurityContext securityContext = getWebSocket().getSecurityContext();
    final App app                         = StructrApp.getInstance(securityContext);

    try {

      final String id = (String) webSocketData.getId();
      final File file;

      try (final Tx tx = app.tx()) {

        file = app.get(File.class, id);

        if (file == null) {
          getWebSocket().send(MessageBuilder.status().code(400).message("File not found: ".concat(id)).build(), true);
          return;
        }

        final String fileExtension = StringUtils.substringAfterLast(file.getName(), ".");
        if (!supportedByArchiveStreamFactory.contains(fileExtension)) {

          getWebSocket().send(MessageBuilder.status().code(400).message("Unsupported archive format: ".concat(fileExtension)).build(), true);
          return;
        }

        tx.success();
      }

      // no transaction here since this is a bulk command
      unarchive(securityContext, file);


    } catch (Throwable t) {

      t.printStackTrace();

      String msg = t.toString();

      try (final Tx tx = app.tx()) {

        // return error message
        getWebSocket().send(MessageBuilder.status().code(400).message("Could not unarchive file: ".concat((msg != null) ? msg : "")).build(), true);

        tx.success();
View Full Code Here


    if (parts == null || parts.length == 1) {
      return null;
    }

    // Find root folder
    final App app = StructrApp.getInstance(securityContext);
    Folder folder = null;
    for (final Folder possibleRootFolder : app.nodeQuery(Folder.class).andName(parts[0])) {

      if (possibleRootFolder.getProperty(Folder.parent) == null) {
        folder = possibleRootFolder;
        break;
      }

    }

    if (folder == null) {

      // Root folder doesn't exist, so create it and all child folders
      folder = app.create(Folder.class, parts[0]);
      logger.log(Level.INFO, "Created root folder {0}", new Object[]{parts[0]});

      for (int i = 1; i < parts.length - 1; i++) {
        Folder childFolder = app.create(Folder.class, parts[i]);
        childFolder.setProperty(Folder.parent, folder);
        logger.log(Level.INFO, "Created {0} {1} with path {2}", new Object[]{childFolder.getType(), childFolder, FileHelper.getFolderPath(childFolder)});
        folder = childFolder;
      }

      return folder;

    }

    // Root folder exists, so walk over children and search for next path part
    for (int i = 1; i < parts.length - 1; i++) {

      Folder subFolder = null;

      for (AbstractFile child : folder.getProperty(Folder.children)) {

        if (child instanceof Folder && child.getName().equals(parts[i])) {
          subFolder = (Folder) child;
          break;
        }

      }

      if (subFolder == null) {

        // sub folder doesn't exist, so create it and all child folders
        subFolder = app.create(Folder.class, parts[i]);
        subFolder.setProperty(Folder.parent, folder);
        logger.log(Level.INFO, "Created {0} {1} with path {2}", new Object[]{subFolder.getType(), subFolder, FileHelper.getFolderPath(subFolder)});

      }
View Full Code Here

  }

  private void unarchive(final SecurityContext securityContext, final File file) throws ArchiveException, IOException, FrameworkException {

    final App app = StructrApp.getInstance(securityContext);
    final InputStream is;

    try (final Tx tx = app.tx()) {

      final String fileName = file.getName();

      logger.log(Level.INFO, "Unarchiving file {0}", fileName);

      is = file.getInputStream();
      tx.success();


      if (is == null) {

        getWebSocket().send(MessageBuilder.status().code(400).message("Could not get input stream from file ".concat(fileName)).build(), true);
        return;
      }
    }

    final ArchiveInputStream in = new ArchiveStreamFactory().createArchiveInputStream(new BufferedInputStream(is));
    ArchiveEntry entry          = in.getNextEntry();
    int overallCount            = 0;

    while (entry != null) {

      try (final Tx tx = app.tx()) {

        int count = 0;

        while (entry != null && count++ < 50) {

          final String entryPath = "/" + PathHelper.clean(entry.getName());
          logger.log(Level.INFO, "Entry path: {0}", entryPath);

          final AbstractFile f = FileHelper.getFileByAbsolutePath(securityContext, entryPath);
          if (f == null) {

            final Folder parentFolder = createOrGetParentFolder(securityContext, entryPath);
            final String name         = PathHelper.getName(entry.getName());

            if (StringUtils.isNotEmpty(name) && (parentFolder == null || !(FileHelper.getFolderPath(parentFolder).equals(entryPath)))) {

              AbstractFile fileOrFolder = null;

              if (entry.isDirectory()) {

                fileOrFolder = app.create(Folder.class, name);

              } else {

                fileOrFolder = ImageHelper.isImageType(name)
                  ? ImageHelper.createImage(securityContext, in, null, Image.class, name, false)
View Full Code Here

  @Override
  public void processMessage(WebSocketMessage webSocketData) {

    final SecurityContext securityContext = getWebSocket().getSecurityContext();
    final App app                         = StructrApp.getInstance(securityContext);

    // new node properties
    final Map<String, Object> properties = webSocketData.getNodeData();
    String parentId                      = (String) properties.get("id");
    final Map<String, Object> relData    = webSocketData.getRelData();

    if (parentId != null) {

      DOMNode parentNode        = (DOMNode) getNode(parentId);
      DOMNode nodeToInsert      = null;

      try {

        PropertyMap nodeProperties = PropertyMap.inputTypeToJavaType(securityContext, properties);

        nodeToInsert = app.create(DOMNode.class, nodeProperties);
       
      } catch (FrameworkException fex) {

        logger.log(Level.WARNING, "Could not create node.", fex);
        getWebSocket().send(MessageBuilder.status().code(fex.getStatus()).message(fex.getMessage()).build(), true);

      }

      if ((nodeToInsert != null) && (parentNode != null)) {

        try {

          PropertyMap relProperties = PropertyMap.inputTypeToJavaType(securityContext, relData);
          app.create(parentNode, nodeToInsert, DOMChildren.class, relProperties);
         
        } catch (FrameworkException t) {

          getWebSocket().send(MessageBuilder.status().code(400).message(t.getMessage()).build(), true);
View Full Code Here

      TestUser user1 = null;
      TestUser user2 = null;
      TestOne t1 = null;
      Class type = TestOne.class;

      final App superUserApp = StructrApp.getInstance();
      try (final Tx tx = app.tx()) {

        List<TestUser> users = createTestNodes(TestUser.class, 2);
        user1 = (TestUser) users.get(0);
        user1.setProperty(AbstractNode.name, "user1");
       
        user2 = (TestUser) users.get(1);
        user2.setProperty(AbstractNode.name, "user2");

        t1 = createTestNode(TestOne.class);

        t1.setProperty(AbstractNode.owner, user1);

        tx.success();

      } catch (FrameworkException ex) {
        logger.log(Level.SEVERE, ex.toString());
      }

      try (final Tx tx = app.tx()) {
       
        assertEquals(user1, t1.getProperty(AbstractNode.owner));

        // Switch user context to user1
        final App user1App = StructrApp.getInstance(SecurityContext.getInstance(user1, AccessMode.Backend));

        // Check if user1 can see t1
        assertEquals(t1, user1App.nodeQuery(type, false).getFirst());
      }
     
      try (final Tx tx = app.tx()) {

        // As superuser, make another user the owner
        t1.setProperty(AbstractNode.owner, user2);

        tx.success();
       
      } catch (FrameworkException ex) {
        logger.log(Level.SEVERE, ex.toString());
      }
     
      try (final Tx tx = app.tx()) {
       
        // Switch user context to user2
        final App user2App = StructrApp.getInstance(SecurityContext.getInstance(user2, AccessMode.Backend));

        // Check if user2 can see t1
        assertEquals(t1, user2App.nodeQuery(type, false).getFirst());

        // Check if user2 is owner of t1
        assertEquals(user2, t1.getProperty(AbstractNode.owner));
      }
     
View Full Code Here

   *
   * With Structr 1.0, this has to be resolved!!
   */
  public void test02SetDifferentPrincipalTypesAsOwner() {

    final App superUserApp = StructrApp.getInstance();
   
    try (final Tx tx = app.tx()) {

      List<TestUser> users = createTestNodes(TestUser.class, 2);
      TestUser user1 = (TestUser) users.get(0);
View Full Code Here

  //~--- methods --------------------------------------------------------
  @Override
  public void processMessage(WebSocketMessage webSocketData) throws FrameworkException {

    final App app          = StructrApp.getInstance(getWebSocket().getSecurityContext());
    final Boolean recValue = (Boolean) webSocketData.getNodeData().get("recursive");
    final boolean rec      = recValue != null ? recValue : false;
    GraphObject obj        = getNode(webSocketData.getId());

    webSocketData.getNodeData().remove("recursive");

    if (obj != null) {

      try (final Tx tx = app.tx()) {

        if (!getWebSocket().getSecurityContext().isAllowed(((AbstractNode) obj), Permission.write)) {

          getWebSocket().send(MessageBuilder.status().message("No write permission").code(400).build(), true);
          logger.log(Level.WARNING, "No write permission for {0} on {1}", new Object[]{getWebSocket().getCurrentUser().toString(), obj.toString()});

          tx.success();
          return;

        }

        tx.success();
      }
    }

    if (obj == null) {

      // No node? Try to find relationship
      obj = getRelationship(webSocketData.getId());
    }

    if (obj != null) {

      final Set<GraphObject> entities = new LinkedHashSet<>();
      try (final Tx tx = app.tx()) {

        collectEntities(entities, obj, null, rec);

        // commit and close transaction
        tx.success();
      }

      final PropertyMap properties         = PropertyMap.inputTypeToJavaType(this.getWebSocket().getSecurityContext(), obj.getClass(), webSocketData.getNodeData());
      final Iterator<GraphObject> iterator = entities.iterator();

      while (iterator.hasNext()) {

        count = 0;
        try (final Tx tx = app.tx()) {

          while (iterator.hasNext() && count++ < 100) {

            setProperties(app, iterator.next(), properties, true);
          }
View Full Code Here

    final Long port                      = (Long)properties.get("port");
    final String key                     = (String)properties.get("key");

    if (sourceId != null && host != null && port != null && username != null && password != null && key != null) {

      final App app    = StructrApp.getInstance();
      try (final Tx tx = app.tx()) {

        final GraphObject root = app.get(sourceId);
        if (root != null) {

          if (root instanceof Syncable) {

            boolean recursive = false;
View Full Code Here

    final String key                     = (String)properties.get("key");
    final Long port                      = (Long)properties.get("port");

    if (sourceId != null && host != null && port != null && username != null && password != null && key != null) {

      final App app    = StructrApp.getInstance();
      try (final Tx tx = app.tx()) {

        boolean recursive = false;
        if (recursiveSource != null) {

          recursive = "true".equals(recursiveSource.toString());
View Full Code Here

    try {

      if (searchValue != null && !StringUtils.isBlank(searchValue.toString())) {

        final App app                          = StructrApp.getInstance(securityContext);
        final PropertyKey key                  = notion.getPrimaryPropertyKey();
        final PropertyConverter inputConverter = key.inputConverter(securityContext);

        // transform search values using input convert of notion property
        final Object transformedValue          = inputConverter != null ? inputConverter.convert(searchValue) : searchValue;

        if (exactMatch) {

          Result<AbstractNode> result = app.nodeQuery(entityProperty.relatedType()).and(key, transformedValue).getResult();

          for (AbstractNode node : result.getResults()) {

            switch (occur) {

              case MUST:

                if (!alreadyAdded) {

                  // the first result is the basis of all subsequent intersections
                  intersectionResult.addAll(entityProperty.getRelatedNodesReverse(securityContext, node, declaringClass, predicate));

                  // the next additions are intersected with this one
                  alreadyAdded = true;

                } else {

                  intersectionResult.retainAll(entityProperty.getRelatedNodesReverse(securityContext, node, declaringClass, predicate));
                }

                break;

              case SHOULD:
                intersectionResult.addAll(entityProperty.getRelatedNodesReverse(securityContext, node, declaringClass, predicate));
                break;

              case MUST_NOT:
                break;
            }
          }

        } else {

          Result<AbstractNode> result = app.nodeQuery(entityProperty.relatedType(), false).and(key, transformedValue, false).getResult();

          // loose search behaves differently, all results must be combined
          for (AbstractNode node : result.getResults()) {

            intersectionResult.addAll(entityProperty.getRelatedNodesReverse(securityContext, node, declaringClass, predicate));
View Full Code Here

TOP

Related Classes of org.structr.core.app.App

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.