From 542ab737a24e71c844182aab729864d403345fe1 Mon Sep 17 00:00:00 2001 From: jendib Date: Wed, 27 Apr 2016 00:05:25 +0200 Subject: [PATCH] #79: POST /theme, GET /theme --- .../docs/rest/resource/ThemeResource.java | 51 ++++++++++++++++++- .../sismics/docs/rest/TestThemeResource.java | 19 +++++-- 2 files changed, 65 insertions(+), 5 deletions(-) diff --git a/docs-web/src/main/java/com/sismics/docs/rest/resource/ThemeResource.java b/docs-web/src/main/java/com/sismics/docs/rest/resource/ThemeResource.java index d1576c44..abf6ee13 100644 --- a/docs-web/src/main/java/com/sismics/docs/rest/resource/ThemeResource.java +++ b/docs-web/src/main/java/com/sismics/docs/rest/resource/ThemeResource.java @@ -10,8 +10,11 @@ import com.sismics.docs.core.dao.jpa.ConfigDao; import com.sismics.docs.core.model.jpa.Config; import com.sismics.docs.rest.constant.BaseFunction; import com.sismics.rest.exception.ForbiddenClientException; +import com.sismics.rest.util.JsonUtil; import com.sismics.rest.util.ValidationUtil; import com.sismics.util.css.Selector; +import org.glassfish.jersey.media.multipart.FormDataBodyPart; +import org.glassfish.jersey.media.multipart.FormDataParam; import java.io.StringReader; import java.util.Map; @@ -38,13 +41,36 @@ public class ThemeResource extends BaseResource { StringBuilder sb = new StringBuilder(); sb.append(new Selector(".navbar") .rule("background-color", themeConfig.getString("color", "#263238"))); + sb.append(themeConfig.getString("css", "")); return Response.ok().entity(sb.toString()).build(); } + /** + * Returns the theme configuration. + * + * @return Response + */ + @GET + public Response get() { + JsonObject themeConfig = getThemeConfig(); + JsonObjectBuilder json = Json.createObjectBuilder(); + json.add("name", themeConfig.getString("name", "Sismics Docs")); + return Response.ok().entity(json.build()).build(); + } + + /** + * Change the theme configuration. + * + * @param color Theme color + * @param name Application name + * @param css Custom CSS + * @return Response + */ @POST - @Path("/color") - public Response color(@FormParam("color") String color) { + public Response theme(@FormParam("color") String color, + @FormParam("name") String name, + @FormParam("css") String css) { if (!authenticate()) { throw new ForbiddenClientException(); } @@ -52,6 +78,7 @@ public class ThemeResource extends BaseResource { // Validate input data ValidationUtil.validateHexColor(color, "color", true); + name = ValidationUtil.validateLength(name, "name", 3, 30, true); // Update the JSON JsonObjectBuilder json = getMutableThemeConfig(); @@ -60,6 +87,12 @@ public class ThemeResource extends BaseResource { } else { json.add("color", color); } + if (Strings.isNullOrEmpty(name)) { + json.add("name", JsonValue.NULL); + } else { + json.add("name", name); + } + json.add("css", JsonUtil.nullable(css)); // Persist the new configuration ConfigDao configDao = new ConfigDao(); @@ -71,6 +104,20 @@ public class ThemeResource extends BaseResource { return Response.ok().entity(response.build()).build(); } + @PUT + @Path("images") + @Consumes("multipart/form-data") + public Response images( + @FormDataParam("logo") FormDataBodyPart logoBodyPart, + @FormDataParam("background") FormDataBodyPart backgrounBodyPart) { + if (!authenticate()) { + throw new ForbiddenClientException(); + } + checkBaseFunction(BaseFunction.ADMIN); + + return Response.ok().build(); + } + /** * Returns the theme configuration object. * diff --git a/docs-web/src/test/java/com/sismics/docs/rest/TestThemeResource.java b/docs-web/src/test/java/com/sismics/docs/rest/TestThemeResource.java index 2de11477..61c06cef 100644 --- a/docs-web/src/test/java/com/sismics/docs/rest/TestThemeResource.java +++ b/docs-web/src/test/java/com/sismics/docs/rest/TestThemeResource.java @@ -25,17 +25,30 @@ public class TestThemeResource extends BaseJerseyTest { // Get the stylesheet anonymously String stylesheet = target().path("/theme/stylesheet").request() .get(String.class); - Assert.assertTrue(stylesheet.contains("background-color: inherit;")); + Assert.assertTrue(stylesheet.contains("background-color: #263238;")); + + // Get the theme configuration anonymously + JsonObject json = target().path("/theme").request() + .get(JsonObject.class); + Assert.assertEquals("Sismics Docs", json.getString("name")); // Update the main color as admin - target().path("/theme/color").request() + target().path("/theme").request() .cookie(TokenBasedSecurityFilter.COOKIE_NAME, adminToken) .post(Entity.form(new Form() - .param("color", "#ff0000")), JsonObject.class); + .param("color", "#ff0000") + .param("name", "My App") + .param("css", ".body { content: 'Custom CSS'; }")), JsonObject.class); // Get the stylesheet anonymously stylesheet = target().path("/theme/stylesheet").request() .get(String.class); Assert.assertTrue(stylesheet.contains("background-color: #ff0000;")); + Assert.assertTrue(stylesheet.contains("Custom CSS")); + + // Get the theme configuration anonymously + json = target().path("/theme").request() + .get(JsonObject.class); + Assert.assertEquals("My App", json.getString("name")); } } \ No newline at end of file