Example #1
0
  private Path resolveResourceInTheme(App app, String uriWithoutContextPath) {
    /* Correct 'uriWithoutContextPath' value must be in
     * "/public/themes/{theme-name}/{sub-directory}/{rest-of-the-path}" format.
     * So there should be at least 5 slashes. Don't worry about multiple consecutive slashes. They  are covered
     * in HttpRequest.isValid() method which is called before this method.
     */

    int slashesCount = 0, thirdSlashIndex = -1, fourthSlashIndex = -1;
    for (int i = 0; i < uriWithoutContextPath.length(); i++) {
      if (uriWithoutContextPath.charAt(i) == '/') {
        slashesCount++;
        if (slashesCount == 3) {
          thirdSlashIndex = i;
        } else if (slashesCount == 4) {
          fourthSlashIndex = i;
        } else if (slashesCount == 5) {
          break;
        }
      }
    }
    if (slashesCount != 5) {
      throw new IllegalArgumentException(
          "Invalid static resource URI '" + uriWithoutContextPath + "'.");
    }
    String themeName = uriWithoutContextPath.substring(thirdSlashIndex + 1, fourthSlashIndex);
    Theme theme = app.getThemes().get(themeName);
    if (theme == null) {
      throw new ResourceNotFoundException(
          "Theme '"
              + themeName
              + "' found in URI '"
              + uriWithoutContextPath
              + "' does not exists in app '"
              + app.getName()
              + "'.");
    }

    // {sub-directory}/{rest-of-the-path}
    String relativePathString =
        uriWithoutContextPath.substring(fourthSlashIndex + 1, uriWithoutContextPath.length());
    return Paths.get(theme.getPath(), DIR_NAME_PUBLIC_RESOURCES, relativePathString);
  }
Example #2
0
  private Path resolveResourceInComponent(App app, String uriWithoutContextPath) {
    /* Correct 'uriWithoutContextPath' value must be in
     * "/public/components/{component-context-path}/{fragment-simple-name}/{sub-directory}/{rest-of-the-path}"
     * format or in
     * "/public/components/{component-context-path}/base/{sub-directory}/{rest-of-the-path}" format.
     * So there should be at least 6 slashes. Don't worry about multiple consecutive slashes. They  are covered in
     * HttpRequest.isValid() method which is called before this method.
     */

    int slashesCount = 0, thirdSlashIndex = -1, fourthSlashIndex = -1, fifthSlashIndex = -1;
    for (int i = 0; i < uriWithoutContextPath.length(); i++) {
      if (uriWithoutContextPath.charAt(i) == '/') {
        slashesCount++;
        if (slashesCount == 3) {
          thirdSlashIndex = i;
        } else if (slashesCount == 4) {
          fourthSlashIndex = i;
        } else if (slashesCount == 5) {
          fifthSlashIndex = i;
        } else if (slashesCount == 6) {
          break;
        }
      }
    }
    if (slashesCount != 6) {
      throw new IllegalArgumentException(
          "Invalid static resource URI '" + uriWithoutContextPath + "'.");
    }
    String componentContextPath =
        uriWithoutContextPath.substring(thirdSlashIndex, fourthSlashIndex);
    Component component = app.getComponents().get(componentContextPath);
    if (component == null) {
      throw new ResourceNotFoundException(
          "Component for context path '"
              + componentContextPath
              + "' found in URI '"
              + uriWithoutContextPath
              + "' does not exists in app '"
              + app.getName()
              + "'.");
    }

    // {fragment-simple-name} OR 'base'
    String fragmentSimpleName =
        uriWithoutContextPath.substring(fourthSlashIndex + 1, fifthSlashIndex);
    // {sub-directory}/{rest-of-the-path}
    String relativePathString =
        uriWithoutContextPath.substring(fifthSlashIndex + 1, uriWithoutContextPath.length());
    if (DIR_NAME_COMPONENT_RESOURCES.equals(fragmentSimpleName)) {
      // Resource is inside the 'public' directory of the 'component'
      return Paths.get(component.getPath(), DIR_NAME_PUBLIC_RESOURCES, relativePathString);
    } else {
      // Resource is inside the 'public' directory of fragment 'fragmentSimpleName' of the
      // 'component'
      return Paths.get(
          component.getPath(),
          DIR_NAME_FRAGMENTS,
          fragmentSimpleName,
          DIR_NAME_PUBLIC_RESOURCES,
          relativePathString);
    }
  }