⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jahiausermanagerldapprovider.java

📁 java 写的一个新闻发布系统
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
            }            if (publicCtx == null) {                JahiaConsole.println("JahiaUserManagerLDAPProvider.checkPublicCtx",                                     "reconnect failed, returning null context...");                // we've tried everything, still can't connect...                return null;            }        }        return publicCtx;    }    private void invalidateCtx(DirContext ctx) {        if (ctx == null) {            JahiaConsole.println("JahiaUserManagerLDAPProvider.invalidateCtx",                                 "Context passed is null, ignoring it...");            return;        }        try {            ctx.close();        } catch (Exception e) {            JahiaConsole.printe("JahiaUSerManagerLDAPProvider.invalidateCtx", e);        }    }    private void invalidatePublicCtx() {        invalidateCtx(publicCtx);        publicCtx = null;    }    private DirContext connectToPublicDir()    throws NamingException {        // Identify service provider to use        JahiaConsole.println("JahiaUserManagerLDAPProvider.connectToPublicDir",                             "Attempting connection to LDAP repository on " +                             ldapProperties.getProperty(LDAP_URL_PROP) + "..." );        Hashtable publicEnv = new Hashtable(11);        publicEnv.put(Context.INITIAL_CONTEXT_FACTORY,                 ldapProperties.getProperty(CONTEXT_FACTORY_PROP));        publicEnv.put(Context.PROVIDER_URL,                     ldapProperties.getProperty(LDAP_URL_PROP));        publicEnv.put(Context.SECURITY_AUTHENTICATION,                     ldapProperties.getProperty(AUTHENTIFICATION_MODE_PROP));        publicEnv.put(Context.SECURITY_PRINCIPAL,                     ldapProperties.getProperty(PUBLIC_BIND_DN_PROP));        if (ldapProperties.getProperty(PUBLIC_BIND_PASSWORD_PROP) != null) {            JahiaConsole.println("JahiaUserManagerLDAPProvider.connectToPublicDir",                                 "Using authentification mode to connect to public dir...");            publicEnv.put(Context.SECURITY_CREDENTIALS,                          ldapProperties.getProperty(PUBLIC_BIND_PASSWORD_PROP));        }        DirContext ctx = null;        // Create the initial directory context        ctx = new InitialDirContext(publicEnv);        if (ctx != null) {            this.connectedToPublic = true;        }        return ctx;    }    private DirContext connectToPrivateDir(String personName, String personPassword)    throws NamingException {        // Identify service provider to use        Hashtable privateEnv = new Hashtable(11);        privateEnv.put(Context.INITIAL_CONTEXT_FACTORY,                      ldapProperties.getProperty(CONTEXT_FACTORY_PROP));        privateEnv.put(Context.PROVIDER_URL,                      ldapProperties.getProperty(LDAP_URL_PROP));        privateEnv.put(Context.SECURITY_AUTHENTICATION,                      ldapProperties.getProperty(AUTHENTIFICATION_MODE_PROP));        privateEnv.put(Context.SECURITY_PRINCIPAL,                      personName+","+                      ldapProperties.getProperty(UID_SEARCH_NAME_PROP));        privateEnv.put(Context.SECURITY_CREDENTIALS,                      personPassword);        // Create the initial directory context        DirContext ctx = new InitialDirContext(privateEnv);        return ctx;    }    private DirContext disconnectDir(DirContext ctx)    throws NamingException {        if (ctx == null) {            JahiaConsole.println("JahiaUserManagerLDAPProvider.disconnectDir",                                 "Context is already null, ignoring it...");        }        ctx.close();        return null;    }    /**     * Retrieves users from the LDAP public repository.     * @param ctx the current context in which to search for the user     * @param filters a set of name=value string that contain RFC 2254 format     * filters in the value, or null if we want to look in the full repository     * @return NamingEnumeration a naming enumeration of SearchResult objects     * that contains the LDAP user entries that correspond to the filter     * @throws NamingException     */    private NamingEnumeration getUsers(DirContext ctx, Properties filters)    throws NamingException {        if (ctx == null) {            throw new NamingException("Context is null !");        }        String personName = null;        StringBuffer filterString = new StringBuffer();        if (filters == null) {            filters = new Properties();            filters.setProperty("objectClass", "*");        }        if (filters != null) {            // let's translate Jahia properties to LDAP properties            mapJahiaPropertiesToLDAP(filters);            if (filters.size() > 0) {                if (filters.size() > 1) {                    filterString.append("(&");                }                Enumeration filterKeys = filters.keys();                while (filterKeys.hasMoreElements()) {                    String filterName = (String) filterKeys.nextElement();                    String filterValue = filters.getProperty(filterName);                    // we do all the RFC 2254 replacement *except* the "*" character                    // since this is actually something we want to use.                    filterValue = JahiaTools.replacePattern(filterValue, "\\", "\\5c");                    filterValue = JahiaTools.replacePattern(filterValue, "(", "\\28");                    filterValue = JahiaTools.replacePattern(filterValue, ")", "\\29");                    if ("*".equals(filterName)) {                        // we must match the value for all the attributes                        // declared in the property file.                        if (this.searchWildCardAttributeList != null) {                            if (this.searchWildCardAttributeList.size()>1) {                                filterString.append("(|");                            }                            Enumeration attributeEnum = this.searchWildCardAttributeList.elements();                            while (attributeEnum.hasMoreElements()) {                                String curAttributeName = (String) attributeEnum.nextElement();                                filterString.append("(");                                filterString.append(curAttributeName);                                filterString.append("=");                                filterString.append(filterValue);                                filterString.append(")");                            }                            if (this.searchWildCardAttributeList.size()>1) {                                filterString.append(")");                            }                        }                    } else {                        filterString.append("(");                        filterString.append(filterName);                        filterString.append("=");                        filterString.append(filterValue);                        filterString.append(")");                    }                }                if (filters.size() > 1) {                    filterString.append(")");                }            }        } else {        }        // Search for objects that have those matching attributes        SearchControls searchCtl = new SearchControls();        searchCtl.setSearchScope(SearchControls.SUBTREE_SCOPE);        int countLimit = Integer.parseInt(ldapProperties.getProperty(this.SEARCH_COUNT_LIMIT_PROP));        searchCtl.setCountLimit(countLimit);        JahiaConsole.println("JahiaUserManagerLDAPProvider.getUsers",                             "Using filter string [" + filterString.toString() + "]...");        NamingEnumeration answer = ctx.search(ldapProperties.getProperty(UID_SEARCH_NAME_PROP),                                              filterString.toString(),                                              searchCtl);        return answer;    }    /**     * Retrieves a user from the LDAP public repository.     * @param ctx the current context in which to search for the user     * @param uid the unique identifier for the user     * @return a SearchResult object, which is the *first* result matching the     * uid     * @throws NamingException     */    private SearchResult getPublicUser(DirContext ctx, String uid)    throws NamingException {        Properties filters = new Properties();        filters.setProperty(ldapProperties.getProperty(UID_SEARCH_ATTRIBUTE_PROP), uid);        NamingEnumeration answer = getUsers(ctx, filters);        SearchResult sr = null;        if (answer.hasMore()) {            // we only take the first value if there are multiple answers, which            // should normally NOT happend if the uid is unique !!            sr = (SearchResult)answer.next();            if (answer.hasMore()) {                // there is at least a second result.                // throw new NamingException("UserLDAPService.getPublicUser>" +                //                           "Warning : multiple users with same UID in LDAP repository.");                JahiaConsole.println("UserLDAPService.getPublicUser",                                     "Warning : multiple users with same UID in LDAP repository.");            }        }        return sr;    }    private String findNamebyUID(DirContext ctx, String uid)    throws NamingException {        String personName = null;        SearchResult sr = getPublicUser(ctx, uid);        if (sr == null)          return null;        Attributes attrs = sr.getAttributes();        // personName = (String) attrs.get(ldapProperties.getProperty(DN_IDENTIFIER_ATTRIBUTE_PROP)).get();        personName = sr.getName();        return personName;    }// FIXME : These following methods are temporary. It is a stupid cut and paste// from JahiaUserManagerDBProvider.java file. They process the LDAP user// properties from/into Jahia DB.    /**     * Retrieves properties from internal jahia DB     * @param userProps the user properties to set     * @param usingUserKey the user whose the properties has to be extracted.     */    private void mapDBToJahiaProperties(Properties userProps, String usingUserKey) {        ServicesRegistry registry = ServicesRegistry.getInstance();        if (registry != null) {            mDBPoolService = registry.getDBPoolService();            if (mDBPoolService == null) {                return;            }        }        Connection  dbConn = getDBConnection (1002);        Statement statement = null;        try        {            statement = dbConn.createStatement();            // Get all the user attributes            String query = "SELECT name_jahia_user_prop, value_jahia_user_prop " +                           "FROM jahia_user_prop " +                           "WHERE id_jahia_users=-1 AND name_jahia_user_prop='{ldap}" + usingUserKey + ":user_homepage'";            ResultSet rs = statement.executeQuery (query);            if (rs != null) {                String propName = null;                String propVal = null;                while (rs.next()) {                    propName = rs.getString("name_jahia_user_prop");                    propVal = rs.getString ("value_jahia_user_prop");                    if ( propVal == null ){                        propVal = "";                    }                    if ( propName != null ){                        userProps.put (propName, propVal);                    }                }            }        }        catch (SQLException ex)        {            toConsole ("SQL Exception occured : Could not read the user ["+usingUserKey+                       "] from the database for site LDAP user");            // FIXME -Fulco- : Don't know yet what to do with this exception.            //                 It should be logged somewhere !        }        finally        {            closeDBConnection (dbConn);            closeStatement (statement);        }    }    //-------------------------------------------------------------------------    private Connection getDBConnection (int debugInfo)    {        Connection dbConn = null;        try {            dbConn = mDBPoolService.getConnection (debugInfo);        }        catch (NullPointerException ex) {            toConsole ("Null Pointer Exception, DB Pool Service instance might be null!");        }        catch (SQLException ex) {            toConsole ("SQL Exception: cannot get a connection.");        }        return dbConn;    }    //-------------------------------------------------------------------------    private void closeDBConnection (Connection dbConn)    {        if ((mDBPoolService != null) && (dbConn != null)) {            try {                mDBPoolService.freeConnection (dbConn);            }            catch (SQLException sqlEx) {                // FIXME -Fulco- : Don't know yet what to do with this exception.                //                 It should be logged somewhere !            }        }    }    //-------------------------------------------------------------------------    private void closeStatement (Statement statement)    {        // Close the opened statement        try {            if (statement!=null) {                statement.close();            }        }        catch (SQLException sqlEx) {            // FIXME -Fulco- : Don't know yet what to do with this exception.            //                 It should be logged somewhere !        }    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -