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

com.marklogic.spark.ConnectionString Maven / Gradle / Ivy

There is a newer version: 2.4.2
Show newest version
/*
 * Copyright © 2024 MarkLogic Corporation. All Rights Reserved.
 */
package com.marklogic.spark;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

public class ConnectionString {

    private final String host;
    private final int port;
    private final String username;
    private final String password;
    private final String database;

    public ConnectionString(String connectionString, String optionNameForErrorMessage) {
        final String errorMessage = String.format(
            "Invalid value for %s; must be username:password@host:port/optionalDatabaseName",
            optionNameForErrorMessage
        );

        String[] parts = connectionString.split("@");
        if (parts.length != 2) {
            throw new ConnectorException(errorMessage);
        }
        String[] tokens = parts[0].split(":");
        if (tokens.length != 2) {
            throw new ConnectorException(errorMessage);
        }
        this.username = decodeValue(tokens[0], "username");
        this.password = decodeValue(tokens[1], "password");

        tokens = parts[1].split(":");
        if (tokens.length != 2) {
            throw new ConnectorException(errorMessage);
        }
        this.host = tokens[0];
        if (tokens[1].contains("/")) {
            tokens = tokens[1].split("/");
            this.port = parsePort(tokens[0], optionNameForErrorMessage);
            this.database = tokens[1];
        } else {
            this.port = parsePort(tokens[1], optionNameForErrorMessage);
            this.database = null;
        }
    }

    private int parsePort(String value, String optionNameForErrorMessage) {
        try {
            return Integer.parseInt(value);
        } catch (NumberFormatException e) {
            throw new ConnectorException(String.format(
                "Invalid value for %s; port must be numeric, but was '%s'", optionNameForErrorMessage, value
            ));
        }
    }

    private String decodeValue(String value, String label) {
        try {
            return URLDecoder.decode(value, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            throw new ConnectorException(String.format("Unable to decode '%s'; cause: %s", label, e.getMessage()));
        }
    }

    public String getHost() {
        return host;
    }

    public int getPort() {
        return port;
    }

    public String getUsername() {
        return username;
    }

    public String getPassword() {
        return password;
    }

    public String getDatabase() {
        return database;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy