From c6b9f38c47eb963f49b3ac28f142be079a774b19 Mon Sep 17 00:00:00 2001 From: Dave Borowitz Date: Mon, 19 Sep 2016 21:47:26 +0200 Subject: [PATCH] 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 --- .../gerrit/extensions/client/UiType.java | 32 +++++++++++++++++++ .../google/gerrit/httpd/GerritOptions.java | 28 ++++++---------- .../google/gerrit/httpd/raw/StaticModule.java | 25 ++++++++------- 3 files changed, 55 insertions(+), 30 deletions(-) create mode 100644 gerrit-extension-api/src/main/java/com/google/gerrit/extensions/client/UiType.java diff --git a/gerrit-extension-api/src/main/java/com/google/gerrit/extensions/client/UiType.java b/gerrit-extension-api/src/main/java/com/google/gerrit/extensions/client/UiType.java new file mode 100644 index 0000000000..0d9df39431 --- /dev/null +++ b/gerrit-extension-api/src/main/java/com/google/gerrit/extensions/client/UiType.java @@ -0,0 +1,32 @@ +// Copyright (C) 2016 The Android Open Source Project +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package com.google.gerrit.extensions.client; + +public enum UiType { + NONE, + GWT, + POLYGERRIT; + + public static UiType parse(String str) { + if (str != null) { + for (UiType type : UiType.values()) { + if (type.name().equalsIgnoreCase(str)) { + return type; + } + } + } + return null; + } +} diff --git a/gerrit-httpd/src/main/java/com/google/gerrit/httpd/GerritOptions.java b/gerrit-httpd/src/main/java/com/google/gerrit/httpd/GerritOptions.java index f6e375fd8e..b821779b84 100644 --- a/gerrit-httpd/src/main/java/com/google/gerrit/httpd/GerritOptions.java +++ b/gerrit-httpd/src/main/java/com/google/gerrit/httpd/GerritOptions.java @@ -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; } } diff --git a/gerrit-httpd/src/main/java/com/google/gerrit/httpd/raw/StaticModule.java b/gerrit-httpd/src/main/java/com/google/gerrit/httpd/raw/StaticModule.java index d3d87736ad..fb047e1628 100644 --- a/gerrit-httpd/src/main/java/com/google/gerrit/httpd/raw/StaticModule.java +++ b/gerrit-httpd/src/main/java/com/google/gerrit/httpd/raw/StaticModule.java @@ -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) {