Skip to content

Commit

Permalink
add spotbugs (airbytehq#10522)
Browse files Browse the repository at this point in the history
  • Loading branch information
cgardens authored Mar 11, 2022
1 parent e27bb74 commit 5fde59f
Show file tree
Hide file tree
Showing 83 changed files with 303 additions and 236 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public void testGetTailExists() throws IOException {
"line7",
"line8");

final Writer writer = new BufferedWriter(new FileWriter(stdoutFile.toString(), true));
final Writer writer = new BufferedWriter(new FileWriter(stdoutFile.toString(), StandardCharsets.UTF_8, true));

for (final String line : Iterables.concat(head, expectedTail)) {
writer.write(line + "\n");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
Expand Down Expand Up @@ -55,7 +56,7 @@ void testSerializeJsonNode() {
assertEquals(
"{\"test\":\"dGVzdA==\"}",
Jsons.serialize(Jsons.jsonNode(ImmutableMap.of(
"test", new BinaryNode("test".getBytes())))));
"test", new BinaryNode("test".getBytes(StandardCharsets.UTF_8))))));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
*/
public class YamlSeedConfigPersistence implements ConfigPersistence {

public static Class<?> DEFAULT_SEED_DEFINITION_RESOURCE_CLASS = SeedType.class;
public static final Class<?> DEFAULT_SEED_DEFINITION_RESOURCE_CLASS = SeedType.class;

private static final Map<AirbyteConfig, SeedType> CONFIG_SCHEMA_MAP = Map.of(
ConfigSchema.STANDARD_SOURCE_DEFINITION, SeedType.STANDARD_SOURCE_DEFINITION,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Supplier;
Expand Down Expand Up @@ -89,7 +90,7 @@ public List<String> tailCloudLog(final LogConfigs configs, final String logPath,
final var poppedBlob = descendingTimestampBlobs.remove(0);
try (final var inMemoryData = new ByteArrayOutputStream()) {
poppedBlob.downloadTo(inMemoryData);
final var currFileLines = inMemoryData.toString().split("\n");
final var currFileLines = inMemoryData.toString(StandardCharsets.UTF_8).split("\n");
final List<String> currFileLinesReversed = Lists.reverse(List.of(currFileLines));
for (final var line : currFileLinesReversed) {
if (linesRead == numLines) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public List<String> getJobLogFile(final WorkerEnvironment workerEnvironment, fin
*/
@VisibleForTesting
public void deleteLogs(final WorkerEnvironment workerEnvironment, final LogConfigs logConfigs, final String logPath) {
if (logPath == null || logPath.equals(Path.of(""))) {
if (logPath == null || logPath.equals("")) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Supplier;
Expand Down Expand Up @@ -172,7 +173,7 @@ private static ArrayList<String> getCurrFile(final S3Client s3Client, final Stri
final var data = s3Client.getObjectAsBytes(getObjReq).asByteArray();
final var is = new ByteArrayInputStream(data);
final var currentFileLines = new ArrayList<String>();
try (final var reader = new BufferedReader(new InputStreamReader(is))) {
try (final var reader = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8))) {
String temp;
while ((temp = reader.readLine()) != null) {
currentFileLines.add(temp);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ public class JsonSecretsProcessor {

private static final Logger LOGGER = LoggerFactory.getLogger(JsonSecretsProcessor.class);

public static String AIRBYTE_SECRET_FIELD = "airbyte_secret";
public static final String AIRBYTE_SECRET_FIELD = "airbyte_secret";
public static final String PROPERTIES_FIELD = "properties";
public static String TYPE_FIELD = "type";
public static String ARRAY_TYPE_FIELD = "array";
public static String ITEMS_FIELD = "items";
public static final String TYPE_FIELD = "type";
public static final String ARRAY_TYPE_FIELD = "array";
public static final String ITEMS_FIELD = "items";

private static final JsonSchemaValidator VALIDATOR = new JsonSchemaValidator();

Expand Down
30 changes: 19 additions & 11 deletions airbyte-db/lib/src/main/java/io/airbyte/db/DataTypeUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,18 @@ public class DataTypeUtils {
private static final Logger LOGGER = LoggerFactory.getLogger(DataTypeUtils.class);

public static final String DATE_FORMAT_PATTERN = "yyyy-MM-dd'T'HH:mm:ss'Z'";
public static final DateFormat DATE_FORMAT = new SimpleDateFormat(DATE_FORMAT_PATTERN); // Quoted "Z" to indicate UTC, no timezone offset

public static final String DATE_FORMAT_WITH_MILLISECONDS_PATTERN = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";
public static final DateFormat DATE_FORMAT_WITH_MILLISECONDS = new SimpleDateFormat(DATE_FORMAT_WITH_MILLISECONDS_PATTERN);

// wrap SimpleDateFormat in a function because SimpleDateFormat is not threadsafe as a static final.
public static DateFormat getDateFormat() {
return new SimpleDateFormat(DATE_FORMAT_PATTERN); // Quoted "Z" to indicate UTC, no timezone offset;
}

// wrap SimpleDateFormat in a function because SimpleDateFormat is not threadsafe as a static final.
public static DateFormat getDateFormatMillisPattern() {
return new SimpleDateFormat(DATE_FORMAT_WITH_MILLISECONDS_PATTERN);
}

public static <T> T returnNullIfInvalid(final DataTypeSupplier<T> valueProducer) {
return returnNullIfInvalid(valueProducer, ignored -> true);
Expand All @@ -44,15 +52,15 @@ public static <T> T returnNullIfInvalid(final DataTypeSupplier<T> valueProducer,
}
}

public static String toISO8601StringWithMicroseconds(Instant instant) {
public static String toISO8601StringWithMicroseconds(final Instant instant) {

String dateWithMilliseconds = DATE_FORMAT_WITH_MILLISECONDS.format(Date.from(instant));
final String dateWithMilliseconds = getDateFormatMillisPattern().format(Date.from(instant));
return dateWithMilliseconds.substring(0, 23) + calculateMicrosecondsString(instant.getNano()) + dateWithMilliseconds.substring(23);
}

private static String calculateMicrosecondsString(int nano) {
var microSeconds = (nano / 1000) % 1000;
String result;
private static String calculateMicrosecondsString(final int nano) {
final var microSeconds = (nano / 1000) % 1000;
final String result;
if (microSeconds < 10) {
result = "00" + microSeconds;
} else if (microSeconds < 100) {
Expand All @@ -64,15 +72,15 @@ private static String calculateMicrosecondsString(int nano) {
}

public static String toISO8601StringWithMilliseconds(final long epochMillis) {
return DATE_FORMAT_WITH_MILLISECONDS.format(Date.from(Instant.ofEpochMilli(epochMillis)));
return getDateFormatMillisPattern().format(Date.from(Instant.ofEpochMilli(epochMillis)));
}

public static String toISO8601String(final long epochMillis) {
return DATE_FORMAT.format(Date.from(Instant.ofEpochMilli(epochMillis)));
return getDateFormat().format(Date.from(Instant.ofEpochMilli(epochMillis)));
}

public static String toISO8601String(final java.util.Date date) {
return DATE_FORMAT.format(date);
return getDateFormat().format(date);
}

public static String toISOTimeString(final LocalDateTime dateTime) {
Expand All @@ -88,7 +96,7 @@ public static String toISO8601String(final LocalDateTime date) {
}

public static String toISO8601String(final Duration duration) {
return DATE_FORMAT.format(Date.from(Instant.ofEpochSecond(Math.abs(duration.getSeconds()), Math.abs(duration.getNano()))));
return getDateFormat().format(Date.from(Instant.ofEpochSecond(Math.abs(duration.getSeconds()), Math.abs(duration.getNano()))));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public Date getDateValue(final FieldValue fieldValue, final DateFormat dateForma
}

@Override
public JsonSchemaType getJsonType(StandardSQLTypeName bigQueryType) {
public JsonSchemaType getJsonType(final StandardSQLTypeName bigQueryType) {
return switch (bigQueryType) {
case BOOL -> JsonSchemaType.BOOLEAN;
case INT64, FLOAT64, NUMERIC, BIGNUMERIC -> JsonSchemaType.NUMBER;
Expand All @@ -129,11 +129,11 @@ public JsonSchemaType getJsonType(StandardSQLTypeName bigQueryType) {
private String getFormattedValue(final StandardSQLTypeName paramType, final String paramValue) {
try {
return switch (paramType) {
case DATE -> BIG_QUERY_DATE_FORMAT.format(DataTypeUtils.DATE_FORMAT.parse(paramValue));
case DATE -> BIG_QUERY_DATE_FORMAT.format(DataTypeUtils.getDateFormat().parse(paramValue));
case DATETIME -> BIG_QUERY_DATETIME_FORMAT
.format(DataTypeUtils.DATE_FORMAT.parse(paramValue));
.format(DataTypeUtils.getDateFormat().parse(paramValue));
case TIMESTAMP -> BIG_QUERY_TIMESTAMP_FORMAT
.format(DataTypeUtils.DATE_FORMAT.parse(paramValue));
.format(DataTypeUtils.getDateFormat().parse(paramValue));
default -> paramValue;
};
} catch (final ParseException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.List;
Expand Down Expand Up @@ -77,7 +78,7 @@ public static void createNextMigrationFile(final String dbIdentifier, final Flyw
final File file = new File(Path.of(filePath).toUri());
FileUtils.forceMkdirParent(file);

try (final PrintWriter writer = new PrintWriter(file)) {
try (final PrintWriter writer = new PrintWriter(file, StandardCharsets.UTF_8)) {
writer.println(newMigration);
} catch (final FileNotFoundException e) {
throw new IOException(e);
Expand All @@ -93,7 +94,7 @@ public static Optional<MigrationVersion> getSecondToLastMigrationVersion(final F
}

public static void dumpSchema(final String schema, final String schemaDumpFile, final boolean printSchema) throws IOException {
try (final PrintWriter writer = new PrintWriter(new File(Path.of(schemaDumpFile).toUri()))) {
try (final PrintWriter writer = new PrintWriter(new File(Path.of(schemaDumpFile).toUri()), StandardCharsets.UTF_8)) {
writer.println(schema);
if (printSchema) {
System.out.println("\n==== Schema ====\n" + schema);
Expand Down Expand Up @@ -138,7 +139,7 @@ private static Optional<MigrationVersion> getLastMigrationVersion(final FlywayDa

@VisibleForTesting
static AirbyteVersion getCurrentAirbyteVersion() {
try (final BufferedReader reader = new BufferedReader(new FileReader("../../.env"))) {
try (final BufferedReader reader = new BufferedReader(new FileReader("../../.env", StandardCharsets.UTF_8))) {
String line;
while ((line = reader.readLine()) != null) {
if (line.startsWith("VERSION")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ public JsonNode rowToJson(final ResultSet queryContext) throws SQLException {
}

protected void putArray(final ObjectNode node, final String columnName, final ResultSet resultSet, final int index) throws SQLException {
ArrayNode arrayNode = new ObjectMapper().createArrayNode();
ResultSet arrayResultSet = resultSet.getArray(index).getResultSet();
final ArrayNode arrayNode = new ObjectMapper().createArrayNode();
final ResultSet arrayResultSet = resultSet.getArray(index).getResultSet();
while (arrayResultSet.next()) {
arrayNode.add(arrayResultSet.getString(2));
}
Expand Down Expand Up @@ -144,19 +144,19 @@ protected void setTimestamp(final PreparedStatement preparedStatement, final int
// value in the following format
try {
var valueWithoutMicros = value;
StringBuilder nanos = new StringBuilder();
var dotIndex = value.indexOf(".");
final StringBuilder nanos = new StringBuilder();
final var dotIndex = value.indexOf(".");
if (dotIndex > 0) {
var micro = value.substring(value.lastIndexOf('.') + 1, value.length() - 1);
final var micro = value.substring(value.lastIndexOf('.') + 1, value.length() - 1);
nanos.append(micro);
valueWithoutMicros = value.replace("." + micro, "");
}
while (nanos.length() != 9) {
nanos.append("0");
}

var timestamp = Timestamp
.from(DataTypeUtils.DATE_FORMAT.parse(valueWithoutMicros).toInstant());
final var timestamp = Timestamp
.from(DataTypeUtils.getDateFormat().parse(valueWithoutMicros).toInstant());
timestamp.setNanos(Integer.parseInt(nanos.toString()));
preparedStatement.setTimestamp(parameterIndex, timestamp);
} catch (final ParseException e) {
Expand All @@ -166,7 +166,7 @@ protected void setTimestamp(final PreparedStatement preparedStatement, final int

protected void setDate(final PreparedStatement preparedStatement, final int parameterIndex, final String value) throws SQLException {
try {
final Timestamp from = Timestamp.from(DataTypeUtils.DATE_FORMAT.parse(value).toInstant());
final Timestamp from = Timestamp.from(DataTypeUtils.getDateFormat().parse(value).toInstant());
preparedStatement.setDate(parameterIndex, new Date(from.getTime()));
} catch (final ParseException e) {
throw new RuntimeException(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import io.sentry.Sentry;
import io.sentry.SentryLevel;
import io.sentry.SpanStatus;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -172,7 +173,7 @@ private void runInternal(final IntegrationConfig parsed) throws Exception {
static void consumeWriteStream(final AirbyteMessageConsumer consumer) throws Exception {
// use a Scanner that only processes new line characters to strictly abide with the
// https://jsonlines.org/ standard
final Scanner input = new Scanner(System.in).useDelimiter("[\r\n]+");
final Scanner input = new Scanner(System.in, StandardCharsets.UTF_8).useDelimiter("[\r\n]+");
consumer.start();
while (input.hasNext()) {
final String inputString = input.next();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.Instant;
Expand Down Expand Up @@ -260,7 +261,7 @@ void testDestinationConsumerLifecycleSuccess() throws Exception {
.withData(Jsons.deserialize("{ \"checkpoint\": \"1\" }")));
System.setIn(new ByteArrayInputStream((Jsons.serialize(message1) + "\n"
+ Jsons.serialize(message2) + "\n"
+ Jsons.serialize(stateMessage)).getBytes()));
+ Jsons.serialize(stateMessage)).getBytes(StandardCharsets.UTF_8)));

try (final AirbyteMessageConsumer airbyteMessageConsumerMock = mock(AirbyteMessageConsumer.class)) {
IntegrationRunner.consumeWriteStream(airbyteMessageConsumerMock);
Expand All @@ -285,7 +286,7 @@ void testDestinationConsumerLifecycleFailure() throws Exception {
.withData(Jsons.deserialize("{ \"color\": \"yellow\" }"))
.withStream(STREAM_NAME)
.withEmittedAt(EMITTED_AT));
System.setIn(new ByteArrayInputStream((Jsons.serialize(message1) + "\n" + Jsons.serialize(message2)).getBytes()));
System.setIn(new ByteArrayInputStream((Jsons.serialize(message1) + "\n" + Jsons.serialize(message2)).getBytes(StandardCharsets.UTF_8)));

try (final AirbyteMessageConsumer airbyteMessageConsumerMock = mock(AirbyteMessageConsumer.class)) {
doThrow(new IOException("error")).when(airbyteMessageConsumerMock).accept(message1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ private static ByteBuffer stringToByteBuffer(final String s) {
@SuppressWarnings("unchecked")
private Map<ByteBuffer, ByteBuffer> load() {
try (final SafeObjectInputStream is = new SafeObjectInputStream(Files.newInputStream(offsetFilePath))) {
// todo (cgardens) - we currently suppress a security warning for this line. use of readObject from
// untrusted sources is considered unsafe. Since the source is controlled by us in this case it
// should be safe. That said, changing this implementation to not use readObject would remove some
// headache.
final Object obj = is.readObject();
if (!(obj instanceof HashMap))
throw new ConnectException("Expected HashMap but found " + obj.getClass());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ public void storeRecord(final HistoryRecord record) throws DatabaseHistoryExcept
public void stop() {
fileDatabaseHistory.stop();
// this is just for tests
resetDbName();
}

public static void resetDbName() {
databaseName = null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import io.airbyte.db.DataTypeUtils;
import io.debezium.spi.converter.CustomConverter;
import io.debezium.spi.converter.RelationalColumn;
import java.nio.charset.StandardCharsets;
import java.time.LocalDate;
import java.util.Arrays;
import java.util.Properties;
Expand Down Expand Up @@ -50,7 +51,7 @@ private void registerText(final RelationalColumn field, final ConverterRegistrat
return DebeziumConverterUtils.convertDefaultValue(field);
}
if (x instanceof byte[]) {
return new String((byte[]) x);
return new String((byte[]) x, StandardCharsets.UTF_8);
} else {
return x.toString();
}
Expand All @@ -62,7 +63,7 @@ private void registerText(final RelationalColumn field, final ConverterRegistrat
* the doc, it should be done by driver, but it fails.
*/
private Object convertDefaultValueNullDate(final RelationalColumn field) {
var defaultValue = DebeziumConverterUtils.convertDefaultValue(field);
final var defaultValue = DebeziumConverterUtils.convertDefaultValue(field);
return (defaultValue == null && !field.isOptional() ? DataTypeUtils.toISO8601String(LocalDate.EPOCH) : defaultValue);
}

Expand Down
Loading

0 comments on commit 5fde59f

Please sign in to comment.