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