Package org.olat.shibboleth.util

Source Code of org.olat.shibboleth.util.AttributeTranslator

/**
* OLAT - Online Learning and Training<br>
* http://www.olat.org
* <p>
* Licensed under the Apache License, Version 2.0 (the "License"); <br>
* you may not use this file except in compliance with the License.<br>
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing,<br>
* software distributed under the License is distributed on an "AS IS" BASIS, <br>
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. <br>
* See the License for the specific language governing permissions and <br>
* limitations under the License.
* <p>
* Copyright (c) since 2004 at Multimedia- & E-Learning Services (MELS),<br>
* University of Zurich, Switzerland.
* <p>
*/

package org.olat.shibboleth.util;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.olat.core.logging.StartupException;


import com.anthonyeden.lib.config.Configuration;


public class AttributeTranslator {

  private static final String CONF_ATTRIBUTE = "Attribute";
  private static final String CONF_ATTRIBUTE_OUTNAME = "outName";
  private static final String CONF_ATTRIBUTE_INNAME = "inName";
  private static final String CONF_ATTRIBUTE_SELECTABLE_VALUE = "selectableValue";

  // contains the mapping from the shib-attribute to the simplified shib attribute
  private Map<String, String> attributeTranslations;
  // contanis predefined values that can be selected for a specific shib-attribute.
  // Note that as key the simplified shib attribute is used and not the original
  // attributes (see attributeTranslations map)
  private Map<String, String[]> attributeSelectableValues;
 
 
  public AttributeTranslator(Configuration attrTransConfig) {
    // fetch attribute translation map
    attributeTranslations = new HashMap<String, String>();
    attributeSelectableValues = new HashMap<String, String[]>();
    if (attrTransConfig != null) {
      List transList = attrTransConfig.getChildren();
      for (Iterator iter = transList.iterator(); iter.hasNext();) {
        Configuration attributeTranslation = (Configuration)iter.next();
        if (!attributeTranslation.getName().equals(CONF_ATTRIBUTE)) {
          throw new StartupException("Invalid child config element: " + attributeTranslation);
        }
        String inname = attributeTranslation.getAttribute(CONF_ATTRIBUTE_INNAME);
        String outname = attributeTranslation.getAttribute(CONF_ATTRIBUTE_OUTNAME);
        attributeTranslations.put(inname, outname);
        // check if this attribute has predefined values
        List selectableValues = attributeTranslation.getChildren(CONF_ATTRIBUTE_SELECTABLE_VALUE);
        if (selectableValues != null && selectableValues.size() != 0) {
          // collect selectable values and add them to map
          String[] values = new String[selectableValues.size()];
          for (int i = 0; i < selectableValues.size(); i++) {
            Configuration config = (Configuration) selectableValues.get(i);
            values[i] = config.getValue();
          }
          // key is the translated name to make code simpler
          attributeSelectableValues.put(outname, values);
        }
      }
    }
  }

 
  public final Map<String, String> translateAttributesMap(Map<String, String> attributesMap) {
    Map<String, String> convertedMap = new HashMap<String, String>(attributesMap.size());
    Iterator<String> keys = attributesMap.keySet().iterator();
    while (keys.hasNext()) {
      String attribute = keys.next();
      String translatedKey = translateAttribute(attribute);
      String value = attributesMap.get(attribute);
     
      convertedMap.put(translatedKey, value);
    }
    return convertedMap;
  }
 
 
  /**
   * Translate Shibboleth Attributes according to configured attribute translations
   * @param inName
   * @return Translated attribute name.
   */
  public String translateAttribute(String inName) {
    String outName = attributeTranslations.get(inName);
    return outName != null ? outName : inName;
  }

  /**
   * Get all valid values for this attribute if such values are defined. When no
   * values are defined, NULL will be returned.
   *
   * @param attribute
   * @return
   */
  public String[] getSelectableValuesForAttribute(String attribute) {
    return this.attributeSelectableValues.get(attribute)
  }
 
  /**
   * Get all attributes identifyers that can be translated
   * @return
   */
  public Set<String> getTranslateableAttributes() {
    return this.attributeTranslations.keySet();
  }
 
}
TOP

Related Classes of org.olat.shibboleth.util.AttributeTranslator

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.