Parse the canonical URL for the instance name

Without this commit, the instance name may default to a full URI,
starting with "https://". This is pointlessly long, defeating the whole
purpose of having a short instance name.

Change-Id: I16647db70a766dcc1d324d10836530958eb1989f
This commit is contained in:
Maxime Guerreiro 2018-03-28 07:43:48 +00:00
parent 65f90698da
commit e1b18c4d65

View File

@ -18,7 +18,10 @@ import com.google.gerrit.common.Nullable;
import com.google.inject.Inject;
import com.google.inject.Provider;
import com.google.inject.Singleton;
import java.net.MalformedURLException;
import java.net.URL;
import org.eclipse.jgit.lib.Config;
import org.eclipse.jgit.util.SystemReader;
/** Provides {@link GerritInstanceName} from {@code gerrit.name}. */
@Singleton
@ -32,17 +35,34 @@ public class GerritInstanceNameProvider implements Provider<String> {
this.instanceName = getInstanceName(config, canonicalUrlProvider);
}
private String getInstanceName(Config config, @Nullable Provider<String> canonicalUrlProvider) {
@Override
public String get() {
return instanceName;
}
private static String getInstanceName(
Config config, @Nullable Provider<String> canonicalUrlProvider) {
String instanceName = config.getString("gerrit", null, "instanceName");
if (instanceName != null || canonicalUrlProvider == null) {
return instanceName;
}
return canonicalUrlProvider.get();
return extractInstanceName(canonicalUrlProvider.get());
}
@Override
public String get() {
return instanceName;
private static String extractInstanceName(String canonicalUrl) {
if (canonicalUrl != null) {
try {
return new URL(canonicalUrl).getHost();
} catch (MalformedURLException e) {
// Try something else.
}
}
// Fall back onto whatever the local operating system thinks
// this server is called. We hopefully didn't get here as a
// good admin would have configured the canonical url.
//
return SystemReader.getInstance().getHostname();
}
}