import org.signal.libsignal.protocol.fingerprint.NumericFingerprintGenerator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-import org.whispersystems.signalservice.api.push.SignalServiceAddress;
+import org.whispersystems.signalservice.api.NetworkResult;
+import org.whispersystems.signalservice.api.push.ServiceId;
import org.whispersystems.signalservice.api.util.StreamDetails;
-import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
-import java.net.URLConnection;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
-import java.nio.file.Files;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.Spliterators;
import java.util.function.BiFunction;
import java.util.function.Consumer;
+import java.util.function.Function;
+import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
public class Utils {
- private final static Logger logger = LoggerFactory.getLogger(Utils.class);
-
- public static String getFileMimeType(final File file, final String defaultMimeType) throws IOException {
- var mime = Files.probeContentType(file.toPath());
- if (mime == null) {
- try (final InputStream bufferedStream = new BufferedInputStream(new FileInputStream(file))) {
- mime = URLConnection.guessContentTypeFromStream(bufferedStream);
- }
- }
- if (mime == null) {
- return defaultMimeType;
- }
- return mime;
- }
+ private static final Logger logger = LoggerFactory.getLogger(Utils.class);
public static Pair<StreamDetails, Optional<String>> createStreamDetailsFromDataURI(final String dataURI) {
final DataURI uri = DataURI.of(dataURI);
public static StreamDetails createStreamDetailsFromFile(final File file) throws IOException {
final InputStream stream = new FileInputStream(file);
final var size = file.length();
- final var mime = getFileMimeType(file, "application/octet-stream");
+ final var mime = MimeUtils.getFileMimeType(file).orElse(MimeUtils.OCTET_STREAM);
return new StreamDetails(stream, mime, size);
}
}
}
- public static Fingerprint computeSafetyNumber(
- boolean isUuidCapable,
- SignalServiceAddress ownAddress,
+ public static Fingerprint computeSafetyNumberForNumber(
+ String ownNumber,
IdentityKey ownIdentityKey,
- SignalServiceAddress theirAddress,
+ String theirNumber,
IdentityKey theirIdentityKey
) {
- int version;
- byte[] ownId;
- byte[] theirId;
-
- if (isUuidCapable) {
- // Version 2: UUID user
- version = 2;
- ownId = ownAddress.getServiceId().toByteArray();
- theirId = theirAddress.getServiceId().toByteArray();
- } else {
- // Version 1: E164 user
- version = 1;
- if (ownAddress.getNumber().isEmpty() || theirAddress.getNumber().isEmpty()) {
- return null;
- }
- ownId = ownAddress.getNumber().get().getBytes();
- theirId = theirAddress.getNumber().get().getBytes();
- }
+ // Version 1: E164 user
+ final var version = 1;
+ final var ownId = ownNumber.getBytes(StandardCharsets.UTF_8);
+ final var theirId = theirNumber.getBytes(StandardCharsets.UTF_8);
+
+ return getFingerprint(version, ownId, ownIdentityKey, theirId, theirIdentityKey);
+ }
+
+ public static Fingerprint computeSafetyNumberForUuid(
+ ServiceId ownServiceId,
+ IdentityKey ownIdentityKey,
+ ServiceId theirServiceId,
+ IdentityKey theirIdentityKey
+ ) {
+ // Version 2: UUID user
+ final var version = 2;
+ final var ownId = ownServiceId.toByteArray();
+ final var theirId = theirServiceId.toByteArray();
+
+ return getFingerprint(version, ownId, ownIdentityKey, theirId, theirIdentityKey);
+ }
+ private static Fingerprint getFingerprint(
+ final int version,
+ final byte[] ownId,
+ final IdentityKey ownIdentityKey,
+ final byte[] theirId,
+ final IdentityKey theirIdentityKey
+ ) {
return new NumericFingerprintGenerator(5200).createFor(version,
ownId,
ownIdentityKey,
public static Locale getDefaultLocale(Locale fallback) {
final var locale = Locale.getDefault();
if (locale == null) {
+ logger.debug("No default locale found, using fallback: {}", fallback);
return fallback;
}
+ final var localeString = locale.getLanguage() + "-" + locale.getCountry();
try {
- Locale.LanguageRange.parse(locale.getLanguage() + "-" + locale.getCountry());
+ Locale.LanguageRange.parse(localeString);
} catch (IllegalArgumentException e) {
- logger.debug("Invalid locale, ignoring: {}", locale);
+ logger.debug("Invalid locale '{}', using fallback: {}", locale, fallback);
return fallback;
}
+ logger.trace("Using default locale: {} ({})", locale, localeString);
return locale;
}
}, leftStream.isParallel() || rightStream.isParallel());
}
+ public static <OK, NK, V> Map<NK, V> mapKeys(Map<OK, V> map, Function<OK, NK> keyMapper) {
+ return map.entrySet().stream().collect(Collectors.toMap(e -> keyMapper.apply(e.getKey()), Map.Entry::getValue));
+ }
+
public static Map<String, String> getQueryMap(String query) {
var params = query.split("&");
var map = new HashMap<String, String>();
for (var param : params) {
final var paramParts = param.split("=");
var name = URLDecoder.decode(paramParts[0], StandardCharsets.UTF_8);
- var value = URLDecoder.decode(paramParts[1], StandardCharsets.UTF_8);
+ var value = paramParts.length == 1 ? null : URLDecoder.decode(paramParts[1], StandardCharsets.UTF_8);
map.put(name, value);
}
return map;
}
+
+ public static <T> T handleResponseException(final NetworkResult<T> response) throws IOException {
+ final var throwableOptional = response.getCause();
+ if (throwableOptional != null) {
+ if (throwableOptional instanceof IOException ioException) {
+ throw ioException;
+ } else {
+ throw new IOException(throwableOptional);
+ }
+ }
+ return response.successOrThrow();
+ }
}