Search in sources :

Example 6 with StringBuffer

use of java.lang.StringBuffer in project Payara by payara.

the class LDAPRealm method userSearch.

/**
 * Do anonymous search for the user. Should be unique if exists.
 */
private String userSearch(DirContext ctx, String baseDN, String filter) {
    if (_logger.isLoggable(Level.FINEST)) {
        _logger.log(Level.FINE, "search: baseDN: " + baseDN + "  filter: " + filter);
    }
    String foundDN = null;
    NamingEnumeration namingEnum = null;
    SearchControls ctls = new SearchControls();
    ctls.setReturningAttributes(_dnOnly);
    ctls.setSearchScope(SearchControls.SUBTREE_SCOPE);
    ctls.setCountLimit(1);
    try {
        namingEnum = ctx.search(baseDN, filter, ctls);
        if (namingEnum.hasMore()) {
            SearchResult res = (SearchResult) namingEnum.next();
            StringBuffer sb = new StringBuffer();
            // for dn name with '/'
            CompositeName compDN = new CompositeName(res.getName());
            String ldapDN = compDN.get(0);
            sb.append(ldapDN);
            if (res.isRelative()) {
                sb.append(",");
                sb.append(baseDN);
            }
            foundDN = sb.toString();
            if (_logger.isLoggable(Level.FINEST)) {
                _logger.log(Level.FINE, "Found user DN: " + foundDN);
            }
        }
    } catch (Exception e) {
        _logger.log(Level.WARNING, "ldaprealm.searcherror", filter);
        _logger.log(Level.WARNING, "security.exception", e);
    } finally {
        if (namingEnum != null) {
            try {
                namingEnum.close();
            } catch (Exception ex) {
            }
        }
    }
    return foundDN;
}
Also used : StringBuffer(java.lang.StringBuffer) CompositeName(javax.naming.CompositeName) NamingEnumeration(javax.naming.NamingEnumeration) SearchControls(javax.naming.directory.SearchControls) SearchResult(javax.naming.directory.SearchResult) LoginException(javax.security.auth.login.LoginException) BadRealmException(com.sun.enterprise.security.auth.realm.BadRealmException) NamingException(javax.naming.NamingException) NoSuchRealmException(com.sun.enterprise.security.auth.realm.NoSuchRealmException) InvalidOperationException(com.sun.enterprise.security.auth.realm.InvalidOperationException) IOException(java.io.IOException) NoSuchUserException(com.sun.enterprise.security.auth.realm.NoSuchUserException)

Example 7 with StringBuffer

use of java.lang.StringBuffer in project Payara by payara.

the class LDAPRealm method RFC2254Encode.

/**
 * Escape special chars in search filter, according to RFC2254
 * @param inName
 * @return
 */
private String RFC2254Encode(String inName) {
    int len = inName.length();
    StringBuffer buf = new StringBuffer(len);
    for (int i = 0; i < len; i++) {
        char ch = inName.charAt(i);
        switch(ch) {
            case '*':
                buf.append("\\2a");
                break;
            case '(':
                buf.append("\\28");
                break;
            case ')':
                buf.append("\\29");
                break;
            case '\\':
                buf.append("\\5c");
                break;
            case 0:
                buf.append("\\00");
                break;
            default:
                buf.append(ch);
        }
    }
    return buf.toString();
}
Also used : StringBuffer(java.lang.StringBuffer)

Example 8 with StringBuffer

use of java.lang.StringBuffer in project opennms by OpenNMS.

the class SimpleNode method dumpSymbolTables.

/**
 * Dump the symbol tables - primarily debugging routine
 */
public void dumpSymbolTables() {
    StringBuffer sb = new StringBuffer();
    OidValues oidValues = null;
    String key = null;
    String oidName = null;
    String entryName = null;
    String indexName = null;
    String baseType = null;
    dumpOidNamesSymbolTable();
    sb.append("Symbol Table oidVarsTableName for tableName/OidName\n");
    for (Enumeration e = oidVarsTableName.keys(); e.hasMoreElements(); ) {
        key = (String) e.nextElement();
        oidName = (String) oidVarsTableName.get(key);
        sb.append(key + "/" + oidName).append("\n");
    }
    sb.append("Symbol Table entryNameAndTableName for entryName/tableName\n");
    for (Enumeration e = entryNameAndTableName.keys(); e.hasMoreElements(); ) {
        key = (String) e.nextElement();
        entryName = (String) entryNameAndTableName.get(key);
        sb.append(key + "/" + entryName).append("\n");
    }
    sb.append("Symbol Table tableAndIndex for tableName/indexName\n");
    for (Enumeration e = tableAndIndex.keys(); e.hasMoreElements(); ) {
        key = (String) e.nextElement();
        indexName = (String) tableAndIndex.get(key);
        sb.append(key + "/" + indexName).append("\n");
    }
    sb.append("Symbol Table typeNameTable for type/baseType\n");
    for (Enumeration e = typeNameTable.keys(); e.hasMoreElements(); ) {
        key = (String) e.nextElement();
        baseType = (String) typeNameTable.get(key);
        sb.append(key + "/" + baseType).append("\n");
    }
    if (printDebug) {
        System.out.println(sb.toString());
    }
}
Also used : StringBuffer(java.lang.StringBuffer) Enumeration(java.util.Enumeration)

Example 9 with StringBuffer

use of java.lang.StringBuffer in project opennms by OpenNMS.

the class SimpleNode method writeOids.

// end collectTextualConventionInfo
/**
 * Walk the symbol tables and output XML
 */
public void writeOids() {
    // write something like:
    // <mibObj oid=".1.3.6.1.2.1.2.2.1.10" instance="ifIndex" alias="ifInOctets" type="counter"/>
    StringBuffer sb = new StringBuffer();
    OidValues oidValues = null;
    TreeMap sortedMap = new TreeMap();
    if (printDebug) {
        System.out.println("Sorted OIDS from Vector");
    }
    sb = new StringBuffer();
    for (int i = 0; i < orderList.size(); i++) {
        String name = (String) orderList.elementAt(i);
        oidValues = (OidValues) oidNames.get(name);
        if (!(oidValues.getTypeId().equals("DEFAULT") || oidValues.getAccess().equals(OidValues.NOT_ACCESSIBLE))) {
            // TBD get the table instance var
            String instanceVar = "0";
            String aliasVar = oidValues.getTextOid();
            int index = aliasVar.lastIndexOf(".") + 1;
            String lastOidStr = aliasVar.substring(index);
            String tableName = (String) oidVarsTableName.get(lastOidStr);
            String numericOid = oidValues.getNumericOid();
            String typeName = oidValues.getTypeId();
            String typeFromTable = (String) typeNameTable.get(typeName);
            if (typeFromTable != null) {
                // this gets the base type
                typeName = typeFromTable;
            }
            if (tableName != null) {
                instanceVar = (String) tableAndIndex.get(tableName);
            }
            // There is a 19 char rrd limitation. Catenate a string onto the alias.
            if (lastOidStr.length() > tooLongSize) {
                lastOidStr = lastOidStr + TOO_LONG;
            }
            sb.append("<mibObj oid=\"").append(numericOid).append("\" ");
            sb.append("instance=\"").append(instanceVar).append("\" ");
            sb.append("alias=\"").append(lastOidStr).append("\" ");
            sb.append("type=\"").append(typeName).append("\" />").append("\n");
            System.out.print(sb.toString());
            sortedMap.put(numericOid, sb.toString());
            sb = new StringBuffer();
        } else {
            if (printDebug && oidValues.getTypeId().equals("DEFAULT")) {
                System.out.println("skipping oid with DEFAULT type, oid=" + oidValues.getTextOid());
            }
            if (printDebug && oidValues.getAccess().equals(OidValues.NOT_ACCESSIBLE)) {
                System.out.println("skipping oid with no ACCESS, oid=" + oidValues.getAccess());
            }
        }
    }
}
Also used : StringBuffer(java.lang.StringBuffer) TreeMap(java.util.TreeMap)

Example 10 with StringBuffer

use of java.lang.StringBuffer in project fresco by facebook.

the class JfifTestUtils method encodeInt2HexString.

public static String encodeInt2HexString(int value, int length, boolean littleEndian) {
    StringBuffer sb = new StringBuffer();
    for (int i = 0; i < length; i++) {
        int oneByte = value & 0xFF;
        if (littleEndian) {
            sb.append(Hex.byte2Hex(oneByte));
        } else {
            sb.insert(0, Hex.byte2Hex(oneByte));
        }
        value = value >> 8;
    }
    return sb.toString();
}
Also used : StringBuffer(java.lang.StringBuffer)

Aggregations

StringBuffer (java.lang.StringBuffer)16 IOException (java.io.IOException)6 BadRealmException (com.sun.enterprise.security.auth.realm.BadRealmException)3 InvalidOperationException (com.sun.enterprise.security.auth.realm.InvalidOperationException)3 NoSuchRealmException (com.sun.enterprise.security.auth.realm.NoSuchRealmException)3 NoSuchUserException (com.sun.enterprise.security.auth.realm.NoSuchUserException)3 NamingException (javax.naming.NamingException)3 LoginException (javax.security.auth.login.LoginException)3 BufferedReader (java.io.BufferedReader)2 FileInputStream (java.io.FileInputStream)2 Enumeration (java.util.Enumeration)2 HashMap (java.util.HashMap)2 Iterator (java.util.Iterator)2 Map (java.util.Map)2 TreeMap (java.util.TreeMap)2 DirContext (javax.naming.directory.DirContext)2 InitialDirContext (javax.naming.directory.InitialDirContext)2 File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1 FileReader (java.io.FileReader)1