parent
4a64d03056
commit
5d891c2c6d
|
|
@ -20,6 +20,7 @@ package org.libreccm.api.admin.configuration;
|
|||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
import javax.json.Json;
|
||||
import javax.json.JsonNumber;
|
||||
import javax.json.JsonValue;
|
||||
import javax.ws.rs.WebApplicationException;
|
||||
|
|
@ -43,4 +44,9 @@ class BigDecimalConverter implements SettingValueConverter<BigDecimal> {
|
|||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public JsonValue convertSettingValue(final BigDecimal value) {
|
||||
return Json.createValue(value);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -18,6 +18,7 @@
|
|||
*/
|
||||
package org.libreccm.api.admin.configuration;
|
||||
|
||||
import javax.json.Json;
|
||||
import javax.json.JsonValue;
|
||||
import javax.ws.rs.WebApplicationException;
|
||||
import javax.ws.rs.core.Response;
|
||||
|
|
@ -30,16 +31,28 @@ class BooleanConverter implements SettingValueConverter<Boolean> {
|
|||
|
||||
@Override
|
||||
public Boolean convertSettingValue(final JsonValue value) {
|
||||
switch(value.getValueType()) {
|
||||
switch (value.getValueType()) {
|
||||
case FALSE:
|
||||
return false;
|
||||
case TRUE:
|
||||
return true;
|
||||
default:
|
||||
throw new WebApplicationException(
|
||||
"The provided value is not a boolean.",
|
||||
Response.Status.BAD_REQUEST
|
||||
);
|
||||
"The provided value is not a boolean.",
|
||||
Response.Status.BAD_REQUEST
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public JsonValue convertSettingValue(final Boolean value) {
|
||||
if (value == null) {
|
||||
return JsonValue.NULL;
|
||||
} else if (value) {
|
||||
return JsonValue.TRUE;
|
||||
} else {
|
||||
return JsonValue.FALSE;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -20,11 +20,22 @@ package org.libreccm.api.admin.configuration;
|
|||
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.libreccm.configuration.AbstractSetting;
|
||||
import org.libreccm.configuration.BigDecimalSetting;
|
||||
import org.libreccm.configuration.BooleanSetting;
|
||||
import org.libreccm.configuration.ConfigurationInfo;
|
||||
import org.libreccm.configuration.ConfigurationManager;
|
||||
import org.libreccm.configuration.DoubleSetting;
|
||||
import org.libreccm.configuration.EnumSetting;
|
||||
import org.libreccm.configuration.LocalizedStringSetting;
|
||||
import org.libreccm.configuration.LongSetting;
|
||||
import org.libreccm.configuration.SettingInfo;
|
||||
import org.libreccm.configuration.SettingManager;
|
||||
import org.libreccm.configuration.StringListSetting;
|
||||
import org.libreccm.configuration.StringSetting;
|
||||
import org.libreccm.core.CoreConstants;
|
||||
import org.libreccm.l10n.LocalizedString;
|
||||
import org.libreccm.security.AuthorizationRequired;
|
||||
import org.libreccm.security.RequiresPrivilege;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
|
|
@ -33,10 +44,14 @@ import java.util.HashMap;
|
|||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import javax.enterprise.context.RequestScoped;
|
||||
import javax.inject.Inject;
|
||||
import javax.json.Json;
|
||||
import javax.json.JsonObject;
|
||||
import javax.json.JsonObjectBuilder;
|
||||
import javax.json.JsonValue;
|
||||
import javax.transaction.Transactional;
|
||||
import javax.ws.rs.Consumes;
|
||||
|
|
@ -54,7 +69,7 @@ import javax.ws.rs.core.Response;
|
|||
* @author <a href="mailto:jens.pelzetter@googlemail.com">Jens Pelzetter</a>
|
||||
*/
|
||||
@RequestScoped
|
||||
@Path("/configuration")
|
||||
@Path("/configurations")
|
||||
public class ConfigurationApi {
|
||||
|
||||
private static final Logger LOGGER = LogManager.getLogger(
|
||||
|
|
@ -64,6 +79,9 @@ public class ConfigurationApi {
|
|||
@Inject
|
||||
private ConfigurationManager confManager;
|
||||
|
||||
@Inject
|
||||
private SettingManager settingManager;
|
||||
|
||||
private final Map<String, SettingValueConverter<?>> converters;
|
||||
|
||||
public ConfigurationApi() {
|
||||
|
|
@ -71,7 +89,7 @@ public class ConfigurationApi {
|
|||
converters.put(BigDecimal.class.getName(), new BigDecimalConverter());
|
||||
converters.put(Boolean.class.getName(), new BooleanConverter());
|
||||
converters.put(Double.class.getName(), new DoubleConverter());
|
||||
converters.put(EnumConverter.class.getName(), new EnumConverter());
|
||||
converters.put(Set.class.getName(), new EnumConverter());
|
||||
converters.put(
|
||||
LocalizedString.class.getName(), new LocalizedStringConverter()
|
||||
);
|
||||
|
|
@ -83,6 +101,7 @@ public class ConfigurationApi {
|
|||
@GET
|
||||
@Path("/")
|
||||
@Produces(MediaType.APPLICATION_JSON)
|
||||
@AuthorizationRequired
|
||||
@RequiresPrivilege(CoreConstants.PRIVILEGE_ADMIN)
|
||||
@Transactional(Transactional.TxType.REQUIRED)
|
||||
public List<ConfigurationInfo> getConfigurations() {
|
||||
|
|
@ -96,6 +115,7 @@ public class ConfigurationApi {
|
|||
@GET
|
||||
@Path("/{confName}")
|
||||
@Produces(MediaType.APPLICATION_JSON)
|
||||
@AuthorizationRequired
|
||||
@RequiresPrivilege(CoreConstants.PRIVILEGE_ADMIN)
|
||||
@Transactional(Transactional.TxType.REQUIRED)
|
||||
public ConfigurationInfo getConfiguration(
|
||||
|
|
@ -123,8 +143,115 @@ public class ConfigurationApi {
|
|||
}
|
||||
|
||||
@GET
|
||||
@Path("/{confName}/{setting}")
|
||||
@Path("/{confName}/settings")
|
||||
@Produces(MediaType.APPLICATION_JSON)
|
||||
@AuthorizationRequired
|
||||
@RequiresPrivilege(CoreConstants.PRIVILEGE_ADMIN)
|
||||
@Transactional(Transactional.TxType.REQUIRED)
|
||||
public JsonObject getSettings(@PathParam("confName") final String confName) {
|
||||
// Check if class exists.
|
||||
try {
|
||||
Class.forName(confName);
|
||||
} catch (ClassNotFoundException ex) {
|
||||
throw new WebApplicationException(
|
||||
String.format("No configuration %s available.", confName),
|
||||
Response.Status.NOT_FOUND
|
||||
);
|
||||
}
|
||||
|
||||
final List<AbstractSetting> settings = settingManager
|
||||
.retrieveAllSettings(confName);
|
||||
final JsonObjectBuilder builder = Json.createObjectBuilder();
|
||||
|
||||
settings
|
||||
.stream()
|
||||
.forEach(
|
||||
setting -> builder.add(
|
||||
setting.getName(),
|
||||
convertSettingValue(setting)));
|
||||
|
||||
return builder.build();
|
||||
}
|
||||
|
||||
private JsonValue convertSettingValue(final AbstractSetting setting) {
|
||||
final String settingType = setting.getClass().getName();
|
||||
|
||||
if (BigDecimalSetting.class.getName().equals(settingType)) {
|
||||
final BigDecimalConverter converter
|
||||
= (BigDecimalConverter) converters.get(
|
||||
BigDecimal.class.getName()
|
||||
);
|
||||
return converter.convertSettingValue(
|
||||
((BigDecimalSetting) setting).getValue()
|
||||
);
|
||||
} else if (BooleanSetting.class.getName().equals(settingType)) {
|
||||
final BooleanConverter converter = (BooleanConverter) converters
|
||||
.get(Boolean.class.getName());
|
||||
return converter.convertSettingValue(
|
||||
((BooleanSetting) setting).getValue()
|
||||
);
|
||||
} else if (DoubleSetting.class.getName().equals(settingType)) {
|
||||
final DoubleConverter converter = (DoubleConverter) converters.get(
|
||||
Double.class.getName()
|
||||
);
|
||||
return converter.convertSettingValue(
|
||||
((DoubleSetting) setting).getValue()
|
||||
);
|
||||
} else if (EnumSetting.class.getName().equals(settingType)) {
|
||||
final EnumConverter converter = (EnumConverter) converters.get(
|
||||
Set.class.getName()
|
||||
);
|
||||
return converter.convertSettingValue(
|
||||
((EnumSetting) setting).getValue()
|
||||
);
|
||||
} else if (LocalizedStringSetting.class.getName().equals(settingType)) {
|
||||
final LocalizedStringConverter converter
|
||||
= (LocalizedStringConverter) converters.get(
|
||||
LocalizedString.class.getName()
|
||||
);
|
||||
return converter.convertSettingValue(
|
||||
((LocalizedStringSetting) setting).getValue()
|
||||
);
|
||||
} else if (LongSetting.class.getName().equals(settingType)) {
|
||||
final LongConverter converter = (LongConverter) converters.get(
|
||||
Long.class.getName()
|
||||
);
|
||||
return converter.convertSettingValue(
|
||||
((LongSetting) setting).getValue()
|
||||
);
|
||||
} else if (StringSetting.class.getName().equals(settingType)) {
|
||||
final StringConverter converter = (StringConverter) converters.get(
|
||||
Set.class.getName()
|
||||
);
|
||||
return converter.convertSettingValue(
|
||||
((StringSetting) setting).getValue()
|
||||
);
|
||||
} else if (StringListSetting.class.getName().equals(settingType)) {
|
||||
final StringListConverter converter
|
||||
= (StringListConverter) converters.get(
|
||||
Set.class.getName()
|
||||
);
|
||||
return converter.convertSettingValue(
|
||||
((StringListSetting) setting).getValue()
|
||||
);
|
||||
} else {
|
||||
throw new WebApplicationException(
|
||||
String.format(
|
||||
"The type \"%s\" of setting \"%s\" of configuration \"%s\" "
|
||||
+ "is unknown.",
|
||||
settingType,
|
||||
setting.getName(),
|
||||
setting.getConfigurationClass()
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@GET
|
||||
@Path("/{confName}/settings/{setting}")
|
||||
@Produces(MediaType.APPLICATION_JSON)
|
||||
@AuthorizationRequired
|
||||
@RequiresPrivilege(CoreConstants.PRIVILEGE_ADMIN)
|
||||
@Transactional(Transactional.TxType.REQUIRED)
|
||||
public Object getSetting(
|
||||
|
|
@ -184,9 +311,10 @@ public class ConfigurationApi {
|
|||
}
|
||||
|
||||
@PUT
|
||||
@Path("/{confName}/{setting}")
|
||||
@Path("/{confName}/settings/{setting}")
|
||||
@Produces(MediaType.APPLICATION_JSON)
|
||||
@Consumes(MediaType.APPLICATION_JSON)
|
||||
@AuthorizationRequired
|
||||
@RequiresPrivilege(CoreConstants.PRIVILEGE_ADMIN)
|
||||
@Transactional(Transactional.TxType.REQUIRED)
|
||||
public Response updateSetting(
|
||||
|
|
|
|||
|
|
@ -18,6 +18,7 @@
|
|||
*/
|
||||
package org.libreccm.api.admin.configuration;
|
||||
|
||||
import javax.json.Json;
|
||||
import javax.json.JsonNumber;
|
||||
import javax.json.JsonValue;
|
||||
import javax.ws.rs.WebApplicationException;
|
||||
|
|
@ -41,4 +42,9 @@ class DoubleConverter implements SettingValueConverter<Double> {
|
|||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public JsonValue convertSettingValue(final Double value) {
|
||||
return Json.createValue(value);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -21,6 +21,7 @@ package org.libreccm.api.admin.configuration;
|
|||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import javax.json.Json;
|
||||
import javax.json.JsonArray;
|
||||
import javax.json.JsonString;
|
||||
import javax.json.JsonValue;
|
||||
|
|
@ -50,6 +51,11 @@ class EnumConverter
|
|||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public JsonValue convertSettingValue(final Set<String> value) {
|
||||
return Json.createArrayBuilder(value).build();
|
||||
}
|
||||
|
||||
private String convertEnumValue(final JsonValue value) {
|
||||
if (value.getValueType() == JsonValue.ValueType.STRING) {
|
||||
return ((JsonString) value).getString();
|
||||
|
|
|
|||
|
|
@ -24,7 +24,9 @@ import java.util.IllformedLocaleException;
|
|||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.json.Json;
|
||||
import javax.json.JsonObject;
|
||||
import javax.json.JsonObjectBuilder;
|
||||
import javax.json.JsonString;
|
||||
import javax.json.JsonValue;
|
||||
import javax.ws.rs.WebApplicationException;
|
||||
|
|
@ -61,6 +63,23 @@ public class LocalizedStringConverter
|
|||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public JsonValue convertSettingValue(final LocalizedString value) {
|
||||
final JsonObjectBuilder builder = Json.createObjectBuilder();
|
||||
|
||||
value
|
||||
.getValues()
|
||||
.entrySet()
|
||||
.stream()
|
||||
.forEach(
|
||||
entry -> builder.add(
|
||||
entry.getKey().toString(), entry.getValue()
|
||||
)
|
||||
);
|
||||
|
||||
return builder.build();
|
||||
}
|
||||
|
||||
private LocalizedValue buildLocalizedValue(
|
||||
final Map.Entry<String, JsonValue> entry
|
||||
) {
|
||||
|
|
|
|||
|
|
@ -18,6 +18,7 @@
|
|||
*/
|
||||
package org.libreccm.api.admin.configuration;
|
||||
|
||||
import javax.json.Json;
|
||||
import javax.json.JsonNumber;
|
||||
import javax.json.JsonValue;
|
||||
import javax.ws.rs.WebApplicationException;
|
||||
|
|
@ -40,4 +41,9 @@ public class LongConverter implements SettingValueConverter<Long> {
|
|||
);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public JsonValue convertSettingValue(final Long value) {
|
||||
return Json.createValue(value);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -30,4 +30,6 @@ interface SettingValueConverter<T> {
|
|||
|
||||
T convertSettingValue(JsonValue value);
|
||||
|
||||
JsonValue convertSettingValue(T value);
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -18,6 +18,7 @@
|
|||
*/
|
||||
package org.libreccm.api.admin.configuration;
|
||||
|
||||
import javax.json.Json;
|
||||
import javax.json.JsonString;
|
||||
import javax.json.JsonValue;
|
||||
import javax.ws.rs.WebApplicationException;
|
||||
|
|
@ -41,4 +42,9 @@ class StringConverter implements SettingValueConverter<String> {
|
|||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public JsonValue convertSettingValue(String value) {
|
||||
return Json.createValue(value);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -21,6 +21,7 @@ package org.libreccm.api.admin.configuration;
|
|||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import javax.json.Json;
|
||||
import javax.json.JsonString;
|
||||
import javax.json.JsonValue;
|
||||
import javax.ws.rs.WebApplicationException;
|
||||
|
|
@ -47,6 +48,11 @@ public class StringListConverter implements SettingValueConverter<List<String>>{
|
|||
);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public JsonValue convertSettingValue(final List<String> value) {
|
||||
return Json.createArrayBuilder(value).build();
|
||||
}
|
||||
|
||||
private String convertValue(final JsonValue value) {
|
||||
if (value.getValueType() == JsonValue.ValueType.STRING) {
|
||||
|
|
|
|||
|
|
@ -92,12 +92,21 @@ public class SettingManager {
|
|||
return settings;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves all settings for a configuration
|
||||
*
|
||||
* @param confName The name of the configuration
|
||||
*
|
||||
* @return All settings of the configuration. If not configuration with
|
||||
* the provided name exists the list will be empty.
|
||||
*/
|
||||
public List<AbstractSetting> retrieveAllSettings(final String confName) {
|
||||
Objects.requireNonNull(confName);
|
||||
|
||||
final TypedQuery<AbstractSetting> query = entityManager
|
||||
.createNamedQuery("AbstractSetting.findAllForClass",
|
||||
AbstractSetting.class);
|
||||
.createNamedQuery(
|
||||
"AbstractSetting.findAllForClass", AbstractSetting.class
|
||||
);
|
||||
query.setParameter("class", confName);
|
||||
|
||||
return query.getResultList();
|
||||
|
|
|
|||
Loading…
Reference in New Issue