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); }