/** * Gets the values of the given attribute contained in the DN. * * <p><strong>NOTE:</strong> no escaping is done on special characters in the * values, which could be different from what would appear in the string * representation of the DN.</p> * * @param rdnSequence list of relative distinguished names * that contains the attributes comprising the DN. * @param attribute Attribute whose values will be retrieved. * @return The attribute values for the given attribute in the order they * appear would appear in the string representation of the DN or an empty * array if the given attribute does not exist. */ private static String[] getAttributeValues(final RDNSequence rdnSequence, final AttributeType attribute) { // Iterates sequence in reverse order as specified in section 2.1 of RFC 2253 final List<String> values = new ArrayList<>(); for (final RDN rdn : rdnSequence.backward()) { for (final Attribute attr : rdn.getAttributes()) { if (attr.getType().equals(attribute)) { values.add(attr.getValue()); } } } return values.toArray(new String[values.size()]); }
/** * Gets the values of the given attribute contained in the DN. * <p> * <p><strong>NOTE:</strong> no escaping is done on special characters in the * values, which could be different from what would appear in the string * representation of the DN.</p> * Iterates sequence in reverse order as specified in section 2.1 of RFC 2253. * * @param rdnSequence list of relative distinguished names * that contains the attributes comprising the DN. * @param attribute Attribute whose values will be retrieved. * @return The attribute values for the given attribute in the order they * appear would appear in the string representation of the DN or an empty * array if the given attribute does not exist. */ private static String[] getAttributeValues(final RDNSequence rdnSequence, final AttributeType attribute) { val values = new ArrayList<String>(); for (val rdn : rdnSequence.backward()) { for (val attr : rdn.getAttributes()) { if (attr.getType().equals(attribute)) { values.add(attr.getValue()); } } } return values.toArray(ArrayUtils.EMPTY_STRING_ARRAY); }
/** * Find an RDN with the specified OID. * * @param sequence the DN components * @param oid the OID to look for * * @return the first matching RDN value, or null */ @Nullable protected String findRDN(@Nonnull final RDNSequence sequence, @Nonnull @NotEmpty final String oid) { // We use backward() here because otherwise the library returns attributes in least to most-specific order. for (final RDN rdn : sequence.backward()) { for (final Attribute attribute : rdn.getAttributes()) { if (attribute.getType().getOid().equals(oid)) { return attribute.getValue(); } } } return null; }
@Override public String format(final X500Principal dn) { final StringBuilder builder = new StringBuilder(); final RDNSequence sequence = NameReader.readX500Principal(dn); int i = 0; for (RDN rdn : sequence.backward()) { if (i++ > 0) { builder.append(RDN_SEPARATOR); } int j = 0; for (Attribute attr : rdn.getAttributes()) { if (j++ > 0) { builder.append(ATV_SEPARATOR); } builder.append(attr.getType()).append('='); final AttributeType type = attr.getType(); if (type instanceof StandardAttributeType) { escape(attr.getValue(), builder); } else { encode(attr.getValue(), builder); } } } return builder.toString(); }