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 <edwin.kempin@sap.com>
This commit is contained in:
Edwin Kempin
2015-05-08 12:50:34 +02:00
committed by David Pursehouse
parent 5a8c9e9649
commit e55a72f504
6 changed files with 44 additions and 55 deletions

View File

@@ -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<String> archiveFormats;
protected int largeChangeSize;
protected String replyLabel;
protected String replyTitle;
@@ -116,14 +114,6 @@ public class GerritConfig implements Cloneable {
this.largeChangeSize = largeChangeSize;
}
public List<String> getArchiveFormats() {
return archiveFormats;
}
public void setArchiveFormats(List<String> formats) {
archiveFormats = formats;
}
public String getReplyTitle() {
return replyTitle;
}

View File

@@ -164,7 +164,7 @@ class DownloadBox extends VerticalPanel {
}
private void insertArchive() {
List<String> activated = Gerrit.getConfig().getArchiveFormats();
List<String> activated = Gerrit.info().download().archives();
if (activated.isEmpty()) {
return;
}

View File

@@ -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<String> schemes() {
return Natives.keys(_schemes());
}
public final List<String> archives() {
List<String> 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<DownloadSchemeInfo> _schemes() /*-{ return this.schemes; }-*/;
private final native JsArrayString _archives() /*-{ return this.archives; }-*/;
protected DownloadInfo() {
}

View File

@@ -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<GerritConfig> {
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<GerritConfig> {
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<GerritConfig> {
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<ArchiveFormat>() {
@Override
public boolean apply(ArchiveFormat format) {
return (format != ArchiveFormat.ZIP);
}
}),
new Function<ArchiveFormat, String>() {
@Override
public String apply(ArchiveFormat in) {
return in.getShortName();
}
})));
config.setReportBugUrl(cfg.getString("gerrit", null, "reportBugUrl"));
config.setReportBugText(cfg.getString("gerrit", null, "reportBugText"));

View File

@@ -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<RevisionResource> {
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<ArchiveFormat>() {
@Override
public boolean apply(ArchiveFormat format) {
return (format != ArchiveFormat.ZIP);
}
});
}
public Set<ArchiveFormat> getAllowed() {

View File

@@ -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<ConfigResource> {
private final Config config;
private final AuthConfig authConfig;
private final Realm realm;
private final DownloadConfig downloadConfig;
private final DynamicMap<DownloadScheme> downloadSchemes;
private final DynamicMap<DownloadCommand> downloadCommands;
private final GetArchive.AllowedFormats archiveFormats;
private final AllProjectsName allProjectsName;
private final AllUsersName allUsersName;
@@ -50,17 +51,17 @@ public class GetServerInfo implements RestReadView<ConfigResource> {
@GerritServerConfig Config config,
AuthConfig authConfig,
Realm realm,
DownloadConfig downloadConfig,
DynamicMap<DownloadScheme> downloadSchemes,
DynamicMap<DownloadCommand> 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<ConfigResource> {
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<ConfigResource> {
public Map<String, DownloadSchemeInfo> schemes;
public List<String> archives;
public DownloadInfo(DownloadConfig downloadConfig,
DynamicMap<DownloadScheme> downloadSchemes,
DynamicMap<DownloadCommand> downloadCommands) {
public DownloadInfo(DynamicMap<DownloadScheme> downloadSchemes,
DynamicMap<DownloadCommand> downloadCommands,
GetArchive.AllowedFormats archiveFormats) {
schemes = new HashMap<>();
for (DynamicMap.Entry<DownloadScheme> e : downloadSchemes) {
DownloadScheme scheme = e.getProvider().get();
@@ -168,14 +169,14 @@ public class GetServerInfo implements RestReadView<ConfigResource> {
new DownloadSchemeInfo(scheme, downloadCommands));
}
}
archives =
Lists.transform(new ArrayList<>(downloadConfig.getArchiveFormats()),
archives = Lists.newArrayList(Iterables.transform(
archiveFormats.getAllowed(),
new Function<ArchiveFormat, String>() {
@Override
public String apply(ArchiveFormat archiveFormat) {
return archiveFormat.name().toLowerCase(Locale.US);
public String apply(ArchiveFormat in) {
return in.getShortName();
}
});
}));
}
}