|
12 | 12 |
|
13 | 13 | import java.io.Serializable; |
14 | 14 | import java.nio.charset.StandardCharsets; |
| 15 | +import java.util.List; |
15 | 16 | import java.util.zip.ZipOutputStream; |
16 | 17 |
|
17 | 18 | public class CloneMasker { |
@@ -40,73 +41,85 @@ public static <T extends Serializable> void copyOut( |
40 | 41 | maskCopyOut(zos, copyOut, masker); |
41 | 42 | } |
42 | 43 |
|
43 | | - private static void maskCopyOut( |
44 | | - final ZipOutputStream zos, |
45 | | - final CopyOut copyOut, |
46 | | - final TableMasker masker |
47 | | - ) throws CloneException { |
48 | | - final var headerRow = CloneException.wrapKnownExceptions( |
49 | | - () -> copyOut.readFromCopy(), |
50 | | - CloneError.COPY_OUT_READ_HEADER_FAILED |
51 | | - ); |
| 44 | + private static void maskCopyOut( |
| 45 | + final ZipOutputStream zos, |
| 46 | + final CopyOut copyOut, |
| 47 | + final TableMasker masker |
| 48 | + ) throws CloneException { |
| 49 | + final var headerRow = CloneException.wrapKnownExceptions( |
| 50 | + () -> copyOut.readFromCopy(), |
| 51 | + CloneError.COPY_OUT_READ_HEADER_FAILED |
| 52 | + ); |
| 53 | + |
| 54 | + if (headerRow == null) { |
| 55 | + throw CloneError.COPY_OUT_HEADER_EMPTY.toException(); |
| 56 | + } |
| 57 | + |
| 58 | + CloneException.wrapKnownExceptions( |
| 59 | + () -> zos.write(headerRow), |
| 60 | + CloneError.COPY_OUT_WRITE_HEADER_FAILED |
| 61 | + ); |
52 | 62 |
|
53 | | - if (headerRow == null) { |
54 | | - throw CloneError.COPY_OUT_HEADER_EMPTY.toException(); |
| 63 | + final var headers = parseCsvValues( |
| 64 | + headerRow, |
| 65 | + CloneError.COPY_OUT_HEADER_PARSER_FAILED, |
| 66 | + CloneError.COPY_OUT_HEADER_PARSER_CLOSE_FAILED |
| 67 | + ); |
| 68 | + |
| 69 | + byte[] row; |
| 70 | + while((row = CloneException.wrapKnownExceptions( |
| 71 | + () -> copyOut.readFromCopy(), |
| 72 | + CloneError.COPY_OUT_READ_ROW_FAILED |
| 73 | + )) != null) { |
| 74 | + final var values = parseCsvValues( |
| 75 | + row, |
| 76 | + CloneError.COPY_OUT_ROW_PARSER_FAILED, |
| 77 | + CloneError.COPY_OUT_ROW_PARSER_CLOSE_FAILED |
| 78 | + ); |
| 79 | + |
| 80 | + final var rowToBeMasked = new TableMasker.Row(headers, values); |
| 81 | + final var maskedRow = masker.mask(rowToBeMasked); |
| 82 | + |
| 83 | + final var maskedCsvRow = printCsvRow(maskedRow); |
| 84 | + CloneException.wrapKnownExceptions( |
| 85 | + () -> zos.write(maskedCsvRow.getBytes(StandardCharsets.UTF_8)), |
| 86 | + CloneError.COPY_OUT_WRITE_ROW_FAILED |
| 87 | + ); |
| 88 | + } |
55 | 89 | } |
56 | 90 |
|
| 91 | + private static String printCsvRow(final TableMasker.Row maskedRow) throws CloneException { |
| 92 | + final var stringBuilder = new StringBuilder(); |
| 93 | + final var printer = CloneException.wrapKnownExceptions( |
| 94 | + () -> new CSVPrinter(stringBuilder, CSVFormat.POSTGRESQL_CSV), |
| 95 | + CloneError.COPY_OUT_PRINTER_CREATION_FAILED |
| 96 | + ); |
57 | 97 | CloneException.wrapKnownExceptions( |
58 | | - () -> zos.write(headerRow), |
59 | | - CloneError.COPY_OUT_WRITE_HEADER_FAILED |
| 98 | + () -> printer.printRecord(maskedRow.values()), |
| 99 | + CloneError.COPY_OUT_PRINT_ROW_FAILED |
60 | 100 | ); |
| 101 | + CloneException.wrapKnownExceptions( |
| 102 | + () -> printer.close(), |
| 103 | + CloneError.COPY_OUT_PRINTER_CLOSE_FAILED |
| 104 | + ); |
| 105 | + return stringBuilder.toString(); |
| 106 | + } |
61 | 107 |
|
62 | | - final var headerLine = new String(headerRow, StandardCharsets.UTF_8); |
63 | | - |
64 | | - final var headerParser = CloneException.wrapKnownExceptions( |
65 | | - () -> CSVParser.parse(headerLine, CSVFormat.POSTGRESQL_CSV), |
66 | | - CloneError.COPY_OUT_HEADER_PARSER_FAILED |
| 108 | + private static List<String> parseCsvValues( |
| 109 | + final byte[] row, |
| 110 | + final CloneError copyOutRowParserFailed, |
| 111 | + final CloneError copyOutRowParserCloseFailed |
| 112 | + ) throws CloneException { |
| 113 | + final var line = new String(row, StandardCharsets.UTF_8); |
| 114 | + final var parser = CloneException.wrapKnownExceptions( |
| 115 | + () -> CSVParser.parse(line, CSVFormat.POSTGRESQL_CSV), |
| 116 | + copyOutRowParserFailed |
67 | 117 | ); |
68 | | - final var headers = headerParser.getRecords().getFirst().toList(); |
| 118 | + final var values = parser.getRecords().getFirst().toList(); |
69 | 119 | CloneException.wrapKnownExceptions( |
70 | | - headerParser::close, |
71 | | - CloneError.COPY_OUT_HEADER_PARSER_CLOSE_FAILED |
| 120 | + parser::close, |
| 121 | + copyOutRowParserCloseFailed |
72 | 122 | ); |
73 | | - |
74 | | - byte[] row; |
75 | | - while((row = CloneException.wrapKnownExceptions( |
76 | | - () -> copyOut.readFromCopy(), |
77 | | - CloneError.COPY_OUT_READ_ROW_FAILED |
78 | | - )) != null) { |
79 | | - final var line = new String(row, StandardCharsets.UTF_8); |
80 | | - final var parser = CloneException.wrapKnownExceptions( |
81 | | - () -> CSVParser.parse(line, CSVFormat.POSTGRESQL_CSV), |
82 | | - CloneError.COPY_OUT_ROW_PARSER_FAILED |
83 | | - ); |
84 | | - final var values = parser.getRecords().getFirst().toList(); |
85 | | - CloneException.wrapKnownExceptions( |
86 | | - parser::close, |
87 | | - CloneError.COPY_OUT_ROW_PARSER_CLOSE_FAILED |
88 | | - ); |
89 | | - |
90 | | - final var rowToBeMasked = new TableMasker.Row(headers, values); |
91 | | - final var maskedRow = masker.mask(rowToBeMasked); |
92 | | - |
93 | | - final var stringBuilder = new StringBuilder(); |
94 | | - final var printer = CloneException.wrapKnownExceptions( |
95 | | - () -> new CSVPrinter(stringBuilder, CSVFormat.POSTGRESQL_CSV), |
96 | | - CloneError.COPY_OUT_PRINTER_CREATION_FAILED |
97 | | - ); |
98 | | - CloneException.wrapKnownExceptions( |
99 | | - () -> printer.printRecord(maskedRow.values()), |
100 | | - CloneError.COPY_OUT_PRINT_ROW_FAILED |
101 | | - ); |
102 | | - CloneException.wrapKnownExceptions( |
103 | | - () -> printer.close(), |
104 | | - CloneError.COPY_OUT_PRINTER_CLOSE_FAILED |
105 | | - ); |
106 | | - CloneException.wrapKnownExceptions( |
107 | | - () -> zos.write(stringBuilder.toString().getBytes(StandardCharsets.UTF_8)), |
108 | | - CloneError.COPY_OUT_WRITE_ROW_FAILED |
109 | | - ); |
110 | | - } |
| 123 | + return values; |
111 | 124 | } |
112 | 125 | } |
0 commit comments