001/**
002 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003 *
004 * Copyright (c) 2006 Sun Microsystems Inc. All Rights Reserved
005 *
006 * The contents of this file are subject to the terms
007 * of the Common Development and Distribution License
008 * (the License). You may not use this file except in
009 * compliance with the License.
010 *
011 * You can obtain a copy of the License at
012 * https://opensso.dev.java.net/public/CDDLv1.0.html or
013 * opensso/legal/CDDLv1.0.txt
014 * See the License for the specific language governing
015 * permission and limitations under the License.
016 *
017 * When distributing Covered Code, include this CDDL
018 * Header Notice in each file and include the License file
019 * at opensso/legal/CDDLv1.0.txt.
020 * If applicable, add the following below the CDDL Header,
021 * with the fields enclosed by brackets [] replaced by
022 * your own identifying information:
023 * "Portions Copyrighted [year] [name of copyright owner]"
024 *
025 * $Id: SelectTransform.java,v 1.2 2008/06/25 05:47:08 qcheng Exp $
026 *
027 */
028
029package com.sun.identity.liberty.ws.authnsvc.protocol;
030
031import java.util.ArrayList;
032import java.util.Iterator;
033
034import com.sun.identity.liberty.ws.authnsvc.AuthnSvcUtils;
035
036/**
037 * The <code>SelectTransform</code> class represents a <code>Transform</code>
038 * that removes all characters except those specified in the "allowed"
039 * parameter.
040 *
041 * @supported.all.api
042 */
043public class SelectTransform extends Transform {
044
045
046    /**
047     * This is the default constructor.
048     */
049    public SelectTransform() {
050        name = SELECT_URI;
051    }
052
053    /**
054     * Constructs  <code>SelectTransform</code> with allowed characters.
055     * @param allowed all characters except specified in 'allowed' will be
056     *                removed
057     */
058    public SelectTransform(String allowed) {
059        name = SELECT_URI;
060        Parameter parameter = new Parameter(Parameter.NAME_ALLOWED, allowed);
061        parameters = new ArrayList();
062        parameters.add(parameter);
063    }
064
065    /**
066     * Transforms password.
067     * @param password original password
068     * @return transformed password
069     */
070    public String transform(String password)
071    {
072        if (AuthnSvcUtils.debug.messageEnabled()) {
073            AuthnSvcUtils.debug.message("SelectTransform.transform");
074        }
075
076        if (parameters == null || parameters.isEmpty()) {
077            if (AuthnSvcUtils.debug.warningEnabled()) {
078                AuthnSvcUtils.debug.warning("SelectTransform.transform: " +
079                                            "no parameter found");
080            }
081            return password;
082        }
083
084        for(Iterator iter = parameters.iterator(); iter.hasNext(); ) {
085
086            Parameter parameter = (Parameter)iter.next();
087            if (parameter.getName().equals(Parameter.NAME_ALLOWED)) {
088
089                String allowed = parameter.getValue();
090                if (AuthnSvcUtils.debug.messageEnabled()) {
091                    AuthnSvcUtils.debug.message("SelectTransform.transform: " +
092                                                "allowed = " + allowed);
093                }
094
095                if (allowed == null || allowed.length() == 0) {
096                    return "";
097                }
098
099                int pLen = password.length();
100                StringBuffer resultSB = new StringBuffer(pLen);
101                for(int i=0; i<pLen; i++) {
102                    char c = password.charAt(i);
103                    if (allowed.indexOf(c) != -1) {
104                        resultSB.append(c);
105                    }
106                }
107
108                return resultSB.toString();
109            }
110        }
111
112        if (AuthnSvcUtils.debug.warningEnabled()) {
113            AuthnSvcUtils.debug.warning("SelectTransform.transform: " +
114                                        "parameter 'allowed' not found");
115        }
116        return password;
117    }
118}