Example #1
0
 /**
  * @param request HTTP request
  * @param response HTTP response
  * @return rendered HTML output
  */
 public String renderPage(HttpRequest request, HttpResponse response) {
   RequestLookup requestLookup = createRequestLookup(request, response);
   API api = new API(sessionRegistry, requestLookup);
   // If exists, render Theme.
   Theme renderingTheme = getRenderingTheme(api);
   if (renderingTheme != null) {
     renderingTheme.render(requestLookup);
   }
   try {
     return renderPage(request.getUriWithoutContextPath(), null, requestLookup, api);
   } catch (SessionNotFoundException e) {
     String loginPageUri = configuration.getLoginPageUri();
     if (loginPageUri == null) {
       throw (HttpErrorException) e;
     } else {
       throw new PageRedirectException(loginPageUri);
     }
   }
 }
Example #2
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 #3
0
  public Optional<String> renderErrorPage(
      HttpErrorException ex, HttpRequest request, HttpResponse response) {
    Map<String, String> errorPages = configuration.getErrorPages();
    String errorPageUri =
        errorPages.getOrDefault(String.valueOf(ex.getHttpStatusCode()), errorPages.get("default"));
    if (errorPageUri == null) {
      return Optional.<String>empty();
    }

    RequestLookup requestLookup = createRequestLookup(request, response);
    API api = new API(sessionRegistry, requestLookup);
    // If exists, render Theme.
    Theme renderingTheme = getRenderingTheme(api);
    if (renderingTheme != null) {
      renderingTheme.render(requestLookup);
    }
    // Create Model with HTTP status code and error message.
    Map<String, Object> modelMap = new HashMap<>(2);
    modelMap.put("status", ex.getHttpStatusCode());
    modelMap.put("message", ex.getMessage());
    return Optional.of(renderPage(errorPageUri, new MapModel(modelMap), requestLookup, api));
  }