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

com.marklogic.flux.impl.ConnectionParamsValidator Maven / Gradle / Ivy

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

import com.marklogic.flux.api.AuthenticationType;
import com.marklogic.flux.api.FluxException;

public class ConnectionParamsValidator {

    private final ParamNames paramNames;

    public ConnectionParamsValidator(boolean isOutput) {
        this.paramNames = new ParamNames(isOutput);
    }

    public void validate(ConnectionInputs connectionInputs) {
        if (connectionInputs.connectionString != null) {
            return;
        }

        if (connectionInputs.host == null) {
            throw new FluxException(String.format("Must specify a MarkLogic host via %s or %s.",
                paramNames.host, paramNames.connectionString));
        }
        if (connectionInputs.port <= 0) {
            throw new FluxException(String.format("Must specify a MarkLogic app server port via %s or %s.",
                paramNames.port, paramNames.connectionString));
        }

        AuthenticationType authType = connectionInputs.authType;
        boolean isDigestOrBasicAuth = authType == null || (AuthenticationType.DIGEST.equals(authType) || AuthenticationType.BASIC.equals(authType));
        if (isDigestOrBasicAuth) {
            validateUsernameAndPassword(connectionInputs);
        }
    }

    private void validateUsernameAndPassword(ConnectionInputs connectionInputs) {
        if (connectionInputs.username == null) {
            throw new FluxException(String.format("Must specify a MarkLogic user via %s when using 'BASIC' or 'DIGEST' authentication.",
                paramNames.username));
        }
        if (connectionInputs.password == null) {
            throw new FluxException(String.format("Must specify a password via %s when using 'BASIC' or 'DIGEST' authentication.",
                paramNames.password));
        }
    }

    private static class ParamNames {
        final String connectionString;
        final String host;
        final String port;
        final String authType;
        final String username;
        final String password;

        ParamNames(boolean isOutput) {
            connectionString = isOutput ? "--output-connection-string" : "--connection-string";
            host = isOutput ? "--output-host" : "--host";
            port = isOutput ? "--output-port" : "--port";
            authType = isOutput ? "--output-auth-type" : "--auth-type";
            username = isOutput ? "--output-username" : "--username";
            password = isOutput ? "--output-password" : "--password";
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy