From e55a72f504ae2aa863af377a378a79ae3d865f4b Mon Sep 17 00:00:00 2001 From: Edwin Kempin Date: Fri, 8 May 2015 12:50:34 +0200 Subject: [PATCH] Gerrit Client: Retrieve archives formats via REST Use the /config/server/info REST endpoint to retrieve the supported archive formats and remove this information from the config that is embedded in the host page data. The way how archives formats are included into the ServerInfo JSON was adapted to the changes done with commit 1e933885. Change-Id: Iaf0a3817c5f239e87a0f9c2b29d198e410e4f49e Signed-off-by: Edwin Kempin --- .../gerrit/common/data/GerritConfig.java | 10 ------ .../gerrit/client/change/DownloadBox.java | 2 +- .../gerrit/client/config/DownloadInfo.java | 13 ++++++++ .../gerrit/httpd/GerritConfigProvider.java | 27 --------------- .../gerrit/server/change/GetArchive.java | 14 +++++++- .../gerrit/server/config/GetServerInfo.java | 33 ++++++++++--------- 6 files changed, 44 insertions(+), 55 deletions(-) diff --git a/gerrit-common/src/main/java/com/google/gerrit/common/data/GerritConfig.java b/gerrit-common/src/main/java/com/google/gerrit/common/data/GerritConfig.java index 88f9c2bbeb..dae18cfdef 100644 --- a/gerrit-common/src/main/java/com/google/gerrit/common/data/GerritConfig.java +++ b/gerrit-common/src/main/java/com/google/gerrit/common/data/GerritConfig.java @@ -14,7 +14,6 @@ package com.google.gerrit.common.data; -import java.util.List; public class GerritConfig implements Cloneable { protected String reportBugUrl; @@ -27,7 +26,6 @@ public class GerritConfig implements Cloneable { protected String anonymousCowardName; protected int suggestFrom; protected int changeUpdateDelay; - protected List archiveFormats; protected int largeChangeSize; protected String replyLabel; protected String replyTitle; @@ -116,14 +114,6 @@ public class GerritConfig implements Cloneable { this.largeChangeSize = largeChangeSize; } - public List getArchiveFormats() { - return archiveFormats; - } - - public void setArchiveFormats(List formats) { - archiveFormats = formats; - } - public String getReplyTitle() { return replyTitle; } diff --git a/gerrit-gwtui/src/main/java/com/google/gerrit/client/change/DownloadBox.java b/gerrit-gwtui/src/main/java/com/google/gerrit/client/change/DownloadBox.java index 0303a880b7..ce2a104fdd 100644 --- a/gerrit-gwtui/src/main/java/com/google/gerrit/client/change/DownloadBox.java +++ b/gerrit-gwtui/src/main/java/com/google/gerrit/client/change/DownloadBox.java @@ -164,7 +164,7 @@ class DownloadBox extends VerticalPanel { } private void insertArchive() { - List activated = Gerrit.getConfig().getArchiveFormats(); + List activated = Gerrit.info().download().archives(); if (activated.isEmpty()) { return; } diff --git a/gerrit-gwtui/src/main/java/com/google/gerrit/client/config/DownloadInfo.java b/gerrit-gwtui/src/main/java/com/google/gerrit/client/config/DownloadInfo.java index e97d472c76..fc914f4665 100644 --- a/gerrit-gwtui/src/main/java/com/google/gerrit/client/config/DownloadInfo.java +++ b/gerrit-gwtui/src/main/java/com/google/gerrit/client/config/DownloadInfo.java @@ -18,16 +18,29 @@ import com.google.gerrit.client.rpc.NativeMap; import com.google.gerrit.client.rpc.NativeString; import com.google.gerrit.client.rpc.Natives; import com.google.gwt.core.client.JavaScriptObject; +import com.google.gwt.core.client.JsArrayString; +import java.util.ArrayList; import java.util.HashSet; +import java.util.List; import java.util.Set; public class DownloadInfo extends JavaScriptObject { public final Set schemes() { return Natives.keys(_schemes()); } + + public final List archives() { + List archives = new ArrayList<>(); + for (String f : Natives.asList(_archives())) { + archives.add(f); + } + return archives; + } + public final native DownloadSchemeInfo scheme(String n) /*-{ return this.schemes[n]; }-*/; private final native NativeMap _schemes() /*-{ return this.schemes; }-*/; + private final native JsArrayString _archives() /*-{ return this.archives; }-*/; protected DownloadInfo() { } diff --git a/gerrit-httpd/src/main/java/com/google/gerrit/httpd/GerritConfigProvider.java b/gerrit-httpd/src/main/java/com/google/gerrit/httpd/GerritConfigProvider.java index 73b67e273b..65e8540c3b 100644 --- a/gerrit-httpd/src/main/java/com/google/gerrit/httpd/GerritConfigProvider.java +++ b/gerrit-httpd/src/main/java/com/google/gerrit/httpd/GerritConfigProvider.java @@ -14,15 +14,9 @@ package com.google.gerrit.httpd; -import com.google.common.base.Function; import com.google.common.base.Optional; -import com.google.common.base.Predicate; -import com.google.common.collect.Iterables; -import com.google.common.collect.Lists; import com.google.gerrit.common.data.GerritConfig; import com.google.gerrit.common.data.GitwebConfig; -import com.google.gerrit.server.change.ArchiveFormat; -import com.google.gerrit.server.change.GetArchive; import com.google.gerrit.server.config.AnonymousCowardName; import com.google.gerrit.server.config.ConfigUtil; import com.google.gerrit.server.config.GerritServerConfig; @@ -40,7 +34,6 @@ import javax.servlet.ServletContext; class GerritConfigProvider implements Provider { private final Config cfg; - private final GetArchive.AllowedFormats archiveFormats; private final GitWebConfig gitWebConfig; private final SshInfo sshInfo; @@ -53,10 +46,8 @@ class GerritConfigProvider implements Provider { GitWebConfig gwc, SshInfo si, ServletContext sc, - GetArchive.AllowedFormats af, @AnonymousCowardName String acn) { cfg = gsc; - archiveFormats = af; gitWebConfig = gwc; sshInfo = si; servletContext = sc; @@ -74,24 +65,6 @@ class GerritConfigProvider implements Provider { cfg, "change", null, "updateDelay", 30, TimeUnit.SECONDS)); config.setLargeChangeSize(cfg.getInt("change", "largeChange", 500)); - // Zip is not supported because it may be interpreted by a Java plugin as a - // valid JAR file, whose code would have access to cookies on the domain. - config.setArchiveFormats(Lists.newArrayList(Iterables.transform( - Iterables.filter( - archiveFormats.getAllowed(), - new Predicate() { - @Override - public boolean apply(ArchiveFormat format) { - return (format != ArchiveFormat.ZIP); - } - }), - new Function() { - @Override - public String apply(ArchiveFormat in) { - return in.getShortName(); - } - }))); - config.setReportBugUrl(cfg.getString("gerrit", null, "reportBugUrl")); config.setReportBugText(cfg.getString("gerrit", null, "reportBugText")); diff --git a/gerrit-server/src/main/java/com/google/gerrit/server/change/GetArchive.java b/gerrit-server/src/main/java/com/google/gerrit/server/change/GetArchive.java index 28ec2c4b34..a2fd004c24 100644 --- a/gerrit-server/src/main/java/com/google/gerrit/server/change/GetArchive.java +++ b/gerrit-server/src/main/java/com/google/gerrit/server/change/GetArchive.java @@ -14,8 +14,10 @@ package com.google.gerrit.server.change; +import com.google.common.base.Predicate; import com.google.common.base.Strings; import com.google.common.collect.ImmutableMap; +import com.google.common.collect.Sets; import com.google.gerrit.extensions.restapi.BadRequestException; import com.google.gerrit.extensions.restapi.BinaryResult; import com.google.gerrit.extensions.restapi.MethodNotAllowedException; @@ -55,7 +57,17 @@ public class GetArchive implements RestReadView { exts.put(format.name().toLowerCase(), format); } extensions = ImmutableMap.copyOf(exts); - allowed = cfg.getArchiveFormats(); + + // Zip is not supported because it may be interpreted by a Java plugin as a + // valid JAR file, whose code would have access to cookies on the domain. + allowed = Sets.filter( + cfg.getArchiveFormats(), + new Predicate() { + @Override + public boolean apply(ArchiveFormat format) { + return (format != ArchiveFormat.ZIP); + } + }); } public Set getAllowed() { diff --git a/gerrit-server/src/main/java/com/google/gerrit/server/config/GetServerInfo.java b/gerrit-server/src/main/java/com/google/gerrit/server/config/GetServerInfo.java index 9d593d4be2..cd46877cc6 100644 --- a/gerrit-server/src/main/java/com/google/gerrit/server/config/GetServerInfo.java +++ b/gerrit-server/src/main/java/com/google/gerrit/server/config/GetServerInfo.java @@ -15,6 +15,7 @@ package com.google.gerrit.server.config; import com.google.common.base.Function; +import com.google.common.collect.Iterables; import com.google.common.collect.Lists; import com.google.gerrit.extensions.config.DownloadCommand; import com.google.gerrit.extensions.config.DownloadScheme; @@ -24,6 +25,7 @@ import com.google.gerrit.reviewdb.client.Account; import com.google.gerrit.reviewdb.client.AuthType; import com.google.gerrit.server.account.Realm; import com.google.gerrit.server.change.ArchiveFormat; +import com.google.gerrit.server.change.GetArchive; import com.google.inject.Inject; import org.eclipse.jgit.lib.Config; @@ -32,16 +34,15 @@ import java.net.MalformedURLException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; -import java.util.Locale; import java.util.Map; public class GetServerInfo implements RestReadView { private final Config config; private final AuthConfig authConfig; private final Realm realm; - private final DownloadConfig downloadConfig; private final DynamicMap downloadSchemes; private final DynamicMap downloadCommands; + private final GetArchive.AllowedFormats archiveFormats; private final AllProjectsName allProjectsName; private final AllUsersName allUsersName; @@ -50,17 +51,17 @@ public class GetServerInfo implements RestReadView { @GerritServerConfig Config config, AuthConfig authConfig, Realm realm, - DownloadConfig downloadConfig, DynamicMap downloadSchemes, DynamicMap downloadCommands, + GetArchive.AllowedFormats archiveFormats, AllProjectsName allProjectsName, AllUsersName allUsersName) { this.config = config; this.authConfig = authConfig; this.realm = realm; - this.downloadConfig = downloadConfig; this.downloadSchemes = downloadSchemes; this.downloadCommands = downloadCommands; + this.archiveFormats = archiveFormats; this.allProjectsName = allProjectsName; this.allUsersName = allUsersName; } @@ -71,7 +72,7 @@ public class GetServerInfo implements RestReadView { info.auth = new AuthInfo(authConfig, realm); info.contactStore = getContactStoreInfo(); info.download = - new DownloadInfo(downloadConfig, downloadSchemes, downloadCommands); + new DownloadInfo(downloadSchemes, downloadCommands, archiveFormats); info.gerrit = new GerritInfo(allProjectsName, allUsersName); return info; } @@ -157,9 +158,9 @@ public class GetServerInfo implements RestReadView { public Map schemes; public List archives; - public DownloadInfo(DownloadConfig downloadConfig, - DynamicMap downloadSchemes, - DynamicMap downloadCommands) { + public DownloadInfo(DynamicMap downloadSchemes, + DynamicMap downloadCommands, + GetArchive.AllowedFormats archiveFormats) { schemes = new HashMap<>(); for (DynamicMap.Entry e : downloadSchemes) { DownloadScheme scheme = e.getProvider().get(); @@ -168,14 +169,14 @@ public class GetServerInfo implements RestReadView { new DownloadSchemeInfo(scheme, downloadCommands)); } } - archives = - Lists.transform(new ArrayList<>(downloadConfig.getArchiveFormats()), - new Function() { - @Override - public String apply(ArchiveFormat archiveFormat) { - return archiveFormat.name().toLowerCase(Locale.US); - } - }); + archives = Lists.newArrayList(Iterables.transform( + archiveFormats.getAllowed(), + new Function() { + @Override + public String apply(ArchiveFormat in) { + return in.getShortName(); + } + })); } }