#79: POST /theme, GET /theme

This commit is contained in:
jendib 2016-04-27 00:05:25 +02:00
parent 6e1276293f
commit 542ab737a2
2 changed files with 65 additions and 5 deletions

View File

@ -10,8 +10,11 @@ import com.sismics.docs.core.dao.jpa.ConfigDao;
import com.sismics.docs.core.model.jpa.Config; import com.sismics.docs.core.model.jpa.Config;
import com.sismics.docs.rest.constant.BaseFunction; import com.sismics.docs.rest.constant.BaseFunction;
import com.sismics.rest.exception.ForbiddenClientException; import com.sismics.rest.exception.ForbiddenClientException;
import com.sismics.rest.util.JsonUtil;
import com.sismics.rest.util.ValidationUtil; import com.sismics.rest.util.ValidationUtil;
import com.sismics.util.css.Selector; 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.io.StringReader;
import java.util.Map; import java.util.Map;
@ -38,13 +41,36 @@ public class ThemeResource extends BaseResource {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
sb.append(new Selector(".navbar") sb.append(new Selector(".navbar")
.rule("background-color", themeConfig.getString("color", "#263238"))); .rule("background-color", themeConfig.getString("color", "#263238")));
sb.append(themeConfig.getString("css", ""));
return Response.ok().entity(sb.toString()).build(); 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 @POST
@Path("/color") public Response theme(@FormParam("color") String color,
public Response color(@FormParam("color") String color) { @FormParam("name") String name,
@FormParam("css") String css) {
if (!authenticate()) { if (!authenticate()) {
throw new ForbiddenClientException(); throw new ForbiddenClientException();
} }
@ -52,6 +78,7 @@ public class ThemeResource extends BaseResource {
// Validate input data // Validate input data
ValidationUtil.validateHexColor(color, "color", true); ValidationUtil.validateHexColor(color, "color", true);
name = ValidationUtil.validateLength(name, "name", 3, 30, true);
// Update the JSON // Update the JSON
JsonObjectBuilder json = getMutableThemeConfig(); JsonObjectBuilder json = getMutableThemeConfig();
@ -60,6 +87,12 @@ public class ThemeResource extends BaseResource {
} else { } else {
json.add("color", color); 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 // Persist the new configuration
ConfigDao configDao = new ConfigDao(); ConfigDao configDao = new ConfigDao();
@ -71,6 +104,20 @@ public class ThemeResource extends BaseResource {
return Response.ok().entity(response.build()).build(); 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. * Returns the theme configuration object.
* *

View File

@ -25,17 +25,30 @@ public class TestThemeResource extends BaseJerseyTest {
// Get the stylesheet anonymously // Get the stylesheet anonymously
String stylesheet = target().path("/theme/stylesheet").request() String stylesheet = target().path("/theme/stylesheet").request()
.get(String.class); .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 // Update the main color as admin
target().path("/theme/color").request() target().path("/theme").request()
.cookie(TokenBasedSecurityFilter.COOKIE_NAME, adminToken) .cookie(TokenBasedSecurityFilter.COOKIE_NAME, adminToken)
.post(Entity.form(new Form() .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 // Get the stylesheet anonymously
stylesheet = target().path("/theme/stylesheet").request() stylesheet = target().path("/theme/stylesheet").request()
.get(String.class); .get(String.class);
Assert.assertTrue(stylesheet.contains("background-color: #ff0000;")); 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"));
} }
} }