org.picketlink.idm.config.LDAPMappingConfiguration Maven / Gradle / Ivy
/*
* JBoss, Home of Professional Open Source
*
* Copyright 2013 Red Hat, Inc. and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.picketlink.idm.config;
import org.picketlink.common.properties.Property;
import org.picketlink.common.properties.query.NamedPropertyCriteria;
import org.picketlink.common.properties.query.PropertyQueries;
import org.picketlink.idm.model.Attribute;
import org.picketlink.idm.model.AttributedType;
import org.picketlink.idm.model.IdentityType;
import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Member;
import java.lang.reflect.Type;
import java.util.Map;
import java.util.Set;
/**
* @author pedroigor
*/
public class LDAPMappingConfiguration {
private final Class extends AttributedType> mappedClass;
private final Set objectClasses;
private final String baseDN;
private final Map mappedProperties;
private final Property idProperty;
private final Class extends AttributedType> relatedAttributedType;
private final String parentMembershipAttributeName;
private final Map parentMapping;
private final Set readOnlyAttributes;
private final int hierarchySearchDepth;
private final Property bindingProperty;
LDAPMappingConfiguration(Class extends AttributedType> mappedClass,
Set objectClasses,
String baseDN,
String idPropertyName,
final String bindingPropertyName,
Map mappedProperties,
Set readOnlyAttributes,
Map parentMapping,
Class extends AttributedType> relatedAttributedType,
String parentMembershipAttributeName,
int hierarchySearchDepth) {
this.mappedClass = mappedClass;
this.objectClasses = objectClasses;
this.baseDN = baseDN;
this.mappedProperties = mappedProperties;
this.readOnlyAttributes = readOnlyAttributes;
this.parentMapping = parentMapping;
this.hierarchySearchDepth = hierarchySearchDepth;
if (idPropertyName != null) {
this.idProperty = PropertyQueries
.createQuery(getMappedClass())
.addCriteria(new NamedPropertyCriteria(idPropertyName)).getFirstResult();
} else {
this.idProperty = null;
}
if (IdentityType.class.isAssignableFrom(mappedClass) && idProperty == null) {
throw new SecurityConfigurationException("Id attribute not mapped to any property of [" + mappedClass + "].");
}
Property bindingProperty = this.idProperty;
if (bindingPropertyName != null) {
bindingProperty = getBindingProperty(bindingPropertyName);
}
this.bindingProperty = bindingProperty;
this.relatedAttributedType = relatedAttributedType;
this.parentMembershipAttributeName = parentMembershipAttributeName;
}
public Class extends AttributedType> getMappedClass() {
return this.mappedClass;
}
public Set getObjectClasses() {
return this.objectClasses;
}
public String getBaseDN() {
return this.baseDN;
}
public Map getMappedProperties() {
return this.mappedProperties;
}
public Property getIdProperty() {
return this.idProperty;
}
public Property getBindingProperty() {
return this.bindingProperty;
}
public Class extends AttributedType> getRelatedAttributedType() {
return this.relatedAttributedType;
}
public String getParentMembershipAttributeName() {
return this.parentMembershipAttributeName;
}
public Map getParentMapping() {
return this.parentMapping;
}
public Set getReadOnlyAttributes() {
return this.readOnlyAttributes;
}
public int getHierarchySearchDepth() {
return this.hierarchySearchDepth;
}
private Property getBindingProperty(final String bindingPropertyName) {
Property bindingProperty = PropertyQueries
.createQuery(getMappedClass())
.addCriteria(new NamedPropertyCriteria(bindingPropertyName)).getFirstResult();
// We don't have Java property, so actually delegate to setAttribute/getAttribute
if (bindingProperty == null) {
bindingProperty = new Property() {
@Override
public String getName() {
return bindingPropertyName;
}
@Override
public Type getBaseType() {
return null;
}
@Override
public Class getJavaClass() {
return String.class;
}
@Override
public AnnotatedElement getAnnotatedElement() {
return null;
}
@Override
public Member getMember() {
return null;
}
@Override
public String getValue(Object instance) {
if (!(instance instanceof AttributedType)) {
throw new IllegalStateException("Instance [ " + instance + " ] not an instance of AttributedType");
}
AttributedType attributedType = (AttributedType) instance;
Attribute attr = attributedType.getAttribute(bindingPropertyName);
return attr!=null ? attr.getValue() : null;
}
@Override
public void setValue(Object instance, String value) {
if (!(instance instanceof AttributedType)) {
throw new IllegalStateException("Instance [ " + instance + " ] not an instance of AttributedType");
}
AttributedType attributedType = (AttributedType) instance;
attributedType.setAttribute(new Attribute(bindingPropertyName, value));
}
@Override
public Class> getDeclaringClass() {
return null;
}
@Override
public boolean isReadOnly() {
return false;
}
@Override
public void setAccessible() {
}
@Override
public boolean isAnnotationPresent(Class annotation) {
return false;
}
};
}
return bindingProperty;
}
}