List of usage examples for javax.naming NamingException getMessage
public String getMessage()
From source file:org.wso2.carbon.ndatasource.core.DataSourceRepository.java
private void unregisterJNDI(DataSourceMetaInfo dsmInfo) { try {/*from www . java 2s. c om*/ PrivilegedCarbonContext.startTenantFlow(); PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantId(this.getTenantId()); JNDIConfig jndiConfig = dsmInfo.getJndiConfig(); if (jndiConfig == null) { return; } try { InitialContext context = new InitialContext(jndiConfig.extractHashtableEnv()); context.unbind(jndiConfig.getName()); } catch (NamingException e) { log.error("Error in unregistering JNDI name: " + jndiConfig.getName() + " - " + e.getMessage(), e); } } finally { PrivilegedCarbonContext.endTenantFlow(); } }
From source file:org.apache.directory.server.tools.commands.exportcmd.ExportCommandExecutor.java
/** * Gets and returns the entries from the server. * /* w ww .jav a 2 s . c o m*/ * @throws ToolCommandException * @throws NamingException */ public NamingEnumeration connectToServerAndGetEntries() throws ToolCommandException { // Connecting to the LDAP Server if (isDebugEnabled()) { notifyOutputListener("Connecting to LDAP server"); notifyOutputListener("Host: " + host); notifyOutputListener("Port: " + port); notifyOutputListener("User DN: " + user); notifyOutputListener("Base DN: " + baseDN); notifyOutputListener("Authentication: " + auth); } Hashtable env = new Hashtable(); env.put(Context.SECURITY_PRINCIPAL, user); env.put(Context.SECURITY_CREDENTIALS, password); env.put(Context.SECURITY_AUTHENTICATION, auth); env.put(Context.PROVIDER_URL, "ldap://" + host + ":" + port + "/" + baseDN); env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); DirContext ctx; try { ctx = new InitialDirContext(env); } catch (NamingException e) { throw new ToolCommandException("Could not connect to the server.\nError: " + e.getMessage()); } // Setting up search scope SearchControls ctls = new SearchControls(); ctls.setSearchScope(scope); // Fetching entries try { return ctx.search(exportPoint, "(objectClass=*)", ctls); } catch (NamingException e) { throw new ToolCommandException("Could not retreive entries"); } }
From source file:org.kitodo.production.services.data.LdapServerService.java
/** * Set next free uidNumber./*w w w . j a v a2s . c o m*/ */ private void setNextUidNumber(LdapServer ldapServer) { Hashtable<String, String> ldapEnvironment = initializeWithLdapConnectionSettings(ldapServer); DirContext ctx; try { ctx = new InitialDirContext(ldapEnvironment); Attributes attrs = ctx.getAttributes(ldapServer.getNextFreeUnixIdPattern()); Attribute la = attrs.get("uidNumber"); String oldValue = (String) la.get(0); int bla = Integer.parseInt(oldValue) + 1; BasicAttribute attrNeu = new BasicAttribute("uidNumber", String.valueOf(bla)); ModificationItem[] mods = new ModificationItem[1]; mods[0] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, attrNeu); ctx.modifyAttributes(ldapServer.getNextFreeUnixIdPattern(), mods); ctx.close(); } catch (NamingException e) { logger.error(e.getMessage(), e); } }
From source file:org.kitodo.production.services.data.LdapServerService.java
/** * Get next free uidNumber.//from w w w. j a v a 2 s .c o m * * @return next free uidNumber */ private String getNextUidNumber(LdapServer ldapServer) { Hashtable<String, String> ldapEnvironment = initializeWithLdapConnectionSettings(ldapServer); DirContext ctx; String rueckgabe = ""; try { ctx = new InitialDirContext(ldapEnvironment); Attributes attrs = ctx.getAttributes(ldapServer.getNextFreeUnixIdPattern()); Attribute la = attrs.get("uidNumber"); rueckgabe = (String) la.get(0); ctx.close(); } catch (NamingException e) { Helper.setErrorMessage(e.getMessage(), logger, e); } return rueckgabe; }
From source file:org.kitodo.production.services.data.LdapServerService.java
/** * Check if User already exists on system. * * @param user/* w ww .ja v a 2s . co m*/ * The User. * @return result as boolean */ public boolean isUserAlreadyExists(User user) { Hashtable<String, String> ldapEnvironment = initializeWithLdapConnectionSettings( user.getLdapGroup().getLdapServer()); DirContext ctx; boolean result = false; try { ctx = new InitialDirContext(ldapEnvironment); Attributes matchAttrs = new BasicAttributes(true); NamingEnumeration<SearchResult> answer = ctx.search(buildUserDN(user), matchAttrs); result = answer.hasMoreElements(); while (answer.hasMore()) { SearchResult sr = answer.next(); logger.debug(">>>{}", sr.getName()); Attributes attrs = sr.getAttributes(); String givenName = getStringForAttribute(attrs, "givenName"); String surName = getStringForAttribute(attrs, "sn"); String mail = getStringForAttribute(attrs, "mail"); String cn = getStringForAttribute(attrs, "cn"); String homeDirectory = getStringForAttribute(attrs, "homeDirectory"); logger.debug(givenName); logger.debug(surName); logger.debug(mail); logger.debug(cn); logger.debug(homeDirectory); } ctx.close(); } catch (NamingException e) { logger.error(e.getMessage(), e); } return result; }
From source file:org.kitodo.production.services.data.LdapServerService.java
/** * Retrieve home directory of given user. * * @param user//from ww w . jav a 2 s .co m * User object * @return path as URI */ public URI getUserHomeDirectory(User user) { String userFolderBasePath = ConfigCore.getParameter(ParameterCore.DIR_USERS); if (ConfigCore.getBooleanParameterOrDefaultValue(ParameterCore.LDAP_USE_LOCAL_DIRECTORY)) { return Paths.get(userFolderBasePath, user.getLogin()).toUri(); } Hashtable<String, String> env = initializeWithLdapConnectionSettings(user.getLdapGroup().getLdapServer()); if (ConfigCore.getBooleanParameterOrDefaultValue(ParameterCore.LDAP_USE_TLS)) { return getUserHomeDirectoryWithTLS(env, userFolderBasePath, user); } if (ConfigCore.getBooleanParameter(ParameterCore.LDAP_USE_SIMPLE_AUTH, false)) { env.put(Context.SECURITY_AUTHENTICATION, "none"); } DirContext ctx; URI userFolderPath = null; try { ctx = new InitialDirContext(env); Attributes attrs = ctx.getAttributes(buildUserDN(user)); Attribute ldapAttribute = attrs.get("homeDirectory"); userFolderPath = URI.create((String) ldapAttribute.get(0)); ctx.close(); } catch (NamingException e) { logger.error(e.getMessage(), e); } if (Objects.nonNull(userFolderPath) && !userFolderPath.isAbsolute()) { if (userFolderPath.getPath().startsWith("/")) { userFolderPath = ServiceManager.getFileService().deleteFirstSlashFromPath(userFolderPath); } return Paths.get(userFolderBasePath, userFolderPath.getRawPath()).toUri(); } else { return userFolderPath; } }
From source file:org.wso2.carbon.datasource.core.DataSourceRepository.java
private void checkAndCreateJNDISubContexts(Context context, String jndiName) throws DataSourceException { String[] tokens = jndiName.split("/"); Context tmpCtx;/*from w w w . j a v a 2s . com*/ String token; for (int i = 0; i < tokens.length - 1; i++) { token = tokens[i]; tmpCtx = (Context) this.lookupJNDISubContext(context, token); if (tmpCtx == null) { try { tmpCtx = context.createSubcontext(token); } catch (NamingException e) { throw new DataSourceException( "Error in creating JNDI subcontext '" + context + "/" + token + ": " + e.getMessage(), e); } } context = tmpCtx; } }
From source file:edu.internet2.middleware.subject.provider.LdapSourceAdapter.java
private Subject createSubject(Attributes attributes) { String name = ""; String subjectID = ""; String description = ""; if (attributes == null) { log.debug("ldap create subject with null attrs"); return (null); }// www. j ava2 s . co m try { Attribute attribute = attributes.get(subjectIDAttributeName); if (attribute == null) { log.error("No value for LDAP attribute \"" + subjectIDAttributeName + "\". It is Grouper attribute \"SubjectID\"."); return null; } subjectID = ((String) attribute.get()).toLowerCase(); attribute = attributes.get(nameAttributeName); if (attribute == null) { log.debug("No immedaite value for attribute \"" + nameAttributeName + "\". Will look later."); } else { name = (String) attribute.get(); } attribute = attributes.get(descriptionAttributeName); if (attribute == null) { log.debug( "No immedaite value for attribute \"" + descriptionAttributeName + "\". Will look later."); } else { description = (String) attribute.get(); } } catch (NamingException ex) { log.error("LDAP Naming Except: " + ex.getMessage(), ex); } LdapSubject subject = new LdapSubject(subjectID, name, description, this.getSubjectType().getName(), this.getId()); // add the attributes Map myAttributes = new HashMap(); try { for (NamingEnumeration e = attributes.getAll(); e.hasMore();) { Attribute attr = (Attribute) e.next(); String attrName = attr.getID(); // skip the basic ones if (attrName.equals(nameAttributeName)) continue; if (attrName.equals(subjectIDAttributeName)) continue; if (attrName.equals(descriptionAttributeName)) continue; Set values = new HashSet(); for (NamingEnumeration en = attr.getAll(); en.hasMore();) { Object value = en.next(); values.add(value.toString()); } myAttributes.put(attrName, values); } subject.setAttributes(myAttributes); } catch (NamingException e) { log.error("Naming error: " + e); } return subject; }
From source file:edu.internet2.middleware.subject.provider.LdapSourceAdapter.java
protected Iterator<SearchResult> getLdapResults(Search search, String searchValue, String[] attributeNames) { Ldap ldap = null;// ww w . j a v a 2 s .c o m String filter = null; Iterator<SearchResult> results = null; int cp; String aff = null; if (!initialized) initializeLdap(); if ((cp = searchValue.indexOf(',')) > 0) { int lb, rb; if ((lb = searchValue.indexOf('[')) > cp && (rb = searchValue.indexOf(']')) > lb) { aff = searchValue.substring(lb + 1, rb); searchValue = searchValue.substring(0, lb); // log.debug("first, last [" + aff + "] search: " + searchValue); filter = search.getParam("affiliationfilter"); } else { // log.debug("first, last search: " + searchValue); filter = search.getParam("firstlastfilter"); } if (filter == null) { log.error("Search filter not found for search type: " + search.getSearchType()); return results; } String last = searchValue.substring(0, cp); String first = searchValue.substring(cp + 1); if (last != null) filter = filter.replaceAll("%LAST%", escapeSearchFilter(last)); if (first != null) filter = filter.replaceAll("%FIRST%", escapeSearchFilter(first)); if (aff != null) filter = filter.replaceAll("%AFFILIATION%", escapeSearchFilter(aff)); } else { // simple search filter = search.getParam("filter"); if (filter == null) { log.error("Search filter not found for search type: " + search.getSearchType()); return results; } filter = filter.replaceAll("%TERM%", escapeSearchFilter(searchValue)); } log.debug("searchType: " + search.getSearchType() + " filter: " + filter); try { ldap = (Ldap) ldapPool.checkOut(); results = ldap.search(new SearchFilter(filter), attributeNames); } catch (NamingException ex) { log.error("Ldap NamingException: " + ex.getMessage(), ex); } catch (Exception ex) { log.error("Ldap Exception: " + ex.getMessage(), ex); } finally { if (ldap != null) { try { ldapPool.checkIn(ldap); } catch (Exception e) { log.error("Could not return Ldap object back to pool", e); } } } return results; }
From source file:ru.efo.security.ADUserDetailsService.java
@Override public ADUserDetails loadUserByUsername(String username) throws UsernameNotFoundException { DirContext context = null;/*from w w w . ja v a 2s .c om*/ try { context = getDirContext(ldapAccount + userSuffix, ldapPassword); logger.log(Level.FINE, "Successfully logged on " + ldapUrl); return loadUserByUsername(context, username, null); } catch (NamingException ex) { logger.log(Level.SEVERE, "Could not login to " + ldapUrl, ex); throw new UsernameNotFoundException(ex.getMessage()); } finally { if (context != null) { try { context.close(); } catch (NamingException ex) { logger.log(Level.WARNING, "Could not close DirContext", ex); } } } }