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);
}
}
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);
}
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());
}
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;
}
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;
}
Aggregations