| | |
| | | */
|
| | | 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;
|
| | |
| | |
|
| | | 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;
|
| | |
| | | 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;
|
| | |
| | |
|
| | | public abstract class BasePage extends SessionPage {
|
| | |
|
| | | private final Logger logger;
|
| | | private transient Logger logger;
|
| | |
|
| | | private transient TimeUtils timeUtils;
|
| | |
|
| | | public BasePage() {
|
| | | super();
|
| | | logger = LoggerFactory.getLogger(getClass());
|
| | | customizeHeader();
|
| | | }
|
| | |
|
| | | public BasePage(PageParameters params) {
|
| | | super(params);
|
| | | logger = LoggerFactory.getLogger(getClass());
|
| | | customizeHeader();
|
| | | }
|
| | |
|
| | | protected Logger logger() {
|
| | | if (logger == null) {
|
| | | logger = LoggerFactory.getLogger(getClass());
|
| | | }
|
| | | return logger;
|
| | | }
|
| | |
|
| | | private void customizeHeader() {
|
| | | 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() {
|
| | |
| | | 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() {
|
| | |
| | | // use default Wicket caching behavior
|
| | | super.setHeaders(response);
|
| | | }
|
| | |
|
| | | // XRF vulnerability. issue-500 / ticket-166
|
| | | response.setHeader("X-Frame-Options", "SAMEORIGIN");
|
| | | }
|
| | |
|
| | | /**
|
| | |
| | | case NONE:
|
| | | break;
|
| | | default:
|
| | | logger.warn(getClass().getSimpleName() + ": unhandled LastModified type " + cacheControl.value());
|
| | | logger().warn(getClass().getSimpleName() + ": unhandled LastModified type " + cacheControl.value());
|
| | | break;
|
| | | }
|
| | | }
|
| | |
| | | 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));
|
| | |
| | | }
|
| | |
|
| | | public void warn(String message, Throwable t) {
|
| | | logger.warn(message, t);
|
| | | logger().warn(message, t);
|
| | | }
|
| | |
|
| | | public void error(String message, boolean redirect) {
|
| | |
| | |
|
| | | public void error(String message, Throwable t, Class<? extends Page> toPage, PageParameters params) {
|
| | | if (t == null) {
|
| | | logger.error(message + " for " + GitBlitWebSession.get().getUsername());
|
| | | logger().error(message + " for " + GitBlitWebSession.get().getUsername());
|
| | | } else {
|
| | | logger.error(message + " for " + GitBlitWebSession.get().getUsername(), t);
|
| | | logger().error(message + " for " + GitBlitWebSession.get().getUsername(), t);
|
| | | }
|
| | | if (toPage != null) {
|
| | | GitBlitWebSession.get().cacheErrorMessage(message);
|
| | |
| | | }
|
| | |
|
| | | public void authenticationError(String message) {
|
| | | logger.error(getRequest().getURL() + " for " + GitBlitWebSession.get().getUsername());
|
| | | logger().error(getRequest().getURL() + " for " + GitBlitWebSession.get().getUsername());
|
| | | if (!GitBlitWebSession.get().isLoggedIn()) {
|
| | | // cache the request if we have not authenticated.
|
| | | // the request will continue after authentication.
|
| | |
| | | }
|
| | | 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));
|
| | | }
|
| | |
|
| | | }
|