Support setting default UI in gerrit.config

The value of gerrit.ui chooses the default UI when the user doesn't
request a different preference via argument or cookie. Sanity check
during startup that the value of gerrit.ui is an enabled UI.

Change-Id: I5f18d3d0a252a886de4ae166a158e19c40eed82f
This commit is contained in:
Dave Borowitz
2016-09-19 15:42:20 +02:00
parent dac0dcb853
commit e8b7303fb9
2 changed files with 47 additions and 11 deletions

View File

@@ -14,23 +14,63 @@
package com.google.gerrit.httpd;
import static com.google.common.base.MoreObjects.firstNonNull;
import static com.google.common.base.Preconditions.checkArgument;
import com.google.common.base.Enums;
import org.eclipse.jgit.lib.Config;
public class GerritOptions {
public enum UiPreference {
NONE,
GWT,
POLYGERRIT;
}
private final boolean headless;
private final boolean slave;
private final boolean enablePolyGerrit;
private final boolean enableGwtUi;
private final boolean forcePolyGerritDev;
private final UiPreference defaultUi;
public GerritOptions(Config cfg, boolean headless, boolean slave,
boolean forcePolyGerritDev) {
this.headless = headless;
this.slave = slave;
this.enablePolyGerrit = forcePolyGerritDev
|| cfg.getBoolean("gerrit", null, "enablePolyGerrit", false);
this.enableGwtUi = cfg.getBoolean("gerrit", null, "enableGwtUi", true);
this.forcePolyGerritDev = forcePolyGerritDev;
this.headless = headless || (!enableGwtUi && !enablePolyGerrit);
UiPreference defaultUi = enablePolyGerrit && !enableGwtUi
? UiPreference.POLYGERRIT
: UiPreference.GWT;
String uiStr = firstNonNull(
cfg.getString("gerrit", null, "ui"),
defaultUi.name().toUpperCase());
this.defaultUi =
Enums.getIfPresent(UiPreference.class, uiStr).or(UiPreference.NONE);
uiStr = defaultUi.name().toLowerCase();
switch (defaultUi) {
case GWT:
checkArgument(enableGwtUi,
"gerrit.ui = %s but GWT UI is disabled", uiStr);
break;
case POLYGERRIT:
checkArgument(enablePolyGerrit,
"gerrit.ui = %s but PolyGerrit is disabled", uiStr);
break;
case NONE:
default:
throw new IllegalArgumentException("invalid gerrit.ui: " + uiStr);
}
}
public boolean headless() {
return headless;
}
public boolean headless() {
@@ -52,4 +92,8 @@ public class GerritOptions {
public boolean forcePolyGerritDev() {
return !headless && forcePolyGerritDev;
}
public UiPreference defaultUi() {
return defaultUi;
}
}

View File

@@ -23,6 +23,7 @@ import com.google.common.base.Enums;
import com.google.common.cache.Cache;
import com.google.common.collect.ImmutableList;
import com.google.gerrit.httpd.GerritOptions;
import com.google.gerrit.httpd.GerritOptions.UiPreference;
import com.google.gerrit.httpd.XsrfCookieFilter;
import com.google.gerrit.httpd.raw.ResourceServlet.Resource;
import com.google.gerrit.launcher.GerritLauncher;
@@ -111,12 +112,6 @@ public class StaticModule extends ServletModule {
private static final int GERRIT_UI_COOKIE_MAX_AGE = 60 * 60 * 24 * 365;
enum UiPreference {
NONE,
GWT,
POLYGERRIT;
}
private final GerritOptions options;
private Paths paths;
@@ -528,10 +523,7 @@ public class StaticModule extends ServletModule {
}
private boolean isPolyGerritCookie(HttpServletRequest req) {
UiPreference pref = UiPreference.GWT;
if (options.enablePolyGerrit() && !options.enableGwtUi()) {
pref = UiPreference.POLYGERRIT;
}
UiPreference pref = options.defaultUi();
Cookie[] all = req.getCookies();
if (all != null) {
for (Cookie c : all) {