ExternalIdKeyFactory: add a static `parse` method
Release-Notes: skip
Google-Bug: b/289356227
Change-Id: I6e24e965eccf90e027dead34ffba6928d6b2bb5e
diff --git a/java/com/google/gerrit/server/account/externalids/ExternalIdKeyFactory.java b/java/com/google/gerrit/server/account/externalids/ExternalIdKeyFactory.java
index 68d8b0c..560a1c7 100644
--- a/java/com/google/gerrit/server/account/externalids/ExternalIdKeyFactory.java
+++ b/java/com/google/gerrit/server/account/externalids/ExternalIdKeyFactory.java
@@ -16,6 +16,8 @@
import com.google.common.annotations.VisibleForTesting;
import com.google.gerrit.common.Nullable;
+import com.google.gerrit.common.UsedAt;
+import com.google.gerrit.common.UsedAt.Project;
import com.google.gerrit.server.config.AuthConfig;
import com.google.inject.ImplementedBy;
import javax.inject.Inject;
@@ -79,12 +81,7 @@
*/
public ExternalId.Key create(
@Nullable String scheme, String id, boolean userNameCaseInsensitive) {
- if (scheme != null
- && (scheme.equals(ExternalId.SCHEME_USERNAME) || scheme.equals(ExternalId.SCHEME_GERRIT))) {
- return ExternalId.Key.create(scheme, id, userNameCaseInsensitive);
- }
-
- return ExternalId.Key.create(scheme, id, false);
+ return createKey(scheme, id, userNameCaseInsensitive);
}
/**
@@ -94,10 +91,45 @@
* @return the external Id key object
*/
public ExternalId.Key parse(String externalId) {
+ return parse(externalId, isUserNameCaseInsensitive);
+ }
+
+ /**
+ * Parses an external ID key from its String representation. Prefer the non-static {@link
+ * #parse(String)} when possible
+ *
+ * @param externalId String representation of external ID key (e.g. username:johndoe)
+ * @param isUserNameCaseInsensitive whether the scheme used by the target host is case sensitive
+ * @return the external Id key object
+ */
+ @UsedAt(Project.GOOGLE)
+ public static ExternalId.Key parse(String externalId, boolean isUserNameCaseInsensitive) {
int c = externalId.indexOf(':');
if (c < 1 || c >= externalId.length() - 1) {
- return create(null, externalId);
+ return createKey(null, externalId, isUserNameCaseInsensitive);
}
- return create(externalId.substring(0, c), externalId.substring(c + 1));
+ return createKey(
+ externalId.substring(0, c), externalId.substring(c + 1), isUserNameCaseInsensitive);
+ }
+
+ /**
+ * Creates an external ID key.
+ *
+ * @param scheme the scheme name, must not contain colons (':'). E.g. {@link
+ * ExternalId#SCHEME_USERNAME}.
+ * @param id the external ID, must not contain colons (':')
+ * @param userNameCaseInsensitive whether the external ID key is matched case insensitively
+ * @return the created external ID key
+ */
+ private static ExternalId.Key createKey(
+ @Nullable String scheme, String id, boolean userNameCaseInsensitive) {
+ if (scheme != null
+ && (scheme.equals(ExternalId.SCHEME_USERNAME)
+ || scheme.equals(
+ com.google.gerrit.server.account.externalids.ExternalId.SCHEME_GERRIT))) {
+ return ExternalId.Key.create(scheme, id, userNameCaseInsensitive);
+ }
+
+ return ExternalId.Key.create(scheme, id, false);
}
}