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:
parent
65f90698da
commit
e1b18c4d65
@ -18,7 +18,10 @@ import com.google.gerrit.common.Nullable;
|
|||||||
import com.google.inject.Inject;
|
import com.google.inject.Inject;
|
||||||
import com.google.inject.Provider;
|
import com.google.inject.Provider;
|
||||||
import com.google.inject.Singleton;
|
import com.google.inject.Singleton;
|
||||||
|
import java.net.MalformedURLException;
|
||||||
|
import java.net.URL;
|
||||||
import org.eclipse.jgit.lib.Config;
|
import org.eclipse.jgit.lib.Config;
|
||||||
|
import org.eclipse.jgit.util.SystemReader;
|
||||||
|
|
||||||
/** Provides {@link GerritInstanceName} from {@code gerrit.name}. */
|
/** Provides {@link GerritInstanceName} from {@code gerrit.name}. */
|
||||||
@Singleton
|
@Singleton
|
||||||
@ -32,17 +35,34 @@ public class GerritInstanceNameProvider implements Provider<String> {
|
|||||||
this.instanceName = getInstanceName(config, canonicalUrlProvider);
|
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");
|
String instanceName = config.getString("gerrit", null, "instanceName");
|
||||||
if (instanceName != null || canonicalUrlProvider == null) {
|
if (instanceName != null || canonicalUrlProvider == null) {
|
||||||
return instanceName;
|
return instanceName;
|
||||||
}
|
}
|
||||||
|
|
||||||
return canonicalUrlProvider.get();
|
return extractInstanceName(canonicalUrlProvider.get());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
private static String extractInstanceName(String canonicalUrl) {
|
||||||
public String get() {
|
if (canonicalUrl != null) {
|
||||||
return instanceName;
|
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();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user