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: TruncationTransform.java,v 1.2 2008/06/25 05:47:08 qcheng Exp $
026 * Portions Copyrighted 2014 ForgeRock AS.
027 */
028
029
030package com.sun.identity.liberty.ws.authnsvc.protocol;
031
032import java.util.ArrayList;
033import java.util.Iterator;
034
035import com.sun.identity.liberty.ws.authnsvc.AuthnSvcUtils;
036
037/**
038 * The <code>TruncationTransform</code> class represents a
039 * <code>Transform</code> that remove all subsequent characters after a given
040 * number of characters have been obtained.
041 *
042 * @supported.all.api
043 * @deprecated since 12.0.0
044 */
045@Deprecated
046public class TruncationTransform extends Transform {
047
048
049    /**
050     * This is the default constructor.
051     */
052    public TruncationTransform() {
053        name = TRUNCATION_URI;
054    }
055
056    /**
057     * Constructs <code>TruncationTransform</code> object with length.
058     * @param length all subsequent characters after the length will be
059     *               removed
060     */
061    public TruncationTransform(int length) {
062        name = TRUNCATION_URI;
063        Parameter parameter = new Parameter(Parameter.NAME_LENGTH, "" +length);
064        parameters = new ArrayList();
065        parameters.add(parameter);
066    }
067
068    /**
069     * Transforms password.
070     * @param password original password
071     * @return transformed password
072     */
073    public String transform(String password)
074    {
075        if (AuthnSvcUtils.debug.messageEnabled()) {
076            AuthnSvcUtils.debug.message("TruncationTransform.transform");
077        }
078
079        if (password == null) {
080            return null;
081        }
082
083        if (parameters == null || parameters.isEmpty()) {
084            if (AuthnSvcUtils.debug.warningEnabled()) {
085                AuthnSvcUtils.debug.warning("TruncationTransform.transform: " +
086                                            "no parameter found");
087            }
088            return password;
089        }
090
091        for(Iterator iter = parameters.iterator(); iter.hasNext(); ) {
092
093            Parameter parameter = (Parameter)iter.next();
094            if (parameter.getName().equals(Parameter.NAME_LENGTH)) {
095
096                try {
097                    int length = Integer.parseInt(parameter.getValue());
098                    if (length < password.length() && length >= 0) {
099                        return password.substring(0, length);
100                    } else {
101                        if (AuthnSvcUtils.debug.messageEnabled()) {
102                            AuthnSvcUtils.debug.message(
103                               "TruncationTransform.transform: parameter " +
104                               "length value isn't less than password length");
105                        }
106                    }
107                } catch (Exception ex) {
108                    if (AuthnSvcUtils.debug.warningEnabled()) {
109                        AuthnSvcUtils.debug.warning(
110                                "TruncationTransform.transform: " +
111                                "parameter value is not integer", ex);
112                    }
113                }
114
115                return password;
116            }
117        }
118
119        if (AuthnSvcUtils.debug.warningEnabled()) {
120            AuthnSvcUtils.debug.warning("TruncationTransform.transform: " +
121                                        "parameter 'name' not found");
122        }
123        return password;
124    }
125}