Merge changes from topic 'arbitrary-download-schemes'

* changes:
  Sort download schemes and command names
  SetPreferences: Enforce download scheme is registered
  Describe allowed values for download_scheme field
  DownloadUrlLink: Kill KnownScheme enum
  Store preferred download scheme as arbitrary strings
  DownloadConfig: Make set methods return ImmutableSet
  Remove DEFAULT_SCHEMES and DEFAULT_COMMANDS
This commit is contained in:
Dave Borowitz 2015-09-09 15:00:57 +00:00 committed by Gerrit Code Review
commit 1912561ec1
19 changed files with 199 additions and 207 deletions

View File

@ -1774,7 +1774,9 @@ Whether the site header should be shown.
|`use_flash_clipboard` |not set if `false`|
Whether to use the flash clipboard widget.
|`download_scheme` ||
The type of download URL the user prefers to use.
The type of download URL the user prefers to use. May be any key from
the `schemes` map in
link:rest-api-config.html#download-info[DownloadInfo].
|`download_command` ||
The type of download command the user prefers to use.
|`copy_self_on_email` |not set if `false`|

View File

@ -21,7 +21,6 @@ import com.google.gerrit.reviewdb.client.AccountGeneralPreferences;
import com.google.gerrit.reviewdb.client.AccountGeneralPreferences.DateFormat;
import com.google.gerrit.reviewdb.client.AccountGeneralPreferences.DiffView;
import com.google.gerrit.reviewdb.client.AccountGeneralPreferences.DownloadCommand;
import com.google.gerrit.reviewdb.client.AccountGeneralPreferences.DownloadScheme;
import com.google.gerrit.reviewdb.client.AccountGeneralPreferences.ReviewCategoryStrategy;
import com.google.gerrit.reviewdb.client.AccountGeneralPreferences.TimeFormat;
import com.google.gwt.core.client.JavaScriptObject;
@ -73,11 +72,7 @@ public class AccountPreferencesInfo extends JavaScriptObject {
public final native boolean useFlashClipboard()
/*-{ return this.use_flash_clipboard || false }-*/;
public final DownloadScheme downloadScheme() {
String s = downloadSchemeRaw();
return s != null ? DownloadScheme.valueOf(s) : null;
}
private final native String downloadSchemeRaw()
public final native String downloadScheme()
/*-{ return this.download_scheme }-*/;
public final DownloadCommand downloadCommand() {
@ -142,10 +137,7 @@ public class AccountPreferencesInfo extends JavaScriptObject {
public final native void useFlashClipboard(boolean u)
/*-{ this.use_flash_clipboard = u }-*/;
public final void downloadScheme(DownloadScheme d) {
downloadSchemeRaw(d != null ? d.toString() : null);
}
private final native void downloadSchemeRaw(String d)
public final native void downloadScheme(String d)
/*-{ this.download_scheme = d }-*/;
public final void downloadCommand(DownloadCommand d) {

View File

@ -26,8 +26,8 @@ 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> schemes() {
return _schemes().sortedKeys();
}
public final List<String> archives() {
@ -46,8 +46,8 @@ public class DownloadInfo extends JavaScriptObject {
}
public static class DownloadSchemeInfo extends JavaScriptObject {
public final Set<String> commandNames() {
return Natives.keys(_commands());
public final List<String> commandNames() {
return _commands().sortedKeys();
}
public final Set<DownloadCommandInfo> commands(String project) {
@ -67,13 +67,14 @@ public class DownloadInfo extends JavaScriptObject {
return project.substring(project.lastIndexOf('/') + 1);
}
public final Set<String> cloneCommandNames() {
return Natives.keys(_cloneCommands());
public final List<String> cloneCommandNames() {
return _cloneCommands().sortedKeys();
}
public final Set<DownloadCommandInfo> cloneCommands(String project) {
Set<DownloadCommandInfo> commands = new HashSet<>();
for (String commandName : cloneCommandNames()) {
public final List<DownloadCommandInfo> cloneCommands(String project) {
List<String> commandNames = cloneCommandNames();
List<DownloadCommandInfo> commands = new ArrayList<>(commandNames.size());
for (String commandName : commandNames) {
commands.add(new DownloadCommandInfo(commandName, cloneCommand(
commandName, project)));
}

View File

@ -18,6 +18,9 @@ import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.core.client.JsArray;
import com.google.gwt.user.client.rpc.AsyncCallback;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;
/** A map of native JSON objects, keyed by a string. */
@ -58,6 +61,13 @@ public class NativeMap<T extends JavaScriptObject> extends JavaScriptObject {
return Natives.keys(this);
}
public final List<String> sortedKeys() {
Set<String> keys = keySet();
List<String> sorted = new ArrayList<>(keys);
Collections.sort(sorted);
return sorted;
}
public final native JsArray<T> values()
/*-{
var s = this;

View File

@ -69,7 +69,6 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
public class ProjectInfoScreen extends ProjectScreen {
private boolean isOwner;
@ -710,7 +709,7 @@ public class ProjectInfoScreen extends ProjectScreen {
}
@Override
protected Set<DownloadCommandInfo> getCommands(DownloadSchemeInfo schemeInfo) {
protected List<DownloadCommandInfo> getCommands(DownloadSchemeInfo schemeInfo) {
return schemeInfo.cloneCommands(project);
}
}

View File

@ -23,10 +23,8 @@ import com.google.gerrit.client.info.ChangeInfo;
import com.google.gerrit.client.info.ChangeInfo.EditInfo;
import com.google.gerrit.client.info.ChangeInfo.FetchInfo;
import com.google.gerrit.client.rpc.NativeMap;
import com.google.gerrit.client.rpc.Natives;
import com.google.gerrit.client.rpc.RestApi;
import com.google.gerrit.extensions.client.ListChangesOption;
import com.google.gerrit.reviewdb.client.AccountGeneralPreferences.DownloadScheme;
import com.google.gerrit.reviewdb.client.PatchSet;
import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.event.dom.client.ChangeEvent;
@ -120,7 +118,7 @@ class DownloadBox extends VerticalPanel {
if (scheme.getItemCount() > 0) {
FetchInfo fetchInfo =
fetch.get(scheme.getValue(scheme.getSelectedIndex()));
for (String commandName : Natives.keys(fetchInfo.commands())) {
for (String commandName : fetchInfo.commands().sortedKeys()) {
CopyableLabel copyLabel =
new CopyableLabel(fetchInfo.command(commandName));
copyLabel.setStyleName(Gerrit.RESOURCES.css().downloadBoxCopyLabel());
@ -210,7 +208,7 @@ class DownloadBox extends VerticalPanel {
}
private void renderScheme() {
for (String id : fetch.keySet()) {
for (String id : fetch.sortedKeys()) {
scheme.addItem(id);
}
if (scheme.getItemCount() == 0) {
@ -221,7 +219,7 @@ class DownloadBox extends VerticalPanel {
scheme.setVisible(false);
} else {
int select = 0;
String find = getUserPreference();
String find = Gerrit.getUserPreferences().downloadScheme();
if (find != null) {
for (int i = 0; i < scheme.getItemCount(); i++) {
if (find.equals(scheme.getValue(i))) {
@ -236,35 +234,13 @@ class DownloadBox extends VerticalPanel {
renderCommands();
}
private static String getUserPreference() {
DownloadScheme pref = Gerrit.getUserPreferences().downloadScheme();
if (pref != null) {
switch (pref) {
case ANON_GIT:
return "git";
case ANON_HTTP:
return "anonymous http";
case HTTP:
return "http";
case SSH:
return "ssh";
case REPO_DOWNLOAD:
return "repo";
default:
return null;
}
}
return null;
}
private void saveScheme() {
DownloadScheme scheme = getSelectedScheme();
String schemeStr = scheme.getValue(scheme.getSelectedIndex());
AccountPreferencesInfo prefs = Gerrit.getUserPreferences();
if (Gerrit.isSignedIn() && scheme != null
&& scheme != prefs.downloadScheme()) {
prefs.downloadScheme(scheme);
if (Gerrit.isSignedIn() && !schemeStr.equals(prefs.downloadScheme())) {
prefs.downloadScheme(schemeStr);
AccountPreferencesInfo in = AccountPreferencesInfo.create();
in.downloadScheme(scheme);
in.downloadScheme(schemeStr);
AccountApi.self().view("preferences")
.put(in, new AsyncCallback<JavaScriptObject>() {
@Override
@ -277,20 +253,4 @@ class DownloadBox extends VerticalPanel {
});
}
}
private DownloadScheme getSelectedScheme() {
String id = scheme.getValue(scheme.getSelectedIndex());
if ("git".equals(id)) {
return DownloadScheme.ANON_GIT;
} else if ("anonymous http".equals(id)) {
return DownloadScheme.ANON_HTTP;
} else if ("http".equals(id)) {
return DownloadScheme.HTTP;
} else if ("ssh".equals(id)) {
return DownloadScheme.SSH;
} else if ("repo".equals(id)) {
return DownloadScheme.REPO_DOWNLOAD;
}
return null;
}
}

View File

@ -1 +0,0 @@
anonymousDownload = Anonymous {0}

View File

@ -21,7 +21,7 @@ import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.InlineLabel;
import com.google.gwtexpui.clippy.client.CopyableLabel;
import java.util.Set;
import java.util.List;
public abstract class DownloadPanel extends FlowPanel {
protected final String project;
@ -63,6 +63,6 @@ public abstract class DownloadPanel extends FlowPanel {
commands.select();
}
protected abstract Set<DownloadCommandInfo> getCommands(
protected abstract List<DownloadCommandInfo> getCommands(
DownloadSchemeInfo schemeInfo);
}

View File

@ -18,7 +18,6 @@ import com.google.gerrit.client.Gerrit;
import com.google.gerrit.client.account.AccountApi;
import com.google.gerrit.client.info.AccountPreferencesInfo;
import com.google.gerrit.client.info.DownloadInfo.DownloadSchemeInfo;
import com.google.gerrit.reviewdb.client.AccountGeneralPreferences.DownloadScheme;
import com.google.gwt.aria.client.Roles;
import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.event.dom.client.ClickEvent;
@ -31,32 +30,6 @@ import java.util.ArrayList;
import java.util.List;
public class DownloadUrlLink extends Anchor implements ClickHandler {
private enum KnownScheme {
ANON_GIT(DownloadScheme.ANON_GIT, "git", Util.M.anonymousDownload("Git")),
ANON_HTTP(DownloadScheme.ANON_HTTP, "anonymous http", Util.M.anonymousDownload("HTTP")),
SSH(DownloadScheme.SSH, "ssh", "SSH"),
HTTP(DownloadScheme.HTTP, "http", "HTTP");
public final DownloadScheme downloadScheme;
public final String name;
public final String text;
private KnownScheme(DownloadScheme downloadScheme, String name, String text) {
this.downloadScheme = downloadScheme;
this.name = name;
this.text = text;
}
static KnownScheme get(String name) {
for (KnownScheme s : values()) {
if (s.name.equals(name)) {
return s;
}
}
return null;
}
}
public static List<DownloadUrlLink> createDownloadUrlLinks(
boolean allowAnonymous, DownloadPanel downloadPanel) {
List<DownloadUrlLink> urls = new ArrayList<>();
@ -65,41 +38,29 @@ public class DownloadUrlLink extends Anchor implements ClickHandler {
if (scheme.isAuthRequired() && !allowAnonymous) {
continue;
}
KnownScheme knownScheme = KnownScheme.get(s);
if (knownScheme != null) {
urls.add(new DownloadUrlLink(downloadPanel, scheme,
knownScheme.downloadScheme, knownScheme.text));
} else {
urls.add(new DownloadUrlLink(downloadPanel, scheme, s));
}
urls.add(new DownloadUrlLink(downloadPanel, scheme, s));
}
return urls;
}
private final DownloadPanel downloadPanel;
private final DownloadSchemeInfo schemeInfo;
private final DownloadScheme scheme;
private final String schemeName;
public DownloadUrlLink(DownloadPanel downloadPanel,
DownloadSchemeInfo schemeInfo, String text) {
this(downloadPanel, schemeInfo, null, text);
}
public DownloadUrlLink(DownloadPanel downloadPanel,
DownloadSchemeInfo schemeInfo, DownloadScheme urlType, String text) {
super(text);
DownloadSchemeInfo schemeInfo, String schemeName) {
super(schemeName);
setStyleName(Gerrit.RESOURCES.css().downloadLink());
Roles.getTabRole().set(getElement());
addClickHandler(this);
this.downloadPanel = downloadPanel;
this.schemeInfo = schemeInfo;
this.scheme = urlType;
this.schemeName = schemeName;
}
public DownloadScheme getUrlType() {
return scheme;
public String getSchemeName() {
return schemeName;
}
@Override
@ -110,11 +71,10 @@ public class DownloadUrlLink extends Anchor implements ClickHandler {
select();
AccountPreferencesInfo prefs = Gerrit.getUserPreferences();
if (Gerrit.isSignedIn() && scheme != null
&& scheme != prefs.downloadScheme()) {
prefs.downloadScheme(scheme);
if (Gerrit.isSignedIn() && !schemeName.equals(prefs.downloadScheme())) {
prefs.downloadScheme(schemeName);
AccountPreferencesInfo in = AccountPreferencesInfo.create();
in.downloadScheme(scheme);
in.downloadScheme(schemeName);
AccountApi.self().view("preferences")
.put(in, new AsyncCallback<JavaScriptObject>() {
@Override

View File

@ -15,7 +15,6 @@
package com.google.gerrit.client.download;
import com.google.gerrit.client.Gerrit;
import com.google.gerrit.reviewdb.client.AccountGeneralPreferences;
import com.google.gwt.aria.client.Roles;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Widget;
@ -33,7 +32,7 @@ public class DownloadUrlPanel extends FlowPanel {
return getWidgetCount() == 0;
}
public void select(AccountGeneralPreferences.DownloadScheme urlType) {
public void select(String schemeName) {
DownloadUrlLink first = null;
for (Widget w : this) {
@ -42,7 +41,7 @@ public class DownloadUrlPanel extends FlowPanel {
if (first == null) {
first = d;
}
if (d.getUrlType() == urlType) {
if (d.getSchemeName().equals(schemeName)) {
d.select();
return;
}

View File

@ -1,21 +0,0 @@
// Copyright (C) 2008 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.client.download;
import com.google.gwt.core.client.GWT;
public class Util {
public static final DownloadMessages M = GWT.create(DownloadMessages.class);
}

View File

@ -14,7 +14,7 @@
package com.google.gerrit.httpd;
import com.google.gerrit.reviewdb.client.AccountGeneralPreferences.DownloadScheme;
import com.google.gerrit.reviewdb.client.CoreDownloadSchemes;
import com.google.gerrit.server.config.AuthConfig;
import com.google.gerrit.server.config.DownloadConfig;
import com.google.inject.Inject;
@ -55,8 +55,7 @@ public class GitOverHttpModule extends ServletModule {
}
private boolean isHttpEnabled(){
return downloadConfig.getDownloadSchemes().contains(DownloadScheme.DEFAULT_DOWNLOADS)
|| downloadConfig.getDownloadSchemes().contains(DownloadScheme.ANON_HTTP)
|| downloadConfig.getDownloadSchemes().contains(DownloadScheme.HTTP);
return downloadConfig.getDownloadSchemes().contains(CoreDownloadSchemes.ANON_HTTP)
|| downloadConfig.getDownloadSchemes().contains(CoreDownloadSchemes.HTTP);
}
}

View File

@ -25,14 +25,9 @@ public final class AccountGeneralPreferences {
/** Valid choices for the page size. */
public static final short[] PAGESIZE_CHOICES = {10, 25, 50, 100};
/** Preferred scheme type to download a change. */
public static enum DownloadScheme {
ANON_GIT, ANON_HTTP, HTTP, SSH, REPO_DOWNLOAD, DEFAULT_DOWNLOADS
}
/** Preferred method to download a change. */
public static enum DownloadCommand {
REPO_DOWNLOAD, PULL, CHECKOUT, CHERRY_PICK, FORMAT_PATCH, DEFAULT_DOWNLOADS
REPO_DOWNLOAD, PULL, CHECKOUT, CHERRY_PICK, FORMAT_PATCH
}
public static enum DateFormat {
@ -187,19 +182,49 @@ public final class AccountGeneralPreferences {
useFlashClipboard = b;
}
public DownloadScheme getDownloadUrl() {
if (downloadUrl == null) {
return null;
public String getDownloadUrl() {
// Translate from legacy enum names to modern display names. (May be removed
// if accompanied by a 2-phase schema upgrade.)
if (downloadUrl != null) {
switch (downloadUrl) {
case "ANON_GIT":
return CoreDownloadSchemes.ANON_GIT;
case "ANON_HTTP":
return CoreDownloadSchemes.ANON_HTTP;
case "HTTP":
return CoreDownloadSchemes.HTTP;
case "SSH":
return CoreDownloadSchemes.SSH;
case "REPO_DOWNLOAD":
return CoreDownloadSchemes.REPO_DOWNLOAD;
}
}
return DownloadScheme.valueOf(downloadUrl);
return downloadUrl;
}
public void setDownloadUrl(DownloadScheme url) {
if (url != null) {
downloadUrl = url.name();
} else {
downloadUrl = null;
public void setDownloadUrl(String url) {
// Translate from modern display names to legacy enum names. (May be removed
// if accompanied by a 2-phase schema upgrade.)
if (downloadUrl != null) {
switch (url) {
case CoreDownloadSchemes.ANON_GIT:
url = "ANON_GIT";
break;
case CoreDownloadSchemes.ANON_HTTP:
url = "ANON_HTTP";
break;
case CoreDownloadSchemes.HTTP:
url = "HTTP";
break;
case CoreDownloadSchemes.SSH:
url = "SSH";
break;
case CoreDownloadSchemes.REPO_DOWNLOAD:
url = "REPO_DOWNLOAD";
break;
}
}
downloadUrl = url;
}
public DownloadCommand getDownloadCommand() {

View File

@ -1,4 +1,4 @@
// Copyright (C) 2008 The Android Open Source Project
// Copyright (C) 2015 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.
@ -12,10 +12,19 @@
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.gerrit.client.download;
package com.google.gerrit.reviewdb.client;
import com.google.gwt.i18n.client.Messages;
/**
* Download scheme string constants supported by the download-commands core
* plugin.
*/
public class CoreDownloadSchemes {
public static final String ANON_GIT = "git";
public static final String ANON_HTTP = "anonymous http";
public static final String HTTP = "http";
public static final String SSH = "ssh";
public static final String REPO_DOWNLOAD = "repo";
public interface DownloadMessages extends Messages {
String anonymousDownload(String protocol);
private CoreDownloadSchemes() {
}
}

View File

@ -24,7 +24,6 @@ import com.google.gerrit.reviewdb.client.AccountGeneralPreferences;
import com.google.gerrit.reviewdb.client.AccountGeneralPreferences.DateFormat;
import com.google.gerrit.reviewdb.client.AccountGeneralPreferences.DiffView;
import com.google.gerrit.reviewdb.client.AccountGeneralPreferences.DownloadCommand;
import com.google.gerrit.reviewdb.client.AccountGeneralPreferences.DownloadScheme;
import com.google.gerrit.reviewdb.client.AccountGeneralPreferences.ReviewCategoryStrategy;
import com.google.gerrit.reviewdb.client.AccountGeneralPreferences.TimeFormat;
import com.google.gerrit.reviewdb.server.ReviewDb;
@ -103,7 +102,7 @@ public class GetPreferences implements RestReadView<AccountResource> {
Short changesPerPage;
Boolean showSiteHeader;
Boolean useFlashClipboard;
DownloadScheme downloadScheme;
String downloadScheme;
DownloadCommand downloadCommand;
Boolean copySelfOnEmail;
DateFormat dateFormat;

View File

@ -23,7 +23,10 @@ import static com.google.gerrit.server.account.GetPreferences.MY;
import static com.google.gerrit.server.account.GetPreferences.URL_ALIAS;
import com.google.common.base.Strings;
import com.google.gerrit.extensions.config.DownloadScheme;
import com.google.gerrit.extensions.registration.DynamicMap;
import com.google.gerrit.extensions.restapi.AuthException;
import com.google.gerrit.extensions.restapi.BadRequestException;
import com.google.gerrit.extensions.restapi.ResourceNotFoundException;
import com.google.gerrit.extensions.restapi.RestModifyView;
import com.google.gerrit.extensions.webui.TopMenu;
@ -32,7 +35,6 @@ import com.google.gerrit.reviewdb.client.AccountGeneralPreferences;
import com.google.gerrit.reviewdb.client.AccountGeneralPreferences.DateFormat;
import com.google.gerrit.reviewdb.client.AccountGeneralPreferences.DiffView;
import com.google.gerrit.reviewdb.client.AccountGeneralPreferences.DownloadCommand;
import com.google.gerrit.reviewdb.client.AccountGeneralPreferences.DownloadScheme;
import com.google.gerrit.reviewdb.client.AccountGeneralPreferences.ReviewCategoryStrategy;
import com.google.gerrit.reviewdb.client.AccountGeneralPreferences.TimeFormat;
import com.google.gerrit.reviewdb.server.ReviewDb;
@ -60,7 +62,7 @@ public class SetPreferences implements RestModifyView<AccountResource, Input> {
public Short changesPerPage;
public Boolean showSiteHeader;
public Boolean useFlashClipboard;
public DownloadScheme downloadScheme;
public String downloadScheme;
public DownloadCommand downloadCommand;
public Boolean copySelfOnEmail;
public DateFormat dateFormat;
@ -80,22 +82,27 @@ public class SetPreferences implements RestModifyView<AccountResource, Input> {
private final Provider<ReviewDb> db;
private final MetaDataUpdate.User metaDataUpdateFactory;
private final AllUsersName allUsersName;
private final DynamicMap<DownloadScheme> downloadSchemes;
@Inject
SetPreferences(Provider<CurrentUser> self, AccountCache cache,
Provider<ReviewDb> db, MetaDataUpdate.User metaDataUpdateFactory,
AllUsersName allUsersName) {
SetPreferences(Provider<CurrentUser> self,
AccountCache cache,
Provider<ReviewDb> db,
MetaDataUpdate.User metaDataUpdateFactory,
AllUsersName allUsersName,
DynamicMap<DownloadScheme> downloadSchemes) {
this.self = self;
this.cache = cache;
this.db = db;
this.metaDataUpdateFactory = metaDataUpdateFactory;
this.allUsersName = allUsersName;
this.downloadSchemes = downloadSchemes;
}
@Override
public GetPreferences.PreferenceInfo apply(AccountResource rsrc, Input i)
throws AuthException, ResourceNotFoundException, OrmException,
IOException, ConfigInvalidException {
throws AuthException, ResourceNotFoundException, BadRequestException,
OrmException, IOException, ConfigInvalidException {
if (self.get() != rsrc.getUser()
&& !self.get().getCapabilities().canModifyAccount()) {
throw new AuthException("restricted to members of Modify Accounts");
@ -134,7 +141,7 @@ public class SetPreferences implements RestModifyView<AccountResource, Input> {
p.setUseFlashClipboard(i.useFlashClipboard);
}
if (i.downloadScheme != null) {
p.setDownloadUrl(i.downloadScheme);
setDownloadScheme(p, i.downloadScheme);
}
if (i.downloadCommand != null) {
p.setDownloadCommand(i.downloadCommand);
@ -225,4 +232,16 @@ public class SetPreferences implements RestModifyView<AccountResource, Input> {
}
}
}
private void setDownloadScheme(AccountGeneralPreferences p, String scheme)
throws BadRequestException {
for (DynamicMap.Entry<DownloadScheme> e : downloadSchemes) {
if (e.getExportName().equals(scheme)
&& e.getProvider().get().isEnabled()) {
p.setDownloadUrl(scheme);
return;
}
}
throw new BadRequestException("Unsupported download scheme: " + scheme);
}
}

View File

@ -14,65 +14,107 @@
package com.google.gerrit.server.config;
import com.google.common.collect.ImmutableSet;
import com.google.gerrit.reviewdb.client.AccountGeneralPreferences.DownloadCommand;
import com.google.gerrit.reviewdb.client.AccountGeneralPreferences.DownloadScheme;
import com.google.gerrit.reviewdb.client.CoreDownloadSchemes;
import com.google.gerrit.server.change.ArchiveFormat;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import org.eclipse.jgit.lib.Config;
import java.util.Collections;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
/** Download protocol from {@code gerrit.config}. */
/**
* Download protocol from {@code gerrit.config}.
* <p>
* Only used to configure the built-in set of schemes and commands in the core
* download-commands plugin; not used by other plugins.
*/
@Singleton
public class DownloadConfig {
private final Set<DownloadScheme> downloadSchemes;
private final Set<DownloadCommand> downloadCommands;
private final Set<ArchiveFormat> archiveFormats;
private final ImmutableSet<String> downloadSchemes;
private final ImmutableSet<DownloadCommand> downloadCommands;
private final ImmutableSet<ArchiveFormat> archiveFormats;
@Inject
DownloadConfig(@GerritServerConfig final Config cfg) {
List<DownloadScheme> allSchemes =
ConfigUtil.getEnumList(cfg, "download", null, "scheme",
DownloadScheme.DEFAULT_DOWNLOADS);
downloadSchemes =
Collections.unmodifiableSet(new HashSet<>(allSchemes));
String[] allSchemes = cfg.getStringList("download", null, "scheme");
if (allSchemes.length == 0) {
downloadSchemes = ImmutableSet.of(
CoreDownloadSchemes.SSH,
CoreDownloadSchemes.HTTP,
CoreDownloadSchemes.ANON_HTTP);
} else {
List<String> normalized = new ArrayList<>(allSchemes.length);
for (String s : allSchemes) {
String core = toCoreScheme(s);
if (core == null) {
throw new IllegalArgumentException(
"not a core download scheme: " + s);
}
normalized.add(core);
}
downloadSchemes = ImmutableSet.copyOf(normalized);
}
DownloadCommand[] downloadCommandValues = DownloadCommand.values();
List<DownloadCommand> allCommands =
ConfigUtil.getEnumList(cfg, "download", null, "command",
DownloadCommand.DEFAULT_DOWNLOADS);
downloadCommands =
Collections.unmodifiableSet(new HashSet<>(allCommands));
downloadCommandValues, null);
if (isOnlyNull(allCommands)) {
downloadCommands = ImmutableSet.copyOf(downloadCommandValues);
} else {
downloadCommands = ImmutableSet.copyOf(allCommands);
}
String v = cfg.getString("download", null, "archive");
if (v == null) {
archiveFormats = EnumSet.allOf(ArchiveFormat.class);
archiveFormats = ImmutableSet.copyOf(EnumSet.allOf(ArchiveFormat.class));
} else if (v.isEmpty() || "off".equalsIgnoreCase(v)) {
archiveFormats = Collections.emptySet();
archiveFormats = ImmutableSet.of();
} else {
archiveFormats = new HashSet<>(ConfigUtil.getEnumList(cfg,
archiveFormats = ImmutableSet.copyOf(ConfigUtil.getEnumList(cfg,
"download", null, "archive",
ArchiveFormat.TGZ));
}
}
private static boolean isOnlyNull(List<?> list) {
return list.size() == 1 && list.get(0) == null;
}
private static String toCoreScheme(String s) {
try {
Field f = CoreDownloadSchemes.class.getField(s.toUpperCase());
int m = Modifier.PUBLIC | Modifier.STATIC | Modifier.FINAL;
if ((f.getModifiers() & m) == m && f.getType() == String.class) {
return (String) f.get(null);
} else {
return null;
}
} catch (NoSuchFieldException | SecurityException | IllegalArgumentException
| IllegalAccessException e) {
return null;
}
}
/** Scheme used to download. */
public Set<DownloadScheme> getDownloadSchemes() {
public ImmutableSet<String> getDownloadSchemes() {
return downloadSchemes;
}
/** Command used to download. */
public Set<DownloadCommand> getDownloadCommands() {
public ImmutableSet<DownloadCommand> getDownloadCommands() {
return downloadCommands;
}
/** Archive formats for downloading. */
public Set<ArchiveFormat> getArchiveFormats() {
public ImmutableSet<ArchiveFormat> getArchiveFormats() {
return archiveFormats;
}
}

View File

@ -14,7 +14,7 @@
package com.google.gerrit.sshd.commands;
import com.google.gerrit.reviewdb.client.AccountGeneralPreferences.DownloadScheme;
import com.google.gerrit.reviewdb.client.CoreDownloadSchemes;
import com.google.gerrit.server.config.DownloadConfig;
import com.google.gerrit.sshd.CommandModule;
import com.google.gerrit.sshd.CommandName;
@ -130,8 +130,6 @@ public class DefaultCommandModule extends CommandModule {
}
private boolean sshEnabled() {
return downloadConfig.getDownloadSchemes().contains(DownloadScheme.SSH)
|| downloadConfig.getDownloadSchemes().contains(
DownloadScheme.DEFAULT_DOWNLOADS);
return downloadConfig.getDownloadSchemes().contains(CoreDownloadSchemes.SSH);
}
}

@ -1 +1 @@
Subproject commit 99e61fb06a4505a9558c23a56213cb32ceaa9cca
Subproject commit 6d4e0a45ad4d7faebc692e5f10e418cbfcf858cb