com.databasesandlife.util.UrlWithUsernamePassword Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of java-common Show documentation
Show all versions of java-common Show documentation
Utility classes developed at Adrian Smith Software (A.S.S.)
package com.databasesandlife.util;
import com.databasesandlife.util.gwtsafe.CleartextPassword;
import com.databasesandlife.util.gwtsafe.ConfigurationException;
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Represents a URL, username, password.
*
* @author This source is copyright Adrian Smith and licensed under the LGPL 3.
* @see Project on GitHub
*/
public class UrlWithUsernamePassword {
public final @Nonnull URL url;
public final @CheckForNull String username;
public final @CheckForNull CleartextPassword password;
/** @param pipeSeparated "url" or "url|username|password" */
public UrlWithUsernamePassword(String pipeSeparated) throws ConfigurationException {
var m = Pattern.compile("^([^|]+?)(\\|([^|]+)\\|([^|]+))?$").matcher(pipeSeparated);
if (! m.matches()) throw new ConfigurationException("'" + pipeSeparated + "' should have 'url|user|pw' form");
try {
url = new URL(m.group(1));
username = m.group(3);
password = Optional.ofNullable(m.group(4)).map(x -> new CleartextPassword(x)).orElse(null);
}
catch (MalformedURLException e) { throw new ConfigurationException("URL '" + m.group(1) + "' malformed: " + e.getMessage(), e); }
}
}