Merge branch 'master' into 'feature/add-openapi-spe'

# Conflicts:
#   src/main/java/dev/amatos/restcountries/controller/CountryControllerV2.java
#   src/main/java/dev/amatos/restcountries/controller/CountryControllerV31.java
This commit is contained in:
Alejandro Matos
2022-03-14 14:47:35 +00:00
9 changed files with 607 additions and 596 deletions
+1 -1
View File
@@ -89,7 +89,7 @@
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.15.0</version>
<version>2.17.0</version>
</dependency>
<dependency>
<groupId>org.jboss.spec.javax.ws.rs</groupId>
@@ -1,5 +1,18 @@
package dev.amatos.restcountries.controller;
import com.google.gson.Gson;
import com.google.gson.JsonParser;
import dev.amatos.restcountries.domain.ICountryRestSymbols;
import dev.amatos.restcountries.domain.ResponseEntity;
import dev.amatos.restcountries.domain.base.BaseCountry;
import io.micronaut.http.HttpResponse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
public class ControllerHelper {
protected static final String[] V3_COUNTRY_FIELDS = new String[]{
@@ -41,4 +54,89 @@ public class ControllerHelper {
"capitalInfo",
"postalCode"
};
protected static final String[] COUNTRY_FIELDS_V2 = new String[]{
"name",
"topLevelDomain",
"alpha2Code",
"alpha3Code",
"callingCodes",
"capital",
"altSpellings",
"region",
"subregion",
"translations",
"population",
"latlng",
"demonym",
"area",
"gini",
"timezones",
"borders",
"nativeName",
"numericCode",
"currencies",
"languages",
"flags",
"regionalBlocs",
"cioc",
"independent",
"continent",
"borders",
"flag",
"flags"
};
protected static HttpResponse<Object> ok(Object object) {
return HttpResponse.ok(object);
}
protected static HttpResponse<Object> notFound() {
var gson = new Gson();
var notFound = Response
.status(Status.NOT_FOUND)
.entity(gson.toJson(new ResponseEntity(Response.Status.NOT_FOUND.getStatusCode(),
Response.Status.NOT_FOUND.getReasonPhrase()))).build().getEntity();
return HttpResponse.notFound(notFound);
}
protected static HttpResponse<Object> internalError() {
var gson = new Gson();
var notFound = Response
.status(Status.INTERNAL_SERVER_ERROR)
.entity(gson.toJson(new ResponseEntity(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(),
Response.Status.INTERNAL_SERVER_ERROR.getReasonPhrase()))).build().getEntity();
return HttpResponse.serverError(notFound);
}
protected static HttpResponse<Object> badRequest() {
var gson = new Gson();
var notFound = Response
.status(Status.BAD_REQUEST)
.entity(gson.toJson(new ResponseEntity(Response.Status.BAD_REQUEST.getStatusCode(),
Response.Status.BAD_REQUEST.getReasonPhrase()))).build().getEntity();
return HttpResponse.badRequest(notFound);
}
protected static Object parsedCountry(Set<? extends BaseCountry> countries, String fields) {
if (fields == null || fields.isEmpty()) {
return countries;
} else {
StringBuilder result = new StringBuilder();
countries.forEach(country -> result.append(
getCountryJson(country, Arrays.asList(fields.split(ICountryRestSymbols.COLON)))));
return result;
}
}
private static String getCountryJson(BaseCountry country, List<String> fields) {
var gson = new Gson();
var parser = new JsonParser();
var jsonObject = parser.parse(gson.toJson(country)).getAsJsonObject();
List<String> excludedFields = new ArrayList<>(Arrays.asList(V3_COUNTRY_FIELDS));
excludedFields.removeAll(fields);
excludedFields.forEach(jsonObject::remove);
return jsonObject.toString();
}
}
@@ -8,7 +8,9 @@ import dev.amatos.restcountries.domain.ICountryRestSymbols;
import dev.amatos.restcountries.domain.ResponseEntity;
import dev.amatos.restcountries.service.v2.CountryServiceV2;
import dev.amatos.restcountries.domain.v2.Country;
import io.micronaut.http.HttpResponse;
import io.micronaut.http.MediaType;
import io.micronaut.http.MutableHttpResponse;
import io.micronaut.http.annotation.Controller;
import io.micronaut.http.annotation.Get;
import io.micronaut.http.annotation.PathVariable;
@@ -32,28 +34,180 @@ public class CountryControllerV2 {
return checkFieldsAndParseCountries(fields, countries);
}
private Object checkFieldsAndParseCountries(Optional<String> fields, List<Country> countries) {
if (fields.isPresent()) {
return parsedCountries(countries, fields.get());
} else {
return parsedCountries(countries, null);
}
}
@Get("alpha/{alphacode}")
public Object getByAlpha(@PathVariable("alphacode") String alpha,
public HttpResponse<Object> getByAlpha(@PathVariable("alphacode") String alpha,
@QueryValue("fields") Optional<String> fields) {
if (alpha.contains("codes")) {
alpha = alpha.replace("codes=", "");
}
if (isEmpty(alpha) || alpha.length() < 2 || alpha.length() > 3) {
return getResponse(Response.Status.BAD_REQUEST);
return ControllerHelper.badRequest();
}
Country country = CountryServiceV2.getInstance().getByAlpha(alpha);
if (country != null) {
return checkFieldsAndParseCountry(country, fields);
return HttpResponse.ok(checkFieldsAndParseCountry(country, fields));
}
return ControllerHelper.notFound();
}
@Get("alpha/")
public HttpResponse<Object> getByAlphaList(@QueryParam("codes") String codes,
@QueryParam("fields") Optional<String> fields) {
if (isEmpty(codes) || codes.length() < 2 || (codes.length() > 3 && !codes.contains(","))) {
return ControllerHelper.badRequest();
}
try {
List<Country> countries = CountryServiceV2.getInstance().getByCodeList(codes);
if (null != countries && !countries.isEmpty()) {
return HttpResponse.ok(checkFieldsAndParseCountries(fields, countries));
}
return ControllerHelper.notFound();
} catch (Exception e) {
return ControllerHelper.internalError();
}
}
@Get("currency/{currency}")
public HttpResponse<Object> getByCurrency(@PathVariable("currency") String currency,
@QueryParam("fields") Optional<String> fields) {
if (isEmpty(currency) || currency.length() != 3) {
return ControllerHelper.badRequest();
}
try {
List<Country> countries = CountryServiceV2.getInstance().getByCurrency(currency);
if (!countries.isEmpty()) {
return HttpResponse.ok(checkFieldsAndParseCountries(fields, countries));
}
return ControllerHelper.notFound();
} catch (Exception e) {
return ControllerHelper.internalError();
}
}
@Get("name/{name}")
public HttpResponse<Object> getByName(@PathVariable("name") String name,
@QueryParam("fullText") Optional<Boolean> fullText,
@QueryParam("fields") Optional<String> fields) {
try {
List<Country> countries = CountryServiceV2.getInstance()
.getByName(name, fullText.orElse(false));
if (!countries.isEmpty()) {
return HttpResponse.ok(checkFieldsAndParseCountries(fields, countries));
}
return ControllerHelper.notFound();
} catch (Exception e) {
return ControllerHelper.internalError();
}
}
@Get("callingcode/{callingCode}")
public HttpResponse<Object> getByCallingCode(@PathVariable("callingCode") String callingCode,
@QueryParam("fields") Optional<String> fields) {
try {
List<Country> countries = CountryServiceV2.getInstance().getByCallingCode(callingCode);
if (!countries.isEmpty()) {
return HttpResponse.ok(checkFieldsAndParseCountries(fields, countries));
}
return ControllerHelper.notFound();
} catch (Exception e) {
return ControllerHelper.internalError();
}
}
@Get("capital/{capital}")
public HttpResponse<Object> getByCapital(@PathVariable("capital") String capital,
@QueryParam("fields") Optional<String> fields) {
try {
List<Country> countries = CountryServiceV2.getInstance().getByCapital(capital);
if (!countries.isEmpty()) {
return HttpResponse.ok(checkFieldsAndParseCountries(fields, countries));
}
return ControllerHelper.notFound();
} catch (Exception e) {
return ControllerHelper.internalError();
}
}
@Get("region/{region}")
public HttpResponse<Object> getByContinent(@PathVariable("region") String region,
@QueryParam("fields") Optional<String> fields) {
try {
List<Country> countries = CountryServiceV2.getInstance().getByRegion(region);
if (!countries.isEmpty()) {
return HttpResponse.ok(checkFieldsAndParseCountries(fields, countries));
}
return ControllerHelper.notFound();
} catch (Exception e) {
return ControllerHelper.internalError();
}
}
@Get("subregion/{subregion}")
public HttpResponse<Object> getBySubRegion(@PathVariable("subregion") String subregion,
@QueryParam("fields") Optional<String> fields) {
try {
List<Country> countries = CountryServiceV2.getInstance().getBySubregion(subregion);
if (!countries.isEmpty()) {
return HttpResponse.ok(checkFieldsAndParseCountries(fields, countries));
}
return ControllerHelper.notFound();
} catch (Exception e) {
return ControllerHelper.internalError();
}
}
@Get("lang/{lang}")
public HttpResponse<Object> getByLanguage(@PathVariable("lang") String language,
@QueryParam("fields") Optional<String> fields) {
try {
List<Country> countries = CountryServiceV2.getInstance().getByLanguage(language);
if (!countries.isEmpty()) {
return HttpResponse.ok(checkFieldsAndParseCountries(fields, countries));
}
return ControllerHelper.notFound();
} catch (Exception e) {
return ControllerHelper.internalError();
}
}
@Get("demonym/{demonym}")
public HttpResponse<Object> getByDemonym(@PathVariable("demonym") String demonym,
@QueryParam("fields") Optional<String> fields) {
try {
List<Country> countries = CountryServiceV2.getInstance().getByDemonym(demonym);
if (!countries.isEmpty()) {
return HttpResponse.ok(checkFieldsAndParseCountries(fields, countries));
}
return ControllerHelper.notFound();
} catch (Exception e) {
return ControllerHelper.internalError();
}
}
@Get("regionalbloc/{regionalBlock}")
public HttpResponse<Object> getByRegionalBloc(@PathVariable("regionalBlock") String regionalBlock,
@QueryParam("fields") Optional<String> fields) {
try {
List<Country> countries = CountryServiceV2.getInstance().getByRegionalBloc(regionalBlock);
if (!countries.isEmpty()) {
return HttpResponse.ok(checkFieldsAndParseCountries(fields, countries));
}
return ControllerHelper.notFound();
} catch (Exception e) {
return ControllerHelper.internalError();
}
}
private List<Country> checkFieldsAndParseCountries(Optional<String> fields,
List<Country> countries) {
if (fields.isPresent()) {
return parsedCountries(countries, fields.get());
} else {
return parsedCountries(countries, null);
}
return getResponse(Response.Status.NOT_FOUND);
}
private Object checkFieldsAndParseCountry(Country country, Optional<String> fields) {
@@ -64,158 +218,7 @@ public class CountryControllerV2 {
}
}
@Get("alpha/")
public Object getByAlphaList(@QueryParam("codes") String codes,
@QueryParam("fields") Optional<String> fields) {
if (isEmpty(codes) || codes.length() < 2 || (codes.length() > 3 && !codes.contains(","))) {
return getResponse(Response.Status.BAD_REQUEST);
}
try {
List<Country> countries = CountryServiceV2.getInstance().getByCodeList(codes);
if (!countries.isEmpty()) {
return checkFieldsAndParseCountries(fields, countries);
}
return getResponse(Response.Status.NOT_FOUND);
} catch (Exception e) {
return getResponse(Response.Status.INTERNAL_SERVER_ERROR);
}
}
@Get("currency/{currency}")
public Object getByCurrency(@PathVariable("currency") String currency,
@QueryParam("fields") Optional<String> fields) {
if (isEmpty(currency) || currency.length() != 3) {
return getResponse(Response.Status.BAD_REQUEST);
}
try {
List<Country> countries = CountryServiceV2.getInstance().getByCurrency(currency);
if (!countries.isEmpty()) {
return checkFieldsAndParseCountries(fields, countries);
}
return getResponse(Response.Status.NOT_FOUND);
} catch (Exception e) {
return getResponse(Response.Status.INTERNAL_SERVER_ERROR);
}
}
@Get("name/{name}")
public Object getByName(@PathVariable("name") String name,
@QueryParam("fullText") Optional<Boolean> fullText,
@QueryParam("fields") Optional<String> fields) {
try {
List<Country> countries = CountryServiceV2.getInstance()
.getByName(name, fullText.orElse(false));
if (!countries.isEmpty()) {
return checkFieldsAndParseCountries(fields, countries);
}
return getResponse(Response.Status.NOT_FOUND);
} catch (Exception e) {
return getResponse(Response.Status.INTERNAL_SERVER_ERROR);
}
}
@Get("callingcode/{callingCode}")
public Object getByCallingCode(@PathVariable("callingCode") String callingCode,
@QueryParam("fields") Optional<String> fields) {
try {
List<Country> countries = CountryServiceV2.getInstance().getByCallingCode(callingCode);
if (!countries.isEmpty()) {
return checkFieldsAndParseCountries(fields, countries);
}
return getResponse(Response.Status.NOT_FOUND);
} catch (Exception e) {
return getResponse(Response.Status.INTERNAL_SERVER_ERROR);
}
}
@Get("capital/{capital}")
public Object getByCapital(@PathVariable("capital") String capital,
@QueryParam("fields") Optional<String> fields) {
try {
List<Country> countries = CountryServiceV2.getInstance().getByCapital(capital);
if (!countries.isEmpty()) {
return checkFieldsAndParseCountries(fields, countries);
}
return getResponse(Response.Status.NOT_FOUND);
} catch (Exception e) {
return getResponse(Response.Status.INTERNAL_SERVER_ERROR);
}
}
@Get("region/{region}")
public Object getByContinent(@PathVariable("region") String region,
@QueryParam("fields") Optional<String> fields) {
try {
List<Country> countries = CountryServiceV2.getInstance().getByRegion(region);
if (!countries.isEmpty()) {
return checkFieldsAndParseCountries(fields, countries);
}
return getResponse(Response.Status.NOT_FOUND);
} catch (Exception e) {
return getResponse(Response.Status.INTERNAL_SERVER_ERROR);
}
}
@Get("subregion/{subregion}")
public Object getBySubRegion(@PathVariable("subregion") String subregion,
@QueryParam("fields") Optional<String> fields) {
try {
List<Country> countries = CountryServiceV2.getInstance().getBySubregion(subregion);
if (!countries.isEmpty()) {
return checkFieldsAndParseCountries(fields, countries);
}
return getResponse(Response.Status.NOT_FOUND);
} catch (Exception e) {
return getResponse(Response.Status.INTERNAL_SERVER_ERROR);
}
}
@Get("lang/{lang}")
public Object getByLanguage(@PathVariable("lang") String language,
@QueryParam("fields") Optional<String> fields) {
try {
List<Country> countries = CountryServiceV2.getInstance().getByLanguage(language);
if (!countries.isEmpty()) {
return checkFieldsAndParseCountries(fields, countries);
}
return getResponse(Response.Status.NOT_FOUND);
} catch (Exception e) {
return getResponse(Response.Status.INTERNAL_SERVER_ERROR);
}
}
@Get("demonym/{demonym}")
public Object getByDemonym(@PathVariable("demonym") String demonym,
@QueryParam("fields") Optional<String> fields) {
try {
List<Country> countries = CountryServiceV2.getInstance().getByDemonym(demonym);
if (!countries.isEmpty()) {
return checkFieldsAndParseCountries(fields, countries);
}
return getResponse(Response.Status.NOT_FOUND);
} catch (Exception e) {
return getResponse(Response.Status.INTERNAL_SERVER_ERROR);
}
}
@Get("regionalbloc/{regionalBlock}")
public Object getByRegionalBloc(@PathVariable("regionalBlock") String regionalBlock,
@QueryParam("fields") Optional<String> fields) {
try {
List<Country> countries = CountryServiceV2.getInstance().getByRegionalBloc(regionalBlock);
if (!countries.isEmpty()) {
return checkFieldsAndParseCountries(fields, countries);
}
return getResponse(Response.Status.NOT_FOUND);
} catch (Exception e) {
return getResponse(Response.Status.INTERNAL_SERVER_ERROR);
}
}
private Object parsedCountries(List<Country> countries, String excludedFields) {
private List<Country> parsedCountries(List<Country> countries, String excludedFields) {
if (excludedFields == null || excludedFields.isEmpty()) {
return countries;
} else {
@@ -224,37 +227,28 @@ public class CountryControllerV2 {
}
}
private String getCountriesJson(List<Country> countries, List<String> fields) {
private List<Country> getCountriesJson(List<Country> countries, List<String> fields) {
List<Country> result = new ArrayList<>();
var gson = new Gson();
var parser = new JsonParser();
JsonArray jsonArray = parser.parse(gson.toJson(countries)).getAsJsonArray();
var resultArray = new JsonArray();
for (var i = 0; i < jsonArray.size(); i++) {
var jsonObject = (JsonObject) jsonArray.get(i);
List<String> excludedFields = getExcludedFields(fields);
for (String excludedField : excludedFields) {
jsonObject.remove(excludedField);
}
resultArray.add(jsonObject);
result.add(gson.fromJson(jsonObject, Country.class));
}
return resultArray.toString();
return result;
}
private List<String> getExcludedFields(List<String> fields) {
List<String> excludedFields = new ArrayList<>(Arrays.asList(COUNTRY_FIELDS));
List<String> excludedFields = new ArrayList<>(Arrays.asList(ControllerHelper.COUNTRY_FIELDS_V2));
excludedFields.removeAll(fields);
return excludedFields;
}
private Object getResponse(Response.Status status) {
var gson = new Gson();
return Response
.status(status)
.entity(gson.toJson(new ResponseEntity(status.getStatusCode(),
status.getReasonPhrase()))).build().getEntity();
}
private Object parsedCountry(Country country, String fields) {
if (fields == null || fields.isEmpty()) {
return country;
@@ -275,38 +269,6 @@ public class CountryControllerV2 {
return jsonObject.toString();
}
private static final String[] COUNTRY_FIELDS = new String[]{
"name",
"topLevelDomain",
"alpha2Code",
"alpha3Code",
"callingCodes",
"capital",
"altSpellings",
"region",
"subregion",
"translations",
"population",
"latlng",
"demonym",
"area",
"gini",
"timezones",
"borders",
"nativeName",
"numericCode",
"currencies",
"languages",
"flags",
"regionalBlocs",
"cioc",
"independent",
"continent",
"borders",
"flag",
"flags"
};
private boolean isEmpty(String value) {
return value == null || value.isEmpty();
}
@@ -29,9 +29,162 @@ import javax.ws.rs.core.Response;
public class CountryControllerV3 extends ControllerHelper {
@Get(uri = "all", produces = MediaType.APPLICATION_JSON)
public Object getAllCountries(@QueryValue("fields") Optional<String> fields) {
public HttpResponse<Object> getAllCountries(@QueryValue("fields") Optional<String> fields) {
var countries = CountryServiceV3.getInstance().getAll();
return checkFieldsAndParseCountries(fields, countries);
return HttpResponse.ok(checkFieldsAndParseCountries(fields, countries));
}
@Get("alpha/{alphacode}")
public HttpResponse<Object> getByAlpha(@PathVariable("alphacode") String alpha,
@QueryValue("fields") Optional<String> fields) {
if (alpha.contains("codes")) {
alpha = alpha.replace("codes=", "");
}
if (isEmpty(alpha) || alpha.length() < 2 || alpha.length() > 3) {
return ControllerHelper.badRequest();
}
var country = CountryServiceV3.getInstance().getByAlpha(alpha);
if (country != null && !country.isEmpty()) {
return HttpResponse.ok(checkFieldsAndParseCountry(country, fields));
}
return ControllerHelper.notFound();
}
@Get("alpha/")
public HttpResponse<Object> getByAlphaList(@QueryParam("codes") String codes,
@QueryParam("fields") Optional<String> fields) {
if (isEmpty(codes) || codes.length() < 2 || (codes.length() > 3 && !codes.contains(","))) {
return ControllerHelper.badRequest();
}
try {
var countries = CountryServiceV3.getInstance().getByCodeList(codes);
if (!countries.isEmpty()) {
return HttpResponse.ok(checkFieldsAndParseCountries(fields, countries));
}
return ControllerHelper.notFound();
} catch (Exception e) {
return ControllerHelper.internalError();
}
}
@Get("currency/{currency}")
public HttpResponse<Object> getByCurrency(@PathVariable("currency") String currency,
@QueryParam("fields") Optional<String> fields) {
if (isEmpty(currency)) {
return ControllerHelper.notFound();
}
try {
var countries = CountryServiceV3.getInstance().getByCurrency(currency);
if (!countries.isEmpty()) {
return HttpResponse.ok(checkFieldsAndParseCountries(fields, countries));
}
return ControllerHelper.notFound();
} catch (Exception e) {
return ControllerHelper.internalError();
}
}
@Get("name/{name}")
public HttpResponse<Object> getByName(@PathVariable("name") String name,
@QueryParam("fullText") Optional<Boolean> fullText,
@QueryParam("fields") Optional<String> fields) {
try {
var countries = CountryServiceV3.getInstance()
.getByName(name, fullText.orElse(false));
if (!countries.isEmpty()) {
return HttpResponse.ok(checkFieldsAndParseCountries(fields, countries));
}
return ControllerHelper.notFound();
} catch (Exception e) {
return ControllerHelper.internalError();
}
}
@Get("capital/{capital}")
public HttpResponse<Object> getByCapital(@PathVariable("capital") String capital,
@QueryParam("fields") Optional<String> fields) {
try {
var countries = CountryServiceV3.getInstance().getByCapital(capital);
if (!countries.isEmpty()) {
return HttpResponse.ok(checkFieldsAndParseCountries(fields, countries));
}
return ControllerHelper.notFound();
} catch (Exception e) {
return ControllerHelper.internalError();
}
}
@Get("region/{region}")
public HttpResponse<Object> getByContinent(@PathVariable("region") String region,
@QueryParam("fields") Optional<String> fields) {
try {
var countries = CountryServiceV3.getInstance().getByRegion(region);
if (!countries.isEmpty()) {
return HttpResponse.ok(checkFieldsAndParseCountries(fields, countries));
}
return ControllerHelper.notFound();
} catch (Exception e) {
return ControllerHelper.internalError();
}
}
@Get("subregion/{subregion}")
public HttpResponse<Object> getBySubRegion(@PathVariable("subregion") String subregion,
@QueryParam("fields") Optional<String> fields) {
try {
var countries = CountryServiceV3.getInstance().getBySubregion(subregion);
if (!countries.isEmpty()) {
return HttpResponse.ok(checkFieldsAndParseCountries(fields, countries));
}
return ControllerHelper.notFound();
} catch (Exception e) {
return ControllerHelper.internalError();
}
}
@Get("lang/{lang}")
public HttpResponse<Object> getByLanguage(@PathVariable("lang") String language,
@QueryParam("fields") Optional<String> fields) {
try {
var countries = CountryServiceV3.getInstance().getByLanguage(language);
if (!countries.isEmpty()) {
return HttpResponse.ok(checkFieldsAndParseCountries(fields, countries));
}
return ControllerHelper.notFound();
} catch (Exception e) {
return ControllerHelper.internalError();
}
}
@Get("demonym/{demonym}")
public HttpResponse<Object> getByDemonym(@PathVariable("demonym") String demonym,
@QueryParam("fields") Optional<String> fields) {
try {
var countries = CountryServiceV3.getInstance().getByDemonym(demonym);
if (!countries.isEmpty()) {
return HttpResponse.ok(checkFieldsAndParseCountries(fields, countries));
}
return ControllerHelper.notFound();
} catch (Exception e) {
return ControllerHelper.internalError();
}
}
@Get("translation/{translation}")
public HttpResponse<Object> getByTranslation(@PathVariable("translation") String translation,
@QueryParam("fields") Optional<String> fields) {
try {
var countries = CountryServiceV3.getInstance().getByTranslation(translation);
if (!countries.isEmpty()) {
return HttpResponse.ok(checkFieldsAndParseCountries(fields, countries));
}
return ControllerHelper.notFound();
} catch (Exception e) {
return ControllerHelper.internalError();
}
}
private Object checkFieldsAndParseCountries(Optional<String> fields,
@@ -43,22 +196,6 @@ public class CountryControllerV3 extends ControllerHelper {
}
}
@Get("alpha/{alphacode}")
public Object getByAlpha(@PathVariable("alphacode") String alpha,
@QueryValue("fields") Optional<String> fields) {
if (alpha.contains("codes")) {
alpha = alpha.replace("codes=", "");
}
if (isEmpty(alpha) || alpha.length() < 2 || alpha.length() > 3) {
return HttpResponse.badRequest(getResponse(Response.Status.BAD_REQUEST));
}
var country = CountryServiceV3.getInstance().getByAlpha(alpha);
if (country != null) {
return checkFieldsAndParseCountry(country, fields);
}
return HttpResponse.notFound(getResponse(Response.Status.NOT_FOUND));
}
private Object checkFieldsAndParseCountry(Set<Country> countries, Optional<String> fields) {
if (fields.isPresent()) {
return parsedCountry(countries, fields.get());
@@ -67,143 +204,6 @@ public class CountryControllerV3 extends ControllerHelper {
}
}
@Get("alpha/")
public Object getByAlphaList(@QueryParam("codes") String codes,
@QueryParam("fields") Optional<String> fields) {
if (isEmpty(codes) || codes.length() < 2 || (codes.length() > 3 && !codes.contains(","))) {
return HttpResponse.badRequest(getResponse(Response.Status.BAD_REQUEST));
}
try {
var countries = CountryServiceV3.getInstance().getByCodeList(codes);
if (!countries.isEmpty()) {
return checkFieldsAndParseCountries(fields, countries);
}
return HttpResponse.notFound(getResponse(Response.Status.NOT_FOUND));
} catch (Exception e) {
return HttpResponse.serverError(getResponse(Response.Status.INTERNAL_SERVER_ERROR));
}
}
@Get("currency/{currency}")
public Object getByCurrency(@PathVariable("currency") String currency,
@QueryParam("fields") Optional<String> fields) {
if (isEmpty(currency)) {
return getResponse(Response.Status.BAD_REQUEST);
}
try {
var countries = CountryServiceV3.getInstance().getByCurrency(currency);
if (!countries.isEmpty()) {
return checkFieldsAndParseCountries(fields, countries);
}
return HttpResponse.notFound(getResponse(Response.Status.NOT_FOUND));
} catch (Exception e) {
return HttpResponse.serverError(getResponse(Response.Status.INTERNAL_SERVER_ERROR));
}
}
@Get("name/{name}")
public Object getByName(@PathVariable("name") String name,
@QueryParam("fullText") Optional<Boolean> fullText,
@QueryParam("fields") Optional<String> fields) {
try {
var countries = CountryServiceV3.getInstance()
.getByName(name, fullText.orElse(false));
if (!countries.isEmpty()) {
return checkFieldsAndParseCountries(fields, countries);
}
return HttpResponse.notFound(getResponse(Response.Status.NOT_FOUND));
} catch (Exception e) {
return HttpResponse.serverError(Response.Status.INTERNAL_SERVER_ERROR);
}
}
@Get("capital/{capital}")
public Object getByCapital(@PathVariable("capital") String capital,
@QueryParam("fields") Optional<String> fields) {
try {
var countries = CountryServiceV3.getInstance().getByCapital(capital);
if (!countries.isEmpty()) {
return checkFieldsAndParseCountries(fields, countries);
}
return HttpResponse.notFound(getResponse(Response.Status.NOT_FOUND));
} catch (Exception e) {
return HttpResponse.serverError(Response.Status.INTERNAL_SERVER_ERROR);
}
}
@Get("region/{region}")
public Object getByContinent(@PathVariable("region") String region,
@QueryParam("fields") Optional<String> fields) {
try {
var countries = CountryServiceV3.getInstance().getByRegion(region);
if (!countries.isEmpty()) {
return checkFieldsAndParseCountries(fields, countries);
}
return HttpResponse.notFound(getResponse(Response.Status.NOT_FOUND));
} catch (Exception e) {
return HttpResponse.serverError(Response.Status.INTERNAL_SERVER_ERROR);
}
}
@Get("subregion/{subregion}")
public Object getBySubRegion(@PathVariable("subregion") String subregion,
@QueryParam("fields") Optional<String> fields) {
try {
var countries = CountryServiceV3.getInstance().getBySubregion(subregion);
if (!countries.isEmpty()) {
return checkFieldsAndParseCountries(fields, countries);
}
return HttpResponse.notFound(getResponse(Response.Status.NOT_FOUND));
} catch (Exception e) {
return HttpResponse.serverError(Response.Status.INTERNAL_SERVER_ERROR);
}
}
@Get("lang/{lang}")
public Object getByLanguage(@PathVariable("lang") String language,
@QueryParam("fields") Optional<String> fields) {
try {
var countries = CountryServiceV3.getInstance().getByLanguage(language);
if (!countries.isEmpty()) {
return checkFieldsAndParseCountries(fields, countries);
}
return HttpResponse.notFound(getResponse(Response.Status.NOT_FOUND));
} catch (Exception e) {
return HttpResponse.serverError(Response.Status.INTERNAL_SERVER_ERROR);
}
}
@Get("demonym/{demonym}")
public Object getByDemonym(@PathVariable("demonym") String demonym,
@QueryParam("fields") Optional<String> fields) {
try {
var countries = CountryServiceV3.getInstance().getByDemonym(demonym);
if (!countries.isEmpty()) {
return checkFieldsAndParseCountries(fields, countries);
}
return HttpResponse.notFound(getResponse(Response.Status.NOT_FOUND));
} catch (Exception e) {
return HttpResponse.serverError(Response.Status.INTERNAL_SERVER_ERROR);
}
}
@Get("translation/{translation}")
public Object getByTranslation(@PathVariable("translation") String translation,
@QueryParam("fields") Optional<String> fields) {
try {
var countries = CountryServiceV3.getInstance().getByTranslation(translation);
if (!countries.isEmpty()) {
return checkFieldsAndParseCountries(fields, countries);
}
return HttpResponse.notFound(getResponse(Response.Status.NOT_FOUND));
} catch (Exception e) {
return HttpResponse.serverError(Response.Status.INTERNAL_SERVER_ERROR);
}
}
private Object parsedCountries(Set<Country> countries, String excludedFields) {
if (excludedFields == null || excludedFields.isEmpty()) {
return countries;
@@ -233,37 +233,6 @@ public class CountryControllerV3 extends ControllerHelper {
return excludedFields;
}
private Object getResponse(Response.Status status) {
var gson = new Gson();
return Response
.status(status)
.entity(gson.toJson(new ResponseEntity(status.getStatusCode(),
status.getReasonPhrase())))
.build()
.getEntity();
}
private Object parsedCountry(Set<Country> countries, String fields) {
if (fields == null || fields.isEmpty()) {
return countries;
} else {
StringBuilder result = new StringBuilder();
countries.forEach(country -> result.append(
getCountryJson(country, Arrays.asList(fields.split(ICountryRestSymbols.COLON)))));
return result;
}
}
private String getCountryJson(Country country, List<String> fields) {
var gson = new Gson();
var parser = new JsonParser();
var jsonObject = parser.parse(gson.toJson(country)).getAsJsonObject();
List<String> excludedFields = getExcludedFields(fields);
excludedFields.forEach(jsonObject::remove);
return jsonObject.toString();
}
private boolean isEmpty(String value) {
return value == null || value.isEmpty();
}
@@ -5,7 +5,6 @@ import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import dev.amatos.restcountries.domain.ICountryRestSymbols;
import dev.amatos.restcountries.domain.ResponseEntity;
import dev.amatos.restcountries.domain.v3.v31.Country;
import dev.amatos.restcountries.service.v3.v31.CountryServiceV31;
import io.micronaut.http.HttpResponse;
@@ -31,16 +30,7 @@ public class CountryControllerV31 extends ControllerHelper {
@Schema(name="RestCountries")
public Object getAllCountries(@QueryValue("fields") Optional<String> fields) {
var countries = CountryServiceV31.getInstance().getAll();
return checkFieldsAndParseCountries(fields, countries);
}
private Object checkFieldsAndParseCountries(Optional<String> fields,
Set<Country> countries) {
if (fields.isPresent()) {
return parsedCountries(countries, fields.get());
} else {
return parsedCountries(countries, null);
}
return HttpResponse.ok(checkFieldsAndParseCountries(fields, countries));
}
@Get("alpha/{alphacode}")
@@ -51,21 +41,13 @@ public class CountryControllerV31 extends ControllerHelper {
alpha = alpha.replace("codes=", "");
}
if (isEmpty(alpha) || alpha.length() < 2 || alpha.length() > 3) {
return HttpResponse.badRequest(getResponse(Response.Status.BAD_REQUEST));
return ControllerHelper.badRequest();
}
var country = CountryServiceV31.getInstance().getByAlpha(alpha);
if (country != null) {
return checkFieldsAndParseCountry(country, fields);
}
return HttpResponse.notFound(getResponse(Response.Status.NOT_FOUND));
}
private Object checkFieldsAndParseCountry(Set<Country> countries, Optional<String> fields) {
if (fields.isPresent()) {
return parsedCountry(countries, fields.get());
} else {
return parsedCountry(countries, null);
if (country != null && !country.isEmpty()) {
return HttpResponse.ok(checkFieldsAndParseCountry(country, fields));
}
return ControllerHelper.notFound();
}
@Get("alpha/")
@@ -73,16 +55,16 @@ public class CountryControllerV31 extends ControllerHelper {
public Object getByAlphaList(@QueryParam("codes") String codes,
@QueryParam("fields") Optional<String> fields) {
if (isEmpty(codes) || codes.length() < 2 || (codes.length() > 3 && !codes.contains(","))) {
return HttpResponse.badRequest(getResponse(Response.Status.BAD_REQUEST));
return ControllerHelper.badRequest();
}
try {
var countries = CountryServiceV31.getInstance().getByCodeList(codes);
if (!countries.isEmpty()) {
return checkFieldsAndParseCountries(fields, countries);
return HttpResponse.ok(checkFieldsAndParseCountries(fields, countries));
}
return HttpResponse.notFound(getResponse(Response.Status.NOT_FOUND));
return ControllerHelper.notFound();
} catch (Exception e) {
return HttpResponse.serverError(getResponse(Response.Status.INTERNAL_SERVER_ERROR));
return ControllerHelper.internalError();
}
}
@@ -92,16 +74,16 @@ public class CountryControllerV31 extends ControllerHelper {
public Object getByCurrency(@PathVariable("currency") String currency,
@QueryParam("fields") Optional<String> fields) {
if (isEmpty(currency)) {
return getResponse(Response.Status.BAD_REQUEST);
return ControllerHelper.badRequest();
}
try {
var countries = CountryServiceV31.getInstance().getByCurrency(currency);
if (!countries.isEmpty()) {
return checkFieldsAndParseCountries(fields, countries);
return HttpResponse.ok(checkFieldsAndParseCountries(fields, countries));
}
return HttpResponse.notFound(getResponse(Response.Status.NOT_FOUND));
return ControllerHelper.notFound();
} catch (Exception e) {
return HttpResponse.serverError(getResponse(Response.Status.INTERNAL_SERVER_ERROR));
return ControllerHelper.internalError();
}
}
@@ -113,9 +95,9 @@ public class CountryControllerV31 extends ControllerHelper {
try {
var countries = CountryServiceV31.getInstance().getByName(name, fullText.orElse(false));
if (!countries.isEmpty()) {
return checkFieldsAndParseCountries(fields, countries);
return HttpResponse.ok(checkFieldsAndParseCountries(fields, countries));
}
return HttpResponse.notFound(getResponse(Response.Status.NOT_FOUND));
return ControllerHelper.notFound();
} catch (Exception e) {
return HttpResponse.serverError(Response.Status.INTERNAL_SERVER_ERROR);
}
@@ -128,9 +110,9 @@ public class CountryControllerV31 extends ControllerHelper {
try {
var countries = CountryServiceV31.getInstance().getByCapital(capital);
if (!countries.isEmpty()) {
return checkFieldsAndParseCountries(fields, countries);
return HttpResponse.ok(checkFieldsAndParseCountries(fields, countries));
}
return HttpResponse.notFound(getResponse(Response.Status.NOT_FOUND));
return ControllerHelper.notFound();
} catch (Exception e) {
return HttpResponse.serverError(Response.Status.INTERNAL_SERVER_ERROR);
}
@@ -144,9 +126,9 @@ public class CountryControllerV31 extends ControllerHelper {
try {
var countries = CountryServiceV31.getInstance().getByRegion(region);
if (!countries.isEmpty()) {
return checkFieldsAndParseCountries(fields, countries);
return HttpResponse.ok(checkFieldsAndParseCountries(fields, countries));
}
return HttpResponse.notFound(getResponse(Response.Status.NOT_FOUND));
return ControllerHelper.notFound();
} catch (Exception e) {
return HttpResponse.serverError(Response.Status.INTERNAL_SERVER_ERROR);
}
@@ -159,9 +141,9 @@ public class CountryControllerV31 extends ControllerHelper {
try {
var countries = CountryServiceV31.getInstance().getBySubregion(subregion);
if (!countries.isEmpty()) {
return checkFieldsAndParseCountries(fields, countries);
return HttpResponse.ok(checkFieldsAndParseCountries(fields, countries));
}
return HttpResponse.notFound(getResponse(Response.Status.NOT_FOUND));
return ControllerHelper.notFound();
} catch (Exception e) {
return HttpResponse.serverError(Response.Status.INTERNAL_SERVER_ERROR);
}
@@ -174,9 +156,9 @@ public class CountryControllerV31 extends ControllerHelper {
try {
var countries = CountryServiceV31.getInstance().getByLanguage(language);
if (!countries.isEmpty()) {
return checkFieldsAndParseCountries(fields, countries);
return HttpResponse.ok(checkFieldsAndParseCountries(fields, countries));
}
return HttpResponse.notFound(getResponse(Response.Status.NOT_FOUND));
return ControllerHelper.notFound();
} catch (Exception e) {
return HttpResponse.serverError(Response.Status.INTERNAL_SERVER_ERROR);
}
@@ -190,9 +172,9 @@ public class CountryControllerV31 extends ControllerHelper {
try {
var countries = CountryServiceV31.getInstance().getByDemonym(demonym);
if (!countries.isEmpty()) {
return checkFieldsAndParseCountries(fields, countries);
return HttpResponse.ok(checkFieldsAndParseCountries(fields, countries));
}
return HttpResponse.notFound(getResponse(Response.Status.NOT_FOUND));
return ControllerHelper.notFound();
} catch (Exception e) {
return HttpResponse.serverError(Response.Status.INTERNAL_SERVER_ERROR);
}
@@ -205,14 +187,31 @@ public class CountryControllerV31 extends ControllerHelper {
try {
var countries = CountryServiceV31.getInstance().getByTranslation(translation);
if (!countries.isEmpty()) {
return checkFieldsAndParseCountries(fields, countries);
return HttpResponse.ok(checkFieldsAndParseCountries(fields, countries));
}
return HttpResponse.notFound(getResponse(Response.Status.NOT_FOUND));
return ControllerHelper.notFound();
} catch (Exception e) {
return HttpResponse.serverError(Response.Status.INTERNAL_SERVER_ERROR);
}
}
private Object checkFieldsAndParseCountries(Optional<String> fields,
Set<Country> countries) {
if (fields.isPresent()) {
return parsedCountries(countries, fields.get());
} else {
return parsedCountries(countries, null);
}
}
private Object checkFieldsAndParseCountry(Set<Country> countries, Optional<String> fields) {
if (fields.isPresent()) {
return parsedCountry(countries, fields.get());
} else {
return parsedCountry(countries, null);
}
}
private Object parsedCountries(Set<Country> countries, String excludedFields) {
if (excludedFields == null || excludedFields.isEmpty()) {
return countries;
@@ -242,37 +241,6 @@ public class CountryControllerV31 extends ControllerHelper {
return excludedFields;
}
private Object getResponse(Response.Status status) {
var gson = new Gson();
return Response
.status(status)
.entity(gson.toJson(new ResponseEntity(status.getStatusCode(),
status.getReasonPhrase())))
.build()
.getEntity();
}
private Object parsedCountry(Set<Country> countries, String fields) {
if (fields == null || fields.isEmpty()) {
return countries;
} else {
StringBuilder result = new StringBuilder();
countries.forEach(country -> result.append(
getCountryJson(country, Arrays.asList(fields.split(ICountryRestSymbols.COLON)))));
return result;
}
}
private String getCountryJson(Country country, List<String> fields) {
var gson = new Gson();
var parser = new JsonParser();
var jsonObject = parser.parse(gson.toJson(country)).getAsJsonObject();
List<String> excludedFields = getExcludedFields(fields);
excludedFields.forEach(jsonObject::remove);
return jsonObject.toString();
}
private boolean isEmpty(String value) {
return value == null || value.isEmpty();
}
@@ -19,145 +19,159 @@ import org.apache.logging.log4j.Logger;
public class CountryServiceBaseV2 {
private static final Logger logger = LogManager.getLogger(CountryServiceBaseV2.class);
private static final Logger logger = LogManager.getLogger(CountryServiceBaseV2.class);
protected <T extends BaseCountry> T getByAlpha(String alpha, List<T> countries) {
int alphaLength = alpha.length();
for (T country : countries) {
if (alphaLength == 2) {
if (country.getAlpha2Code().equalsIgnoreCase(alpha)) {
return country;
}
} else if (alphaLength == 3 && country.getAlpha3Code().equalsIgnoreCase(alpha)) {
return country;
}
protected <T extends BaseCountry> T getByAlpha(String alpha, List<T> countries) {
int alphaLength = alpha.length();
for (T country : countries) {
if (alphaLength == 2) {
if (country.getAlpha2Code().equalsIgnoreCase(alpha)) {
return country;
}
return null;
} else if (alphaLength == 3 && country.getAlpha3Code().equalsIgnoreCase(alpha)) {
return country;
}
}
return null;
}
protected List<? extends BaseCountry> getByCodeList(String codeList, List<? extends BaseCountry> countries) {
List<BaseCountry> result = new ArrayList<>();
if (codeList == null) return result;
protected List<? extends BaseCountry> getByCodeList(String codeList,
List<? extends BaseCountry> countries) {
List<BaseCountry> result = new ArrayList<>();
if (codeList == null) {
return result;
}
String[] codes = codeList.split(ICountryRestSymbols.COLON);
for (String code : codes) {
BaseCountry country = getByAlpha(code, countries);
if (!result.contains(country))
result.add(country);
}
return result;
}
protected List<? extends BaseCountry> getByName(String name, boolean fullText, List<? extends BaseCountry> countries) {
if (fullText) {
return fulltextSearch(name, countries);
} else {
return substringSearch(name, countries);
String[] codes = codeList.split(ICountryRestSymbols.COLON);
for (String code : codes) {
BaseCountry country = getByAlpha(code, countries);
if (null != country && !result.contains(country)) {
result.add(country);
}
}
return result;
}
protected List<? extends BaseCountry> getByCallingCode(String callingCode, List<? extends BaseCountry> countries) {
List<BaseCountry> result = new ArrayList<>();
for (BaseCountry country : countries) {
for (String c : country.getCallingCodes()) {
if (c.equals(callingCode))
result.add(country);
}
}
return result;
protected List<? extends BaseCountry> getByName(String name, boolean fullText,
List<? extends BaseCountry> countries) {
if (fullText) {
return fulltextSearch(name, countries);
} else {
return substringSearch(name, countries);
}
}
protected List<? extends BaseCountry> getByCapital(String capital, List<? extends BaseCountry> countries) {
List<BaseCountry> result = new ArrayList<>();
for (BaseCountry country : countries) {
if (normalize(country.getCapital().toLowerCase()).contains(normalize(capital.toLowerCase()))) {
result.add(country);
}
}
return result;
protected List<? extends BaseCountry> getByCallingCode(String callingCode,
List<? extends BaseCountry> countries) {
List<BaseCountry> result = new ArrayList<>();
for (BaseCountry country : countries) {
for (String c : country.getCallingCodes()) {
if (c.equals(callingCode)) {
result.add(country);
}
}
}
return result;
}
protected List<? extends BaseCountry> getByRegion(String region, List<? extends BaseCountry> countries) {
List<BaseCountry> result = new ArrayList<>();
for (BaseCountry country : countries) {
if (country.getRegion().equalsIgnoreCase(region)) {
result.add(country);
}
}
return result;
protected List<? extends BaseCountry> getByCapital(String capital,
List<? extends BaseCountry> countries) {
List<BaseCountry> result = new ArrayList<>();
for (BaseCountry country : countries) {
if (normalize(country.getCapital().toLowerCase()).contains(
normalize(capital.toLowerCase()))) {
result.add(country);
}
}
return result;
}
protected List<? extends BaseCountry> getBySubregion(String subregion, List<? extends BaseCountry> countries) {
List<BaseCountry> result = new ArrayList<>();
for (BaseCountry country : countries) {
if (country.getSubregion().equalsIgnoreCase(subregion)) {
result.add(country);
}
}
return result;
protected List<? extends BaseCountry> getByRegion(String region,
List<? extends BaseCountry> countries) {
List<BaseCountry> result = new ArrayList<>();
for (BaseCountry country : countries) {
if (country.getRegion().equalsIgnoreCase(region)) {
result.add(country);
}
}
return result;
}
private List<? extends BaseCountry> fulltextSearch(String name, List<? extends BaseCountry> countries) {
// Using 2 different 'for' loops to give priority to 'name' matches over alternative spellings
List<BaseCountry> result = new ArrayList<>();
for (BaseCountry country : countries) {
if (normalize(country.getName().toLowerCase()).equals(normalize(name.toLowerCase()))) {
result.add(country);
}
}
for (BaseCountry country : countries) {
for (String alternative : country.getAltSpellings()) {
if (normalize(alternative.toLowerCase()).equals(normalize(name.toLowerCase()))
&& !result.contains(country)) {
result.add(country);
}
}
}
return result;
protected List<? extends BaseCountry> getBySubregion(String subregion,
List<? extends BaseCountry> countries) {
List<BaseCountry> result = new ArrayList<>();
for (BaseCountry country : countries) {
if (country.getSubregion().equalsIgnoreCase(subregion)) {
result.add(country);
}
}
return result;
}
private List<? extends BaseCountry> substringSearch(String name, List<? extends BaseCountry> countries) {
// Using 2 different 'for' loops to give priority to 'name' matches over alternative spellings
List<BaseCountry> result = new ArrayList<>();
for (BaseCountry country : countries) {
if (normalize(country.getName().toLowerCase()).contains(normalize(name.toLowerCase()))) {
result.add(country);
}
}
for (BaseCountry country : countries) {
for (String alternative : country.getAltSpellings()) {
if (normalize(alternative.toLowerCase()).contains(normalize(name.toLowerCase()))
&& !result.contains(country)) {
result.add(country);
}
}
}
return result;
private List<? extends BaseCountry> fulltextSearch(String name,
List<? extends BaseCountry> countries) {
// Using 2 different 'for' loops to give priority to 'name' matches over alternative spellings
List<BaseCountry> result = new ArrayList<>();
for (BaseCountry country : countries) {
if (normalize(country.getName().toLowerCase()).equals(normalize(name.toLowerCase()))) {
result.add(country);
}
}
for (BaseCountry country : countries) {
for (String alternative : country.getAltSpellings()) {
if (normalize(alternative.toLowerCase()).equals(normalize(name.toLowerCase()))
&& !result.contains(country)) {
result.add(country);
}
}
}
return result;
}
protected String normalize(String string) {
return Normalizer.normalize(string, Normalizer.Form.NFD)
.replaceAll("\\p{InCombiningDiacriticalMarks}+", "");
private List<? extends BaseCountry> substringSearch(String name,
List<? extends BaseCountry> countries) {
// Using 2 different 'for' loops to give priority to 'name' matches over alternative spellings
List<BaseCountry> result = new ArrayList<>();
for (BaseCountry country : countries) {
if (normalize(country.getName().toLowerCase()).contains(normalize(name.toLowerCase()))) {
result.add(country);
}
}
protected List<? extends BaseCountry> loadJson(String filename, Class<? extends BaseCountry> clazz) {
logger.info("Loading JSON {}", filename);
List<BaseCountry> countries = new ArrayList<>();
InputStream is = CountryServiceBaseV2.class.getClassLoader().getResourceAsStream(filename);
var gson = new Gson();
JsonReader reader;
try {
assert is != null;
reader = new JsonReader(new InputStreamReader(is, StandardCharsets.UTF_8));
reader.beginArray();
while (reader.hasNext()) {
BaseCountry country = gson.fromJson(reader, clazz);
countries.add(country);
}
} catch (Exception e) {
e.printStackTrace();
logger.error("Could not load JSON {}", filename);
for (BaseCountry country : countries) {
for (String alternative : country.getAltSpellings()) {
if (normalize(alternative.toLowerCase()).contains(normalize(name.toLowerCase()))
&& !result.contains(country)) {
result.add(country);
}
return countries;
}
}
return result;
}
protected String normalize(String string) {
return Normalizer.normalize(string, Normalizer.Form.NFD)
.replaceAll("\\p{InCombiningDiacriticalMarks}+", "");
}
protected List<? extends BaseCountry> loadJson(String filename,
Class<? extends BaseCountry> clazz) {
logger.info("Loading JSON {}", filename);
List<BaseCountry> countries = new ArrayList<>();
InputStream is = CountryServiceBaseV2.class.getClassLoader().getResourceAsStream(filename);
var gson = new Gson();
JsonReader reader;
try {
assert is != null;
reader = new JsonReader(new InputStreamReader(is, StandardCharsets.UTF_8));
reader.beginArray();
while (reader.hasNext()) {
BaseCountry country = gson.fromJson(reader, clazz);
countries.add(country);
}
} catch (Exception e) {
e.printStackTrace();
logger.error("Could not load JSON {}", filename);
}
return countries;
}
}
+1 -1
View File
@@ -329,7 +329,7 @@
"name": "Algeria",
"nativeName": "\u0627\u0644\u062c\u0632\u0627\u0626\u0631",
"numericCode": "012",
"population": 43851043,
"population": 44700000,
"region": "Africa",
"regionalBlocs": [
{
+2 -2
View File
@@ -13253,7 +13253,7 @@
"svg": "https://mainfacts.com/media/images/coats_of_arms/dz.svg",
"png": "https://mainfacts.com/media/images/coats_of_arms/dz.png"
},
"population": 510713,
"population": 44700000,
"maps": {
"googleMaps": "https://goo.gl/maps/RsAyAfyaiNVb8DpW8",
"openStreetMaps": "https://www.openstreetmap.org/relation/192756"
@@ -46910,7 +46910,7 @@
"68"
]
},
"capital": [],
"capital": ["Washington DC"],
"capitalInfo": {
"latlng": []
},
+1 -1
View File
@@ -12274,7 +12274,7 @@
"https://flagcdn.com/dz.svg",
"https://flagcdn.com/w320/dz.png"
],
"population": 510713,
"population": 44700000,
"maps": {
"googleMaps": "https://goo.gl/maps/RsAyAfyaiNVb8DpW8",
"openStreetMaps": "https://www.openstreetmap.org/relation/192756"