Java Code Examples for javax.naming.NamingEnumeration.next()

The following are Jave code examples for showing how to use next() of the javax.naming.NamingEnumeration class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: Android_Code_Arbiter   File: JndiLdap.java   View Source Code Vote up 7 votes
private static String dnFromUser(String username) throws NamingException {
    Properties props = new Properties();
    props.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    props.put(Context.PROVIDER_URL, "ldap://ldap.example.com");
    props.put(Context.REFERRAL, "ignore");

    InitialDirContext context = new InitialDirContext(props);

    SearchControls ctrls = new SearchControls();
    ctrls.setReturningAttributes(new String[]{"givenName", "sn"});
    ctrls.setSearchScope(SearchControls.SUBTREE_SCOPE);

    NamingEnumeration<SearchResult> answers = context.search("dc=People,dc=example,dc=com", "(uid=" + username + ")", ctrls);
    SearchResult result = answers.next();

    return result.getNameInNamespace();
}
 
Example 2
Project: pooled-jms   File: JmsPoolXAConnectionFactory.java   View Source Code Vote up 6 votes
private void configFromJndiConf(Object rootContextName) {
    if (rootContextName instanceof String) {
        String name = (String) rootContextName;
        name = name.substring(0, name.lastIndexOf('/')) + "/conf" + name.substring(name.lastIndexOf('/'));
        try {
            InitialContext ctx = new InitialContext();
            NamingEnumeration<Binding> bindings = ctx.listBindings(name);

            while (bindings.hasMore()) {
                Binding bd = bindings.next();
                IntrospectionSupport.setProperty(this, bd.getName(), bd.getObject());
            }

        } catch (Exception ignored) {
            if (LOG.isTraceEnabled()) {
                LOG.trace("exception on config from jndi: " + name, ignored);
            }
        }
    }
}
 
Example 3
Project: openjdk-jdk10   File: LDAPCertStoreImpl.java   View Source Code Vote up 6 votes
/**
 * Get the values for the given attribute. If the attribute is null
 * or does not contain any values, a zero length byte array is
 * returned. NOTE that it is assumed that all values are byte arrays.
 */
private byte[][] getAttributeValues(Attribute attr)
        throws NamingException {
    byte[][] values;
    if (attr == null) {
        values = BB0;
    } else {
        values = new byte[attr.size()][];
        int i = 0;
        NamingEnumeration<?> enum_ = attr.getAll();
        while (enum_.hasMore()) {
            Object obj = enum_.next();
            if (debug != null) {
                if (obj instanceof String) {
                    debug.println("LDAPCertStore.getAttrValues() "
                        + "enum.next is a string!: " + obj);
                }
            }
            byte[] value = (byte[])obj;
            values[i++] = value;
        }
    }
    return values;
}
 
Example 4
Project: jdk8u-jdk   File: LDAPCertStore.java   View Source Code Vote up 6 votes
/**
 * Get the values for the given attribute. If the attribute is null
 * or does not contain any values, a zero length byte array is
 * returned. NOTE that it is assumed that all values are byte arrays.
 */
private byte[][] getAttributeValues(Attribute attr)
        throws NamingException {
    byte[][] values;
    if (attr == null) {
        values = BB0;
    } else {
        values = new byte[attr.size()][];
        int i = 0;
        NamingEnumeration<?> enum_ = attr.getAll();
        while (enum_.hasMore()) {
            Object obj = enum_.next();
            if (debug != null) {
                if (obj instanceof String) {
                    debug.println("LDAPCertStore.getAttrValues() "
                        + "enum.next is a string!: " + obj);
                }
            }
            byte[] value = (byte[])obj;
            values[i++] = value;
        }
    }
    return values;
}
 
Example 5
Project: openjdk-jdk10   File: Rdn.java   View Source Code Vote up 6 votes
/**
 * Constructs an Rdn from the given attribute set. See
 * {@link javax.naming.directory.Attributes Attributes}.
 * <p>
 * The string attribute values are not interpreted as
 * <a href="http://www.ietf.org/rfc/rfc2253.txt">RFC 2253</a>
 * formatted RDN strings. That is, the values are used
 * literally (not parsed) and assumed to be unescaped.
 *
 * @param attrSet The non-null and non-empty attributes containing
 * type/value mappings.
 * @throws InvalidNameException If contents of {@code attrSet} cannot
 *          be used to construct a valid RDN.
 */
public Rdn(Attributes attrSet) throws InvalidNameException {
    if (attrSet.size() == 0) {
        throw new InvalidNameException("Attributes cannot be empty");
    }
    entries = new ArrayList<>(attrSet.size());
    NamingEnumeration<? extends Attribute> attrs = attrSet.getAll();
    try {
        for (int nEntries = 0; attrs.hasMore(); nEntries++) {
            RdnEntry entry = new RdnEntry();
            Attribute attr = attrs.next();
            entry.type = attr.getID();
            entry.value = attr.get();
            entries.add(nEntries, entry);
        }
    } catch (NamingException e) {
        InvalidNameException e2 = new InvalidNameException(
                                    e.getMessage());
        e2.initCause(e);
        throw e2;
    }
    sort(); // arrange entries for comparison
}
 
Example 6
Project: lams   File: LdapService.java   View Source Code Vote up 6 votes
private List<String> getAttributeStrings(Attribute attr) {
try {
    ArrayList<String> attrValues = new ArrayList<String>();
    if (attr != null) {
	NamingEnumeration attrEnum = attr.getAll();
	while (attrEnum.hasMore()) {
	    Object attrValue = attrEnum.next();
	    if (attrValue != null) {
		attrValues.add(attrValue.toString());
	    }
	}
	return attrValues;
    }
} catch (NamingException e) {
    log.error("===> Naming exception occurred: " + e.getMessage());
}
return null;
   }
 
Example 7
Project: jerrydog   File: ManagerServlet.java   View Source Code Vote up 5 votes
/**
 * List the resources of the given context.
 */
protected void printResources(PrintWriter writer, String prefix,
                              javax.naming.Context namingContext,
                              String type, Class clazz) {

    try {
        NamingEnumeration items = namingContext.listBindings("");
        while (items.hasMore()) {
            Binding item = (Binding) items.next();
            if (item.getObject() instanceof javax.naming.Context) {
                printResources
                    (writer, prefix + item.getName() + "/",
                     (javax.naming.Context) item.getObject(), type, clazz);
            } else {
                if ((clazz != null) &&
                    (!(clazz.isInstance(item.getObject())))) {
                    continue;
                }
                writer.print(prefix + item.getName());
                writer.print(':');
                writer.print(item.getClassName());
                // Do we want a description if available?
                writer.println();
            }
        }
    } catch (Throwable t) {
        log("ManagerServlet.resources[" + type + "]", t);
        writer.println(sm.getString("managerServlet.exception",
                                    t.toString()));
    }

}
 
Example 8
Project: tomcat7   File: ManagerServlet.java   View Source Code Vote up 5 votes
/**
 * List the resources of the given context.
 */
protected void printResources(PrintWriter writer, String prefix,
                              javax.naming.Context namingContext,
                              String type, Class<?> clazz,
                              StringManager smClient) {

    try {
        NamingEnumeration<Binding> items = namingContext.listBindings("");
        while (items.hasMore()) {
            Binding item = items.next();
            if (item.getObject() instanceof javax.naming.Context) {
                printResources
                    (writer, prefix + item.getName() + "/",
                     (javax.naming.Context) item.getObject(), type, clazz,
                     smClient);
            } else {
                if ((clazz != null) &&
                    (!(clazz.isInstance(item.getObject())))) {
                    continue;
                }
                writer.print(prefix + item.getName());
                writer.print(':');
                writer.print(item.getClassName());
                // Do we want a description if available?
                writer.println();
            }
        }
    } catch (Throwable t) {
        ExceptionUtils.handleThrowable(t);
        log("ManagerServlet.resources[" + type + "]", t);
        writer.println(smClient.getString("managerServlet.exception",
                t.toString()));
    }

}
 
Example 9
Project: tomcat7   File: JNDIRealm.java   View Source Code Vote up 5 votes
/**
 * Add values of a specified attribute to a list
 *
 * @param attrId Attribute name
 * @param attrs Attributes containing the new values
 * @param values ArrayList containing values found so far
 *
 * @exception NamingException if a directory server error occurs
 */
private ArrayList<String> addAttributeValues(String attrId,
                                     Attributes attrs,
                                     ArrayList<String> values)
    throws NamingException{

    if (containerLog.isTraceEnabled())
        containerLog.trace("  retrieving values for attribute " + attrId);
    if (attrId == null || attrs == null)
        return values;
    if (values == null)
        values = new ArrayList<String>();
    Attribute attr = attrs.get(attrId);
    if (attr == null)
        return values;
    NamingEnumeration<?> e = attr.getAll();
    try {
        while(e.hasMore()) {
            String value = (String)e.next();
            values.add(value);
        }
    } catch (PartialResultException ex) {
        if (!adCompat)
            throw ex;
    } finally {
        e.close();
    }
    return values;
}
 
Example 10
Project: uavstack   File: GUISSOLdapClient.java   View Source Code Vote up 5 votes
@SuppressWarnings("rawtypes")
private Map<String, String> formatEmailInfo(SearchResult sResult, String targetKey) {

    if (null == sResult) {
        return Collections.emptyMap();
    }

    Map<String, String> result = new LinkedHashMap<String, String>();
    try {
        NamingEnumeration namingEnumeration = sResult.getAttributes().getAll();
        while (namingEnumeration.hasMoreElements()) {
            Attribute attr = (Attribute) namingEnumeration.next();
            String attrId = attr.getID();
            String attrValue = attr.getAll().next().toString();
            if (targetKey.equals(attrId)) {
                result.put("email", attrValue);
            }
            if ("cn".equals(attrId)) {
                result.put("name", attrValue);
            }
        }

    }
    catch (Exception e) {
        loggerError("formatEmailInfo 591", "", e);
    }

    return result;
}
 
Example 11
Project: tomcat7   File: DirContextURLConnection.java   View Source Code Vote up 5 votes
/**
 * Returns the name of the specified header field.
 */
@Override
public String getHeaderField(String name) {

    if (!connected) {
        // Try to connect (silently)
        try {
            connect();
        } catch (IOException e) {
            // Ignore
        }
    }

    if (attributes == null)
        return (null);

    NamingEnumeration<String> attributeEnum = attributes.getIDs();
    try {
        while (attributeEnum.hasMore()) {
            String attributeID = attributeEnum.next();
            if (attributeID.equalsIgnoreCase(name)) {
                Attribute attribute = attributes.get(attributeID);
                if (attribute == null) return null;
                Object attrValue = attribute.get(attribute.size()-1);
                return getHeaderValueAsString(attrValue);
            }
        }
    } catch (NamingException ne) {
        // Shouldn't happen
    }

    return (null);

}
 
Example 12
Project: lazycat   File: DirContextURLConnection.java   View Source Code Vote up 5 votes
/**
 * Returns the name of the specified header field.
 */
@Override
public String getHeaderField(String name) {

	if (!connected) {
		// Try to connect (silently)
		try {
			connect();
		} catch (IOException e) {
			// Ignore
		}
	}

	if (attributes == null)
		return (null);

	NamingEnumeration<String> attributeEnum = attributes.getIDs();
	try {
		while (attributeEnum.hasMore()) {
			String attributeID = attributeEnum.next();
			if (attributeID.equalsIgnoreCase(name)) {
				Attribute attribute = attributes.get(attributeID);
				if (attribute == null)
					return null;
				Object attrValue = attribute.get(attribute.size() - 1);
				return getHeaderValueAsString(attrValue);
			}
		}
	} catch (NamingException ne) {
		// Shouldn't happen
	}

	return (null);

}
 
Example 13
Project: sdirobot   File: CManagerMap.java   View Source Code Vote up 5 votes
public static boolean enumerate(Context _ctx, NamingEnumeration e, String string,String __dbtype) throws NamingException {
    while (e.hasMore()) {
        Binding binding = (Binding) e.next();
        Common.debugingLine2D("DataSource binding Name: " + binding.getName());
        // System.out.println("Type: " + binding.getClassName());
        //  System.out.println("Value: " + binding.getObject());

        if(binding.getName().endsWith(__dbtype))
        {
        DataSource _ds1 = (DataSource) _ctx.lookup("jdbc/" + binding.getName());
        addDs(binding.getName(), _ds1);
        }
    }
    return !_m_conn.isEmpty();
}
 
Example 14
Project: parabuild-ci   File: ManagerServlet.java   View Source Code Vote up 5 votes
/**
 * List the resources of the given context.
 */
protected void printResources(final PrintWriter writer, final String prefix,
                              final javax.naming.Context namingContext,
                              final String type, final Class clazz) {

  try {
    final NamingEnumeration items = namingContext.listBindings("");
    while (items.hasMore()) {
      final Binding item = (Binding) items.next();
      if (item.getObject() instanceof javax.naming.Context) {
        printResources
                (writer, prefix + item.getName() + '/',
                        (javax.naming.Context) item.getObject(), type, clazz);
      } else {
        if (clazz != null &&
                !clazz.isInstance(item.getObject())) {
          continue;
        }
        writer.print(prefix + item.getName());
        writer.print(':');
        writer.print(item.getClassName());
        // Do we want a description if available?
        writer.println();
      }
    }
  } catch (Throwable t) {
    log("ManagerServlet.resources[" + type + ']', t);
    writer.println(sm.getString("managerServlet.exception",
            t.toString()));
  }

}
 
Example 15
Project: apache-tomcat-7.0.73-with-comment   File: JNDIRealm.java   View Source Code Vote up 5 votes
/**
 * Add values of a specified attribute to a list
 *
 * @param attrId Attribute name
 * @param attrs Attributes containing the new values
 * @param values ArrayList containing values found so far
 *
 * @exception NamingException if a directory server error occurs
 */
private ArrayList<String> addAttributeValues(String attrId,
                                     Attributes attrs,
                                     ArrayList<String> values)
    throws NamingException{

    if (containerLog.isTraceEnabled())
        containerLog.trace("  retrieving values for attribute " + attrId);
    if (attrId == null || attrs == null)
        return values;
    if (values == null)
        values = new ArrayList<String>();
    Attribute attr = attrs.get(attrId);
    if (attr == null)
        return values;
    NamingEnumeration<?> e = attr.getAll();
    try {
        while(e.hasMore()) {
            String value = (String)e.next();
            values.add(value);
        }
    } catch (PartialResultException ex) {
        if (!adCompat)
            throw ex;
    } finally {
        e.close();
    }
    return values;
}
 
Example 16
Project: OpenJSharp   File: BasicAttributes.java   View Source Code Vote up 5 votes
/**
 * Determines whether this <tt>BasicAttributes</tt> is equal to another
 * <tt>Attributes</tt>
 * Two <tt>Attributes</tt> are equal if they are both instances of
 * <tt>Attributes</tt>,
 * treat the case of attribute IDs the same way, and contain the
 * same attributes. Each <tt>Attribute</tt> in this <tt>BasicAttributes</tt>
 * is checked for equality using <tt>Object.equals()</tt>, which may have
 * be overridden by implementations of <tt>Attribute</tt>).
 * If a subclass overrides <tt>equals()</tt>,
 * it should override <tt>hashCode()</tt>
 * as well so that two <tt>Attributes</tt> instances that are equal
 * have the same hash code.
 * @param obj the possibly null object to compare against.
 *
 * @return true If obj is equal to this BasicAttributes.
 * @see #hashCode
 */
public boolean equals(Object obj) {
    if ((obj != null) && (obj instanceof Attributes)) {
        Attributes target = (Attributes)obj;

        // Check case first
        if (ignoreCase != target.isCaseIgnored()) {
            return false;
        }

        if (size() == target.size()) {
            Attribute their, mine;
            try {
                NamingEnumeration<?> theirs = target.getAll();
                while (theirs.hasMore()) {
                    their = (Attribute)theirs.next();
                    mine = get(their.getID());
                    if (!their.equals(mine)) {
                        return false;
                    }
                }
            } catch (NamingException e) {
                return false;
            }
            return true;
        }
    }
    return false;
}
 
Example 17
Project: lams   File: JNDIRealm.java   View Source Code Vote up 5 votes
/**
 * Add values of a specified attribute to a list
 *
 * @param attrId Attribute name
 * @param attrs Attributes containing the new values
 * @param values ArrayList containing values found so far
 *
 * @exception NamingException if a directory server error occurs
 */
private ArrayList<String> addAttributeValues(String attrId,
                                     Attributes attrs,
                                     ArrayList<String> values)
    throws NamingException{

    if (containerLog.isTraceEnabled())
        containerLog.trace("  retrieving values for attribute " + attrId);
    if (attrId == null || attrs == null)
        return values;
    if (values == null)
        values = new ArrayList<String>();
    Attribute attr = attrs.get(attrId);
    if (attr == null)
        return (values);
    NamingEnumeration e = attr.getAll();
    try {
        while(e.hasMore()) {
            String value = (String)e.next();
            values.add(value);
        }
    } catch (PartialResultException ex) {
        if (!adCompat)
            throw ex;
    }
    return values;
}
 
Example 18
Project: lams   File: DirContextURLConnection.java   View Source Code Vote up 5 votes
/**
 * Returns an unmodifiable Map of the header fields.
 */
public Map getHeaderFields() {

  if (!connected) {
      // Try to connect (silently)
      try {
          connect();
      } catch (IOException e) {
      }
  }

  if (attributes == null)
      return (Collections.EMPTY_MAP);

  HashMap headerFields = new HashMap(attributes.size());
  NamingEnumeration attributeEnum = attributes.getIDs();
  try {
      while (attributeEnum.hasMore()) {
          String attributeID = (String)attributeEnum.next();
          Attribute attribute = attributes.get(attributeID);
          if (attribute == null) continue;
          ArrayList attributeValueList = new ArrayList(attribute.size());
          NamingEnumeration attributeValues = attribute.getAll();
          while (attributeValues.hasMore()) {
              Object attrValue = attributeValues.next();
              attributeValueList.add(getHeaderValueAsString(attrValue));
          }
          attributeValueList.trimToSize(); // should be a no-op if attribute.size() didn't lie
          headerFields.put(attributeID, Collections.unmodifiableList(attributeValueList));
      }
  } catch (NamingException ne) {
        // Shouldn't happen
  }

  return Collections.unmodifiableMap(headerFields);

}
 
Example 19
Project: jdk8u-jdk   File: BasicAttributes.java   View Source Code Vote up 5 votes
/**
 * Determines whether this <tt>BasicAttributes</tt> is equal to another
 * <tt>Attributes</tt>
 * Two <tt>Attributes</tt> are equal if they are both instances of
 * <tt>Attributes</tt>,
 * treat the case of attribute IDs the same way, and contain the
 * same attributes. Each <tt>Attribute</tt> in this <tt>BasicAttributes</tt>
 * is checked for equality using <tt>Object.equals()</tt>, which may have
 * be overridden by implementations of <tt>Attribute</tt>).
 * If a subclass overrides <tt>equals()</tt>,
 * it should override <tt>hashCode()</tt>
 * as well so that two <tt>Attributes</tt> instances that are equal
 * have the same hash code.
 * @param obj the possibly null object to compare against.
 *
 * @return true If obj is equal to this BasicAttributes.
 * @see #hashCode
 */
public boolean equals(Object obj) {
    if ((obj != null) && (obj instanceof Attributes)) {
        Attributes target = (Attributes)obj;

        // Check case first
        if (ignoreCase != target.isCaseIgnored()) {
            return false;
        }

        if (size() == target.size()) {
            Attribute their, mine;
            try {
                NamingEnumeration<?> theirs = target.getAll();
                while (theirs.hasMore()) {
                    their = (Attribute)theirs.next();
                    mine = get(their.getID());
                    if (!their.equals(mine)) {
                        return false;
                    }
                }
            } catch (NamingException e) {
                return false;
            }
            return true;
        }
    }
    return false;
}
 
Example 20
Project: lazycat   File: DirContextURLConnection.java   View Source Code Vote up 4 votes
/**
 * Returns an unmodifiable Map of the header fields.
 */
@Override
public Map<String, List<String>> getHeaderFields() {

	if (!connected) {
		// Try to connect (silently)
		try {
			connect();
		} catch (IOException e) {
			// Ignore
		}
	}

	if (attributes == null)
		return (Collections.emptyMap());

	HashMap<String, List<String>> headerFields = new HashMap<String, List<String>>(attributes.size());
	NamingEnumeration<String> attributeEnum = attributes.getIDs();
	try {
		while (attributeEnum.hasMore()) {
			String attributeID = attributeEnum.next();
			Attribute attribute = attributes.get(attributeID);
			if (attribute == null)
				continue;
			ArrayList<String> attributeValueList = new ArrayList<String>(attribute.size());
			NamingEnumeration<?> attributeValues = attribute.getAll();
			while (attributeValues.hasMore()) {
				Object attrValue = attributeValues.next();
				attributeValueList.add(getHeaderValueAsString(attrValue));
			}
			attributeValueList.trimToSize(); // should be a no-op if
												// attribute.size() didn't
												// lie
			headerFields.put(attributeID, Collections.unmodifiableList(attributeValueList));
		}
	} catch (NamingException ne) {
		// Shouldn't happen
	}

	return Collections.unmodifiableMap(headerFields);

}