use of cz.metacentrum.perun.core.api.exceptions.InternalErrorException in project perun by CESNET.
the class ExtSourcesManagerImpl method deleteExtSource.
public void deleteExtSource(PerunSession sess, ExtSource extSource) throws InternalErrorException, ExtSourceAlreadyRemovedException {
try {
// Delete associated attributes
jdbc.update("delete from ext_sources_attributes where ext_sources_id=?", extSource.getId());
// Delete the external source
int numAffected = jdbc.update("delete from ext_sources where id=?", extSource.getId());
if (numAffected == 0)
throw new ExtSourceAlreadyRemovedException("ExtSource: " + extSource);
} catch (RuntimeException e) {
throw new InternalErrorException(e);
}
}
use of cz.metacentrum.perun.core.api.exceptions.InternalErrorException in project perun by CESNET.
the class ExtSourceLdap method getGroupSubjects.
public List<Map<String, String>> getGroupSubjects(Map<String, String> attributes) throws InternalErrorException {
NamingEnumeration<SearchResult> results = null;
List<String> ldapGroupSubjects = new ArrayList<String>();
// Get the LDAP group name
String ldapGroupName = attributes.get(GroupsManager.GROUPMEMBERSQUERY_ATTRNAME);
// Get optional filter for members filtering
String filter = attributes.get(GroupsManager.GROUPMEMBERSFILTER_ATTRNAME);
try {
log.trace("LDAP External Source: searching for group subjects [{}]", ldapGroupName);
String attrName;
if (getAttributes().containsKey("memberAttribute")) {
attrName = (String) getAttributes().get("memberAttribute");
} else {
// Default value
attrName = "uniqueMember";
}
List<String> retAttrs = new ArrayList<String>();
retAttrs.add(attrName);
String[] retAttrsArray = retAttrs.toArray(new String[retAttrs.size()]);
Attributes attrs = getContext().getAttributes(ldapGroupName, retAttrsArray);
Attribute ldapAttribute = null;
// Get the list of returned groups, should be only one
if (attrs.get(attrName) != null) {
// Get the attribute which holds group subjects
ldapAttribute = attrs.get(attrName);
}
if (ldapAttribute != null) {
// Get the DNs of the subjects
for (int i = 0; i < ldapAttribute.size(); i++) {
String ldapSubjectDN = (String) ldapAttribute.get(i);
ldapGroupSubjects.add(ldapSubjectDN);
log.trace("LDAP External Source: found group subject [{}].", ldapSubjectDN);
}
}
List<Map<String, String>> subjects = new ArrayList<Map<String, String>>();
// If attribute filter not exists, use optional default filter from extSource definition
if (filter == null)
filter = filteredQuery;
// Now query LDAP again and search for each subject
for (String ldapSubjectName : ldapGroupSubjects) {
subjects.addAll(this.querySource(filter, ldapSubjectName, 0));
}
return subjects;
} catch (NamingException e) {
log.error("LDAP exception during running query '{}'", ldapGroupName);
throw new InternalErrorException("Entry '" + ldapGroupName + "' was not found in LDAP.", e);
} finally {
try {
if (results != null) {
results.close();
}
} catch (Exception e) {
log.error("LDAP exception during closing result, while running query '{}'", ldapGroupName);
throw new InternalErrorException(e);
}
}
}
use of cz.metacentrum.perun.core.api.exceptions.InternalErrorException in project perun by CESNET.
the class ExtSourceLdap method querySource.
/**
* Query LDAP using query in defined base. Results can be limited to the maxResults.
*
* @param query
* @param base
* @param maxResults
* @return List of Map of the LDAP attribute names and theirs values
* @throws InternalErrorException
*/
protected List<Map<String, String>> querySource(String query, String base, int maxResults) throws InternalErrorException {
NamingEnumeration<SearchResult> results = null;
List<Map<String, String>> subjects = new ArrayList<Map<String, String>>();
try {
// If query is null, then we are finding object by the base
if (query == null) {
log.trace("search base [{}]", base);
// TODO jmena atributu spise prijimiat pres vstupni parametr metody
Attributes ldapAttributes = getContext().getAttributes(base);
if (ldapAttributes.size() > 0) {
Map<String, String> attributes = this.getSubjectAttributes(ldapAttributes);
if (!attributes.isEmpty()) {
subjects.add(attributes);
}
}
} else {
log.trace("search string [{}]", query);
SearchControls controls = new SearchControls();
controls.setSearchScope(SearchControls.SUBTREE_SCOPE);
// Set timeout to 5s
controls.setTimeLimit(5000);
if (maxResults > 0) {
controls.setCountLimit(maxResults);
}
if (base == null)
base = "";
results = getContext().search(base, query, controls);
while (results.hasMore()) {
SearchResult searchResult = (SearchResult) results.next();
Attributes attributes = searchResult.getAttributes();
Map<String, String> subjectAttributes = this.getSubjectAttributes(attributes);
if (!subjectAttributes.isEmpty()) {
subjects.add(subjectAttributes);
}
}
}
log.trace("Returning [{}] subjects", subjects.size());
return subjects;
} catch (NamingException e) {
log.error("LDAP exception during running query '{}'", query);
throw new InternalErrorException("LDAP exception during running query: " + query + ".", e);
} finally {
try {
if (results != null) {
results.close();
}
} catch (Exception e) {
log.error("LDAP exception during closing result, while running query '{}'", query);
throw new InternalErrorException(e);
}
}
}
use of cz.metacentrum.perun.core.api.exceptions.InternalErrorException in project perun by CESNET.
the class ExtSourceLdap method initContext.
protected void initContext() throws InternalErrorException {
// Load mapping between LDAP attributes and Perun attributes
Hashtable<String, String> env = new Hashtable<String, String>();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.SECURITY_AUTHENTICATION, "simple");
if (getAttributes().containsKey("referral")) {
env.put(Context.REFERRAL, (String) getAttributes().get("referral"));
}
if (getAttributes().containsKey("url")) {
env.put(Context.PROVIDER_URL, (String) getAttributes().get("url"));
} else {
throw new InternalErrorException("url attributes is required");
}
if (getAttributes().containsKey("user")) {
env.put(Context.SECURITY_PRINCIPAL, (String) getAttributes().get("user"));
}
if (getAttributes().containsKey("password")) {
env.put(Context.SECURITY_CREDENTIALS, (String) getAttributes().get("password"));
}
if (getAttributes().containsKey("filteredQuery")) {
filteredQuery = (String) getAttributes().get("filteredQuery");
}
try {
// ldapMapping contains entries like: firstName={givenName},lastName={sn},email={mail}
if (getAttributes().get("ldapMapping") == null) {
throw new InternalErrorException("ldapMapping attributes is required");
}
String[] ldapMapping = ((String) getAttributes().get("ldapMapping")).trim().split(",\n");
mapping = new HashMap<String, String>();
for (String entry : ldapMapping) {
String[] values = entry.trim().split("=", 2);
mapping.put(values[0].trim(), values[1].trim());
}
this.dirContext = new InitialDirContext(env);
} catch (NamingException e) {
log.error("LDAP exception during creating the context.");
throw new InternalErrorException(e);
}
}
use of cz.metacentrum.perun.core.api.exceptions.InternalErrorException in project perun by CESNET.
the class ExtSourceLdap method close.
public void close() throws InternalErrorException {
if (this.dirContext != null) {
try {
this.dirContext.close();
this.dirContext = null;
} catch (NamingException e) {
throw new InternalErrorException(e);
}
}
}
Aggregations