From 9a9e86829e16989477de6b660833a60e1724b6c2 Mon Sep 17 00:00:00 2001 From: Benjamin Gamard Date: Thu, 22 Feb 2018 12:57:33 +0100 Subject: [PATCH] #179: default language (api) --- .../docs/core/constant/ConfigType.java | 5 +++ .../src/main/resources/config.properties | 2 +- .../resources/db/update/dbupdate-016-0.sql | 2 ++ docs-web/src/dev/resources/config.properties | 2 +- .../docs/rest/resource/AppResource.java | 36 +++++++++++++++++++ docs-web/src/prod/resources/config.properties | 2 +- .../src/stress/resources/config.properties | 2 +- .../sismics/docs/rest/TestAppResource.java | 23 ++++++++++++ 8 files changed, 70 insertions(+), 4 deletions(-) create mode 100644 docs-core/src/main/resources/db/update/dbupdate-016-0.sql diff --git a/docs-core/src/main/java/com/sismics/docs/core/constant/ConfigType.java b/docs-core/src/main/java/com/sismics/docs/core/constant/ConfigType.java index 5d0525ff..990e95ba 100644 --- a/docs-core/src/main/java/com/sismics/docs/core/constant/ConfigType.java +++ b/docs-core/src/main/java/com/sismics/docs/core/constant/ConfigType.java @@ -20,6 +20,11 @@ public enum ConfigType { */ GUEST_LOGIN, + /** + * Default language. + */ + DEFAULT_LANGUAGE, + /** * SMTP server configuration. */ diff --git a/docs-core/src/main/resources/config.properties b/docs-core/src/main/resources/config.properties index a1d48290..0f5bd5e5 100644 --- a/docs-core/src/main/resources/config.properties +++ b/docs-core/src/main/resources/config.properties @@ -1 +1 @@ -db.version=15 \ No newline at end of file +db.version=16 \ No newline at end of file diff --git a/docs-core/src/main/resources/db/update/dbupdate-016-0.sql b/docs-core/src/main/resources/db/update/dbupdate-016-0.sql new file mode 100644 index 00000000..fdb2c913 --- /dev/null +++ b/docs-core/src/main/resources/db/update/dbupdate-016-0.sql @@ -0,0 +1,2 @@ +insert into T_CONFIG(CFG_ID_C, CFG_VALUE_C) values('DEFAULT_LANGUAGE', 'eng'); +update T_CONFIG set CFG_VALUE_C = '16' where CFG_ID_C = 'DB_VERSION'; diff --git a/docs-web/src/dev/resources/config.properties b/docs-web/src/dev/resources/config.properties index c51c6ec6..548243cf 100644 --- a/docs-web/src/dev/resources/config.properties +++ b/docs-web/src/dev/resources/config.properties @@ -1,3 +1,3 @@ api.current_version=${project.version} api.min_version=1.0 -db.version=15 \ No newline at end of file +db.version=16 \ No newline at end of file diff --git a/docs-web/src/main/java/com/sismics/docs/rest/resource/AppResource.java b/docs-web/src/main/java/com/sismics/docs/rest/resource/AppResource.java index 558761b4..11e52c20 100644 --- a/docs-web/src/main/java/com/sismics/docs/rest/resource/AppResource.java +++ b/docs-web/src/main/java/com/sismics/docs/rest/resource/AppResource.java @@ -16,6 +16,7 @@ import com.sismics.docs.core.util.DirectoryUtil; import com.sismics.docs.core.util.jpa.PaginatedList; import com.sismics.docs.core.util.jpa.PaginatedLists; import com.sismics.docs.rest.constant.BaseFunction; +import com.sismics.rest.exception.ClientException; import com.sismics.rest.exception.ForbiddenClientException; import com.sismics.rest.exception.ServerException; import com.sismics.rest.util.ValidationUtil; @@ -39,6 +40,7 @@ import javax.ws.rs.core.Response; import java.io.IOException; import java.nio.file.DirectoryStream; import java.nio.file.Files; +import java.text.MessageFormat; import java.util.*; /** @@ -79,6 +81,7 @@ public class AppResource extends BaseResource { String currentVersion = configBundle.getString("api.current_version"); String minVersion = configBundle.getString("api.min_version"); Boolean guestLogin = ConfigUtil.getConfigBooleanValue(ConfigType.GUEST_LOGIN); + String defaultLanguage = ConfigUtil.getConfigStringValue(ConfigType.DEFAULT_LANGUAGE); UserDao userDao = new UserDao(); DocumentDao documentDao = new DocumentDao(); String globalQuotaStr = System.getenv(Constants.GLOBAL_QUOTA_ENV); @@ -91,6 +94,7 @@ public class AppResource extends BaseResource { .add("current_version", currentVersion.replace("-SNAPSHOT", "")) .add("min_version", minVersion) .add("guest_login", guestLogin) + .add("default_language", defaultLanguage) .add("total_memory", Runtime.getRuntime().totalMemory()) .add("free_memory", Runtime.getRuntime().freeMemory()) .add("document_count", documentDao.getDocumentCount()) @@ -131,6 +135,38 @@ public class AppResource extends BaseResource { return Response.ok().build(); } + /** + * General application configuration. + * + * @api {post} /app/config General application configuration + * @apiName PostAppConfig + * @apiGroup App + * @apiParam {String} default_language Default language + * @apiError (client) ForbiddenError Access denied + * @apiPermission admin + * @apiVersion 1.5.0 + * + * @param defaultLanguage Default language + * @return Response + */ + @POST + @Path("config") + public Response config(@FormParam("default_language") String defaultLanguage) { + if (!authenticate()) { + throw new ForbiddenClientException(); + } + checkBaseFunction(BaseFunction.ADMIN); + ValidationUtil.validateRequired(defaultLanguage, "default_language"); + if (!Constants.SUPPORTED_LANGUAGES.contains(defaultLanguage)) { + throw new ClientException("ValidationError", MessageFormat.format("{0} is not a supported language", defaultLanguage)); + } + + ConfigDao configDao = new ConfigDao(); + configDao.update(ConfigType.DEFAULT_LANGUAGE, defaultLanguage); + + return Response.ok().build(); + } + /** * Get the SMTP server configuration. * diff --git a/docs-web/src/prod/resources/config.properties b/docs-web/src/prod/resources/config.properties index c51c6ec6..548243cf 100644 --- a/docs-web/src/prod/resources/config.properties +++ b/docs-web/src/prod/resources/config.properties @@ -1,3 +1,3 @@ api.current_version=${project.version} api.min_version=1.0 -db.version=15 \ No newline at end of file +db.version=16 \ No newline at end of file diff --git a/docs-web/src/stress/resources/config.properties b/docs-web/src/stress/resources/config.properties index c51c6ec6..548243cf 100644 --- a/docs-web/src/stress/resources/config.properties +++ b/docs-web/src/stress/resources/config.properties @@ -1,3 +1,3 @@ api.current_version=${project.version} api.min_version=1.0 -db.version=15 \ No newline at end of file +db.version=16 \ No newline at end of file diff --git a/docs-web/src/test/java/com/sismics/docs/rest/TestAppResource.java b/docs-web/src/test/java/com/sismics/docs/rest/TestAppResource.java index dab82572..d595c4dc 100644 --- a/docs-web/src/test/java/com/sismics/docs/rest/TestAppResource.java +++ b/docs-web/src/test/java/com/sismics/docs/rest/TestAppResource.java @@ -36,6 +36,7 @@ public class TestAppResource extends BaseJerseyTest { Long totalMemory = json.getJsonNumber("total_memory").longValue(); Assert.assertTrue(totalMemory > 0 && totalMemory > freeMemory); Assert.assertFalse(json.getBoolean("guest_login")); + Assert.assertEquals("eng", json.getString("default_language")); Assert.assertTrue(json.containsKey("global_storage_current")); Assert.assertTrue(json.getJsonNumber("active_user_count").longValue() > 0); @@ -56,6 +57,28 @@ public class TestAppResource extends BaseJerseyTest { .cookie(TokenBasedSecurityFilter.COOKIE_NAME, adminToken) .post(Entity.form(new Form())); Assert.assertEquals(Status.OK, Status.fromStatusCode(response.getStatus())); + + // Change the default language + response = target().path("/app/config").request() + .cookie(TokenBasedSecurityFilter.COOKIE_NAME, adminToken) + .post(Entity.form(new Form().param("default_language", "fra"))); + Assert.assertEquals(Status.OK, Status.fromStatusCode(response.getStatus())); + + // Check the application info + json = target().path("/app").request() + .get(JsonObject.class); + Assert.assertEquals("fra", json.getString("default_language")); + + // Change the default language + response = target().path("/app/config").request() + .cookie(TokenBasedSecurityFilter.COOKIE_NAME, adminToken) + .post(Entity.form(new Form().param("default_language", "eng"))); + Assert.assertEquals(Status.OK, Status.fromStatusCode(response.getStatus())); + + // Check the application info + json = target().path("/app").request() + .get(JsonObject.class); + Assert.assertEquals("eng", json.getString("default_language")); } /**