Clean up UiPreference
Rename UiType as it is a neutral type, not necessarily tied to a user preference. Move to client extension API package in preparation for exposing via REST API to the GWT UI. Add a convenience method for parsing, albeit without Optional as we don't have that in GWT. Change-Id: Ib2a7061ce2c34bf6d452d060cc045cf65481b208
This commit is contained in:
@@ -17,23 +17,17 @@ 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 com.google.gerrit.extensions.client.UiType;
|
||||
|
||||
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;
|
||||
private final UiType defaultUi;
|
||||
|
||||
public GerritOptions(Config cfg, boolean headless, boolean slave,
|
||||
boolean forcePolyGerritDev) {
|
||||
@@ -44,24 +38,22 @@ public class GerritOptions {
|
||||
this.forcePolyGerritDev = forcePolyGerritDev;
|
||||
this.headless = headless || (!enableGwtUi && !enablePolyGerrit);
|
||||
|
||||
UiPreference defaultUi = enablePolyGerrit && !enableGwtUi
|
||||
? UiPreference.POLYGERRIT
|
||||
: UiPreference.GWT;
|
||||
UiType defaultUi = enablePolyGerrit && !enableGwtUi
|
||||
? UiType.POLYGERRIT
|
||||
: UiType.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();
|
||||
defaultUi.name());
|
||||
this.defaultUi = firstNonNull(UiType.parse(uiStr), UiType.NONE);
|
||||
|
||||
switch (defaultUi) {
|
||||
case GWT:
|
||||
checkArgument(enableGwtUi,
|
||||
"gerrit.ui = %s but GWT UI is disabled", uiStr);
|
||||
"gerrit.ui = %s but GWT UI is disabled", defaultUi);
|
||||
break;
|
||||
case POLYGERRIT:
|
||||
checkArgument(enablePolyGerrit,
|
||||
"gerrit.ui = %s but PolyGerrit is disabled", uiStr);
|
||||
"gerrit.ui = %s but PolyGerrit is disabled", defaultUi);
|
||||
break;
|
||||
case NONE:
|
||||
default:
|
||||
@@ -89,7 +81,7 @@ public class GerritOptions {
|
||||
return !headless && forcePolyGerritDev;
|
||||
}
|
||||
|
||||
public UiPreference defaultUi() {
|
||||
public UiType defaultUi() {
|
||||
return defaultUi;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,11 +19,10 @@ import static com.google.common.base.Preconditions.checkState;
|
||||
import static java.nio.file.Files.exists;
|
||||
import static java.nio.file.Files.isReadable;
|
||||
|
||||
import com.google.common.base.Enums;
|
||||
import com.google.common.cache.Cache;
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import com.google.gerrit.extensions.client.UiType;
|
||||
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;
|
||||
@@ -514,34 +513,36 @@ public class StaticModule extends ServletModule {
|
||||
}
|
||||
String param = req.getParameter("polygerrit");
|
||||
if ("1".equals(param)) {
|
||||
return setPolyGerritCookie(req, res, UiPreference.POLYGERRIT);
|
||||
return setPolyGerritCookie(req, res, UiType.POLYGERRIT);
|
||||
} else if ("0".equals(param)) {
|
||||
return setPolyGerritCookie(req, res, UiPreference.GWT);
|
||||
return setPolyGerritCookie(req, res, UiType.GWT);
|
||||
} else {
|
||||
return isPolyGerritCookie(req);
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isPolyGerritCookie(HttpServletRequest req) {
|
||||
UiPreference pref = options.defaultUi();
|
||||
UiType type = options.defaultUi();
|
||||
Cookie[] all = req.getCookies();
|
||||
if (all != null) {
|
||||
for (Cookie c : all) {
|
||||
if (GERRIT_UI_COOKIE.equals(c.getName())) {
|
||||
String v = c.getValue().toUpperCase();
|
||||
pref = Enums.getIfPresent(UiPreference.class, v).or(pref);
|
||||
break;
|
||||
UiType t = UiType.parse(c.getValue());
|
||||
if (t != null) {
|
||||
type = t;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return pref == UiPreference.POLYGERRIT;
|
||||
return type == UiType.POLYGERRIT;
|
||||
}
|
||||
|
||||
private boolean setPolyGerritCookie(HttpServletRequest req,
|
||||
HttpServletResponse res, UiPreference pref) {
|
||||
HttpServletResponse res, UiType pref) {
|
||||
// Only actually set a cookie if both UIs are enabled in the server;
|
||||
// otherwise clear it.
|
||||
Cookie cookie = new Cookie(GERRIT_UI_COOKIE, pref.name().toLowerCase());
|
||||
Cookie cookie = new Cookie(GERRIT_UI_COOKIE, pref.name());
|
||||
if (options.enablePolyGerrit() && options.enableGwtUi()) {
|
||||
cookie.setPath("/");
|
||||
cookie.setSecure(isSecure(req));
|
||||
@@ -551,7 +552,7 @@ public class StaticModule extends ServletModule {
|
||||
cookie.setMaxAge(0);
|
||||
}
|
||||
res.addCookie(cookie);
|
||||
return pref == UiPreference.POLYGERRIT;
|
||||
return pref == UiType.POLYGERRIT;
|
||||
}
|
||||
|
||||
private static boolean isSecure(HttpServletRequest req) {
|
||||
|
||||
Reference in New Issue
Block a user