Paul Martin
2016-04-27 c2188a840bc4153ae92112b04b2e06a90d3944aa
src/main/java/com/gitblit/wicket/pages/BasePage.java
@@ -15,6 +15,8 @@
 */
package com.gitblit.wicket.pages;
import java.io.IOException;
import java.io.InputStream;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Calendar;
@@ -31,6 +33,7 @@
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.io.IOUtils;
import org.apache.wicket.Application;
import org.apache.wicket.Page;
import org.apache.wicket.PageParameters;
@@ -39,9 +42,12 @@
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.ExternalLink;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.markup.html.resources.JavascriptResourceReference;
import org.apache.wicket.markup.repeater.RepeatingView;
import org.apache.wicket.protocol.http.RequestUtils;
import org.apache.wicket.protocol.http.WebResponse;
import org.apache.wicket.protocol.http.servlet.ServletWebRequest;
import org.apache.wicket.request.target.basic.RedirectRequestTarget;
import org.apache.wicket.util.time.Duration;
import org.apache.wicket.util.time.Time;
import org.slf4j.Logger;
@@ -90,6 +96,13 @@
      if (app().settings().getBoolean(Keys.web.useResponsiveLayout, true)) {
         add(CSSPackageResource.getHeaderContribution("bootstrap/css/bootstrap-responsive.css"));
      }
      if (app().settings().getBoolean(Keys.web.hideHeader, false)) {
         add(CSSPackageResource.getHeaderContribution("hideheader.css"));
      }
   }
   protected String getContextUrl() {
      return getRequest().getRelativePathPrefixToContextRoot();
   }
   protected String getCanonicalUrl() {
@@ -100,6 +113,15 @@
      String relativeUrl = urlFor(clazz, params).toString();
      String canonicalUrl = RequestUtils.toAbsolutePath(relativeUrl);
      return canonicalUrl;
   }
   protected void redirectTo(Class<? extends BasePage> pageClass) {
      redirectTo(pageClass, null);
   }
   protected void redirectTo(Class<? extends BasePage> pageClass, PageParameters parameters) {
      String absoluteUrl = getCanonicalUrl(pageClass, parameters);
      getRequestCycle().setRequestTarget(new RedirectRequestTarget(absoluteUrl));
   }
   protected String getLanguageCode() {
@@ -156,6 +178,9 @@
         // use default Wicket caching behavior
         super.setHeaders(response);
      }
      // XRF vulnerability. issue-500 / ticket-166
      response.setHeader("X-Frame-Options", "SAMEORIGIN");
   }
   /**
@@ -205,17 +230,21 @@
      response.setDateHeader("Expires", System.currentTimeMillis() + Duration.minutes(expires).getMilliseconds());
   }
   protected void setupPage(String repositoryName, String pageName) {
   protected String getPageTitle(String repositoryName) {
      String siteName = app().settings().getString(Keys.web.siteName, Constants.NAME);
      if (StringUtils.isEmpty(siteName)) {
         siteName = Constants.NAME;
      }
      if (repositoryName != null && repositoryName.trim().length() > 0) {
         add(new Label("title", repositoryName + " - " + siteName));
         return repositoryName + " - " + siteName;
      } else {
         add(new Label("title", siteName));
         return siteName;
      }
   }
   protected void setupPage(String repositoryName, String pageName) {
      add(new Label("title", getPageTitle(repositoryName)));
      getBottomScriptContainer();
      String rootLinkUrl = app().settings().getString(Keys.web.rootLink, urlFor(GitBlitWebApp.get().getHomePage(), null).toString());
      ExternalLink rootLink = new ExternalLink("rootLink", rootLinkUrl);
      WicketUtils.setHtmlTooltip(rootLink, app().settings().getString(Keys.web.siteName, Constants.NAME));
@@ -456,4 +485,63 @@
      }
      error(message, true);
   }
   protected String readResource(String resource) {
      StringBuilder sb = new StringBuilder();
      InputStream is = null;
      try {
         is = getClass().getResourceAsStream(resource);
         List<String> lines = IOUtils.readLines(is);
         for (String line : lines) {
            sb.append(line).append('\n');
         }
      } catch (IOException e) {
      } finally {
         if (is != null) {
            try {
               is.close();
            } catch (IOException e) {
            }
         }
      }
      return sb.toString();
   }
   private RepeatingView getBottomScriptContainer() {
      RepeatingView bottomScriptContainer = (RepeatingView) get("bottomScripts");
      if (bottomScriptContainer == null) {
         bottomScriptContainer = new RepeatingView("bottomScripts");
         bottomScriptContainer.setRenderBodyOnly(true);
         add(bottomScriptContainer);
      }
      return bottomScriptContainer;
   }
   /**
    * Adds a HTML script element loading the javascript designated by the given path.
    *
    * @param scriptPath
    *            page-relative path to the Javascript resource; normally starts with "scripts/"
    */
   protected void addBottomScript(String scriptPath) {
      RepeatingView bottomScripts = getBottomScriptContainer();
      Label script = new Label(bottomScripts.newChildId(), "<script type='text/javascript' src='"
            + urlFor(new JavascriptResourceReference(this.getClass(), scriptPath)) + "'></script>\n");
      bottomScripts.add(script.setEscapeModelStrings(false).setRenderBodyOnly(true));
   }
   /**
    * Adds a HTML script element containing the given code.
    *
    * @param code
    *            inline script code
    */
   protected void addBottomScriptInline(String code) {
      RepeatingView bottomScripts = getBottomScriptContainer();
      Label script = new Label(bottomScripts.newChildId(),
            "<script type='text/javascript'>/*<![CDATA[*/\n" + code + "\n//]]>\n</script>\n");
      bottomScripts.add(script.setEscapeModelStrings(false).setRenderBodyOnly(true));
   }
}