2012-08-28 10 views
6

La domanda dice tutto. Quando sto stampando un attributo che è:Come estrarre il valore da javax.naming.directory.Attribute

cn: WF-008-DAM-PS 

Il frammento di codice è:

private void searchGroup() throws NamingException { 
    NamingEnumeration<SearchResult> searchResults = getLdapDirContext().search(groupDN, "(objectclass=groupOfUniqueNames)", getSearchControls()); 
    String searchGroupCn = getCNForBrand(m_binder.getLocal("brandId"), m_binder.getLocal("brandName")); 
    Log.info(searchGroupCn); 
    while (searchResults.hasMore()) { 
     SearchResult searchResult = searchResults.next(); 
     Attributes attributes = searchResult.getAttributes(); 
     Attribute groupCn = attributes.get("cn"); 
     if(groupCn != null) { 
      Log.info(groupCn.toString());    
     } 
    } 
} 

Come posso ottenere solo il valore che è: WF-008-DAM-PS, che è senza la parte chiave? Saluti.

risposta

4

Invocare il metodo getValue() o il metodo getValue(int).

+0

Sono questi due metodi sono presenti in javax.naming.directory.BasicAttribute o javax.naming.directory.Attribute? C'è un metodo get (int). –

+0

'Attributo' è un'interfaccia,' BasicAttribute' implementa 'Attributo'. Quindi, 'Object finale o = groupCn.getValue()', assumendo 'groupCn' è a valore singolo. Se è multivalore, utilizzare l'indice intero come parametro su 'groupCn.getValue (index)' –

+0

Grazie, ma non esiste il metodo getValue() neanche in http://docs.oracle.com/javase/1.4. 2/docs/api/javax/naming/directory/BasicAttribute.html o http://docs.oracle.com/javase/1.4.2/docs/api/javax/naming/directory/Attribute.html –

6

La soluzione è:

Attribute groupCn = attributes.get("cn"); 
String value = groupCn.get(); 
1

generale

Diciamo che abbiamo:

Attributes attributes; 
Attribute a = attributes.get("something"); 
  • if(a.size() == 1)
    • quindi è possibile utilizzare a.get() o a.get(0) per ottenere il valore unico
  • if(a.size() > 1)

    • scorrere tutti i valori:

      for (int i = 0 ; i < a.size() ; i++) { 
          Object currentVal = a.get(i); 
          // do something with currentVal 
      } 
      

      Se si utilizza a.get() qui, ritornerà solo il primo valore, perché la sua implementazione interna (in BasicAttribute) è la seguente:

      public Object get() throws NamingException { 
          if (values.size() == 0) { 
           throw new NoSuchElementException("Attribute " + getID() + " has no value"); 
          } else { 
           return values.elementAt(0); 
          } 
      } 
      

Entrambi i metodi (get(int) e get()) getta un NamingException.

esempio pratico
(quando l'istanza Attribute ha più valori)

LdapContext ctx = new InitialLdapContext(env, null); 

Attributes attributes = ctx.getAttributes("", new String[] { "supportedSASLMechanisms" }); 
System.out.println(attributes); // {supportedsaslmechanisms=supportedSASLMechanisms: GSSAPI, EXTERNAL, DIGEST-MD5} 

Attribute a = atts.get("supportedsaslmechanisms"); 
System.out.println(a); // supportedSASLMechanisms: GSSAPI, EXTERNAL, DIGEST-MD5 

System.out.println(a.get()); // GSSAPI 

for (int i = 0; i < a.size(); i++) { 
    System.out.print(a.get(i) + " "); // GSSAPI EXTERNAL DIGEST-MD5 
} 
+0

@Downvoter, per favore aggiungi una spiegazione sulla tua decisione ... Penso che questa sia un'ottima risposta. –

Problemi correlati