save();
}
/*package*/ StandardCredentials migrateCredentials(ModelObject context, String legacyRealm, Credential legacyCredential)
throws IOException {
CredentialsStore store = CredentialsProvider.lookupStores(context).iterator().next();
StandardCredentials credential = legacyCredential.toCredentials(null, legacyRealm);
if (credential != null) {
return credential;
}
credential = legacyCredential.toCredentials(legacyRealm);
if (store.isDomainsModifiable()) {
Matcher matcher = Pattern.compile("\\s*<([^>]+)>.*").matcher(legacyRealm);
if (matcher.matches()) {
String url = matcher.group(1);
if (url.startsWith("http:") || url.startsWith("svn:") || url.startsWith("https:") || url
.startsWith("svn+ssh:")) {
// this is a reasonably valid URL
List<DomainRequirement> requirements = URIRequirementBuilder.fromUri(url).build();
HostnameRequirement hostnameRequirement = null;
SchemeRequirement schemeRequirement = null;
for (DomainRequirement r : requirements) {
if (hostnameRequirement == null && r instanceof HostnameRequirement) {
hostnameRequirement = (HostnameRequirement) r;
}
if (schemeRequirement == null && r instanceof SchemeRequirement) {
schemeRequirement = (SchemeRequirement) r;
}
if (schemeRequirement != null && hostnameRequirement != null) {
break;
}
}
Domain domain = null;
if (hostnameRequirement != null) {
for (Domain d : store.getDomains()) {
HostnameSpecification spec = null;
for (DomainSpecification s : d.getSpecifications()) {
if (s instanceof HostnameSpecification) {
spec = (HostnameSpecification) s;
break;
}
}
if (spec != null && spec.test(hostnameRequirement).isMatch() && d.test(requirements)) {
domain = d;
break;
}
}
}
if (domain == null) {
if (hostnameRequirement != null) {
List<DomainSpecification> specs = new ArrayList<DomainSpecification>();
specs.add(
new HostnameSpecification(hostnameRequirement.getHostname(), null));
if (schemeRequirement != null) {
specs.add(new SchemeSpecification(schemeRequirement.getScheme()));
}
domain = new Domain(hostnameRequirement.getHostname(), null, specs);
if (store.addDomain(domain, credential)) {
return credential;
}
}
} else {
if (store.addCredentials(domain, credential)) {
return credential;
}
}
}
}
}
store.addCredentials(Domain.global(), credential);
return credential;
}