mirror of
https://github.com/AsamK/signal-cli.git
synced 2026-08-25 05:26:03 +00:00
Three interrelated fixes for the HTTP SSE endpoint: 1. **SignalAccountFiles** — Replace ACI.parseOrThrow() with UUID-string lookup from accountsStore.getAllAccounts(). The old approach failed when a raw UUID string (from URL query param) was passed. Added getAccountNumberByAci() helper to reduce duplication. 2. **MultiAccountManagerImpl** — Catch IllegalArgumentException in getManager() for both phone number and ACI lookup paths. Also check if the UUID corresponds to an already-loaded manager before trying to initByAci(), preventing OverlappingFileLockException when SSE requests arrive with a UUID for an account that was loaded at startup. 3. **Util.getQueryMap()** — Preserve '+' characters in query parameter values by escaping them before URLDecoder.decode(). Without this, URLDecoder converts '+' to space, breaking phone numbers like '+4915422389' which become ' 4915422389'.
88 lines
2.8 KiB
Java
88 lines
2.8 KiB
Java
package org.asamk.signal.util;
|
|
|
|
import com.fasterxml.jackson.annotation.JsonAutoDetect;
|
|
import com.fasterxml.jackson.annotation.PropertyAccessor;
|
|
import com.fasterxml.jackson.core.JsonGenerator;
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
import java.net.URLDecoder;
|
|
import java.nio.charset.StandardCharsets;
|
|
import java.util.Arrays;
|
|
import java.util.HashMap;
|
|
import java.util.List;
|
|
import java.util.Locale;
|
|
import java.util.Map;
|
|
import java.util.Optional;
|
|
import java.util.stream.Collectors;
|
|
|
|
public class Util {
|
|
|
|
private static final Logger logger = LoggerFactory.getLogger(Util.class);
|
|
|
|
private Util() {
|
|
}
|
|
|
|
public static String getStringIfNotBlank(Optional<String> value) {
|
|
var string = value.orElse(null);
|
|
if (string == null || string.isBlank()) {
|
|
return null;
|
|
}
|
|
return string;
|
|
}
|
|
|
|
public static String dashSeparatedToCamelCaseString(String s) {
|
|
var parts = s.split("-");
|
|
return toCamelCaseString(Arrays.asList(parts));
|
|
}
|
|
|
|
private static String toCamelCaseString(List<String> strings) {
|
|
if (strings.isEmpty()) {
|
|
return "";
|
|
}
|
|
return strings.getFirst() + strings.stream()
|
|
.skip(1)
|
|
.filter(s -> !s.isEmpty())
|
|
.map(s -> Character.toUpperCase(s.charAt(0)) + s.substring(1).toLowerCase(Locale.ROOT))
|
|
.collect(Collectors.joining());
|
|
}
|
|
|
|
public static String formatSafetyNumber(String digits) {
|
|
if (digits == null) {
|
|
return null;
|
|
}
|
|
|
|
final var partCount = 12;
|
|
var partSize = digits.length() / partCount;
|
|
var f = new StringBuilder(digits.length() + partCount);
|
|
for (var i = 0; i < partCount; i++) {
|
|
f.append(digits, i * partSize, (i * partSize) + partSize);
|
|
if (i != partCount - 1) {
|
|
f.append(" ");
|
|
}
|
|
}
|
|
return f.toString();
|
|
}
|
|
|
|
public static ObjectMapper createJsonObjectMapper() {
|
|
var objectMapper = new ObjectMapper();
|
|
objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.PUBLIC_ONLY);
|
|
objectMapper.disable(JsonGenerator.Feature.AUTO_CLOSE_TARGET);
|
|
return objectMapper;
|
|
}
|
|
|
|
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("=", 2);
|
|
var name = URLDecoder.decode(paramParts[0], StandardCharsets.UTF_8);
|
|
var value = paramParts.length == 1 ? null : URLDecoder.decode(paramParts[1].replace("+", "%2B"), StandardCharsets.UTF_8);
|
|
map.put(name, value);
|
|
}
|
|
return map;
|
|
}
|
|
}
|