use of org.apache.qpid.server.model.preferences.UserPreferences in project qpid-broker-j by apache.
the class RestServlet method doGetUserPreferences.
private void doGetUserPreferences(final ConfiguredObject<?> managedObject, final Class<? extends ConfiguredObject> configuredClass, final ConfiguredObjectFinder finder, final RequestInfo requestInfo, final HttpServletRequest request, final HttpServletResponse response) throws IOException, ServletException {
Collection<ConfiguredObject<?>> allObjects = getTargetObjects(configuredClass, finder, requestInfo, Collections.<Predicate<ConfiguredObject<?>>>emptyList());
if (allObjects == null || (allObjects.isEmpty() && isSingleObjectRequest(requestInfo, finder.getHierarchy(configuredClass)))) {
sendJsonErrorResponse(request, response, HttpServletResponse.SC_NOT_FOUND, "Not Found");
return;
}
final Object responseObject;
if (requestInfo.hasWildcard()) {
responseObject = new ArrayList<>(allObjects.size());
for (ConfiguredObject<?> target : allObjects) {
final UserPreferences userPreferences = target.getUserPreferences();
try {
final Object preferences = _userPreferenceHandler.handleGET(userPreferences, requestInfo);
if (preferences == null || (preferences instanceof Collection && ((Collection) preferences).isEmpty()) || (preferences instanceof Map && ((Map) preferences).isEmpty())) {
continue;
}
((List<Object>) responseObject).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 {
ConfiguredObject<?> target = allObjects.iterator().next();
final UserPreferences userPreferences = target.getUserPreferences();
responseObject = _userPreferenceHandler.handleGET(userPreferences, requestInfo);
}
sendJsonResponse(responseObject, request, response);
}
use of org.apache.qpid.server.model.preferences.UserPreferences in project qpid-broker-j by apache.
the class AbstractConfiguredObject method createUserPreferences.
private void createUserPreferences() {
if (this instanceof UserPreferencesCreator) {
return;
}
UserPreferencesCreator preferenceCreator = getAncestor(UserPreferencesCreator.class);
if (preferenceCreator != null) {
UserPreferences userPreferences = preferenceCreator.createUserPreferences(this);
setUserPreferences(userPreferences);
}
}
use of org.apache.qpid.server.model.preferences.UserPreferences in project qpid-broker-j by apache.
the class RestUserPreferenceHandler method handlePUT.
public void handlePUT(ConfiguredObject<?> target, RequestInfo requestInfo, Object providedObject) {
UserPreferences userPreferences = target.getUserPreferences();
if (userPreferences == null) {
throw new NotFoundException("User preferences are not available");
}
final List<String> preferencesParts = requestInfo.getPreferencesParts();
if (preferencesParts.size() == 2) {
if (!(providedObject instanceof Map)) {
throw new IllegalArgumentException("expected object");
}
Map<String, Object> providedAttributes = (Map<String, Object>) providedObject;
String type = preferencesParts.get(0);
String name = preferencesParts.get(1);
ensureAttributeMatches(providedAttributes, "name", name);
ensureAttributeMatches(providedAttributes, "type", type);
Preference preference = PreferenceFactory.fromAttributes(target, providedAttributes);
awaitFuture(userPreferences.replaceByTypeAndName(type, name, preference));
} else if (preferencesParts.size() == 1) {
String type = preferencesParts.get(0);
if (!(providedObject instanceof List)) {
throw new IllegalArgumentException("expected a list of objects");
}
List<Preference> replacementPreferences = validateAndConvert(target, type, (List<Object>) providedObject);
awaitFuture(userPreferences.replaceByType(type, replacementPreferences));
} else if (preferencesParts.size() == 0) {
if (!(providedObject instanceof Map)) {
throw new IllegalArgumentException("expected object");
}
List<Preference> replacementPreferences = validateAndConvert(target, (Map<String, Object>) providedObject);
awaitFuture(userPreferences.replace(replacementPreferences));
} else {
throw new IllegalArgumentException(String.format("unexpected path '%s'", Joiner.on("/").join(preferencesParts)));
}
}
use of org.apache.qpid.server.model.preferences.UserPreferences 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;
}
use of org.apache.qpid.server.model.preferences.UserPreferences in project qpid-broker-j by apache.
the class RestUserPreferenceHandler method handlePOST.
public void handlePOST(ConfiguredObject<?> target, RequestInfo requestInfo, Object providedObject) {
UserPreferences userPreferences = target.getUserPreferences();
if (userPreferences == null) {
throw new NotFoundException("User preferences are not available");
}
final List<String> preferencesParts = requestInfo.getPreferencesParts();
final Set<Preference> preferences = new LinkedHashSet<>();
if (preferencesParts.size() == 1) {
String type = preferencesParts.get(0);
if (!(providedObject instanceof List)) {
throw new IllegalArgumentException("expected a list of objects");
}
preferences.addAll(validateAndConvert(target, type, (List<Object>) providedObject));
} else if (preferencesParts.size() == 0) {
if (!(providedObject instanceof Map)) {
throw new IllegalArgumentException("expected object");
}
preferences.addAll(validateAndConvert(target, (Map<String, Object>) providedObject));
} else {
throw new IllegalArgumentException(String.format("unexpected path '%s'", Joiner.on("/").join(preferencesParts)));
}
awaitFuture(userPreferences.updateOrAppend(preferences));
}
Aggregations