Search in sources :

Example 26 with NoSQLServerException

use of org.talend.repository.nosql.exceptions.NoSQLServerException in project tbd-studio-se by Talend.

the class CassandraOldVersionMetadataHandler method getColumns.

@Override
public List<Object> getColumns(NoSQLConnection connection, String ksName, String cfName) throws NoSQLServerException {
    List<Object> columns = new ArrayList<Object>();
    try {
        Object keySpace = getKeySpace(connection, ksName);
        if (keySpace == null) {
            return columns;
        }
        // $NON-NLS-1$
        Collection<Object> cfDefs = (List<Object>) NoSQLReflection.invokeMethod(keySpace, "getCfDefs");
        for (Object cfDef : cfDefs) {
            // $NON-NLS-1$
            String name = (String) NoSQLReflection.invokeMethod(cfDef, "getName");
            if (name != null && cfName.equals(name)) {
                // $NON-NLS-1$
                columns.addAll((List<Object>) NoSQLReflection.invokeMethod(cfDef, "getColumnMetadata"));
                break;
            }
        }
    } catch (Exception e) {
        throw new NoSQLServerException(e);
    }
    return columns;
}
Also used : NoSQLServerException(org.talend.repository.nosql.exceptions.NoSQLServerException) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) NoSQLServerException(org.talend.repository.nosql.exceptions.NoSQLServerException) NoSQLReflectionException(org.talend.repository.nosql.exceptions.NoSQLReflectionException)

Example 27 with NoSQLServerException

use of org.talend.repository.nosql.exceptions.NoSQLServerException in project tbd-studio-se by Talend.

the class CassandraOldVersionMetadataHandler method closeConnections.

@Override
public void closeConnections() throws NoSQLServerException {
    try {
        if (cluster != null) {
            // $NON-NLS-1$
            Object connMgr = NoSQLReflection.invokeMethod(cluster, "getConnectionManager");
            // $NON-NLS-1$
            NoSQLReflection.invokeMethod(connMgr, "shutdown");
            cluster = null;
        }
    } catch (NoSQLReflectionException e) {
        throw new NoSQLServerException(e);
    }
}
Also used : NoSQLServerException(org.talend.repository.nosql.exceptions.NoSQLServerException) NoSQLReflectionException(org.talend.repository.nosql.exceptions.NoSQLReflectionException)

Example 28 with NoSQLServerException

use of org.talend.repository.nosql.exceptions.NoSQLServerException in project tbd-studio-se by Talend.

the class CassandraOldVersionMetadataHandler method getColumnTalendType.

@Override
public String getColumnTalendType(Object column) throws NoSQLServerException {
    String talendType = null;
    try {
        String dbType = getColumnDbType(column);
        MappingTypeRetriever mappingTypeRetriever = MetadataTalendType.getMappingTypeRetriever(ICassandraConstants.DBM_ID);
        talendType = mappingTypeRetriever.getDefaultSelectedTalendType(dbType);
        if (talendType == null) {
            talendType = JavaTypesManager.STRING.getId();
        }
    } catch (Exception e) {
        throw new NoSQLServerException(e);
    }
    return talendType;
}
Also used : MappingTypeRetriever(org.talend.core.model.metadata.MappingTypeRetriever) NoSQLServerException(org.talend.repository.nosql.exceptions.NoSQLServerException) NoSQLServerException(org.talend.repository.nosql.exceptions.NoSQLServerException) NoSQLReflectionException(org.talend.repository.nosql.exceptions.NoSQLReflectionException)

Example 29 with NoSQLServerException

use of org.talend.repository.nosql.exceptions.NoSQLServerException in project tbd-studio-se by Talend.

the class MongoDBConnectionUtil method getMongo4ConnStringVersion.

public static synchronized Object getMongo4ConnStringVersion(NoSQLConnection connection, ContextType contextType, ClassLoader classLoader, boolean requireAuth, boolean requireEncryption) throws NoSQLServerException {
    Object mongo = null;
    String user = connection.getAttributes().get(IMongoDBAttributes.USERNAME);
    String pass = connection.getAttributes().get(IMongoDBAttributes.PASSWORD);
    if (contextType != null) {
        user = ContextParameterUtils.getOriginalValue(contextType, user);
        pass = ContextParameterUtils.getOriginalValue(contextType, pass);
    } else {
        pass = connection.getValue(pass, false);
    }
    try {
        String connString = extractFromContext(connection, IMongoDBAttributes.CONN_STRING);
        Object clientSettingsBuilder = // $NON-NLS-1$ //$NON-NLS-2$
        NoSQLReflection.invokeStaticMethod(// $NON-NLS-1$ //$NON-NLS-2$
        "com.mongodb.MongoClientSettings", // $NON-NLS-1$ //$NON-NLS-2$
        "builder", new Object[0], classLoader);
        Object connectionString = NoSQLReflection.newInstance("com.mongodb.ConnectionString", new Object[] { connString }, classLoader, String.class);
        clientSettingsBuilder = NoSQLReflection.invokeMethod(clientSettingsBuilder, "applyConnectionString", new Object[] { connectionString }, connectionString.getClass());
        Object mongoCredential = getCredential(connection, contextType, user, pass, classLoader);
        clientSettingsBuilder = NoSQLReflection.invokeMethod(clientSettingsBuilder, "credential", new Object[] { mongoCredential }, Class.forName("com.mongodb.MongoCredential", true, classLoader));
        String authMechanism = connection.getAttributes().get(IMongoDBAttributes.AUTHENTICATION_MECHANISM);
        // 
        if ((requireAuth && authMechanism.equals(IMongoConstants.X509)) || requireEncryption) {
            // enable ssl & context
            // $NON-NLS-1$
            Class<?> blockClasszz = Class.forName("com.mongodb.Block", false, classLoader);
            Class[] interfaces = new Class[1];
            interfaces[0] = blockClasszz;
            Object block = Proxy.newProxyInstance(classLoader, interfaces, (proxy, method, args) -> {
                switch(method.getName()) {
                    case // $NON-NLS-1$
                    "apply":
                        if (args[0] != null) {
                            SSLContext sslContext = null;
                            if (requireAuth && authMechanism.equals(IMongoConstants.X509)) {
                                sslContext = mongoX509SSLContext(connection);
                            } else if (requireEncryption) {
                                sslContext = StudioSSLContextProvider.getContext();
                            }
                            // $NON-NLS-1$
                            NoSQLReflection.invokeMethod(// $NON-NLS-1$
                            args[0], // $NON-NLS-1$
                            "context", // $NON-NLS-1$
                            new Object[] { sslContext }, // $NON-NLS-1$
                            SSLContext.class);
                        }
                        return null;
                    default:
                        throw new NoSQLServerException(// $NON-NLS-1$
                        Messages.getString("MongoDBConnectionUtil.CannotFindMethod", method.getName()));
                }
            });
            clientSettingsBuilder = NoSQLReflection.invokeMethod(clientSettingsBuilder, "applyToSslSettings", new Object[] { block }, blockClasszz);
            // $NON-NLS-1$
            blockClasszz = Class.forName("com.mongodb.Block", false, classLoader);
            interfaces = new Class[1];
            interfaces[0] = blockClasszz;
            block = Proxy.newProxyInstance(classLoader, interfaces, (proxy, method, args) -> {
                switch(method.getName()) {
                    case // $NON-NLS-1$
                    "apply":
                        if (args[0] != null) {
                            // $NON-NLS-1$
                            NoSQLReflection.invokeMethod(// $NON-NLS-1$
                            args[0], // $NON-NLS-1$
                            "enabled", // $NON-NLS-1$
                            new Object[] { true }, boolean.class);
                        }
                        return null;
                    default:
                        throw new Exception(// $NON-NLS-1$
                        "MongoDBConnectionUtil.CannotFindMethod" + method.getName());
                }
            });
            clientSettingsBuilder = NoSQLReflection.invokeMethod(clientSettingsBuilder, "applyToSslSettings", new Object[] { block }, blockClasszz);
        }
        Object mongoClientSettings = NoSQLReflection.invokeMethod(clientSettingsBuilder, "build", new Object[0]);
        mongo = // $NON-NLS-1$ //$NON-NLS-2$
        NoSQLReflection.invokeStaticMethod(// $NON-NLS-1$ //$NON-NLS-2$
        "com.mongodb.client.MongoClients", // $NON-NLS-1$ //$NON-NLS-2$
        "create", new Object[] { mongoClientSettings }, classLoader, // $NON-NLS-1$
        Class.forName("com.mongodb.MongoClientSettings", true, classLoader));
        mongos.add(mongo);
    } catch (Exception e) {
        throw new NoSQLServerException(e);
    }
    return mongo;
}
Also used : X509Certificate(java.security.cert.X509Certificate) SSLContext(javax.net.ssl.SSLContext) StringUtils(org.apache.commons.lang.StringUtils) ASN1Sequence(org.bouncycastle.asn1.ASN1Sequence) IPreferenceStore(org.eclipse.jface.preference.IPreferenceStore) CertificateFactory(java.security.cert.CertificateFactory) JSONObject(org.talend.utils.json.JSONObject) NoSQLClassLoaderFactory(org.talend.repository.nosql.factory.NoSQLClassLoaderFactory) TalendQuoteUtils(org.talend.core.utils.TalendQuoteUtils) SecureRandom(java.security.SecureRandom) Matcher(java.util.regex.Matcher) ByteArrayInputStream(java.io.ByteArrayInputStream) NoSQLReflectionException(org.talend.repository.nosql.exceptions.NoSQLReflectionException) Map(java.util.Map) IMongoConstants(org.talend.repository.nosql.db.common.mongodb.IMongoConstants) NoSQLReflection(org.talend.repository.nosql.reflection.NoSQLReflection) ASN1Object(org.bouncycastle.asn1.ASN1Object) ContextType(org.talend.designer.core.model.utils.emf.talendfile.ContextType) TrustManagerFactory(javax.net.ssl.TrustManagerFactory) ContextParameterUtils(org.talend.core.model.utils.ContextParameterUtils) Set(java.util.Set) KeyStore(java.security.KeyStore) NoSQLConnection(org.talend.repository.model.nosql.NoSQLConnection) KeyFactory(java.security.KeyFactory) Base64(java.util.Base64) List(java.util.List) Certificate(java.security.cert.Certificate) RSAPrivateKeyStructure(org.bouncycastle.asn1.pkcs.RSAPrivateKeyStructure) ConnectionContextHelper(org.talend.metadata.managment.ui.utils.ConnectionContextHelper) PrivateKey(java.security.PrivateKey) PluginUtil(org.talend.core.ui.utils.PluginUtil) Pattern(java.util.regex.Pattern) INoSQLCommonAttributes(org.talend.repository.nosql.constants.INoSQLCommonAttributes) Proxy(java.lang.reflect.Proxy) StudioEncryption(org.talend.utils.security.StudioEncryption) HashMap(java.util.HashMap) NoSQLServerException(org.talend.repository.nosql.exceptions.NoSQLServerException) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) StudioSSLContextProvider(org.talend.core.utils.StudioSSLContextProvider) ExceptionHandler(org.talend.commons.exception.ExceptionHandler) JSONArray(org.talend.utils.json.JSONArray) JSONException(org.talend.utils.json.JSONException) Properties(java.util.Properties) CoreRuntimePlugin(org.talend.core.runtime.CoreRuntimePlugin) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) Iterator(java.util.Iterator) Files(java.nio.file.Files) KeyManagerFactory(javax.net.ssl.KeyManagerFactory) Messages(org.talend.repository.nosql.i18n.Messages) FileInputStream(java.io.FileInputStream) File(java.io.File) KeyManager(javax.net.ssl.KeyManager) RSAPrivateKeySpec(java.security.spec.RSAPrivateKeySpec) Paths(java.nio.file.Paths) IMongoDBAttributes(org.talend.repository.nosql.db.common.mongodb.IMongoDBAttributes) SSLPreferenceConstants(org.talend.core.prefs.SSLPreferenceConstants) Collections(java.util.Collections) InputStream(java.io.InputStream) NoSQLServerException(org.talend.repository.nosql.exceptions.NoSQLServerException) JSONObject(org.talend.utils.json.JSONObject) ASN1Object(org.bouncycastle.asn1.ASN1Object) SSLContext(javax.net.ssl.SSLContext) NoSQLReflectionException(org.talend.repository.nosql.exceptions.NoSQLReflectionException) NoSQLServerException(org.talend.repository.nosql.exceptions.NoSQLServerException) JSONException(org.talend.utils.json.JSONException)

Example 30 with NoSQLServerException

use of org.talend.repository.nosql.exceptions.NoSQLServerException in project tbd-studio-se by Talend.

the class MongoDBConnectionUtil method getDB.

public static synchronized Object getDB(NoSQLConnection connection, String dbName, Object mongoClient) throws NoSQLServerException {
    Object db = null;
    if (StringUtils.isEmpty(dbName)) {
        return db;
    }
    try {
        String requireAuthAttr = connection.getAttributes().get(IMongoDBAttributes.REQUIRED_AUTHENTICATION);
        boolean requireAuth = requireAuthAttr == null ? false : Boolean.valueOf(requireAuthAttr);
        // 
        String requireEncryptionAttr = connection.getAttributes().get(IMongoDBAttributes.REQUIRED_ENCRYPTION);
        boolean requireEncryption = requireEncryptionAttr == null ? false : Boolean.valueOf(requireEncryptionAttr);
        updateConfigProperties(requireEncryption);
        if (isUpgradeLatestVersion(connection)) {
            if (mongoClient == null) {
                mongoClient = getMongo(connection, requireAuth, requireEncryption);
            }
            db = // $NON-NLS-1$
            NoSQLReflection.invokeMethod(// $NON-NLS-1$
            mongoClient, // $NON-NLS-1$
            "getDatabase", new Object[] { dbName });
        } else if (isUpgradeVersion(connection)) {
            if (mongoClient == null) {
                mongoClient = getMongo(connection, requireAuth, requireEncryption);
            }
            db = // $NON-NLS-1$
            NoSQLReflection.invokeMethod(// $NON-NLS-1$
            mongoClient, // $NON-NLS-1$
            "getDB", new Object[] { dbName });
        } else {
            if (mongoClient == null) {
                mongoClient = getMongo(connection);
            }
            // $NON-NLS-1$
            db = NoSQLReflection.invokeMethod(mongoClient, "getDB", new Object[] { dbName });
            // Do authenticate
            if (requireAuth) {
                String userName = connection.getAttributes().get(IMongoDBAttributes.USERNAME);
                String password = connection.getValue(connection.getAttributes().get(IMongoDBAttributes.PASSWORD), false);
                if (connection.isContextMode()) {
                    ContextType contextType = ConnectionContextHelper.getContextTypeForContextMode(connection);
                    userName = ContextParameterUtils.getOriginalValue(contextType, userName);
                    password = ContextParameterUtils.getOriginalValue(contextType, password);
                }
                if (userName != null && password != null) {
                    boolean authorized = (Boolean) NoSQLReflection.invokeMethod(db, "authenticate", // $NON-NLS-1$
                    new Object[] { userName, password.toCharArray() });
                    if (!authorized) {
                        // $NON-NLS-1$
                        throw new NoSQLServerException(Messages.getString("MongoDBConnectionUtil.ConnotLogon", dbName));
                    }
                }
            }
        }
    } catch (NoSQLReflectionException e) {
        throw new NoSQLServerException(e);
    }
    return db;
}
Also used : ContextType(org.talend.designer.core.model.utils.emf.talendfile.ContextType) NoSQLServerException(org.talend.repository.nosql.exceptions.NoSQLServerException) JSONObject(org.talend.utils.json.JSONObject) ASN1Object(org.bouncycastle.asn1.ASN1Object) NoSQLReflectionException(org.talend.repository.nosql.exceptions.NoSQLReflectionException)

Aggregations

NoSQLServerException (org.talend.repository.nosql.exceptions.NoSQLServerException)36 NoSQLReflectionException (org.talend.repository.nosql.exceptions.NoSQLReflectionException)33 ArrayList (java.util.ArrayList)13 ContextType (org.talend.designer.core.model.utils.emf.talendfile.ContextType)13 ASN1Object (org.bouncycastle.asn1.ASN1Object)9 JSONObject (org.talend.utils.json.JSONObject)9 List (java.util.List)7 JSONException (org.talend.utils.json.JSONException)6 HashSet (java.util.HashSet)5 HashMap (java.util.HashMap)4 Map (java.util.Map)4 SSLContext (javax.net.ssl.SSLContext)3 File (java.io.File)2 Collection (java.util.Collection)2 Iterator (java.util.Iterator)2 MappingTypeRetriever (org.talend.core.model.metadata.MappingTypeRetriever)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 FileInputStream (java.io.FileInputStream)1 InputStream (java.io.InputStream)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1