Search in sources :

Example 1 with UserPreferences

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);
}
Also used : UserPreferences(org.apache.qpid.server.model.preferences.UserPreferences) Collection(java.util.Collection) ConfiguredObject(org.apache.qpid.server.model.ConfiguredObject) AbstractConfiguredObject(org.apache.qpid.server.model.AbstractConfiguredObject) ConfiguredObject(org.apache.qpid.server.model.ConfiguredObject) AbstractConfiguredObject(org.apache.qpid.server.model.AbstractConfiguredObject) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 2 with UserPreferences

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);
    }
}
Also used : UserPreferences(org.apache.qpid.server.model.preferences.UserPreferences) UserPreferencesCreator(org.apache.qpid.server.store.preferences.UserPreferencesCreator)

Example 3 with 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)));
    }
}
Also used : UserPreferences(org.apache.qpid.server.model.preferences.UserPreferences) Preference(org.apache.qpid.server.model.preferences.Preference) ConfiguredObject(org.apache.qpid.server.model.ConfiguredObject) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map)

Example 4 with UserPreferences

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

Example 5 with UserPreferences

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));
}
Also used : UserPreferences(org.apache.qpid.server.model.preferences.UserPreferences) LinkedHashSet(java.util.LinkedHashSet) Preference(org.apache.qpid.server.model.preferences.Preference) ArrayList(java.util.ArrayList) List(java.util.List) ConfiguredObject(org.apache.qpid.server.model.ConfiguredObject) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

UserPreferences (org.apache.qpid.server.model.preferences.UserPreferences)5 ArrayList (java.util.ArrayList)4 Map (java.util.Map)4 ConfiguredObject (org.apache.qpid.server.model.ConfiguredObject)4 HashMap (java.util.HashMap)3 List (java.util.List)3 Collection (java.util.Collection)2 Preference (org.apache.qpid.server.model.preferences.Preference)2 LinkedHashMap (java.util.LinkedHashMap)1 LinkedHashSet (java.util.LinkedHashSet)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 ConcurrentMap (java.util.concurrent.ConcurrentMap)1 NotFoundException (org.apache.qpid.server.management.plugin.servlet.rest.NotFoundException)1 RequestInfo (org.apache.qpid.server.management.plugin.servlet.rest.RequestInfo)1 AbstractConfiguredObject (org.apache.qpid.server.model.AbstractConfiguredObject)1 UserPreferencesCreator (org.apache.qpid.server.store.preferences.UserPreferencesCreator)1