All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.databasesandlife.util.UrlWithUsernamePassword Maven / Gradle / Ivy

There is a newer version: 21.0.1
Show newest version
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); }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy