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

org.datacleaner.util.RemoteServersUtils Maven / Gradle / Ivy

There is a newer version: 6.0.0
Show newest version
/**
 * DataCleaner (community edition)
 * Copyright (C) 2014 Neopost - Customer Information Management
 *
 * This copyrighted material is made available to anyone wishing to use, modify,
 * copy, or redistribute it subject to the terms and conditions of the GNU
 * Lesser General Public License, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
 * for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this distribution; if not, write to:
 * Free Software Foundation, Inc.
 * 51 Franklin Street, Fifth Floor
 * Boston, MA  02110-1301  USA
 */
package org.datacleaner.util;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

import org.apache.commons.io.IOUtils;
import org.datacleaner.Version;
import org.datacleaner.configuration.DataCleanerEnvironment;
import org.datacleaner.configuration.RemoteServerConfiguration;
import org.datacleaner.configuration.RemoteServerData;
import org.datacleaner.configuration.RemoteServerDataImpl;
import org.datacleaner.descriptors.CompositeDescriptorProvider;
import org.datacleaner.descriptors.RemoteDescriptorProvider;
import org.datacleaner.descriptors.RemoteDescriptorProviderImpl;
import org.datacleaner.restclient.ComponentRESTClient;

/**
 * Utilities for better work with remote servers.
 */
public class RemoteServersUtils {

    /**
     * It creates new RemoteServerData and Remote server provider
     *
     * @param serverName
     * @param serverUrl
     * @param userName
     * @param password
     */
    public static void addRemoteServer(final DataCleanerEnvironment env, final String serverName, String serverUrl,
            final String userName, final String password) {
        if (RemoteDescriptorProvider.DATACLOUD_SERVER_NAME.equals(serverName)) {
            serverUrl = RemoteDescriptorProvider.DATACLOUD_URL;
        }
        getMutableServerConfig(env).addServer(serverName, serverUrl, userName, password);

        final RemoteServerData remoteServerData = new RemoteServerDataImpl(serverUrl, serverName, userName, password);

        if (!(env.getDescriptorProvider() instanceof CompositeDescriptorProvider)) {
            throw new IllegalStateException("DescriptorProvider is not instance of CompositeDescriptorProvider class.");
        }
        final CompositeDescriptorProvider descriptorProvider =
                (CompositeDescriptorProvider) env.getDescriptorProvider();
        descriptorProvider
                .addDelegate(new RemoteDescriptorProviderImpl(remoteServerData, env.getRemoteServerConfiguration()));
        descriptorProvider.refresh();
    }

    /**
     * Update remote server credentials
     *
     * @param serverName
     * @param userName
     * @param password
     */
    public static void updateRemoteServerCredentials(final DataCleanerEnvironment env, final String serverName,
            final String userName, final String password) {
        getMutableServerConfig(env).updateServerCredentials(serverName, userName, password);
        env.getDescriptorProvider().refresh();
    }

    public static void checkServerWithCredentials(final String url, final String username, final String password)
            throws Exception {
        new ComponentRESTClient(url, username, password, Version.getVersion());
    }

    public static String getDataCloudTermsAndConditions() throws IOException {
        final InputStream in = new URL(RemoteDescriptorProvider.DATACLOUD_TERMS_PURE_URL).openStream();
        try {
            return IOUtils.toString(in);
        } finally {
            IOUtils.closeQuietly(in);
        }
    }

    private static MutableRemoteServerConfigurationImpl getMutableServerConfig(final DataCleanerEnvironment env) {
        final RemoteServerConfiguration remServerConfig = env.getRemoteServerConfiguration();
        if (!(remServerConfig instanceof MutableRemoteServerConfigurationImpl)) {
            throw new UnsupportedOperationException(
                    "Remote server config is not instance of class " + MutableRemoteServerConfigurationImpl.class);
        }
        return (MutableRemoteServerConfigurationImpl) remServerConfig;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy