[Major] Refactored EnumHandler for proper use in REST and different realms

This commit is contained in:
Robert von Burg 2019-04-10 14:10:29 +02:00
parent 749e1f3b0a
commit ca7f80d58e
7 changed files with 46 additions and 65 deletions

View File

@ -15,6 +15,7 @@
*/
package li.strolch.runtime.query.enums;
import static li.strolch.model.StrolchModelConstants.TYPE_ENUMERATION;
import static li.strolch.utils.helper.StringHelper.UNDERLINE;
import java.text.MessageFormat;
@ -26,14 +27,11 @@ import java.util.Set;
import li.strolch.agent.api.ComponentContainer;
import li.strolch.agent.api.StrolchComponent;
import li.strolch.exception.StrolchException;
import li.strolch.model.Locator;
import li.strolch.model.ParameterBag;
import li.strolch.model.Resource;
import li.strolch.model.parameter.StringParameter;
import li.strolch.persistence.api.StrolchTransaction;
import li.strolch.privilege.model.Certificate;
import li.strolch.runtime.StrolchConstants;
import li.strolch.runtime.configuration.ComponentConfiguration;
import li.strolch.utils.dbc.DBC;
/**
@ -41,49 +39,18 @@ import li.strolch.utils.dbc.DBC;
*/
public class DefaultEnumHandler extends StrolchComponent implements EnumHandler {
private static final String PROP_REALM = "realm"; //$NON-NLS-1$
private String realm;
private Map<String, Locator> enumLocators;
public DefaultEnumHandler(ComponentContainer container, String componentName) {
super(container, componentName);
}
@Override
public void initialize(ComponentConfiguration configuration) throws Exception {
this.enumLocators = new HashMap<>();
this.realm = configuration.getString(PROP_REALM, StrolchConstants.DEFAULT_REALM);
Set<String> enumNames = configuration.getPropertyKeys();
for (String enumName : enumNames) {
if (enumName.equals(PROP_REALM)) {
continue;
}
String enumLocatorS = configuration.getString(enumName, null);
Locator enumLocator = Locator.valueOf(enumLocatorS);
logger.info(MessageFormat.format("Registered enum {0} at {1}", enumName, enumLocator)); //$NON-NLS-1$
this.enumLocators.put(enumName, enumLocator);
}
super.initialize(configuration);
}
@Override
public StrolchEnum getEnum(Certificate certificate, String name, Locale locale) {
DBC.PRE.assertNotEmpty("Enum name must be given!", name); //$NON-NLS-1$
DBC.PRE.assertNotNull("Locale must be given!", locale); //$NON-NLS-1$
Locator enumLocator = this.enumLocators.get(name);
if (enumLocator == null)
throw new StrolchException(
MessageFormat.format("No enumeration is configured for the name {0}", name)); //$NON-NLS-1$
try (StrolchTransaction tx = getContainer().getRealm(this.realm).openTx(certificate, EnumHandler.class, true)) {
Resource enumeration = tx.findElement(enumLocator);
try (StrolchTransaction tx = openTx(certificate, true)) {
Resource enumeration = tx.getResourceBy(TYPE_ENUMERATION, name, true);
ParameterBag enumValuesByLanguage = findParameterBagByLanguage(enumeration, locale);
Set<String> parameterKeySet = enumValuesByLanguage.getParameterKeySet();

View File

@ -1,12 +1,12 @@
/*
* Copyright 2013 Robert von Burg <eitch@eitchnet.ch>
*
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@ -24,5 +24,5 @@ import li.strolch.privilege.model.Certificate;
*/
public interface EnumHandler {
public StrolchEnum getEnum(Certificate certificate, String name, Locale locale);
StrolchEnum getEnum(Certificate certificate, String name, Locale locale);
}

View File

@ -1,12 +1,12 @@
/*
* Copyright 2013 Robert von Burg <eitch@eitchnet.ch>
*
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@ -19,7 +19,9 @@ import java.util.Locale;
import java.util.Map;
import java.util.Set;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import li.strolch.model.Tags;
import li.strolch.utils.helper.StringHelper;
/**
@ -84,8 +86,13 @@ public class StrolchEnum {
jsonObject.addProperty("name", this.name);
jsonObject.addProperty("locale", this.locale);
JsonObject valuesJ = new JsonObject();
this.values.forEach(valuesJ::addProperty);
JsonArray valuesJ = new JsonArray();
for (Map.Entry<String, String> entry : this.values.entrySet()) {
JsonObject valueJ = new JsonObject();
valueJ.addProperty(Tags.Json.KEY, entry.getKey());
valueJ.addProperty(Tags.Json.VALUE, entry.getValue());
valuesJ.add(valueJ);
}
jsonObject.add("values", valuesJ);
return jsonObject;

View File

@ -54,12 +54,6 @@
<api>li.strolch.runtime.query.enums.EnumHandler</api>
<impl>li.strolch.runtime.query.enums.DefaultEnumHandler</impl>
<depends>RealmHandler</depends>
<Properties>
<realm>defaultRealm</realm>
<salutations>Resource/Enumeration/salutations</salutations>
<sex>Resource/Enumeration/sex</sex>
<religions>Resource/Enumeration/religions</religions>
</Properties>
</Component>
<Component>
<name>PolicyHandler</name>

View File

@ -63,6 +63,7 @@ public class StrolchModelConstants {
public static final String BAG_RELATIONS = "relations";
public static final String BAG_PARAMETERS = "parameters";
public static final String TYPE_PARAMETERS = "Parameters";
public static final String TYPE_ENUMERATION = "Enumeration";
/**
* ID of the admin role which has access to all resources

View File

@ -1,12 +1,12 @@
/*
* Copyright 2015 Robert von Burg <eitch@eitchnet.ch>
*
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@ -21,7 +21,6 @@ import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.util.Locale;
@ -29,7 +28,6 @@ import java.util.Locale;
import li.strolch.privilege.model.Certificate;
import li.strolch.rest.RestfulStrolchComponent;
import li.strolch.rest.StrolchRestfulConstants;
import li.strolch.rest.helper.RestfulHelper;
import li.strolch.runtime.query.enums.EnumHandler;
import li.strolch.runtime.query.enums.StrolchEnum;
import org.slf4j.Logger;
@ -46,8 +44,7 @@ public class EnumQuery {
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("{name}")
public Response getEnum(@PathParam("name") String name, @Context HttpServletRequest request,
@Context HttpHeaders headers) {
public Response getEnum(@PathParam("name") String name, @Context HttpServletRequest request) {
try {
@ -55,8 +52,28 @@ public class EnumQuery {
.getComponent(EnumHandler.class);
Certificate cert = (Certificate) request.getAttribute(StrolchRestfulConstants.STROLCH_CERTIFICATE);
Locale locale = RestfulHelper.getLocale(headers);
StrolchEnum strolchEnum = enumHandler.getEnum(cert, name, locale);
StrolchEnum strolchEnum = enumHandler.getEnum(cert, name, cert.getLocale());
return Response.ok().entity(strolchEnum.toJson().toString()).build();
} catch (Exception e) {
logger.error(e.getMessage(), e);
return Response.serverError().entity(e.getMessage()).build();
}
}
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("{name}/{locale}")
public Response getEnumBySpecificLocale(@PathParam("name") String name, @PathParam("locale") String locale,
@Context HttpServletRequest request) {
try {
EnumHandler enumHandler = RestfulStrolchComponent.getInstance().getContainer()
.getComponent(EnumHandler.class);
Certificate cert = (Certificate) request.getAttribute(StrolchRestfulConstants.STROLCH_CERTIFICATE);
StrolchEnum strolchEnum = enumHandler.getEnum(cert, name, Locale.forLanguageTag(locale));
return Response.ok().entity(strolchEnum.toJson().toString()).build();
} catch (Exception e) {

View File

@ -53,11 +53,6 @@
<api>li.strolch.runtime.query.enums.EnumHandler</api>
<impl>li.strolch.runtime.query.enums.DefaultEnumHandler</impl>
<depends>RealmHandler</depends>
<Properties>
<realm>defaultRealm</realm>
<salutation>Resource/Enumeration/salutation</salutation>
<sex>Resource/Enumeration/sex</sex>
</Properties>
</Component>
</env>
</StrolchConfiguration>