View Javadoc
1   package org.pojomatic.formatter;
2   
3   import java.util.Arrays;
4   
5   import org.pojomatic.annotations.Property;
6   
7   /**
8    * A property formatter which shows only the last 4 characters of the string representation of the
9    * {@link Property}, with all others replaced by an asterisk ('*').
10   * Useful for credit card numbers, social security numbers, etc.
11   * <p>
12   * For example, a 16 character {@code String} representing a credit card number would
13   * be formatted as "************1234".
14   *
15   * @deprecated While this formatter can prevent the toString representation of an object including a full account
16   * number, the full number can still be visible in heap dumps. A more secure approach is to avoid storing plaintext
17   * private account numbers in memory at all (or at most, for as long as it takes to encrypt them). This formatter will
18   * be removed in a future release of Pojomatic. Clients who feel they still need this functionality should implement it
19   * themselves.
20   */
21  @Deprecated
22  public class AccountNumberFormatter extends DefaultPropertyFormatter {
23    private static final int DEFAULT_PLAINTEXT_CHARS = 4;
24    private static final int DEFAULT_FILL_CHAR = '*';
25  
26    private int plaintextChars = DEFAULT_PLAINTEXT_CHARS;
27    private char fillChar = DEFAULT_FILL_CHAR;
28  
29    @Override
30    public String format(Object value) {
31      String rep = super.format(value);
32      int repLength = rep.length();
33      if (repLength <= getPlaintextChars()) {
34        return rep;
35      } else {
36        char[] repChars = rep.toCharArray();
37        Arrays.fill(repChars, 0, repLength - getPlaintextChars(), getFillChar());
38        return String.valueOf(repChars);
39      }
40    }
41  
42    protected int getPlaintextChars() {
43      return plaintextChars;
44    }
45  
46    protected void setPlaintextChars(int plaintextChars) {
47      this.plaintextChars = plaintextChars;
48    }
49  
50    protected char getFillChar() {
51      return fillChar;
52    }
53  
54    protected void setFillChar(char fillChar) {
55      this.fillChar = fillChar;
56    }
57  
58  }