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 * Portions Copyrighted 2014 ForgeRock AS.
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 * @deprecated since 12.0.0
043 */
044@Deprecated
045public class SelectTransform extends Transform {
046
047
048    /**
049     * This is the default constructor.
050     */
051    public SelectTransform() {
052        name = SELECT_URI;
053    }
054
055    /**
056     * Constructs  <code>SelectTransform</code> with allowed characters.
057     * @param allowed all characters except specified in 'allowed' will be
058     *                removed
059     */
060    public SelectTransform(String allowed) {
061        name = SELECT_URI;
062        Parameter parameter = new Parameter(Parameter.NAME_ALLOWED, allowed);
063        parameters = new ArrayList();
064        parameters.add(parameter);
065    }
066
067    /**
068     * Transforms password.
069     * @param password original password
070     * @return transformed password
071     */
072    public String transform(String password)
073    {
074        if (AuthnSvcUtils.debug.messageEnabled()) {
075            AuthnSvcUtils.debug.message("SelectTransform.transform");
076        }
077
078        if (parameters == null || parameters.isEmpty()) {
079            if (AuthnSvcUtils.debug.warningEnabled()) {
080                AuthnSvcUtils.debug.warning("SelectTransform.transform: " +
081                                            "no parameter found");
082            }
083            return password;
084        }
085
086        for(Iterator iter = parameters.iterator(); iter.hasNext(); ) {
087
088            Parameter parameter = (Parameter)iter.next();
089            if (parameter.getName().equals(Parameter.NAME_ALLOWED)) {
090
091                String allowed = parameter.getValue();
092                if (AuthnSvcUtils.debug.messageEnabled()) {
093                    AuthnSvcUtils.debug.message("SelectTransform.transform: " +
094                                                "allowed = " + allowed);
095                }
096
097                if (allowed == null || allowed.length() == 0) {
098                    return "";
099                }
100
101                int pLen = password.length();
102                StringBuffer resultSB = new StringBuffer(pLen);
103                for(int i=0; i<pLen; i++) {
104                    char c = password.charAt(i);
105                    if (allowed.indexOf(c) != -1) {
106                        resultSB.append(c);
107                    }
108                }
109
110                return resultSB.toString();
111            }
112        }
113
114        if (AuthnSvcUtils.debug.warningEnabled()) {
115            AuthnSvcUtils.debug.warning("SelectTransform.transform: " +
116                                        "parameter 'allowed' not found");
117        }
118        return password;
119    }
120}