Migrate VoiceVerificationController to Util#findBestLocale
This commit is contained in:
parent
deece33a0d
commit
1f53900345
|
@ -5,6 +5,11 @@
|
||||||
|
|
||||||
package org.whispersystems.textsecuregcm.controllers;
|
package org.whispersystems.textsecuregcm.controllers;
|
||||||
|
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Locale.LanguageRange;
|
||||||
|
import java.util.Set;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
import javax.ws.rs.POST;
|
import javax.ws.rs.POST;
|
||||||
import javax.ws.rs.Path;
|
import javax.ws.rs.Path;
|
||||||
import javax.ws.rs.PathParam;
|
import javax.ws.rs.PathParam;
|
||||||
|
@ -12,8 +17,7 @@ import javax.ws.rs.Produces;
|
||||||
import javax.ws.rs.QueryParam;
|
import javax.ws.rs.QueryParam;
|
||||||
import javax.ws.rs.core.MediaType;
|
import javax.ws.rs.core.MediaType;
|
||||||
import javax.ws.rs.core.Response;
|
import javax.ws.rs.core.Response;
|
||||||
import java.util.List;
|
import org.whispersystems.textsecuregcm.util.Util;
|
||||||
import java.util.Set;
|
|
||||||
|
|
||||||
@SuppressWarnings("OptionalUsedAsFieldOrParameterType")
|
@SuppressWarnings("OptionalUsedAsFieldOrParameterType")
|
||||||
@Path("/v1/voice/")
|
@Path("/v1/voice/")
|
||||||
|
@ -46,6 +50,8 @@ public class VoiceVerificationController {
|
||||||
" <Play>%s</Play>\n" +
|
" <Play>%s</Play>\n" +
|
||||||
"</Response>";
|
"</Response>";
|
||||||
|
|
||||||
|
private static final String DEFAULT_LOCALE = "en-US";
|
||||||
|
|
||||||
|
|
||||||
private final String baseUrl;
|
private final String baseUrl;
|
||||||
private final Set<String> supportedLocales;
|
private final Set<String> supportedLocales;
|
||||||
|
@ -65,19 +71,22 @@ public class VoiceVerificationController {
|
||||||
return Response.status(400).build();
|
return Response.status(400).build();
|
||||||
}
|
}
|
||||||
|
|
||||||
for (final String locale : locales) {
|
if (locales == null) {
|
||||||
if (locale != null && supportedLocales.contains(locale)) {
|
locales = Collections.emptyList();
|
||||||
return getLocalizedDescription(code, locale);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for (final String locale : locales) {
|
final List<LanguageRange> priorityList;
|
||||||
if (locale != null && locale.split("-").length >= 1 && supportedLocales.contains(locale.split("-")[0])) {
|
try {
|
||||||
return getLocalizedDescription(code, locale.split("-")[0]);
|
priorityList = locales.stream()
|
||||||
}
|
.map(LanguageRange::new)
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
} catch (final IllegalArgumentException e) {
|
||||||
|
return Response.status(400).build();
|
||||||
}
|
}
|
||||||
|
|
||||||
return getLocalizedDescription(code, "en-US");
|
final String localeMatch = Util.findBestLocale(priorityList, supportedLocales).orElse(DEFAULT_LOCALE);
|
||||||
|
|
||||||
|
return getLocalizedDescription(code, localeMatch);
|
||||||
}
|
}
|
||||||
|
|
||||||
private Response getLocalizedDescription(String code, String locale) {
|
private Response getLocalizedDescription(String code, String locale) {
|
||||||
|
|
|
@ -5,7 +5,15 @@
|
||||||
|
|
||||||
package org.whispersystems.textsecuregcm.tests.controllers;
|
package org.whispersystems.textsecuregcm.tests.controllers;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
import com.google.common.collect.ImmutableSet;
|
import com.google.common.collect.ImmutableSet;
|
||||||
|
import io.dropwizard.auth.PolymorphicAuthValueFactoryProvider;
|
||||||
|
import io.dropwizard.testing.FixtureHelpers;
|
||||||
|
import io.dropwizard.testing.junit.ResourceTestRule;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.HashSet;
|
||||||
|
import javax.ws.rs.core.Response;
|
||||||
import org.glassfish.jersey.test.grizzly.GrizzlyWebTestContainerFactory;
|
import org.glassfish.jersey.test.grizzly.GrizzlyWebTestContainerFactory;
|
||||||
import org.junit.Rule;
|
import org.junit.Rule;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
@ -16,15 +24,6 @@ import org.whispersystems.textsecuregcm.storage.Account;
|
||||||
import org.whispersystems.textsecuregcm.tests.util.AuthHelper;
|
import org.whispersystems.textsecuregcm.tests.util.AuthHelper;
|
||||||
import org.whispersystems.textsecuregcm.util.SystemMapper;
|
import org.whispersystems.textsecuregcm.util.SystemMapper;
|
||||||
|
|
||||||
import javax.ws.rs.core.Response;
|
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.HashSet;
|
|
||||||
|
|
||||||
import io.dropwizard.auth.PolymorphicAuthValueFactoryProvider;
|
|
||||||
import io.dropwizard.testing.FixtureHelpers;
|
|
||||||
import io.dropwizard.testing.junit.ResourceTestRule;
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
|
||||||
|
|
||||||
public class VoiceVerificationControllerTest {
|
public class VoiceVerificationControllerTest {
|
||||||
|
|
||||||
@Rule
|
@Rule
|
||||||
|
@ -125,7 +124,18 @@ public class VoiceVerificationControllerTest {
|
||||||
.post(null);
|
.post(null);
|
||||||
|
|
||||||
assertThat(response.getStatus()).isEqualTo(400);
|
assertThat(response.getStatus()).isEqualTo(400);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testTwimlMalformedLocale() {
|
||||||
|
Response response =
|
||||||
|
resources.getJerseyTest()
|
||||||
|
.target("/v1/voice/description/123456")
|
||||||
|
.queryParam("l", "it IT ,")
|
||||||
|
.request()
|
||||||
|
.post(null);
|
||||||
|
|
||||||
|
assertThat(response.getStatus()).isEqualTo(400);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue