diff --git a/server/src/main/java/org/opensearch/common/Classes.java b/server/src/main/java/org/opensearch/common/Classes.java index 1b297639aff6a..1fb7fde5f963b 100644 --- a/server/src/main/java/org/opensearch/common/Classes.java +++ b/server/src/main/java/org/opensearch/common/Classes.java @@ -41,25 +41,6 @@ */ public class Classes { - /** - * The package separator character '.' - */ - private static final char PACKAGE_SEPARATOR = '.'; - - /** - * Determine the name of the package of the given class: - * e.g. "java.lang" for the java.lang.String class. - * - * @param clazz the class - * @return the package name, or the empty String if the class - * is defined in the default package - */ - public static String getPackageName(Class clazz) { - String className = clazz.getName(); - int lastDotIndex = className.lastIndexOf(PACKAGE_SEPARATOR); - return (lastDotIndex != -1 ? className.substring(0, lastDotIndex) : ""); - } - public static boolean isInnerClass(Class clazz) { return !Modifier.isStatic(clazz.getModifiers()) && clazz.getEnclosingClass() != null; } diff --git a/server/src/main/java/org/opensearch/common/LegacyTimeBasedUUIDGenerator.java b/server/src/main/java/org/opensearch/common/LegacyTimeBasedUUIDGenerator.java deleted file mode 100644 index 1e2d9b87281d6..0000000000000 --- a/server/src/main/java/org/opensearch/common/LegacyTimeBasedUUIDGenerator.java +++ /dev/null @@ -1,102 +0,0 @@ -/* - * SPDX-License-Identifier: Apache-2.0 - * - * The OpenSearch Contributors require contributions made to - * this file be licensed under the Apache-2.0 license or a - * compatible open source license. - */ - -/* - * Licensed to Elasticsearch under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch licenses this file to you under - * the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -/* - * Modifications Copyright OpenSearch Contributors. See - * GitHub history for details. - */ - -package org.opensearch.common; - -import java.util.Base64; -import java.util.concurrent.atomic.AtomicInteger; - -/** - * These are essentially flake ids, but we use 6 (not 8) bytes for timestamp, and use 3 (not 2) bytes for sequence number. - * For more information about flake ids, check out - * https://archive.fo/2015.07.08-082503/http://www.boundary.com/blog/2012/01/flake-a-decentralized-k-ordered-unique-id-generator-in-erlang/ - * - * @opensearch.internal - */ - -class LegacyTimeBasedUUIDGenerator implements UUIDGenerator { - - // We only use bottom 3 bytes for the sequence number. Paranoia: init with random int so that if JVM/OS/machine goes down, clock slips - // backwards, and JVM comes back up, we are less likely to be on the same sequenceNumber at the same time: - private final AtomicInteger sequenceNumber = new AtomicInteger(SecureRandomHolder.INSTANCE.nextInt()); - - // Used to ensure clock moves forward: - private long lastTimestamp; - - private static final byte[] SECURE_MUNGED_ADDRESS = MacAddressProvider.getSecureMungedAddress(); - - static { - assert SECURE_MUNGED_ADDRESS.length == 6; - } - - /** Puts the lower numberOfLongBytes from l into the array, starting index pos. */ - private static void putLong(byte[] array, long l, int pos, int numberOfLongBytes) { - for (int i = 0; i < numberOfLongBytes; ++i) { - array[pos + numberOfLongBytes - i - 1] = (byte) (l >>> (i * 8)); - } - } - - @Override - public String getBase64UUID() { - final int sequenceId = sequenceNumber.incrementAndGet() & 0xffffff; - long timestamp = System.currentTimeMillis(); - - synchronized (this) { - // Don't let timestamp go backwards, at least "on our watch" (while this JVM is running). We are still vulnerable if we are - // shut down, clock goes backwards, and we restart... for this we randomize the sequenceNumber on init to decrease chance of - // collision: - timestamp = Math.max(lastTimestamp, timestamp); - - if (sequenceId == 0) { - // Always force the clock to increment whenever sequence number is 0, in case we have a long time-slip backwards: - timestamp++; - } - - lastTimestamp = timestamp; - } - - final byte[] uuidBytes = new byte[15]; - - // Only use lower 6 bytes of the timestamp (this will suffice beyond the year 10000): - putLong(uuidBytes, timestamp, 0, 6); - - // MAC address adds 6 bytes: - System.arraycopy(SECURE_MUNGED_ADDRESS, 0, uuidBytes, 6, SECURE_MUNGED_ADDRESS.length); - - // Sequence number adds 3 bytes: - putLong(uuidBytes, sequenceId, 12, 3); - - assert 9 + SECURE_MUNGED_ADDRESS.length == uuidBytes.length; - - return Base64.getUrlEncoder().withoutPadding().encodeToString(uuidBytes); - } -} diff --git a/server/src/main/java/org/opensearch/common/Numbers.java b/server/src/main/java/org/opensearch/common/Numbers.java index 7a87cd58b0e29..dbcde890e8fe2 100644 --- a/server/src/main/java/org/opensearch/common/Numbers.java +++ b/server/src/main/java/org/opensearch/common/Numbers.java @@ -57,28 +57,6 @@ public static long bytesToLong(BytesRef bytes) { return (((long) high) << 32) | (low & 0x0ffffffffL); } - public static byte[] intToBytes(int val) { - byte[] arr = new byte[4]; - arr[0] = (byte) (val >>> 24); - arr[1] = (byte) (val >>> 16); - arr[2] = (byte) (val >>> 8); - arr[3] = (byte) (val); - return arr; - } - - /** - * Converts an int to a byte array. - * - * @param val The int to convert to a byte array - * @return The byte array converted - */ - public static byte[] shortToBytes(int val) { - byte[] arr = new byte[2]; - arr[0] = (byte) (val >>> 8); - arr[1] = (byte) (val); - return arr; - } - /** * Converts a long to a byte array. * @@ -98,16 +76,6 @@ public static byte[] longToBytes(long val) { return arr; } - /** - * Converts a double to a byte array. - * - * @param val The double to convert to a byte array - * @return The byte array converted - */ - public static byte[] doubleToBytes(double val) { - return longToBytes(Double.doubleToRawLongBits(val)); - } - /** Returns true if value is neither NaN nor infinite. */ public static boolean isValidDouble(double value) { if (Double.isNaN(value) || Double.isInfinite(value)) { diff --git a/server/src/main/java/org/opensearch/common/RandomBasedUUIDGenerator.java b/server/src/main/java/org/opensearch/common/RandomBasedUUIDGenerator.java index fdc53d8335c2f..f83ef930688f8 100644 --- a/server/src/main/java/org/opensearch/common/RandomBasedUUIDGenerator.java +++ b/server/src/main/java/org/opensearch/common/RandomBasedUUIDGenerator.java @@ -32,9 +32,6 @@ package org.opensearch.common; -import org.opensearch.common.settings.SecureString; - -import java.util.Arrays; import java.util.Base64; import java.util.Random; @@ -54,27 +51,6 @@ public String getBase64UUID() { return getBase64UUID(SecureRandomHolder.INSTANCE); } - /** - * Returns a Base64 encoded {@link SecureString} of a Version 4.0 compatible UUID - * as defined here: http://www.ietf.org/rfc/rfc4122.txt - */ - public SecureString getBase64UUIDSecureString() { - byte[] uuidBytes = null; - byte[] encodedBytes = null; - try { - uuidBytes = getUUIDBytes(SecureRandomHolder.INSTANCE); - encodedBytes = Base64.getUrlEncoder().withoutPadding().encode(uuidBytes); - return new SecureString(CharArrays.utf8BytesToChars(encodedBytes)); - } finally { - if (uuidBytes != null) { - Arrays.fill(uuidBytes, (byte) 0); - } - if (encodedBytes != null) { - Arrays.fill(encodedBytes, (byte) 0); - } - } - } - /** * Returns a Base64 encoded version of a Version 4.0 compatible UUID * randomly initialized by the given {@link java.util.Random} instance diff --git a/server/src/main/java/org/opensearch/common/Strings.java b/server/src/main/java/org/opensearch/common/Strings.java index 68b22589de76e..7ec053522c5a6 100644 --- a/server/src/main/java/org/opensearch/common/Strings.java +++ b/server/src/main/java/org/opensearch/common/Strings.java @@ -80,67 +80,6 @@ public static void spaceify(int spaces, String from, StringBuilder to) throws Ex } } - /** - * Splits a backslash escaped string on the separator. - *

- * Current backslash escaping supported: - *
\n \t \r \b \f are escaped the same as a Java String - *
Other characters following a backslash are produced verbatim (\c => c) - * - * @param s the string to split - * @param separator the separator to split on - * @param decode decode backslash escaping - */ - public static List splitSmart(String s, String separator, boolean decode) { - ArrayList lst = new ArrayList<>(2); - StringBuilder sb = new StringBuilder(); - int pos = 0, end = s.length(); - while (pos < end) { - if (s.startsWith(separator, pos)) { - if (sb.length() > 0) { - lst.add(sb.toString()); - sb = new StringBuilder(); - } - pos += separator.length(); - continue; - } - - char ch = s.charAt(pos++); - if (ch == '\\') { - if (!decode) sb.append(ch); - if (pos >= end) break; // ERROR, or let it go? - ch = s.charAt(pos++); - if (decode) { - switch (ch) { - case 'n': - ch = '\n'; - break; - case 't': - ch = '\t'; - break; - case 'r': - ch = '\r'; - break; - case 'b': - ch = '\b'; - break; - case 'f': - ch = '\f'; - break; - } - } - } - - sb.append(ch); - } - - if (sb.length() > 0) { - lst.add(sb.toString()); - } - - return lst; - } - // --------------------------------------------------------------------- // General convenience methods for working with Strings // --------------------------------------------------------------------- @@ -303,7 +242,7 @@ public static String replace(String inString, String oldPattern, String newPatte // the index of an occurrence we've found, or -1 int patLen = oldPattern.length(); while (index >= 0) { - sb.append(inString.substring(pos, index)); + sb.append(inString, pos, index); sb.append(newPattern); pos = index + patLen; index = inString.indexOf(oldPattern, pos); @@ -875,10 +814,6 @@ public static boolean isNullOrEmpty(@Nullable String s) { return s == null || s.isEmpty(); } - public static String coalesceToEmpty(@Nullable String s) { - return s == null ? "" : s; - } - public static String padStart(String s, int minimumLength, char c) { if (s == null) { throw new NullPointerException("s"); diff --git a/server/src/main/java/org/opensearch/common/UUIDs.java b/server/src/main/java/org/opensearch/common/UUIDs.java index a04a10430254f..c7d14878e8bd4 100644 --- a/server/src/main/java/org/opensearch/common/UUIDs.java +++ b/server/src/main/java/org/opensearch/common/UUIDs.java @@ -32,8 +32,6 @@ package org.opensearch.common; -import org.opensearch.common.settings.SecureString; - import java.util.Random; /** @@ -44,7 +42,6 @@ public class UUIDs { private static final RandomBasedUUIDGenerator RANDOM_UUID_GENERATOR = new RandomBasedUUIDGenerator(); - private static final UUIDGenerator LEGACY_TIME_UUID_GENERATOR = new LegacyTimeBasedUUIDGenerator(); private static final UUIDGenerator TIME_UUID_GENERATOR = new TimeBasedUUIDGenerator(); /** Generates a time-based UUID (similar to Flake IDs), which is preferred when generating an ID to be indexed into a Lucene index as @@ -53,11 +50,6 @@ public static String base64UUID() { return TIME_UUID_GENERATOR.getBase64UUID(); } - /** Legacy implementation of {@link #base64UUID()}, for pre 6.0 indices. */ - public static String legacyBase64UUID() { - return LEGACY_TIME_UUID_GENERATOR.getBase64UUID(); - } - /** Returns a Base64 encoded version of a Version 4.0 compatible UUID as defined here: http://www.ietf.org/rfc/rfc4122.txt, using the * provided {@code Random} instance */ public static String randomBase64UUID(Random random) { @@ -70,9 +62,4 @@ public static String randomBase64UUID() { return RANDOM_UUID_GENERATOR.getBase64UUID(); } - /** Returns a Base64 encoded {@link SecureString} of a Version 4.0 compatible UUID as defined here: http://www.ietf.org/rfc/rfc4122.txt, - * using a private {@code SecureRandom} instance */ - public static SecureString randomBase64UUIDSecureString() { - return RANDOM_UUID_GENERATOR.getBase64UUIDSecureString(); - } }