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.Arrays;
016: import java.util.Collection;
017: import java.util.HashSet;
018: import java.util.Iterator;
019: import java.util.Set;
020:
021: import org.acegisecurity.GrantedAuthority;
022: import org.apache.commons.collections.Transformer;
023: import org.apache.commons.logging.Log;
024: import org.apache.commons.logging.LogFactory;
025: import org.springframework.util.Assert;
026:
027: import org.pentaho.messages.Messages;
028:
029: /**
030: * Transforms a <code>GrantedAuthority</code> into a <code>String</code>.
031: * Can handle either a single <code>GrantedAuthority</code> or a collection
032: * (or array) of <code>GrantedAuthority</code> instances. Always returns a
033: * collection or array if collection or array was the input.
034: *
035: * <p>
036: * Transformer input: <code>GrantedAuthority</code> instance,
037: * <code>Collection</code> of <code>GrantedAuthority</code> instances, or
038: * array of <code>GrantedAuthority</code> instances.
039: * </p>
040: * <p>
041: * Transformer output: <code>String</code> instance, <code>Collection</code>
042: * of <code>String</code> instances, or array of <code>String</code>
043: * instances.
044: * </p>
045: *
046: * @author mlowery
047: */
048: public class GrantedAuthorityToString implements Transformer {
049: // ~ Static fields/initializers ============================================
050:
051: private static final Log logger = LogFactory
052: .getLog(GrantedAuthorityToString.class);
053:
054: // ~ Instance fields =======================================================
055:
056: private String rolePrefix = "ROLE_"; //$NON-NLS-1$
057:
058: // ~ Constructors ==========================================================
059:
060: public GrantedAuthorityToString() {
061: super ();
062: }
063:
064: // ~ Methods ===============================================================
065:
066: public Object transform(final Object obj) {
067: if (logger.isDebugEnabled()) {
068: String input = (obj instanceof Object[] ? Arrays.asList(
069: (Object[]) obj).toString() : obj.toString());
070: logger
071: .debug(Messages
072: .getString(
073: "GrantedAuthorityToString.DEBUG_INPUT_TO_TRANSFORM", //$NON-NLS-1$
074: input));
075: }
076:
077: Object transformed = obj;
078: if (obj instanceof GrantedAuthority) {
079: transformed = transformItem(obj);
080: } else if (obj instanceof Collection) {
081: transformed = new HashSet();
082: Set authSet = (Set) transformed;
083: Iterator iter = ((Collection) obj).iterator();
084: while (iter.hasNext()) {
085: authSet.add(transformItem(iter.next()));
086: }
087: } else if (obj instanceof Object[]) {
088: transformed = new HashSet();
089: Set authSet = (Set) transformed;
090: Object[] objArray = (Object[]) obj;
091: for (int i = 0; i < objArray.length; i++) {
092: authSet.add(transformItem(objArray[i]));
093: }
094: transformed = authSet.toArray();
095: }
096: return transformed;
097: }
098:
099: protected Object transformItem(final Object obj) {
100: Object transformed = obj;
101: if (obj instanceof GrantedAuthority) {
102: String auth = ((GrantedAuthority) obj).getAuthority();
103: if (auth.startsWith(rolePrefix)) {
104: auth = auth.substring(rolePrefix.length());
105: }
106: transformed = auth;
107: }
108: return transformed;
109: }
110:
111: public void setRolePrefix(final String rolePrefix) {
112: Assert.hasLength(rolePrefix);
113: this.rolePrefix = rolePrefix;
114: }
115:
116: }
|