Search in sources :

Example 1 with OpsSearch

use of com.tremolosecurity.scalejs.operators.data.OpsSearch in project OpenUnison by TremoloSecurity.

the class ScaleJSOperator method runSearch.

private void runSearch(HttpFilterRequest request, HttpFilterResponse response, Gson gson) throws Exception, LDAPException, IOException {
    String json = new String((byte[]) request.getAttribute(ProxySys.MSG_BODY));
    OpsSearch opsSearch = gson.fromJson(json, OpsSearch.class);
    List<AttributeConfig> forSearch = opsSearch.getToSearch();
    List<FilterBuilder> filter = new ArrayList<FilterBuilder>();
    for (AttributeConfig attr : forSearch) {
        if (attr.isPicked()) {
            filter.add(equal(attr.getName(), attr.getValue()));
        }
    }
    FilterBuilder[] fb = new FilterBuilder[filter.size()];
    filter.toArray(fb);
    FilterBuilder baseFilter = (FilterBuilder) request.getAttribute("ops.search.filter");
    String filterString;
    if (baseFilter != null) {
        FilterBuilder localFilter = and(fb);
        filterString = and(localFilter, baseFilter).toString();
    } else {
        filterString = and(fb).toString();
    }
    String searchBase = this.config.getBaseLabelToDN().get(opsSearch.getBase());
    if (searchBase == null) {
        throw new Exception("Invalid search base");
    }
    List<HashMap<String, String>> resList = new ArrayList<HashMap<String, String>>();
    LDAPSearchResults res = GlobalEntries.getGlobalEntries().getConfigManager().getMyVD().search(searchBase, 2, filterString, new ArrayList<String>());
    while (res.hasMore()) {
        HashMap<String, String> ret = new HashMap<String, String>();
        resList.add(ret);
        LDAPEntry entry = res.next();
        ret.put("dn", entry.getDN());
        for (AttributeConfig attr : this.config.getResultsAttributes()) {
            if (entry.getAttribute(attr.getName()) != null) {
                String val = entry.getAttribute(attr.getName()).getStringValue();
                ret.put(attr.getName(), val);
            } else {
                ret.put(attr.getName(), "");
            }
        }
    }
    ScaleJSUtils.addCacheHeaders(response);
    response.setContentType("application/json");
    response.getWriter().println(gson.toJson(resList).trim());
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) LDAPException(com.novell.ldap.LDAPException) IOException(java.io.IOException) LDAPEntry(com.novell.ldap.LDAPEntry) LDAPSearchResults(com.novell.ldap.LDAPSearchResults) FilterBuilder(org.apache.directory.ldap.client.api.search.FilterBuilder) OpsSearch(com.tremolosecurity.scalejs.operators.data.OpsSearch) AttributeConfig(com.tremolosecurity.scalejs.operators.config.AttributeConfig)

Aggregations

LDAPEntry (com.novell.ldap.LDAPEntry)1 LDAPException (com.novell.ldap.LDAPException)1 LDAPSearchResults (com.novell.ldap.LDAPSearchResults)1 AttributeConfig (com.tremolosecurity.scalejs.operators.config.AttributeConfig)1 OpsSearch (com.tremolosecurity.scalejs.operators.data.OpsSearch)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 FilterBuilder (org.apache.directory.ldap.client.api.search.FilterBuilder)1