001/**
002 * Copyright 2005-2018 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.rice.krad.datadictionary.mask;
017
018import org.kuali.rice.krad.datadictionary.parse.BeanTag;
019import org.kuali.rice.krad.datadictionary.parse.BeanTagAttribute;
020
021import java.io.Serializable;
022
023/**
024 * The displayMask element specifies the type of masking to
025 * be used to hide the value from un-authorized users.
026 * There are three types of masking.
027 */
028@BeanTag(name = "mask")
029public class Mask implements Serializable {
030    private static final long serialVersionUID = 4035984416568235531L;
031
032    protected MaskFormatter maskFormatter;
033    protected Class<? extends MaskFormatter> maskFormatterClass;
034
035    /**
036     * Masks a data value with the configured maskFormatter;
037     *
038     * @param value of the object
039     * @return string value of the masked object
040     */
041    public String maskValue(Object value) {
042        if (maskFormatter == null) {
043            if (maskFormatterClass != null) {
044                try {
045                    maskFormatter = maskFormatterClass.newInstance();
046                } catch (Exception e) {
047                    throw new RuntimeException(
048                            "Unable to create instance of mask formatter class: " + maskFormatterClass.getName());
049                }
050            } else {
051                throw new RuntimeException("Mask formatter not set for secure field.");
052            }
053        }
054
055        return maskFormatter.maskValue(value);
056    }
057
058    /**
059     * Gets the maskFormatter attribute.
060     *
061     * @return Returns the maskFormatter.
062     */
063    @BeanTagAttribute(name = "maskFormater", type = BeanTagAttribute.AttributeType.SINGLEBEAN)
064    public MaskFormatter getMaskFormatter() {
065        return maskFormatter;
066    }
067
068    /**
069     * @param maskFormatter instance to be used for masking field values.
070     */
071    public void setMaskFormatter(MaskFormatter maskFormatter) {
072        this.maskFormatter = maskFormatter;
073    }
074
075    /**
076     * Gets the maskFormatterClass attribute.
077     *
078     * @return Returns the maskFormatterClass.
079     */
080    @BeanTagAttribute(name = "maskFormatterClass", type = BeanTagAttribute.AttributeType.SINGLEBEAN)
081    public Class<? extends MaskFormatter> getMaskFormatterClass() {
082        return maskFormatterClass;
083    }
084
085    /**
086     * @param maskFormatterClass element is used when a custom masking
087     * algorithm is desired.  This element specifies the name of a
088     * class that will perform the masking for unauthorized users.
089     */
090    public void setMaskFormatterClass(Class<? extends MaskFormatter> maskFormatterClass) {
091        this.maskFormatterClass = maskFormatterClass;
092    }
093
094}