private void renderPage(
      final HttpServletRequest req,
      String reqPagePath,
      HttpServletResponse res,
      Site site,
      Page page,
      String[] requestContext)
      throws PebbleException, IOException {

    TemplateContext global = new TemplateContext();
    global.setRequestContext(requestContext);
    for (String key : req.getParameterMap().keySet()) {
      global.put(key, req.getParameter(key));
    }

    global.put("page", makePageWrapper(page));
    populateSiteInfo(req, page, site, global);

    List<TemplateContext> components = new ArrayList<TemplateContext>();

    for (Component component : page.getComponentsSet()) {
      TemplateContext local = new TemplateContext();
      component.handle(page, local, global);
      components.add(local);
    }

    global.put("components", components);

    PebbleTemplate compiledTemplate =
        engine.getTemplate(site.getTheme().getType() + "/" + page.getTemplate().getFilePath());

    res.setStatus(200);
    res.setContentType("text/html");
    compiledTemplate.evaluate(res.getWriter(), global);
  }
  private void renderCMSPage(
      final HttpServletRequest req, HttpServletResponse res, Site sites, String pageSlug)
      throws ServletException, IOException, PebbleException {
    if (pageSlug.startsWith("/")) {
      pageSlug = pageSlug.substring(1);
    }
    String[] parts = pageSlug.split("/");

    String pageName = parts[0];

    Page page;
    if (Strings.isNullOrEmpty(pageName) && sites.getInitialPage() != null) {
      page = sites.getInitialPage();
    } else {
      page =
          sites
              .getPagesSet()
              .stream()
              .filter(p -> pageName.equals(p.getSlug()))
              .findAny()
              .orElse(null);
    }

    if (page == null || page.getTemplate() == null) {
      errorPage(req, res, sites, 404);
    } else if (!page.isPublished() || !page.getCanViewGroup().isMember(Authenticate.getUser())) {
      errorPage(req, res, sites, 404);
    } else {
      try {
        renderPage(req, pageSlug, res, sites, page, parts);
      } catch (ResourceNotFoundException e) {
        errorPage(req, res, sites, 404);
      }
    }
  }
  private Map<String, Object> makePageWrapper(Page page) {
    HashMap<String, Object> result = new HashMap<String, Object>();

    result.put("name", page.getName());
    result.put("user", new UserWrap(page.getCreatedBy()));
    result.put("createdBy", new UserWrap(page.getCreatedBy()));
    result.put("creationDate", page.getCreationDate());
    return result;
  }
 public static Optional<Page> pageForCategory(Site site, String categorySlug) {
   for (Page page : site.getPagesSet()) {
     for (Component component : page.getComponentsSet()) {
       if (isCategoryComponentForSlug(component, categorySlug)) {
         return Optional.of(page);
       }
     }
   }
   return Optional.empty();
 }
  @Override
  public void handle(Page page, TemplateContext local, TemplateContext global) {
    Homepage homepage = homepage(page.getSite());
    if (homepage == null || !homepage.isHomepageActivated()) {
      return; // TODO we might want 404 here
    }

    global.put("homepage", homepage);
    Person owner = homepage.getPerson();
    global.put("owner", owner);

    if (homepage.getShowCurrentAttendingExecutionCourses()) {
      SortedSet<Attends> attendedCoursesByName =
          new TreeSet<Attends>(Attends.ATTENDS_COMPARATOR_BY_EXECUTION_COURSE_NAME);
      attendedCoursesByName.addAll(homepage.getPerson().getCurrentAttends());
      global.put("attendingCourses", attendedCoursesByName);
    }

    List<? extends PartyContact> emails = owner.getEmailAddresses();
    global.put("visibleEmails", getSortedFilteredContacts(emails));

    List<? extends PartyContact> phones = owner.getPhones();
    global.put(
        "visiblePersonalPhones", getSortedFilteredContacts(phones, PartyContactType.PERSONAL));
    global.put("visibleWorkPhones", getSortedFilteredContacts(phones, PartyContactType.WORK));

    List<? extends PartyContact> mobilePhones = owner.getMobilePhones();
    global.put("visibleMobilePhones", getSortedFilteredContacts(mobilePhones));

    List<? extends PartyContact> websites = owner.getWebAddresses();
    global.put("visibleWebsites", getSortedFilteredContacts(websites));
  }
 @Override
 public void handle(Page page, TemplateContext componentContext, TemplateContext global) {
   PostMetadata metadata = metadata(page.getSite());
   String staticDir = page.getSite().getStaticDirectory();
   global.put("announcements", postsForCategory(page.getSite().categoryForSlug("announcement")));
   global.put("events", postsForCategory(page.getSite().categoryForSlug("event")));
   global.put(
       "backgroundImage",
       metadata.getAsString("backgroundImage").orElse(staticDir + "/images/bolonha_bck.gif"));
   global.put(
       "mainImage", metadata.getAsString("mainImage").orElse(staticDir + "/images/bolonha.gif"));
   global.put("link", metadata.getAsString("link").orElse("http://www.bolonha.ist.eu"));
   global.put("showIntroduction", metadata.getAsBoolean("showIntroduction").orElse(true));
   global.put("repeatType", metadata.getAsString("repeatType").orElse("repeat-x"));
   global.put("color", metadata.getAsString("color").orElse("#019AD7"));
   global.put("showBanner", metadata.getAsBoolean("showBanner").orElse(true));
   global.put("weight", metadata.getAsNumber("weight").orElse(100));
   global.put("weightPercentage", metadata.getAsNumber("weightPercentage").orElse(100));
   global.put("showPersonalizedLogo", metadata.getAsBoolean("showPersonalizedLogo").orElse(false));
   global.put("showAnnouncements", metadata.getAsBoolean("showAnnouncements").orElse(true));
   global.put("showEvents", metadata.getAsBoolean("showEvents").orElse(true));
   pageForCategory(page.getSite(), "announcement")
       .ifPresent(
           announcementsPage -> {
             global.put("announcementsUrl", announcementsPage.getAddress());
           });
   pageForCategory(page.getSite(), "event")
       .ifPresent(
           eventsPage -> {
             global.put("eventsUrl", eventsPage.getAddress());
           });
 }
 protected Site site(Page page) {
   if (page.getSite().getOwner() != null) {
     return page.getSite();
   }
   throw new ResourceNotFoundException();
 }