001: /*
002: * Copyright 2007 Pentaho Corporation. All rights reserved.
003: * This software was developed by Pentaho Corporation and is provided under the terms
004: * of the Mozilla Public License, Version 1.1, or any later version. You may not use
005: * this file except in compliance with the license. If you need a copy of the license,
006: * please go to http://www.mozilla.org/MPL/MPL-1.1.txt. The Original Code is the Pentaho
007: * BI Platform. The Initial Developer is Pentaho Corporation.
008: *
009: * Software distributed under the Mozilla Public License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
011: * the license for the specific language governing your rights and limitations.
012: */
013: package com.pentaho.security.ldap.transform;
014:
015: import java.util.Collection;
016: import java.util.HashSet;
017: import java.util.Iterator;
018: import java.util.Set;
019:
020: import org.acegisecurity.GrantedAuthorityImpl;
021: import org.apache.commons.collections.Transformer;
022: import org.apache.commons.logging.Log;
023: import org.apache.commons.logging.LogFactory;
024:
025: import org.pentaho.messages.Messages;
026:
027: /**
028: * Transforms a <code>String</code> into a <code>GrantedAuthority</code>.
029: * Can handle either a single <code>String</code> or a collection (or array)
030: * of <code>String</code> instances. Always returns a collection or array if
031: * collection or array was the input.
032: *
033: * <p>
034: * Transformer input: <code>String</code> instance, <code>Collection</code>
035: * of <code>String</code> instances, or array of <code>String</code>
036: * instances.
037: * </p>
038: * <p>
039: * Transformer output: <code>GrantedAuthority</code> instance,
040: * <code>Collection</code> of <code>GrantedAuthority</code> instances, or
041: * array of <code>GrantedAuthority</code> instances.
042: * </p>
043: *
044: * @author mlowery
045: */
046: public class StringToGrantedAuthority implements Transformer {
047: // ~ Static fields/initializers ============================================
048: private static final Log logger = LogFactory
049: .getLog(StringToGrantedAuthority.class);
050:
051: // ~ Instance fields =======================================================
052:
053: private String rolePrefix = "ROLE_"; //$NON-NLS-1$
054:
055: private boolean convertToUpperCase = true;
056:
057: // ~ Constructors ==========================================================
058: public StringToGrantedAuthority() {
059: super ();
060: }
061:
062: // ~ Methods ===============================================================
063:
064: public Object transform(final Object obj) {
065: if (logger.isDebugEnabled()) {
066: logger
067: .debug(Messages
068: .getString(
069: "StringToGrantedAuthority.DEBUG_INPUT_TO_TRANSFORM", (null != obj) ? obj.toString() : "null")); //$NON-NLS-1$ //$NON-NLS-2$
070: }
071: Object transformed = obj;
072: if (obj instanceof String) {
073: transformed = transformItem(obj);
074: } else if (obj instanceof Collection) {
075: transformed = new HashSet();
076: Set authSet = (Set) transformed;
077: Iterator iter = ((Collection) obj).iterator();
078: while (iter.hasNext()) {
079: authSet.add(transformItem(iter.next()));
080: }
081: } else if (obj instanceof Object[]) {
082: transformed = new HashSet();
083: Set authSet = (Set) transformed;
084: Object[] objArray = (Object[]) obj;
085: for (int i = 0; i < objArray.length; i++) {
086: authSet.add(transformItem(objArray[i]));
087: }
088: transformed = authSet.toArray();
089: }
090: return transformed;
091: }
092:
093: protected Object transformItem(final Object obj) {
094: Object transformed = obj;
095: if (obj instanceof String) {
096: String converted = rolePrefix
097: + (convertToUpperCase ? ((String) obj)
098: .toUpperCase() : obj.toString());
099: transformed = new GrantedAuthorityImpl(converted);
100: }
101: return transformed;
102: }
103:
104: public void setConvertToUpperCase(boolean convertToUpperCase) {
105: this .convertToUpperCase = convertToUpperCase;
106: }
107:
108: public void setRolePrefix(String rolePrefix) {
109: this.rolePrefix = rolePrefix;
110: }
111:
112: }
|