diff options
| author | Robert Godfrey <rgodfrey@apache.org> | 2014-07-29 12:58:37 +0000 |
|---|---|---|
| committer | Robert Godfrey <rgodfrey@apache.org> | 2014-07-29 12:58:37 +0000 |
| commit | 2db948b9045d5c13919db8db3bfeca3712203a56 (patch) | |
| tree | 69e9fdc687a976e60d3d3648dd4bf9825dc2ce2d /qpid/java/broker-plugins | |
| parent | 14e411f0f8fbbd10aa8bf08e8926332265e04b1c (diff) | |
| download | qpid-python-2db948b9045d5c13919db8db3bfeca3712203a56.tar.gz | |
QPID-5937 : [Java Broker] Add parameters to REST servlet to allow return of actual vs. effective attribute values
git-svn-id: https://svn.apache.org/repos/asf/qpid/trunk@1614333 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'qpid/java/broker-plugins')
7 files changed, 123 insertions, 19 deletions
diff --git a/qpid/java/broker-plugins/derby-store/src/test/java/org/apache/qpid/server/store/derby/DerbyMessageStoreQuotaEventsTest.java b/qpid/java/broker-plugins/derby-store/src/test/java/org/apache/qpid/server/store/derby/DerbyMessageStoreQuotaEventsTest.java index e39a35f9a4..0aeb6bd0e8 100644 --- a/qpid/java/broker-plugins/derby-store/src/test/java/org/apache/qpid/server/store/derby/DerbyMessageStoreQuotaEventsTest.java +++ b/qpid/java/broker-plugins/derby-store/src/test/java/org/apache/qpid/server/store/derby/DerbyMessageStoreQuotaEventsTest.java @@ -54,7 +54,7 @@ public class DerbyMessageStoreQuotaEventsTest extends MessageStoreQuotaEventsTes { final DerbyVirtualHost parent = mock(DerbyVirtualHost.class); when(parent.getContext()).thenReturn(createContextSettings()); - when(parent.getContextKeys()).thenReturn(Collections.emptySet()); + when(parent.getContextKeys(false)).thenReturn(Collections.emptySet()); when(parent.getStorePath()).thenReturn(storeLocation); when(parent.getStoreOverfullSize()).thenReturn(OVERFULL_SIZE); when(parent.getStoreUnderfullSize()).thenReturn(UNDERFULL_SIZE); diff --git a/qpid/java/broker-plugins/jdbc-store/src/main/java/org/apache/qpid/server/store/jdbc/GenericJDBCConfigurationStore.java b/qpid/java/broker-plugins/jdbc-store/src/main/java/org/apache/qpid/server/store/jdbc/GenericJDBCConfigurationStore.java index 9453c135e9..554caebf5f 100644 --- a/qpid/java/broker-plugins/jdbc-store/src/main/java/org/apache/qpid/server/store/jdbc/GenericJDBCConfigurationStore.java +++ b/qpid/java/broker-plugins/jdbc-store/src/main/java/org/apache/qpid/server/store/jdbc/GenericJDBCConfigurationStore.java @@ -114,7 +114,7 @@ public class GenericJDBCConfigurationStore extends AbstractJDBCConfigurationStor { Map<String, String> providerAttributes = new HashMap<>(); Set<String> providerAttributeNames = connectionProviderFactory.getProviderAttributeNames(); - providerAttributeNames.retainAll(parent.getContextKeys()); + providerAttributeNames.retainAll(parent.getContextKeys(false)); for(String attr : providerAttributeNames) { providerAttributes.put(attr, parent.getContextValue(String.class, attr)); diff --git a/qpid/java/broker-plugins/jdbc-store/src/main/java/org/apache/qpid/server/store/jdbc/GenericJDBCMessageStore.java b/qpid/java/broker-plugins/jdbc-store/src/main/java/org/apache/qpid/server/store/jdbc/GenericJDBCMessageStore.java index 3304d01d86..338ef52078 100644 --- a/qpid/java/broker-plugins/jdbc-store/src/main/java/org/apache/qpid/server/store/jdbc/GenericJDBCMessageStore.java +++ b/qpid/java/broker-plugins/jdbc-store/src/main/java/org/apache/qpid/server/store/jdbc/GenericJDBCMessageStore.java @@ -93,7 +93,7 @@ public class GenericJDBCMessageStore extends GenericAbstractJDBCMessageStore { Map<String, String> providerAttributes = new HashMap<>(); Set<String> providerAttributeNames = connectionProviderFactory.getProviderAttributeNames(); - providerAttributeNames.retainAll(parent.getContextKeys()); + providerAttributeNames.retainAll(parent.getContextKeys(false)); for(String attr : providerAttributeNames) { providerAttributes.put(attr, parent.getContextValue(String.class, attr)); diff --git a/qpid/java/broker-plugins/jdbc-store/src/main/java/org/apache/qpid/server/store/jdbc/JDBCDetails.java b/qpid/java/broker-plugins/jdbc-store/src/main/java/org/apache/qpid/server/store/jdbc/JDBCDetails.java index 8cd4996033..eec2d49a80 100644 --- a/qpid/java/broker-plugins/jdbc-store/src/main/java/org/apache/qpid/server/store/jdbc/JDBCDetails.java +++ b/qpid/java/broker-plugins/jdbc-store/src/main/java/org/apache/qpid/server/store/jdbc/JDBCDetails.java @@ -224,7 +224,7 @@ public abstract class JDBCDetails public static JDBCDetails getDetailsForJdbcUrl(String jdbcUrl, final ConfiguredObject<?> object) { - final Set<String> contextKeys = object.getContextKeys(); + final Set<String> contextKeys = object.getContextKeys(false); Map<String,String> mapConversion = new AbstractMap<String, String>() { @Override diff --git a/qpid/java/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/servlet/rest/ConfiguredObjectToMapConverter.java b/qpid/java/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/servlet/rest/ConfiguredObjectToMapConverter.java index 7c5ba5a6be..bc563c141e 100644 --- a/qpid/java/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/servlet/rest/ConfiguredObjectToMapConverter.java +++ b/qpid/java/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/servlet/rest/ConfiguredObjectToMapConverter.java @@ -21,6 +21,7 @@ package org.apache.qpid.server.management.plugin.servlet.rest; import java.util.ArrayList; import java.util.Collection; +import java.util.HashMap; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; @@ -33,32 +34,54 @@ public class ConfiguredObjectToMapConverter public static final String STATISTICS_MAP_KEY = "statistics"; public Map<String, Object> convertObjectToMap(final ConfiguredObject<?> confObject, - Class<? extends ConfiguredObject> clazz, - int depth) + Class<? extends ConfiguredObject> clazz, + int depth, + final boolean useActualValues, + final boolean includeSystemContext) { Map<String, Object> object = new LinkedHashMap<String, Object>(); - incorporateAttributesIntoMap(confObject, object); + incorporateAttributesIntoMap(confObject, object, useActualValues, includeSystemContext); incorporateStatisticsIntoMap(confObject, object); if(depth > 0) { - incorporateChildrenIntoMap(confObject, clazz, depth, object); + incorporateChildrenIntoMap(confObject, clazz, depth, object, useActualValues, includeSystemContext); } return object; } private void incorporateAttributesIntoMap( - final ConfiguredObject<?> confObject, Map<String, Object> object) + final ConfiguredObject<?> confObject, + Map<String, Object> object, + final boolean useActualValues, + final boolean includeSystemContext) { + for(String name : confObject.getAttributeNames()) { - Object value = confObject.getAttribute(name); + Object value = useActualValues ? confObject.getActualAttributes().get(name) : confObject.getAttribute(name); if(value instanceof ConfiguredObject) { object.put(name, ((ConfiguredObject) value).getName()); } + else if(ConfiguredObject.CONTEXT.equals(name)) + { + Map<String,Object> contextValues = new HashMap<>(); + if(useActualValues) + { + contextValues.putAll(confObject.getContext()); + } + else + { + for(String contextName : confObject.getContextKeys(!includeSystemContext)) + { + contextValues.put(contextName, confObject.getContextValue(String.class, contextName)); + } + } + object.put(ConfiguredObject.CONTEXT, contextValues); + } else if(value instanceof Collection) { List<Object> converted = new ArrayList(); @@ -98,7 +121,7 @@ public class ConfiguredObjectToMapConverter private void incorporateChildrenIntoMap( final ConfiguredObject confObject, Class<? extends ConfiguredObject> clazz, int depth, - Map<String, Object> object) + Map<String, Object> object, final boolean useActualValues, final boolean includeSystemContext) { for(Class<? extends ConfiguredObject> childClass : confObject.getModel().getChildTypes(clazz)) { @@ -109,7 +132,7 @@ public class ConfiguredObjectToMapConverter for(ConfiguredObject child : children) { - childObjects.add(convertObjectToMap(child, childClass, depth-1)); + childObjects.add(convertObjectToMap(child, childClass, depth-1, useActualValues, includeSystemContext)); } if(!childObjects.isEmpty()) diff --git a/qpid/java/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/servlet/rest/RestServlet.java b/qpid/java/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/servlet/rest/RestServlet.java index b261927ee7..87a4b6fb1a 100644 --- a/qpid/java/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/servlet/rest/RestServlet.java +++ b/qpid/java/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/servlet/rest/RestServlet.java @@ -53,9 +53,11 @@ public class RestServlet extends AbstractServlet private static final String HIERARCHY_INIT_PARAMETER = "hierarchy"; public static final String DEPTH_PARAM = "depth"; + public static final String ACTUALS_PARAM = "actuals"; public static final String SORT_PARAM = "sort"; + public static final String INCLUDE_SYS_CONTEXT_PARAM = "includeSysContext"; - public static final Set<String> RESERVED_PARAMS = new HashSet<String>(Arrays.asList(DEPTH_PARAM, SORT_PARAM)); + public static final Set<String> RESERVED_PARAMS = new HashSet<String>(Arrays.asList(DEPTH_PARAM, SORT_PARAM, ACTUALS_PARAM, INCLUDE_SYS_CONTEXT_PARAM)); private Class<? extends ConfiguredObject>[] _hierarchy; @@ -311,12 +313,14 @@ public class RestServlet extends AbstractServlet // TODO - sort special params, everything else should act as a filter int depth = getDepthParameterFromRequest(request); + boolean actuals = getBooleanParameterFromRequest(request, ACTUALS_PARAM); + boolean includeSystemContext = getBooleanParameterFromRequest(request, INCLUDE_SYS_CONTEXT_PARAM); List<Map<String, Object>> output = new ArrayList<Map<String, Object>>(); for(ConfiguredObject configuredObject : allObjects) { output.add(_objectConverter.convertObjectToMap(configuredObject, getConfiguredClass(), - depth)); + depth, actuals, includeSystemContext)); } final Writer writer = new BufferedWriter(response.getWriter()); @@ -576,5 +580,22 @@ public class RestServlet extends AbstractServlet return depth; } + private boolean getBooleanParameterFromRequest(HttpServletRequest request, final String paramName) + { + boolean value = false; + final String stringValue = request.getParameter(paramName); + if(stringValue!=null) + { + try + { + value = Boolean.parseBoolean(stringValue); + } + catch (NumberFormatException e) + { + LOGGER.warn("Could not parse " + stringValue + " as integer"); + } + } + return value; + } } diff --git a/qpid/java/broker-plugins/management-http/src/test/java/org/apache/qpid/server/management/plugin/servlet/rest/ConfiguredObjectToMapConverterTest.java b/qpid/java/broker-plugins/management-http/src/test/java/org/apache/qpid/server/management/plugin/servlet/rest/ConfiguredObjectToMapConverterTest.java index 82c8d01379..39e8ff6185 100644 --- a/qpid/java/broker-plugins/management-http/src/test/java/org/apache/qpid/server/management/plugin/servlet/rest/ConfiguredObjectToMapConverterTest.java +++ b/qpid/java/broker-plugins/management-http/src/test/java/org/apache/qpid/server/management/plugin/servlet/rest/ConfiguredObjectToMapConverterTest.java @@ -19,15 +19,19 @@ */ package org.apache.qpid.server.management.plugin.servlet.rest; +import static org.apache.qpid.server.management.plugin.servlet.rest.ConfiguredObjectToMapConverter.STATISTICS_MAP_KEY; +import static org.mockito.Matchers.eq; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; -import static org.apache.qpid.server.management.plugin.servlet.rest.ConfiguredObjectToMapConverter.STATISTICS_MAP_KEY; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; import java.util.List; import java.util.Map; +import java.util.Set; import junit.framework.TestCase; @@ -52,7 +56,8 @@ public class ConfiguredObjectToMapConverterTest extends TestCase when(_configuredObject.getStatistics()).thenReturn(Collections.singletonMap(statisticName, (Number) statisticValue)); - Map<String, Object> resultMap = _converter.convertObjectToMap(_configuredObject, ConfiguredObject.class, 0); + Map<String, Object> resultMap = _converter.convertObjectToMap(_configuredObject, ConfiguredObject.class, 0, + false, false); Map<String, Object> statsAsMap = (Map<String, Object>) resultMap.get(STATISTICS_MAP_KEY); assertNotNull("Statistics should be part of map", statsAsMap); assertEquals("Unexpected number of statistics", 1, statsAsMap.size()); @@ -65,7 +70,8 @@ public class ConfiguredObjectToMapConverterTest extends TestCase final String attributeValue = "value"; configureMockToReturnOneAttribute(_configuredObject, attributeName, attributeValue); - Map<String, Object> resultMap = _converter.convertObjectToMap(_configuredObject, ConfiguredObject.class, 0); + Map<String, Object> resultMap = _converter.convertObjectToMap(_configuredObject, ConfiguredObject.class, 0, + false, false); assertEquals("Unexpected number of attributes", 1, resultMap.size()); assertEquals("Unexpected attribute value", attributeValue, resultMap.get(attributeName)); } @@ -82,7 +88,8 @@ public class ConfiguredObjectToMapConverterTest extends TestCase configureMockToReturnOneAttribute(_configuredObject, attributeName, attributeValue); - Map<String, Object> resultMap = _converter.convertObjectToMap(_configuredObject, ConfiguredObject.class, 0); + Map<String, Object> resultMap = _converter.convertObjectToMap(_configuredObject, ConfiguredObject.class, 0, + false, false); assertEquals("Unexpected number of attributes", 1, resultMap.size()); assertEquals("Unexpected attribute value", "attributeConfiguredObjectName", resultMap.get(attributeName)); } @@ -100,7 +107,8 @@ public class ConfiguredObjectToMapConverterTest extends TestCase configureMockToReturnOneAttribute(mockChild, childAttributeName, childAttributeValue); when(_configuredObject.getChildren(TestChild.class)).thenReturn(Arrays.asList(mockChild)); - Map<String, Object> resultMap = _converter.convertObjectToMap(_configuredObject, ConfiguredObject.class, 1); + Map<String, Object> resultMap = _converter.convertObjectToMap(_configuredObject, ConfiguredObject.class, 1, + false, false); assertEquals("Unexpected parent map size", 1, resultMap.size()); final List<Map<String, Object>> childList = (List<Map<String, Object>>) resultMap.get("testchilds"); @@ -112,6 +120,58 @@ public class ConfiguredObjectToMapConverterTest extends TestCase assertEquals("Unexpected child attribute value", childAttributeValue, childMap.get(childAttributeName)); } + public void testActuals() + { + final String childAttributeName = "childattribute"; + final String childAttributeValue = "childvalue"; + final String childActualAttributeValue = "${actualvalue}"; + final Map<String,Object> actualContext = Collections.<String,Object>singletonMap("key","value"); + final Set<String> inheritedKeys = new HashSet<>(Arrays.asList("key","inheritedkey")); + + Model model = createTestModel(); + + TestChild mockChild = mock(TestChild.class); + when(mockChild.getModel()).thenReturn(model); + when(_configuredObject.getModel()).thenReturn(model); + when(_configuredObject.getAttributeNames()).thenReturn(Collections.singletonList(ConfiguredObject.CONTEXT)); + when(_configuredObject.getContextValue(eq(String.class), eq("key"))).thenReturn("value"); + when(_configuredObject.getContextValue(eq(String.class),eq("inheritedkey"))).thenReturn("foo"); + when(_configuredObject.getContextKeys(false)).thenReturn(inheritedKeys); + when(_configuredObject.getContext()).thenReturn(actualContext); + when(mockChild.getAttributeNames()).thenReturn(Arrays.asList(childAttributeName, ConfiguredObject.CONTEXT)); + when(mockChild.getAttribute(childAttributeName)).thenReturn(childAttributeValue); + when(mockChild.getActualAttributes()).thenReturn(Collections.singletonMap(childAttributeName, childActualAttributeValue)); + when(_configuredObject.getChildren(TestChild.class)).thenReturn(Arrays.asList(mockChild)); + + + Map<String, Object> resultMap = _converter.convertObjectToMap(_configuredObject, ConfiguredObject.class, 1, true, + false); + assertEquals("Unexpected parent map size", 2, resultMap.size()); + assertEquals("Incorrect context", resultMap.get(ConfiguredObject.CONTEXT), actualContext); + List<Map<String, Object>> childList = (List<Map<String, Object>>) resultMap.get("testchilds"); + assertEquals("Unexpected number of children", 1, childList.size()); + Map<String, Object> childMap = childList.get(0); + assertEquals("Unexpected child map size", 2, childMap.size()); + assertNotNull(childMap); + + assertEquals("Unexpected child attribute value", childActualAttributeValue, childMap.get(childAttributeName)); + + resultMap = _converter.convertObjectToMap(_configuredObject, ConfiguredObject.class, 1, false, false); + assertEquals("Unexpected parent map size", 2, resultMap.size()); + Map<String, Object> inheritedContext = new HashMap<>(); + inheritedContext.put("key","value"); + inheritedContext.put("inheritedkey","foo"); + assertEquals("Incorrect context", resultMap.get(ConfiguredObject.CONTEXT), inheritedContext); + childList = (List<Map<String, Object>>) resultMap.get("testchilds"); + assertEquals("Unexpected number of children", 1, childList.size()); + childMap = childList.get(0); + assertEquals("Unexpected child map size", 2, childMap.size()); + assertNotNull(childMap); + + assertEquals("Unexpected child attribute value", childAttributeValue, childMap.get(childAttributeName)); + + } + private Model createTestModel() { Model model = mock(Model.class); |
