Search in sources :

Example 36 with Preference

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

the class RestUserPreferenceHandlerTest method testGetVisiblePreferencesByTypeAndName.

@Test
public void testGetVisiblePreferencesByTypeAndName() throws Exception {
    final String prefName = "testpref";
    final String prefType = "X-testtype";
    final RequestInfo rootRequestInfo = RequestInfo.createVisiblePreferencesRequestInfo(Collections.<String>emptyList(), Arrays.asList(prefType, prefName), Collections.<String, List<String>>emptyMap());
    Subject.doAs(_subject, new PrivilegedAction<Void>() {

        @Override
        public Void run() {
            final Set<Preference> preferences = new HashSet<>();
            Map<String, Object> pref1Attributes = createPreferenceAttributes(null, null, prefType, prefName, null, MYUSER_SERIALIZATION, Collections.singleton(MYGROUP_SERIALIZATION), Collections.<String, Object>emptyMap());
            Preference p1 = PreferenceFactory.fromAttributes(_configuredObject, pref1Attributes);
            preferences.add(p1);
            Map<String, Object> pref2Attributes = createPreferenceAttributes(null, null, prefType, "testPref2", null, MYUSER_SERIALIZATION, Collections.<String>emptySet(), Collections.<String, Object>emptyMap());
            Preference p2 = PreferenceFactory.fromAttributes(_configuredObject, pref2Attributes);
            preferences.add(p2);
            awaitPreferenceFuture(_userPreferences.updateOrAppend(preferences));
            return null;
        }
    });
    Subject testSubject2 = TestPrincipalUtils.createTestSubject("testUser2", MYGROUP);
    Subject.doAs(testSubject2, new PrivilegedAction<Void>() {

        @Override
        public Void run() {
            Map<String, Object> preference = (Map<String, Object>) _handler.handleGET(_userPreferences, rootRequestInfo);
            assertEquals("Unexpected name of preferences", prefName, preference.get(Preference.NAME_ATTRIBUTE));
            Set<Principal> visibilityList = (Set<Principal>) preference.get(Preference.VISIBILITY_LIST_ATTRIBUTE);
            assertEquals("Unexpected number of principals in visibility list", (long) 1, (long) visibilityList.size());
            assertTrue("Unexpected principal in visibility list", GenericPrincipal.principalsEqual(_groupPrincipal, visibilityList.iterator().next()));
            assertTrue("Unexpected owner", GenericPrincipal.principalsEqual(_userPrincipal, (Principal) preference.get(Preference.OWNER_ATTRIBUTE)));
            return null;
        }
    });
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) Preference(org.apache.qpid.server.model.preferences.Preference) ConfiguredObject(org.apache.qpid.server.model.ConfiguredObject) HashMap(java.util.HashMap) Map(java.util.Map) Subject(javax.security.auth.Subject) AuthenticatedPrincipal(org.apache.qpid.server.security.auth.AuthenticatedPrincipal) GroupPrincipal(org.apache.qpid.server.security.group.GroupPrincipal) GenericPrincipal(org.apache.qpid.server.model.preferences.GenericPrincipal) Principal(java.security.Principal) Test(org.junit.Test)

Example 37 with Preference

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

the class RestUserPreferenceHandlerTest method setUp.

@Before
public void setUp() throws Exception {
    _configuredObject = mock(ConfiguredObject.class);
    _preferenceStore = mock(PreferenceStore.class);
    _preferenceTaskExecutor = new CurrentThreadTaskExecutor();
    _preferenceTaskExecutor.start();
    _userPreferences = new UserPreferencesImpl(_preferenceTaskExecutor, _configuredObject, _preferenceStore, Collections.<Preference>emptyList());
    _subject = TestPrincipalUtils.createTestSubject(MYUSER, MYGROUP);
    _groupPrincipal = _subject.getPrincipals(GroupPrincipal.class).iterator().next();
    _userPrincipal = _subject.getPrincipals(AuthenticatedPrincipal.class).iterator().next();
    when(_configuredObject.getUserPreferences()).thenReturn(_userPreferences);
}
Also used : UserPreferencesImpl(org.apache.qpid.server.model.preferences.UserPreferencesImpl) Preference(org.apache.qpid.server.model.preferences.Preference) GroupPrincipal(org.apache.qpid.server.security.group.GroupPrincipal) CurrentThreadTaskExecutor(org.apache.qpid.server.configuration.updater.CurrentThreadTaskExecutor) ConfiguredObject(org.apache.qpid.server.model.ConfiguredObject) PreferenceStore(org.apache.qpid.server.store.preferences.PreferenceStore) AuthenticatedPrincipal(org.apache.qpid.server.security.auth.AuthenticatedPrincipal) Before(org.junit.Before)

Example 38 with Preference

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

the class RestUserPreferenceHandler method validateAndConvert.

private List<Preference> validateAndConvert(final ConfiguredObject<?> target, final String type, final List<Object> providedObjects) {
    List<Preference> replacementPreferences = new ArrayList<>();
    for (Object preferenceObject : providedObjects) {
        if (!(preferenceObject instanceof Map)) {
            throw new IllegalArgumentException("expected a list of objects");
        }
        Map<String, Object> preferenceAttributes = (Map<String, Object>) preferenceObject;
        ensureAttributeMatches(preferenceAttributes, "type", type);
        Preference preference = PreferenceFactory.fromAttributes(target, preferenceAttributes);
        replacementPreferences.add(preference);
    }
    return replacementPreferences;
}
Also used : Preference(org.apache.qpid.server.model.preferences.Preference) ArrayList(java.util.ArrayList) ConfiguredObject(org.apache.qpid.server.model.ConfiguredObject) HashMap(java.util.HashMap) Map(java.util.Map)

Example 39 with Preference

use of org.apache.qpid.server.model.preferences.Preference 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)

Example 40 with Preference

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

the class RestUserPreferenceHandler method handleGET.

public Object handleGET(UserPreferences userPreferences, RequestInfo requestInfo) {
    if (userPreferences == null) {
        throw new NotFoundException("User preferences are not available");
    }
    final List<String> preferencesParts = requestInfo.getPreferencesParts();
    final Map<String, List<String>> queryParameters = requestInfo.getQueryParameters();
    UUID id = getIdFromQueryParameters(queryParameters);
    final ListenableFuture<Set<Preference>> allPreferencesFuture;
    if (requestInfo.getType() == RequestType.USER_PREFERENCES) {
        allPreferencesFuture = userPreferences.getPreferences();
    } else if (requestInfo.getType() == RequestType.VISIBLE_PREFERENCES) {
        allPreferencesFuture = userPreferences.getVisiblePreferences();
    } else {
        throw new IllegalStateException(String.format("RestUserPreferenceHandler called with a unsupported request type: %s", requestInfo.getType()));
    }
    final Set<Preference> allPreferences;
    allPreferences = awaitFuture(allPreferencesFuture);
    if (preferencesParts.size() == 2) {
        String type = preferencesParts.get(0);
        String name = preferencesParts.get(1);
        Preference foundPreference = null;
        for (Preference preference : allPreferences) {
            if (preference.getType().equals(type) && preference.getName().equals(name)) {
                if (id == null || id.equals(preference.getId())) {
                    foundPreference = preference;
                }
                break;
            }
        }
        if (foundPreference != null) {
            return foundPreference.getAttributes();
        } else {
            String errorMessage;
            if (id == null) {
                errorMessage = String.format("Preference with name '%s' of type '%s' cannot be found", name, type);
            } else {
                errorMessage = String.format("Preference with name '%s' of type '%s' and id '%s' cannot be found", name, type, id);
            }
            throw new NotFoundException(errorMessage);
        }
    } else if (preferencesParts.size() == 1) {
        String type = preferencesParts.get(0);
        List<Map<String, Object>> preferences = new ArrayList<>();
        for (Preference preference : allPreferences) {
            if (preference.getType().equals(type)) {
                if (id == null || id.equals(preference.getId())) {
                    preferences.add(preference.getAttributes());
                }
            }
        }
        return preferences;
    } else if (preferencesParts.size() == 0) {
        final Map<String, List<Map<String, Object>>> preferences = new HashMap<>();
        for (Preference preference : allPreferences) {
            if (id == null || id.equals(preference.getId())) {
                final String type = preference.getType();
                if (!preferences.containsKey(type)) {
                    preferences.put(type, new ArrayList<Map<String, Object>>());
                }
                preferences.get(type).add(preference.getAttributes());
            }
        }
        return preferences;
    } else {
        throw new IllegalArgumentException(String.format("unexpected path '%s'", Joiner.on("/").join(preferencesParts)));
    }
}
Also used : Set(java.util.Set) LinkedHashSet(java.util.LinkedHashSet) HashMap(java.util.HashMap) Preference(org.apache.qpid.server.model.preferences.Preference) ArrayList(java.util.ArrayList) List(java.util.List) ConfiguredObject(org.apache.qpid.server.model.ConfiguredObject) UUID(java.util.UUID) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

Preference (org.apache.qpid.server.model.preferences.Preference)40 ConfiguredObject (org.apache.qpid.server.model.ConfiguredObject)39 Test (org.junit.Test)31 Subject (javax.security.auth.Subject)16 HashMap (java.util.HashMap)11 Map (java.util.Map)9 Set (java.util.Set)9 HashSet (java.util.HashSet)8 ArrayList (java.util.ArrayList)6 List (java.util.List)6 AuthenticatedPrincipal (org.apache.qpid.server.security.auth.AuthenticatedPrincipal)5 Date (java.util.Date)4 GroupPrincipal (org.apache.qpid.server.security.group.GroupPrincipal)4 Principal (java.security.Principal)3 UUID (java.util.UUID)3 GenericPrincipal (org.apache.qpid.server.model.preferences.GenericPrincipal)3 UserPreferencesImpl (org.apache.qpid.server.model.preferences.UserPreferencesImpl)3 LinkedHashSet (java.util.LinkedHashSet)2 CurrentThreadTaskExecutor (org.apache.qpid.server.configuration.updater.CurrentThreadTaskExecutor)2 UserPreferences (org.apache.qpid.server.model.preferences.UserPreferences)2