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 *
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 */
044public class TruncationTransform extends Transform {
045
046
047    /**
048     * This is the default constructor.
049     */
050    public TruncationTransform() {
051        name = TRUNCATION_URI;
052    }
053
054    /**
055     * Constructs <code>TruncationTransform</code> object with length.
056     * @param length all subsequent characters after the length will be
057     *               removed
058     */
059    public TruncationTransform(int length) {
060        name = TRUNCATION_URI;
061        Parameter parameter = new Parameter(Parameter.NAME_LENGTH, "" +length);
062        parameters = new ArrayList();
063        parameters.add(parameter);
064    }
065
066    /**
067     * Transforms password.
068     * @param password original password
069     * @return transformed password
070     */
071    public String transform(String password)
072    {
073        if (AuthnSvcUtils.debug.messageEnabled()) {
074            AuthnSvcUtils.debug.message("TruncationTransform.transform");
075        }
076
077        if (password == null) {
078            return null;
079        }
080
081        if (parameters == null || parameters.isEmpty()) {
082            if (AuthnSvcUtils.debug.warningEnabled()) {
083                AuthnSvcUtils.debug.warning("TruncationTransform.transform: " +
084                                            "no parameter found");
085            }
086            return password;
087        }
088
089        for(Iterator iter = parameters.iterator(); iter.hasNext(); ) {
090
091            Parameter parameter = (Parameter)iter.next();
092            if (parameter.getName().equals(Parameter.NAME_LENGTH)) {
093
094                try {
095                    int length = Integer.parseInt(parameter.getValue());
096                    if (length < password.length() && length >= 0) {
097                        return password.substring(0, length);
098                    } else {
099                        if (AuthnSvcUtils.debug.messageEnabled()) {
100                            AuthnSvcUtils.debug.message(
101                               "TruncationTransform.transform: parameter " +
102                               "length value isn't less than password length");
103                        }
104                    }
105                } catch (Exception ex) {
106                    if (AuthnSvcUtils.debug.warningEnabled()) {
107                        AuthnSvcUtils.debug.warning(
108                                "TruncationTransform.transform: " +
109                                "parameter value is not integer", ex);
110                    }
111                }
112
113                return password;
114            }
115        }
116
117        if (AuthnSvcUtils.debug.warningEnabled()) {
118            AuthnSvcUtils.debug.warning("TruncationTransform.transform: " +
119                                        "parameter 'name' not found");
120        }
121        return password;
122    }
123}