Search in sources :

Example 86 with ConfiguredObject

use of org.apache.qpid.server.model.ConfiguredObject in project qpid-broker-j by apache.

the class LatestManagementController method createOrUpdate.

@Override
public ConfiguredObject<?> createOrUpdate(final ConfiguredObject<?> root, final String category, final List<String> path, final Map<String, Object> providedObject, final boolean isPost) throws ManagementException {
    try {
        final List<String> hierarchy = getCategoryHierarchy(root, category);
        if (path.isEmpty() && hierarchy.size() == 0) {
            root.setAttributes(providedObject);
            return null;
        }
        final Class<? extends ConfiguredObject> categoryClass = getRequestCategoryClass(category, root.getModel());
        ConfiguredObject theParent = root;
        if (hierarchy.size() > 1) {
            final ConfiguredObjectFinder finder = getConfiguredObjectFinder(root);
            theParent = finder.findObjectParentsFromPath(path, getHierarchy(finder, categoryClass), categoryClass);
        }
        final boolean isFullObjectURL = path.size() == hierarchy.size();
        if (isFullObjectURL) {
            final String name = path.get(path.size() - 1);
            final ConfiguredObject<?> configuredObject = theParent.getChildByName(categoryClass, name);
            if (configuredObject != null) {
                configuredObject.setAttributes(providedObject);
                return null;
            } else if (isPost) {
                throw createNotFoundManagementException(String.format("%s '%s' not found", categoryClass.getSimpleName(), name));
            } else {
                providedObject.put(ConfiguredObject.NAME, name);
            }
        }
        return theParent.createChild(categoryClass, providedObject);
    } catch (RuntimeException e) {
        throw ManagementException.toManagementException(e, getCategoryMapping(category), path);
    } catch (Error e) {
        throw ManagementException.handleError(e);
    }
}
Also used : ConfiguredObject(org.apache.qpid.server.model.ConfiguredObject) ConfiguredObjectFinder(org.apache.qpid.server.model.ConfiguredObjectFinder)

Example 87 with ConfiguredObject

use of org.apache.qpid.server.model.ConfiguredObject in project qpid-broker-j by apache.

the class LatestManagementController method invoke.

@Override
@SuppressWarnings("unchecked")
public ManagementResponse invoke(final ConfiguredObject<?> root, final String category, final List<String> names, final String operationName, final Map<String, Object> operationArguments, final boolean isPost, final boolean isSecureOrAllowedOnInsecureChannel) throws ManagementException {
    ResponseType responseType = ResponseType.DATA;
    Object returnValue;
    try {
        final ConfiguredObject<?> target = getTarget(root, category, names);
        final Map<String, ConfiguredObjectOperation<?>> availableOperations = root.getModel().getTypeRegistry().getOperations(target.getClass());
        final ConfiguredObjectOperation operation = availableOperations.get(operationName);
        if (operation == null) {
            throw createNotFoundManagementException(String.format("No such operation '%s' in '%s'", operationName, category));
        }
        final Map<String, Object> arguments;
        if (isPost) {
            arguments = operationArguments;
        } else {
            final Set<String> supported = ((List<OperationParameter>) operation.getParameters()).stream().map(OperationParameter::getName).collect(Collectors.toSet());
            arguments = operationArguments.entrySet().stream().filter(e -> !RESERVED_PARAMS.contains(e.getKey()) || supported.contains(e.getKey())).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
        }
        if (operation.isSecure(target, arguments) && !isSecureOrAllowedOnInsecureChannel) {
            throw createForbiddenManagementException(String.format("Operation '%s' can only be performed over a secure (HTTPS) connection", operationName));
        }
        if (!isPost && !operation.isNonModifying()) {
            throw createNotAllowedManagementException(String.format("Operation '%s' modifies the object so you must use POST.", operationName), Collections.singletonMap("Allow", "POST"));
        }
        returnValue = operation.perform(target, arguments);
        if (ConfiguredObject.class.isAssignableFrom(operation.getReturnType()) || returnsCollectionOfConfiguredObjects(operation)) {
            responseType = ResponseType.MODEL_OBJECT;
        }
    } catch (RuntimeException e) {
        throw ManagementException.toManagementException(e, getCategoryMapping(category), names);
    } catch (Error e) {
        throw ManagementException.handleError(e);
    }
    return new ControllerManagementResponse(responseType, returnValue);
}
Also used : ResponseType(org.apache.qpid.server.management.plugin.ResponseType) ConfiguredObject(org.apache.qpid.server.model.ConfiguredObject) ArrayList(java.util.ArrayList) List(java.util.List) ConfiguredObject(org.apache.qpid.server.model.ConfiguredObject) ConfiguredObjectOperation(org.apache.qpid.server.model.ConfiguredObjectOperation) ControllerManagementResponse(org.apache.qpid.server.management.plugin.controller.ControllerManagementResponse)

Example 88 with ConfiguredObject

use of org.apache.qpid.server.model.ConfiguredObject in project qpid-broker-j by apache.

the class LatestManagementController method getCategoryHierarchy.

@Override
public List<String> getCategoryHierarchy(final ConfiguredObject<?> root, final String category) {
    ConfiguredObjectFinder finder = getConfiguredObjectFinder(root);
    Class<? extends ConfiguredObject>[] hierarchy = finder.getHierarchy(category.toLowerCase());
    if (hierarchy == null) {
        return Collections.emptyList();
    }
    return Arrays.stream(hierarchy).map(Class::getSimpleName).collect(Collectors.toList());
}
Also used : ConfiguredObject(org.apache.qpid.server.model.ConfiguredObject) ConfiguredObjectFinder(org.apache.qpid.server.model.ConfiguredObjectFinder)

Example 89 with ConfiguredObject

use of org.apache.qpid.server.model.ConfiguredObject in project qpid-broker-j by apache.

the class LatestManagementController method getConfiguredObjectFinder.

private ConfiguredObjectFinder getConfiguredObjectFinder(final ConfiguredObject<?> root) {
    ConfiguredObjectFinder finder = _configuredObjectFinders.get(root);
    if (finder == null) {
        finder = new ConfiguredObjectFinder(root);
        final ConfiguredObjectFinder existingValue = _configuredObjectFinders.putIfAbsent(root, finder);
        if (existingValue != null) {
            finder = existingValue;
        } else {
            final AbstractConfigurationChangeListener deletionListener = new AbstractConfigurationChangeListener() {

                @Override
                public void stateChanged(final ConfiguredObject<?> object, final State oldState, final State newState) {
                    if (newState == State.DELETED) {
                        _configuredObjectFinders.remove(root);
                    }
                }
            };
            root.addChangeListener(deletionListener);
            if (root.getState() == State.DELETED) {
                _configuredObjectFinders.remove(root);
                root.removeChangeListener(deletionListener);
            }
        }
    }
    return finder;
}
Also used : State(org.apache.qpid.server.model.State) ConfiguredObject(org.apache.qpid.server.model.ConfiguredObject) ConfiguredObjectFinder(org.apache.qpid.server.model.ConfiguredObjectFinder) AbstractConfigurationChangeListener(org.apache.qpid.server.model.AbstractConfigurationChangeListener)

Example 90 with ConfiguredObject

use of org.apache.qpid.server.model.ConfiguredObject in project qpid-broker-j by apache.

the class LatestManagementController method getPreferences.

@Override
public Object getPreferences(final ConfiguredObject<?> root, final String category, final List<String> path, final Map<String, List<String>> parameters) throws ManagementException {
    Object responseObject;
    try {
        final List<String> hierarchy = getCategoryHierarchy(root, category);
        final Collection<ConfiguredObject<?>> allObjects = getTargetObjects(root, category, path, null);
        if (allObjects.isEmpty() && isFullPath(root, path, category)) {
            throw createNotFoundManagementException("Not Found");
        }
        final RequestInfo requestInfo = RequestInfo.createPreferencesRequestInfo(path.subList(0, hierarchy.size()), path.subList(hierarchy.size() + 1, path.size()), parameters);
        if (path.contains("*")) {
            List<Object> preferencesList = new ArrayList<>(allObjects.size());
            responseObject = preferencesList;
            for (ConfiguredObject<?> target : allObjects) {
                try {
                    final UserPreferences userPreferences = target.getUserPreferences();
                    final Object preferences = _userPreferenceHandler.handleGET(userPreferences, requestInfo);
                    if (preferences == null || (preferences instanceof Collection && ((Collection) preferences).isEmpty()) || (preferences instanceof Map && ((Map) preferences).isEmpty())) {
                        continue;
                    }
                    preferencesList.add(preferences);
                } catch (NotFoundException e) {
                // The case where the preference's type and name is provided, but this particular object does not
                // have a matching preference.
                }
            }
        } else {
            final ConfiguredObject<?> target = allObjects.iterator().next();
            final UserPreferences userPreferences = target.getUserPreferences();
            responseObject = _userPreferenceHandler.handleGET(userPreferences, requestInfo);
        }
    } catch (RuntimeException e) {
        throw ManagementException.toManagementException(e, getCategoryMapping(category), path);
    } catch (Error e) {
        throw ManagementException.handleError(e);
    }
    return responseObject;
}
Also used : UserPreferences(org.apache.qpid.server.model.preferences.UserPreferences) ArrayList(java.util.ArrayList) NotFoundException(org.apache.qpid.server.management.plugin.servlet.rest.NotFoundException) RequestInfo(org.apache.qpid.server.management.plugin.servlet.rest.RequestInfo) Collection(java.util.Collection) ConfiguredObject(org.apache.qpid.server.model.ConfiguredObject) ConfiguredObject(org.apache.qpid.server.model.ConfiguredObject) ConcurrentMap(java.util.concurrent.ConcurrentMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Aggregations

ConfiguredObject (org.apache.qpid.server.model.ConfiguredObject)117 ArrayList (java.util.ArrayList)43 HashMap (java.util.HashMap)35 Test (org.junit.Test)33 Map (java.util.Map)29 List (java.util.List)27 LinkedHashMap (java.util.LinkedHashMap)25 UUID (java.util.UUID)21 AbstractConfiguredObject (org.apache.qpid.server.model.AbstractConfiguredObject)21 AbstractConfigurationChangeListener (org.apache.qpid.server.model.AbstractConfigurationChangeListener)15 Collection (java.util.Collection)12 LegacyConfiguredObject (org.apache.qpid.server.management.plugin.controller.LegacyConfiguredObject)12 ConfiguredObjectFinder (org.apache.qpid.server.model.ConfiguredObjectFinder)12 ManagedObject (org.apache.qpid.server.model.ManagedObject)11 State (org.apache.qpid.server.model.State)10 Date (java.util.Date)7 TreeMap (java.util.TreeMap)6 CountDownLatch (java.util.concurrent.CountDownLatch)6 GenericLegacyConfiguredObject (org.apache.qpid.server.management.plugin.controller.GenericLegacyConfiguredObject)6 InternalMessageHeader (org.apache.qpid.server.message.internal.InternalMessageHeader)6