qpid-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From lqu...@apache.org
Subject svn commit: r1748729 [2/3] - in /qpid/java/trunk: broker-core/src/main/java/org/apache/qpid/server/model/ broker-core/src/main/java/org/apache/qpid/server/model/preferences/ broker-core/src/test/java/org/apache/qpid/server/model/testmodels/singleton/ b...
Date Thu, 16 Jun 2016 14:00:24 GMT
Added: qpid/java/trunk/broker-core/src/test/java/org/apache/qpid/server/model/testmodels/singleton/PreferencesTest.java
URL: http://svn.apache.org/viewvc/qpid/java/trunk/broker-core/src/test/java/org/apache/qpid/server/model/testmodels/singleton/PreferencesTest.java?rev=1748729&view=auto
==============================================================================
--- qpid/java/trunk/broker-core/src/test/java/org/apache/qpid/server/model/testmodels/singleton/PreferencesTest.java (added)
+++ qpid/java/trunk/broker-core/src/test/java/org/apache/qpid/server/model/testmodels/singleton/PreferencesTest.java Thu Jun 16 14:00:23 2016
@@ -0,0 +1,946 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+
+package org.apache.qpid.server.model.testmodels.singleton;
+
+import java.security.Principal;
+import java.security.PrivilegedAction;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+import java.util.UUID;
+
+import javax.security.auth.Subject;
+
+import org.apache.qpid.server.model.ConfiguredObject;
+import org.apache.qpid.server.model.Model;
+import org.apache.qpid.server.model.preferences.Preference;
+import org.apache.qpid.server.model.preferences.PreferenceValue;
+import org.apache.qpid.server.security.auth.AuthenticatedPrincipal;
+import org.apache.qpid.server.security.auth.TestPrincipalUtils;
+import org.apache.qpid.server.security.group.GroupPrincipal;
+import org.apache.qpid.test.utils.QpidTestCase;
+
+public class PreferencesTest extends QpidTestCase
+{
+    private final Model _model = TestModel.getInstance();
+    private ConfiguredObject<?> _testObject;
+    private Subject _testSubject;
+
+    @Override
+    public void setUp() throws Exception
+    {
+        super.setUp();
+        final String objectName = getTestName();
+        _testObject = _model.getObjectFactory()
+                            .create(TestSingleton.class,
+                                    Collections.<String, Object>singletonMap(ConfiguredObject.NAME, objectName));
+        _testSubject = TestPrincipalUtils.createTestSubject("testUser");
+    }
+
+    public void testCreatePreference()
+    {
+        final Map<String, Object> prefValueMap = Collections.<String, Object>singletonMap("myprefkey", "myprefvalue");
+        Preference p = _testObject.getUserPreferences()
+                                  .createPreference(null,
+                                                    "X-PREF1",
+                                                    "myprefname",
+                                                    "myprefdescription",
+                                                    Collections.<Principal>emptySet(), prefValueMap);
+        assertNotNull("Creation failed", p);
+        assertEquals("Unexpected preference name", "myprefname", p.getName());
+        assertEquals("Unexpected preference description", "myprefdescription", p.getDescription());
+        assertEquals("Unexpected preference visibility list", Collections.emptySet(), p.getVisibilityList());
+        final PreferenceValue preferenceValue = p.getValue();
+        assertNotNull("Preference value is null", preferenceValue);
+        assertEquals("Unexpected preference value", prefValueMap, preferenceValue.getAttributes());
+    }
+
+    public void testPreferenceNameIsMandatory()
+    {
+        final Map<String, Object> prefValueMap = Collections.emptyMap();
+        try
+        {
+            _testObject.getUserPreferences().createPreference(null,
+                                                              "X-PREF1",
+                                                              null,
+                                                              "myprefdescription",
+                                                              Collections.<Principal>emptySet(), prefValueMap);
+            fail("Preference name must not be null");
+        }
+        catch (IllegalArgumentException e)
+        {
+            // pass
+        }
+        try
+        {
+            _testObject.getUserPreferences().createPreference(null,
+                                                              "X-PREF1",
+                                                              "",
+                                                              "myprefdescription",
+                                                              Collections.<Principal>emptySet(), prefValueMap);
+            fail("Preference name must not be empty");
+        }
+        catch (IllegalArgumentException e)
+        {
+            // pass
+        }
+    }
+
+    public void testPreferenceHasUuid()
+    {
+        Preference p1 = _testObject.getUserPreferences().createPreference(null,
+                                                                          "X-TESTPREF",
+                                                                          "testProp1",
+                                                                          "",
+                                                                          null, Collections.<String, Object>emptyMap());
+        Preference p2 = _testObject.getUserPreferences().createPreference(null,
+                                                                          "X-TESTPREF",
+                                                                          "testProp2",
+                                                                          "",
+                                                                          Collections.<Principal>emptySet(),
+                                                                          Collections.<String, Object>emptyMap());
+        UUID id1 = p1.getId();
+        UUID id2 = p2.getId();
+        assertNotNull("preference id must not be null", id1);
+        assertNotNull("preference id must not be null", id2);
+        assertTrue("preference ids must be unique", !id1.equals(id2));
+    }
+
+    public void testPreferenceOwner()
+    {
+        Preference p = Subject.doAs(_testSubject, new PrivilegedAction<Preference>()
+        {
+            @Override
+            public Preference run()
+            {
+                return _testObject.getUserPreferences().createPreference(null,
+                                                                         "X-TESTPREF",
+                                                                         "testProp1",
+                                                                         null,
+                                                                         null, Collections.<String, Object>emptyMap());
+            }
+        });
+        final Principal testPrincipal = _testSubject.getPrincipals(AuthenticatedPrincipal.class).iterator().next();
+        assertEquals("Unexpected preference owner", testPrincipal, p.getOwner());
+    }
+
+    public void testAssociatedObject()
+    {
+        Preference p = _testObject.getUserPreferences().createPreference(null,
+                                                                         "X-TESTPREF",
+                                                                         "testProp1",
+                                                                         null,
+                                                                         null, Collections.<String, Object>emptyMap());
+        assertEquals("Unexpected associated object", _testObject, p.getAssociatedObject());
+    }
+
+    public void testType()
+    {
+        final String type = "X-TESTPREF";
+        Preference p = _testObject.getUserPreferences().createPreference(null,
+                                                                         type,
+                                                                         "testProp1",
+                                                                         null,
+                                                                         null, Collections.<String, Object>emptyMap());
+        assertEquals("Unexpected type", type, p.getType());
+    }
+
+    public void testSimpleRoundTrip()
+    {
+        Subject.doAs(_testSubject, new PrivilegedAction<Void>()
+        {
+            @Override
+            public Void run()
+            {
+                Preference p = _testObject.getUserPreferences().createPreference(null,
+                                                                                 "X-TestPropType",
+                                                                                 "testProp1",
+                                                                                 null,
+                                                                                 null,
+                                                                                 Collections.<String, Object>emptyMap());
+                Set<Preference> preferences = Collections.singleton(p);
+                _testObject.getUserPreferences().updateOrAppend(preferences);
+                assertEquals("roundtrip failed", preferences, _testObject.getUserPreferences().getPreferences());
+                return null;
+            }
+        });
+    }
+
+    public void testOnlyAllowUpdateOwnedPreferences()
+    {
+        final Preference p = Subject.doAs(_testSubject, new PrivilegedAction<Preference>()
+        {
+            @Override
+            public Preference run()
+            {
+                return _testObject.getUserPreferences().createPreference(null,
+                                                                         "X-testType",
+                                                                         "prop1",
+                                                                         null,
+                                                                         null,
+                                                                         Collections.<String, Object>emptyMap());
+            }
+        });
+
+        Subject testSubject2 = TestPrincipalUtils.createTestSubject("testUser2");
+        Subject.doAs(testSubject2, new PrivilegedAction<Void>()
+        {
+            @Override
+            public Void run()
+            {
+                Set<Preference> preferences = Collections.singleton(p);
+                try
+                {
+                    _testObject.getUserPreferences().updateOrAppend(preferences);
+                    fail("Saving of preferences owned by somebody else should not be allowed");
+                }
+                catch (SecurityException e)
+                {
+                    // pass
+                }
+                return null;
+            }
+        });
+    }
+
+    public void testGetOnlyOwnedPreferences()
+    {
+        final Set<Preference> p1s = Subject.doAs(_testSubject, new PrivilegedAction<Set<Preference>>()
+        {
+            @Override
+            public Set<Preference> run()
+            {
+                Preference p = _testObject.getUserPreferences().createPreference(null,
+                                                                                 "X-testType",
+                                                                                 "prop1",
+                                                                                 null,
+                                                                                 null,
+                                                                                 Collections.<String, Object>emptyMap());
+                Set<Preference> preferences = Collections.singleton(p);
+                _testObject.getUserPreferences().updateOrAppend(preferences);
+                return _testObject.getUserPreferences().getPreferences();
+            }
+        });
+
+        Subject testSubject2 = TestPrincipalUtils.createTestSubject("testUser2");
+        Subject.doAs(testSubject2, new PrivilegedAction<Void>()
+        {
+            @Override
+            public Void run()
+            {
+                Preference p = _testObject.getUserPreferences().createPreference(null,
+                                                                                 "X-testType",
+                                                                                 "prop2",
+                                                                                 null,
+                                                                                 null,
+                                                                                 Collections.<String, Object>emptyMap());
+                Set<Preference> preferences = Collections.singleton(p);
+                _testObject.getUserPreferences().updateOrAppend(preferences);
+                Set<Preference> p2s = _testObject.getUserPreferences().getPreferences();
+                assertEquals("Unexpected preferences for subject 2", preferences, p2s);
+                return null;
+            }
+        });
+
+        Subject.doAs(_testSubject, new PrivilegedAction<Void>()
+        {
+            @Override
+            public Void run()
+            {
+                Set<Preference> preferences = _testObject.getUserPreferences().getPreferences();
+                assertEquals("Unexpected preferences for subject 1", p1s, preferences);
+                return null;
+            }
+        });
+    }
+
+    public void testUpdate()
+    {
+        Subject.doAs(_testSubject, new PrivilegedAction<Void>()
+        {
+            @Override
+            public Void run()
+            {
+                Set<Preference> preferences;
+                Preference p1 = _testObject.getUserPreferences().createPreference(null,
+                                                                                  "X-testType",
+                                                                                  "propName",
+                                                                                  null,
+                                                                                  null,
+                                                                                  Collections.<String, Object>emptyMap());
+                preferences = Collections.singleton(p1);
+                _testObject.getUserPreferences().updateOrAppend(preferences);
+                Preference p2 = _testObject.getUserPreferences().createPreference(p1.getId(),
+                                                                                  "X-testType",
+                                                                                  "newPropName",
+                                                                                  "newDescription",
+                                                                                  null,
+                                                                                  Collections.<String, Object>emptyMap());
+                preferences = Collections.singleton(p2);
+                _testObject.getUserPreferences().updateOrAppend(preferences);
+
+                preferences = _testObject.getUserPreferences().getPreferences();
+                assertEquals("Unexpected number of preferences", 1, preferences.size());
+                Preference newPreference = preferences.iterator().next();
+                assertEquals("Unexpected preference id", p2.getId(), newPreference.getId());
+                assertEquals("Unexpected preference type", p2.getType(), newPreference.getType());
+                assertEquals("Unexpected preference name", p2.getName(), newPreference.getName());
+                assertEquals("Unexpected preference description", p2.getDescription(), newPreference.getDescription());
+                return null;
+            }
+        });
+    }
+
+    public void testProhibitTypeChange()
+    {
+        Subject.doAs(_testSubject, new PrivilegedAction<Void>()
+        {
+            @Override
+            public Void run()
+            {
+                Set<Preference> preferences;
+                Preference p1 = _testObject.getUserPreferences().createPreference(null,
+                                                                                  "X-testType",
+                                                                                  "propName",
+                                                                                  null,
+                                                                                  null,
+                                                                                  Collections.<String, Object>emptyMap());
+                preferences = Collections.singleton(p1);
+                _testObject.getUserPreferences().updateOrAppend(preferences);
+                Preference p2 = _testObject.getUserPreferences().createPreference(p1.getId(),
+                                                                                  "X-differentTestType",
+                                                                                  "propName",
+                                                                                  null,
+                                                                                  null,
+                                                                                  Collections.<String, Object>emptyMap());
+                preferences = Collections.singleton(p2);
+                try
+                {
+                    _testObject.getUserPreferences().updateOrAppend(preferences);
+                    fail("Type change should not be allowed");
+                }
+                catch (IllegalArgumentException e)
+                {
+                    // pass
+                }
+                return null;
+            }
+        });
+    }
+
+    public void testProhibitDuplicateNamesOfSameType()
+    {
+        Subject.doAs(_testSubject, new PrivilegedAction<Void>()
+        {
+            @Override
+            public Void run()
+            {
+                Set<Preference> preferences;
+                Preference p1 = _testObject.getUserPreferences().createPreference(null,
+                                                                                  "X-testType",
+                                                                                  "propName",
+                                                                                  null,
+                                                                                  null,
+                                                                                  Collections.<String, Object>emptyMap());
+                preferences = Collections.singleton(p1);
+                _testObject.getUserPreferences().updateOrAppend(preferences);
+                Preference p2 = _testObject.getUserPreferences().createPreference(null,
+                                                                                  "X-testType",
+                                                                                  "propName",
+                                                                                  null,
+                                                                                  null,
+                                                                                  Collections.<String, Object>emptyMap());
+                preferences = Collections.singleton(p2);
+                try
+                {
+                    _testObject.getUserPreferences().updateOrAppend(preferences);
+                    fail("Property with same name and same type should not be allowed");
+                }
+                catch (IllegalArgumentException e)
+                {
+                    // pass
+                }
+                return null;
+            }
+        });
+    }
+
+    public void testProhibitDuplicateNamesOfSameTypeInSameUpdate()
+    {
+        Subject.doAs(_testSubject, new PrivilegedAction<Void>()
+        {
+            @Override
+            public Void run()
+            {
+                Set<Preference> preferences = new HashSet<>();
+                Preference p1 = _testObject.getUserPreferences().createPreference(null,
+                                                                                  "X-testType",
+                                                                                  "propName",
+                                                                                  null,
+                                                                                  null,
+                                                                                  Collections.<String, Object>emptyMap());
+                preferences.add(p1);
+                Preference p2 = _testObject.getUserPreferences().createPreference(null,
+                                                                                  "X-testType",
+                                                                                  "propName",
+                                                                                  null,
+                                                                                  null,
+                                                                                  Collections.<String, Object>emptyMap());
+                preferences.add(p2);
+                try
+                {
+                    _testObject.getUserPreferences().updateOrAppend(preferences);
+                    fail("Property with same name and same type should not be allowed");
+                }
+                catch (IllegalArgumentException e)
+                {
+                    // pass
+                }
+                return null;
+            }
+        });
+    }
+
+    public void testReplace()
+    {
+        final String preferenceType = "X-testType";
+        Subject testSubject2 = TestPrincipalUtils.createTestSubject("testUser2");
+        final Preference unaffectedPreference = Subject.doAs(testSubject2, new PrivilegedAction<Preference>()
+        {
+            @Override
+            public Preference run()
+            {
+                Set<Preference> preferences;
+                Preference p1 = _testObject.getUserPreferences().createPreference(null,
+                                                                                  preferenceType,
+                                                                                  "propName",
+                                                                                  null,
+                                                                                  null,
+                                                                                  Collections.<String, Object>emptyMap());
+                preferences = Collections.singleton(p1);
+                _testObject.getUserPreferences().updateOrAppend(preferences);
+                return p1;
+            }
+        });
+
+        Subject.doAs(_testSubject, new PrivilegedAction<Void>()
+        {
+            @Override
+            public Void run()
+            {
+                Set<Preference> preferences;
+                Preference p1 = _testObject.getUserPreferences().createPreference(null,
+                                                                                  preferenceType,
+                                                                                  "propName",
+                                                                                  null,
+                                                                                  null,
+                                                                                  Collections.<String, Object>emptyMap());
+                preferences = Collections.singleton(p1);
+                _testObject.getUserPreferences().updateOrAppend(preferences);
+
+                Preference p2 = _testObject.getUserPreferences().createPreference(null,
+                                                                                  preferenceType,
+                                                                                  "newPropName",
+                                                                                  null,
+                                                                                  null,
+                                                                                  Collections.<String, Object>emptyMap());
+                preferences = Collections.singleton(p2);
+                _testObject.getUserPreferences().replace(preferences);
+
+                Collection<Preference> retrievedPreferences = _testObject.getUserPreferences().getPreferences();
+                assertEquals("Unexpected number of preferences", 1, retrievedPreferences.size());
+                assertEquals("Unexpected preference", p2, retrievedPreferences.iterator().next());
+
+                return null;
+            }
+        });
+
+        Subject.doAs(testSubject2, new PrivilegedAction<Void>()
+        {
+            @Override
+            public Void run()
+            {
+                Collection<Preference> retrievedPreferences = _testObject.getUserPreferences().getPreferences();
+                assertEquals("Unexpected number of preferences", 1, retrievedPreferences.size());
+                assertEquals("Unexpected preference", unaffectedPreference, retrievedPreferences.iterator().next());
+                return null;
+            }
+        });
+    }
+
+    public void testDelete()
+    {
+        final String preferenceType = "X-testType";
+        Subject testSubject2 = TestPrincipalUtils.createTestSubject("testUser2");
+        final Preference unaffectedPreference = Subject.doAs(testSubject2, new PrivilegedAction<Preference>()
+        {
+            @Override
+            public Preference run()
+            {
+                Set<Preference> preferences;
+                Preference p1 = _testObject.getUserPreferences().createPreference(null,
+                                                                                  preferenceType,
+                                                                                  "propName",
+                                                                                  null,
+                                                                                  null,
+                                                                                  Collections.<String, Object>emptyMap());
+                preferences = Collections.singleton(p1);
+                _testObject.getUserPreferences().updateOrAppend(preferences);
+                return p1;
+            }
+        });
+
+        Subject.doAs(_testSubject, new PrivilegedAction<Void>()
+        {
+            @Override
+            public Void run()
+            {
+                Set<Preference> preferences;
+                Preference p1 = _testObject.getUserPreferences().createPreference(null,
+                                                                                  preferenceType,
+                                                                                  "propName",
+                                                                                  null,
+                                                                                  null,
+                                                                                  Collections.<String, Object>emptyMap());
+                preferences = Collections.singleton(p1);
+                _testObject.getUserPreferences().updateOrAppend(preferences);
+
+                _testObject.getUserPreferences().replace(Collections.<Preference>emptySet());
+
+                Collection<Preference> retrievedPreferences = _testObject.getUserPreferences().getPreferences();
+                assertEquals("Unexpected number of preferences", 0, retrievedPreferences.size());
+
+                return null;
+            }
+        });
+
+        Subject.doAs(testSubject2, new PrivilegedAction<Void>()
+        {
+            @Override
+            public Void run()
+            {
+                Collection<Preference> retrievedPreferences = _testObject.getUserPreferences().getPreferences();
+                assertEquals("Unexpected number of preferences", 1, retrievedPreferences.size());
+                assertEquals("Unexpected preference", unaffectedPreference, retrievedPreferences.iterator().next());
+                return null;
+            }
+        });
+    }
+
+    public void testDeleteByType()
+    {
+        final String preferenceType = "X-testType";
+        final String unaffectedPreferenceType = "X-unaffectedType";
+        Subject testSubject2 = TestPrincipalUtils.createTestSubject("testUser2");
+        final Preference unaffectedPreference = Subject.doAs(testSubject2, new PrivilegedAction<Preference>()
+        {
+            @Override
+            public Preference run()
+            {
+                Set<Preference> preferences;
+                Preference p1 = _testObject.getUserPreferences().createPreference(null,
+                                                                                  preferenceType,
+                                                                                  "propName",
+                                                                                  null,
+                                                                                  null,
+                                                                                  Collections.<String, Object>emptyMap());
+                preferences = Collections.singleton(p1);
+                _testObject.getUserPreferences().updateOrAppend(preferences);
+                return p1;
+            }
+        });
+
+        Subject.doAs(_testSubject, new PrivilegedAction<Void>()
+        {
+            @Override
+            public Void run()
+            {
+                Set<Preference> preferences = new HashSet<>();
+                Preference p1 = _testObject.getUserPreferences().createPreference(null,
+                                                                                  preferenceType,
+                                                                                  "propName",
+                                                                                  null,
+                                                                                  null,
+                                                                                  Collections.<String, Object>emptyMap());
+                preferences.add(p1);
+                Preference p2 = _testObject.getUserPreferences().createPreference(null,
+                                                                                  unaffectedPreferenceType,
+                                                                                  "propName",
+                                                                                  null,
+                                                                                  null,
+                                                                                  Collections.<String, Object>emptyMap());
+                preferences.add(p2);
+                _testObject.getUserPreferences().updateOrAppend(preferences);
+
+                _testObject.getUserPreferences().replaceByType(preferenceType, Collections.<Preference>emptySet());
+
+                Collection<Preference> retrievedPreferences = _testObject.getUserPreferences().getPreferences();
+                assertEquals("Unexpected number of preferences", 1, retrievedPreferences.size());
+                assertTrue("Unexpected preference", retrievedPreferences.contains(p2));
+                return null;
+            }
+        });
+
+        Subject.doAs(testSubject2, new PrivilegedAction<Void>()
+        {
+            @Override
+            public Void run()
+            {
+                Collection<Preference> retrievedPreferences = _testObject.getUserPreferences().getPreferences();
+                assertEquals("Unexpected number of preferences", 1, retrievedPreferences.size());
+                assertEquals("Unexpected preference", unaffectedPreference, retrievedPreferences.iterator().next());
+                return null;
+            }
+        });
+    }
+
+    public void testDeleteByTypeAndName()
+    {
+        final String preferenceType = "X-testType";
+        Subject testSubject2 = TestPrincipalUtils.createTestSubject("testUser2");
+        final Preference unaffectedPreference = Subject.doAs(testSubject2, new PrivilegedAction<Preference>()
+        {
+            @Override
+            public Preference run()
+            {
+                Set<Preference> preferences;
+                Preference p1 = _testObject.getUserPreferences().createPreference(null,
+                                                                                  preferenceType,
+                                                                                  "propName",
+                                                                                  null,
+                                                                                  null,
+                                                                                  Collections.<String, Object>emptyMap());
+                preferences = Collections.singleton(p1);
+                _testObject.getUserPreferences().updateOrAppend(preferences);
+                return p1;
+            }
+        });
+
+        Subject.doAs(_testSubject, new PrivilegedAction<Void>()
+        {
+            @Override
+            public Void run()
+            {
+                Set<Preference> preferences = new HashSet<>();
+                Preference p1 = _testObject.getUserPreferences().createPreference(null,
+                                                                                  preferenceType,
+                                                                                  "propName",
+                                                                                  null,
+                                                                                  null,
+                                                                                  Collections.<String, Object>emptyMap());
+                preferences.add(p1);
+                Preference p2 = _testObject.getUserPreferences().createPreference(null,
+                                                                                  preferenceType,
+                                                                                  "unaffectedPropName",
+                                                                                  null,
+                                                                                  null,
+                                                                                  Collections.<String, Object>emptyMap());
+                preferences.add(p2);
+                _testObject.getUserPreferences().updateOrAppend(preferences);
+
+                _testObject.getUserPreferences().replaceByTypeAndName(preferenceType, "propName", null);
+
+                Collection<Preference> retrievedPreferences = _testObject.getUserPreferences().getPreferences();
+                assertEquals("Unexpected number of preferences", 1, retrievedPreferences.size());
+                assertTrue("Unexpected preference", retrievedPreferences.contains(p2));
+
+                return null;
+            }
+        });
+
+        Subject.doAs(testSubject2, new PrivilegedAction<Void>()
+        {
+            @Override
+            public Void run()
+            {
+                Collection<Preference> retrievedPreferences = _testObject.getUserPreferences().getPreferences();
+                assertEquals("Unexpected number of preferences", 1, retrievedPreferences.size());
+                assertEquals("Unexpected preference", unaffectedPreference, retrievedPreferences.iterator().next());
+                return null;
+            }
+        });
+    }
+
+    public void testReplaceByType()
+    {
+        final String replaceType = "X-replaceType";
+        final String unaffectedType = "X-unaffectedType";
+        Subject testSubject2 = TestPrincipalUtils.createTestSubject("testUser2");
+        final Preference unaffectedPreference = Subject.doAs(testSubject2, new PrivilegedAction<Preference>()
+        {
+            @Override
+            public Preference run()
+            {
+                Set<Preference> preferences;
+                Preference p1 = _testObject.getUserPreferences().createPreference(null,
+                                                                                  replaceType,
+                                                                                  "propName",
+                                                                                  null,
+                                                                                  null,
+                                                                                  Collections.<String, Object>emptyMap());
+                preferences = Collections.singleton(p1);
+                _testObject.getUserPreferences().updateOrAppend(preferences);
+                return p1;
+            }
+        });
+
+        Subject.doAs(_testSubject, new PrivilegedAction<Void>()
+        {
+            @Override
+            public Void run()
+            {
+                Set<Preference> preferences = new HashSet<>();
+                Preference p1 = _testObject.getUserPreferences().createPreference(null,
+                                                                                  replaceType,
+                                                                                  "propName",
+                                                                                  null,
+                                                                                  null,
+                                                                                  Collections.<String, Object>emptyMap());
+                preferences.add(p1);
+                Preference p2 = _testObject.getUserPreferences().createPreference(null,
+                                                                                  unaffectedType,
+                                                                                  "propName",
+                                                                                  null,
+                                                                                  null,
+                                                                                  Collections.<String, Object>emptyMap());
+                preferences.add(p2);
+                _testObject.getUserPreferences().updateOrAppend(preferences);
+
+                Preference p3 = _testObject.getUserPreferences().createPreference(null,
+                                                                                  replaceType,
+                                                                                  "newPropName",
+                                                                                  null,
+                                                                                  null,
+                                                                                  Collections.<String, Object>emptyMap());
+                preferences = Collections.singleton(p3);
+                _testObject.getUserPreferences().replaceByType(replaceType, preferences);
+
+                Set<Preference> retrievedPreferences = _testObject.getUserPreferences().getPreferences();
+                assertEquals("Unexpected number of preferences", 2, retrievedPreferences.size());
+                assertTrue("Preference of different type was replaced", retrievedPreferences.contains(p2));
+                assertTrue("Preference was not replaced", retrievedPreferences.contains(p3));
+                return null;
+            }
+        });
+
+        Subject.doAs(testSubject2, new PrivilegedAction<Void>()
+        {
+            @Override
+            public Void run()
+            {
+                Collection<Preference> retrievedPreferences = _testObject.getUserPreferences().getPreferences();
+                assertEquals("Unexpected number of preferences", 1, retrievedPreferences.size());
+                assertEquals("Preference of different user was replaced",
+                             unaffectedPreference,
+                             retrievedPreferences.iterator().next());
+                return null;
+            }
+        });
+    }
+
+    public void testReplaceByTypeAndName()
+    {
+        final String replaceType = "X-replaceType";
+        final String unaffectedType = "X-unaffectedType";
+        Subject testSubject2 = TestPrincipalUtils.createTestSubject("testUser2");
+        final Preference unaffectedPreference = Subject.doAs(testSubject2, new PrivilegedAction<Preference>()
+        {
+            @Override
+            public Preference run()
+            {
+                Set<Preference> preferences;
+                Preference p1 = _testObject.getUserPreferences().createPreference(null,
+                                                                                  replaceType,
+                                                                                  "propName",
+                                                                                  null,
+                                                                                  null,
+                                                                                  Collections.<String, Object>emptyMap());
+                preferences = Collections.singleton(p1);
+                _testObject.getUserPreferences().updateOrAppend(preferences);
+                return p1;
+            }
+        });
+
+        Subject.doAs(_testSubject, new PrivilegedAction<Void>()
+        {
+            @Override
+            public Void run()
+            {
+                Set<Preference> preferences = new HashSet<>();
+                Preference p1 = _testObject.getUserPreferences().createPreference(null,
+                                                                                  replaceType,
+                                                                                  "propName",
+                                                                                  null,
+                                                                                  null,
+                                                                                  Collections.<String, Object>emptyMap());
+                preferences.add(p1);
+                Preference p1b = _testObject.getUserPreferences().createPreference(null,
+                                                                                  replaceType,
+                                                                                  "unaffectedPropName",
+                                                                                  null,
+                                                                                  null,
+                                                                                  Collections.<String, Object>emptyMap());
+                preferences.add(p1b);
+                Preference p2 = _testObject.getUserPreferences().createPreference(null,
+                                                                                  unaffectedType,
+                                                                                  "propName",
+                                                                                  null,
+                                                                                  null,
+                                                                                  Collections.<String, Object>emptyMap());
+                preferences.add(p2);
+                _testObject.getUserPreferences().updateOrAppend(preferences);
+
+                Preference p3 = _testObject.getUserPreferences().createPreference(null,
+                                                                                  replaceType,
+                                                                                  "propName",
+                                                                                  "new description",
+                                                                                  null,
+                                                                                  Collections.<String, Object>emptyMap());
+                _testObject.getUserPreferences().replaceByTypeAndName(replaceType, "propName", p3);
+
+                Set<Preference> retrievedPreferences = _testObject.getUserPreferences().getPreferences();
+                assertEquals("Unexpected number of preferences", 3, retrievedPreferences.size());
+                assertTrue("Preference of different name was replaced", retrievedPreferences.contains(p1b));
+                assertTrue("Preference of different type was replaced", retrievedPreferences.contains(p2));
+                assertTrue("Preference was not replaced", retrievedPreferences.contains(p3));
+                return null;
+            }
+        });
+
+        Subject.doAs(testSubject2, new PrivilegedAction<Void>()
+        {
+            @Override
+            public Void run()
+            {
+                Collection<Preference> retrievedPreferences = _testObject.getUserPreferences().getPreferences();
+                assertEquals("Unexpected number of preferences", 1, retrievedPreferences.size());
+                assertEquals("Preference of different user was replaced",
+                             unaffectedPreference,
+                             retrievedPreferences.iterator().next());
+                return null;
+            }
+        });
+    }
+
+    public void testGetVisiblePreferences()
+    {
+        final Principal testPrincipal = _testSubject.getPrincipals().iterator().next();
+        Subject testSubject2 = TestPrincipalUtils.createTestSubject("testUser2");
+        final Preference p1 = Subject.doAs(testSubject2, new PrivilegedAction<Preference>()
+        {
+            @Override
+            public Preference run()
+            {
+                Set<Preference> preferences;
+                Preference p1 = _testObject.getUserPreferences().createPreference(null,
+                                                                                  "X-testType",
+                                                                                  "propName1",
+                                                                                  null,
+                                                                                  Collections.singleton(testPrincipal),
+                                                                                  Collections.<String, Object>emptyMap());
+                preferences = Collections.singleton(p1);
+                _testObject.getUserPreferences().updateOrAppend(preferences);
+                return p1;
+            }
+        });
+
+        Subject.doAs(_testSubject, new PrivilegedAction<Void>()
+        {
+            @Override
+            public Void run()
+            {
+                Set<Preference> preferences = new HashSet<>();
+                Preference p2 = _testObject.getUserPreferences().createPreference(null,
+                                                                                  "X-testType",
+                                                                                  "propName",
+                                                                                  null,
+                                                                                  null,
+                                                                                  Collections.<String, Object>emptyMap());
+                preferences.add(p2);
+                _testObject.getUserPreferences().updateOrAppend(preferences);
+
+                Set<Preference> retrievedPreferences = _testObject.getUserPreferences().getVisiblePreferences();
+                assertEquals("Unexpected number of preferences", 1, retrievedPreferences.size());
+                assertTrue("Preference of different type was replaced", retrievedPreferences.contains(p1));
+                return null;
+            }
+        });
+    }
+
+    public void testGetVisiblePreferencesSharedByGroup()
+    {
+        Subject testSubjectWithGroup = TestPrincipalUtils.createTestSubject("testUser", "testGroup");
+
+        Principal tempGroupPrincipal = null;
+        for (Principal principal : testSubjectWithGroup.getPrincipals())
+        {
+            if (principal instanceof GroupPrincipal)
+            {
+                tempGroupPrincipal = principal;
+                break;
+            }
+        }
+        final Principal groupPrincipal = tempGroupPrincipal;
+
+        Subject testSubject2 = TestPrincipalUtils.createTestSubject("testUser2");
+        final Preference p1 = Subject.doAs(testSubject2, new PrivilegedAction<Preference>()
+        {
+            @Override
+            public Preference run()
+            {
+                Set<Preference> preferences;
+                Preference p1 = _testObject.getUserPreferences().createPreference(null,
+                                                                                  "X-testType",
+                                                                                  "propName1",
+                                                                                  null,
+                                                                                  Collections.singleton(groupPrincipal),
+                                                                                  Collections.<String, Object>emptyMap());
+                preferences = Collections.singleton(p1);
+                _testObject.getUserPreferences().updateOrAppend(preferences);
+                return p1;
+            }
+        });
+
+        Subject.doAs(testSubjectWithGroup, new PrivilegedAction<Void>()
+        {
+            @Override
+            public Void run()
+            {
+                Set<Preference> preferences = new HashSet<>();
+                Preference p2 = _testObject.getUserPreferences().createPreference(null,
+                                                                                  "X-testType",
+                                                                                  "propName",
+                                                                                  null,
+                                                                                  null,
+                                                                                  Collections.<String, Object>emptyMap());
+                preferences.add(p2);
+                _testObject.getUserPreferences().updateOrAppend(preferences);
+
+                Set<Preference> retrievedPreferences = _testObject.getUserPreferences().getVisiblePreferences();
+                assertEquals("Unexpected number of preferences", 1, retrievedPreferences.size());
+                assertTrue("Preference of different type was replaced", retrievedPreferences.contains(p1));
+                return null;
+            }
+        });
+    }
+}

Added: qpid/java/trunk/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/servlet/rest/NotFoundException.java
URL: http://svn.apache.org/viewvc/qpid/java/trunk/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/servlet/rest/NotFoundException.java?rev=1748729&view=auto
==============================================================================
--- qpid/java/trunk/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/servlet/rest/NotFoundException.java (added)
+++ qpid/java/trunk/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/servlet/rest/NotFoundException.java Thu Jun 16 14:00:23 2016
@@ -0,0 +1,33 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.qpid.server.management.plugin.servlet.rest;
+
+public class NotFoundException extends IllegalArgumentException
+{
+    public NotFoundException(final String s)
+    {
+        super(s);
+    }
+
+    public NotFoundException(final String message, final Throwable cause)
+    {
+        super(message, cause);
+    }
+}

Modified: qpid/java/trunk/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/servlet/rest/RequestInfo.java
URL: http://svn.apache.org/viewvc/qpid/java/trunk/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/servlet/rest/RequestInfo.java?rev=1748729&r1=1748728&r2=1748729&view=diff
==============================================================================
--- qpid/java/trunk/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/servlet/rest/RequestInfo.java (original)
+++ qpid/java/trunk/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/servlet/rest/RequestInfo.java Thu Jun 16 14:00:23 2016
@@ -27,17 +27,29 @@ public class RequestInfo
     private final RequestType _type;
     private final List<String> _modelParts;
     private final String _operationName;
+    private final List<String> _preferencesParts;
 
-    public RequestInfo(final RequestType type, final List<String> modelParts)
+    public static RequestInfo createModelRequestInfo(final List<String> modelParts)
     {
-        this(type, modelParts, null);
+        return new RequestInfo(RequestType.MODEL_OBJECT, modelParts, null, Collections.<String>emptyList());
     }
 
-    public RequestInfo(final RequestType type, final List<String> modelParts, final String operationName)
+    public static RequestInfo createOperationRequestInfo(final List<String> modelParts, final String operationName)
+    {
+        return new RequestInfo(RequestType.OPERATION, modelParts, operationName, Collections.<String>emptyList());
+    }
+
+    public static RequestInfo createPreferencesRequestInfo(final List<String> modelParts, final List<String> preferencesParts)
+    {
+        return new RequestInfo(RequestType.USER_PREFERENCES, modelParts, null, preferencesParts);
+    }
+
+    private RequestInfo(final RequestType type, final List<String> modelParts, final String operationName, final List<String> preferencesParts)
     {
         _type = type;
         _operationName = operationName;
         _modelParts = Collections.unmodifiableList(modelParts);
+        _preferencesParts = Collections.unmodifiableList(preferencesParts);
     }
 
     public RequestType getType()
@@ -59,8 +71,13 @@ public class RequestInfo
         return _operationName;
     }
 
+    public List<String> getPreferencesParts()
+    {
+        return _preferencesParts;
+    }
+
     enum RequestType
     {
-        OPERATION, MODEL_OBJECT
+        OPERATION, USER_PREFERENCES, MODEL_OBJECT
     }
 }

Modified: qpid/java/trunk/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/servlet/rest/RequestInfoParser.java
URL: http://svn.apache.org/viewvc/qpid/java/trunk/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/servlet/rest/RequestInfoParser.java?rev=1748729&r1=1748728&r2=1748729&view=diff
==============================================================================
--- qpid/java/trunk/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/servlet/rest/RequestInfoParser.java (original)
+++ qpid/java/trunk/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/servlet/rest/RequestInfoParser.java Thu Jun 16 14:00:23 2016
@@ -29,6 +29,8 @@ import org.apache.qpid.server.model.Conf
 
 public class RequestInfoParser
 {
+    private static final String USER_PREFERENCES = "userpreferences";
+
     private final List<Class<? extends ConfiguredObject>> _hierarchy;
 
     public RequestInfoParser(final Class<? extends ConfiguredObject>... hierarchy)
@@ -62,7 +64,7 @@ public class RequestInfoParser
         else
         {
             throw new IllegalArgumentException(String.format("Unexpected method type '%s' for path '%s%s'",
-                                               method, servletPath, pathInfo));
+                                                             method, servletPath, pathInfo));
         }
     }
 
@@ -70,42 +72,60 @@ public class RequestInfoParser
     {
         if (parts.size() <= _hierarchy.size())
         {
-            return new RequestInfo(RequestInfo.RequestType.MODEL_OBJECT, parts);
+            return RequestInfo.createModelRequestInfo(parts);
         }
-        else
+        else if (parts.size() > _hierarchy.size())
         {
-            String expectedPath = buildExpectedPath(servletPath, _hierarchy);
-            throw new IllegalArgumentException(String.format("Invalid DELETE path '%s%s'. Expected: '%s'",
-                                                             servletPath, pathInfo,
-                                                             expectedPath));
-        }
+            if (USER_PREFERENCES.equals(parts.get(_hierarchy.size())))
+            {
+                return RequestInfo.createPreferencesRequestInfo(parts.subList(0, _hierarchy.size()),
+                                                                parts.subList(_hierarchy.size() + 1, parts.size()));
+            }
+        }
+        String expectedPath = buildExpectedPath(servletPath, _hierarchy);
+        throw new IllegalArgumentException(String.format(
+                "Invalid DELETE path '%s%s'. Expected: '%s' or '%s/userpreferences[/<preference type>[/<preference name>]]'",
+                servletPath,
+                pathInfo,
+                expectedPath,
+                expectedPath));
     }
 
     private RequestInfo parseGet(final String servletPath, final String pathInfo, final List<String> parts)
     {
         if (parts.size() <= _hierarchy.size())
         {
-            return new RequestInfo(RequestInfo.RequestType.MODEL_OBJECT, parts);
+            return RequestInfo.createModelRequestInfo(parts);
         }
-        else if (parts.size() == _hierarchy.size() + 1)
+        else if (parts.size() > _hierarchy.size())
         {
-            return new RequestInfo(RequestInfo.RequestType.OPERATION, parts.subList(0, _hierarchy.size()),
-                                   parts.get(parts.size() - 1));
-        }
-        else
-        {
-            String expectedPath = buildExpectedPath(servletPath, _hierarchy);
-            throw new IllegalArgumentException(String.format("Invalid GET path '%s%s'. Expected: '%s[/<operation name>]'",
-                                                             servletPath, pathInfo,
-                                                             expectedPath));
-        }
+            if (USER_PREFERENCES.equals(parts.get(_hierarchy.size())))
+            {
+                return RequestInfo.createPreferencesRequestInfo(parts.subList(0, _hierarchy.size()),
+                                                                parts.subList(_hierarchy.size() + 1, parts.size()));
+            }
+            else if (parts.size() == _hierarchy.size() + 1)
+            {
+                return RequestInfo.createOperationRequestInfo(parts.subList(0, _hierarchy.size()),
+                                                              parts.get(parts.size() - 1));
+            }
+        }
+        String expectedPath = buildExpectedPath(servletPath, _hierarchy);
+        throw new IllegalArgumentException(String.format("Invalid GET path '%s%s'. Expected: '%s[/<operation name>]'",
+                                                         servletPath, pathInfo,
+                                                         expectedPath));
     }
 
     private RequestInfo parsePut(final String servletPath, final String pathInfo, final List<String> parts)
     {
         if (parts.size() == _hierarchy.size() || parts.size() == _hierarchy.size() - 1)
         {
-            return new RequestInfo(RequestInfo.RequestType.MODEL_OBJECT, parts);
+            return RequestInfo.createModelRequestInfo(parts);
+        }
+        else if (parts.size() > _hierarchy.size() && USER_PREFERENCES.equals(parts.get(_hierarchy.size())))
+        {
+            return RequestInfo.createPreferencesRequestInfo(parts.subList(0, _hierarchy.size()),
+                                                            parts.subList(_hierarchy.size() + 1, parts.size()));
         }
         else
         {
@@ -120,23 +140,29 @@ public class RequestInfoParser
     {
         if (parts.size() == _hierarchy.size() || parts.size() == _hierarchy.size() - 1)
         {
-            return new RequestInfo(RequestInfo.RequestType.MODEL_OBJECT, parts);
-        }
-        else if (parts.size() == _hierarchy.size() + 1)
-        {
-            return new RequestInfo(RequestInfo.RequestType.OPERATION, parts.subList(0, _hierarchy.size()),
-                                   parts.get(parts.size() - 1));
+            return RequestInfo.createModelRequestInfo(parts);
         }
-        else
+        else if (parts.size() > _hierarchy.size())
         {
-            String expectedFullPath = buildExpectedPath(servletPath, _hierarchy);
-            String expectedParentPath = buildExpectedPath(servletPath, _hierarchy.subList(0, _hierarchy.size() - 1));
-
-            throw new IllegalArgumentException(String.format("Invalid POST path '%s%s'. Expected: '%s/<operation name>'"
-                                                             + " or '%s'",
-                                                             servletPath, pathInfo,
-                                                             expectedFullPath, expectedParentPath));
-        }
+            if (USER_PREFERENCES.equals(parts.get(_hierarchy.size())))
+            {
+                return RequestInfo.createPreferencesRequestInfo(parts.subList(0, _hierarchy.size()),
+                                                                parts.subList(_hierarchy.size() + 1, parts.size()));
+            }
+            else if (parts.size() == _hierarchy.size() + 1)
+            {
+                return RequestInfo.createOperationRequestInfo(parts.subList(0, _hierarchy.size()),
+                                                              parts.get(parts.size() - 1));
+            }
+        }
+        String expectedFullPath = buildExpectedPath(servletPath, _hierarchy);
+        String expectedParentPath = buildExpectedPath(servletPath, _hierarchy.subList(0, _hierarchy.size() - 1));
+
+        throw new IllegalArgumentException(String.format("Invalid POST path '%s%s'. Expected: '%s/<operation name>'"
+                                                         + " or '%s'"
+                                                         + " or '%s/userpreferences[/<preference type>]'",
+                                                         servletPath, pathInfo,
+                                                         expectedFullPath, expectedParentPath, expectedFullPath));
     }
 
     private String buildExpectedPath(final String servletPath, final List<Class<? extends ConfiguredObject>> hierarchy)

Modified: qpid/java/trunk/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/servlet/rest/RestServlet.java
URL: http://svn.apache.org/viewvc/qpid/java/trunk/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/servlet/rest/RestServlet.java?rev=1748729&r1=1748728&r2=1748729&view=diff
==============================================================================
--- qpid/java/trunk/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/servlet/rest/RestServlet.java (original)
+++ qpid/java/trunk/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/servlet/rest/RestServlet.java Thu Jun 16 14:00:23 2016
@@ -44,6 +44,7 @@ import javax.servlet.http.Part;
 
 import com.fasterxml.jackson.core.JsonParseException;
 import com.fasterxml.jackson.databind.ObjectMapper;
+import com.google.common.base.Joiner;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -56,6 +57,7 @@ import org.apache.qpid.server.model.Cont
 import org.apache.qpid.server.model.IllegalStateTransitionException;
 import org.apache.qpid.server.model.IntegrityViolationException;
 import org.apache.qpid.server.model.Model;
+import org.apache.qpid.server.model.preferences.UserPreferences;
 import org.apache.qpid.server.util.ServerScopedRuntimeException;
 import org.apache.qpid.server.util.urlstreamhandler.data.Handler;
 import org.apache.qpid.util.DataUrlUtils;
@@ -342,10 +344,11 @@ public class RestServlet extends Abstrac
     }
 
     @Override
-    protected void doGetWithSubjectAndActor(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
+    protected void doGetWithSubjectAndActor(HttpServletRequest request, HttpServletResponse response)
+            throws ServletException, IOException
     {
         RequestInfo requestInfo = _requestInfoParser.parse(request);
-        switch(requestInfo.getType())
+        switch (requestInfo.getType())
         {
             case OPERATION:
             {
@@ -365,7 +368,7 @@ public class RestServlet extends Abstrac
 
                 Collection<ConfiguredObject<?>> allObjects = getObjects(request, requestInfo);
 
-                if (allObjects == null || allObjects.isEmpty() && isSingleObjectRequest(requestInfo))
+                if (allObjects == null || (allObjects.isEmpty() && isSingleObjectRequest(requestInfo)))
                 {
                     sendJsonErrorResponse(request, response, HttpServletResponse.SC_NOT_FOUND, "Not Found");
                     return;
@@ -465,7 +468,6 @@ public class RestServlet extends Abstrac
                                                                            excludeInheritedContext)));
                 }
 
-
                 boolean sendCachingHeaders = attachmentFilename == null;
                 sendJsonResponse(extractInitialConfig && output.size() == 1 ? output.get(0) : output,
                                  request,
@@ -474,13 +476,22 @@ public class RestServlet extends Abstrac
                                  sendCachingHeaders);
                 break;
             }
+            case USER_PREFERENCES:
+            {
+                doGetUserPreferences(requestInfo, request, response);
+                break;
+            }
+
             default:
             {
-                throw new IllegalStateException(String.format("Unexpected request type '%s' for path '%s'", requestInfo.getType(), request.getPathInfo()));
+                throw new IllegalStateException(String.format("Unexpected request type '%s' for path '%s'",
+                                                              requestInfo.getType(),
+                                                              request.getPathInfo()));
             }
         }
     }
 
+
     private boolean isSingleObjectRequest(final RequestInfo requestInfo)
     {
         if (_hierarchy.length > 0)
@@ -515,13 +526,15 @@ public class RestServlet extends Abstrac
     }
 
     @Override
-    protected void doPutWithSubjectAndActor(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
+    protected void doPutWithSubjectAndActor(HttpServletRequest request, HttpServletResponse response)
+            throws ServletException, IOException
     {
         performCreateOrUpdate(request, response);
     }
 
     @Override
-    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
+    protected void service(HttpServletRequest request, HttpServletResponse response)
+            throws ServletException, IOException
     {
         try
         {
@@ -534,12 +547,13 @@ public class RestServlet extends Abstrac
         }
     }
 
-    private void performCreateOrUpdate(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
+    private void performCreateOrUpdate(HttpServletRequest request, HttpServletResponse response)
+            throws IOException, ServletException
     {
         response.setContentType("application/json");
 
         RequestInfo requestInfo = _requestInfoParser.parse(request);
-        switch(requestInfo.getType())
+        switch (requestInfo.getType())
         {
             case MODEL_OBJECT:
             {
@@ -553,7 +567,6 @@ public class RestServlet extends Abstrac
                     return;
                 }
 
-
                 ConfiguredObject theParent = getBroker();
                 ConfiguredObject[] otherParents = null;
                 Class<? extends ConfiguredObject> objClass = getConfiguredClass();
@@ -601,52 +614,85 @@ public class RestServlet extends Abstrac
                 doOperation(requestInfo, request, response);
                 break;
             }
+            case USER_PREFERENCES:
+            {
+                doPostOrPutUserPreference(requestInfo, request, response);
+                break;
+            }
             default:
             {
-                throw new IllegalStateException(String.format("Unexpected request type '%s' for path '%s'", requestInfo.getType(), request.getPathInfo()));
+                throw new IllegalStateException(String.format("Unexpected request type '%s' for path '%s'",
+                                                              requestInfo.getType(),
+                                                              request.getPathInfo()));
             }
         }
     }
 
-    private void doOperation(final RequestInfo requestInfo, final HttpServletRequest request,
-                             final HttpServletResponse response) throws IOException, ServletException
+    private void doGetUserPreferences(final RequestInfo requestInfo,
+                                      final HttpServletRequest request,
+                                      final HttpServletResponse response) throws IOException, ServletException
     {
-        ConfiguredObject<?> subject;
-        final List<String> names = requestInfo.getModelParts();
+        ConfiguredObject<?> target = getTarget(requestInfo, request, response);
+        final UserPreferences userPreferences = target.getUserPreferences();
 
-        if (names.isEmpty() && _hierarchy.length == 0)
+        Object responseObject = new RestUserPreferenceHandler().handleGET(userPreferences, requestInfo);
+        sendJsonResponse(responseObject, request, response);
+
+    }
+
+    private void doPostOrPutUserPreference(final RequestInfo requestInfo,
+                                           final HttpServletRequest request,
+                                           final HttpServletResponse response) throws IOException, ServletException
+    {
+        ConfiguredObject<?> target = getTarget(requestInfo, request, response);
+
+        final UserPreferences userPreferences = target.getUserPreferences();
+        final RestUserPreferenceHandler restUserPreferenceHandler = new RestUserPreferenceHandler();
+
+        if ("POST".equals(request.getMethod()))
         {
-            subject = getBroker();
+            Object providedObject = getRequestProvidedObject(request, Object.class);
+            restUserPreferenceHandler.handlePOST(userPreferences, requestInfo, providedObject);
         }
-        else
+        else if ("PUT".equals(request.getMethod()))
         {
-            ConfiguredObject theParent = getBroker();
-            ConfiguredObject[] otherParents = null;
-            Class<? extends ConfiguredObject> objClass = getConfiguredClass();
-            if (_hierarchy.length > 1)
-            {
-                List<ConfiguredObject> parents = findAllObjectParents(names);
-                theParent = parents.remove(0);
-                otherParents = parents.toArray(new ConfiguredObject[parents.size()]);
-            }
-            Map<String, Object> objectName =
-                    Collections.<String, Object>singletonMap("name", names.get(names.size() - 1));
-            subject = findObjectToUpdateInParent(objClass, objectName, theParent, otherParents);
-            if(subject == null)
+            Map<String, Object> providedObject = getRequestProvidedObject(request);
+            final RestUserPreferenceHandler.ActionTaken actionTaken =
+                    restUserPreferenceHandler.handlePUT(userPreferences, requestInfo, providedObject);
+
+            switch(actionTaken)
             {
-                sendJsonErrorResponse(request,
-                                      response,
-                                      HttpServletResponse.SC_NOT_FOUND,
-                                      getConfiguredClass().getSimpleName()
-                                      + " '"
-                                      + names.get(names.size() - 1)
-                                      + "' not found.");
-                return;
+                case CREATED:
+                    response.setHeader("Location", request.getRequestURL().toString());
+                    response.setStatus(HttpServletResponse.SC_CREATED);
+                    break;
+                case UPDATED:
+                    response.setStatus(HttpServletResponse.SC_OK);
+                    break;
+                default:
+                    throw new IllegalStateException("Unexpected action taken value : " + actionTaken);
             }
         }
+        else
+        {
+            sendJsonErrorResponse(request,
+                                  response,
+                                  HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
+                                  "unexpected http method");
+        }
+    }
+
+    private void doOperation(final RequestInfo requestInfo, final HttpServletRequest request,
+                             final HttpServletResponse response) throws IOException, ServletException
+    {
+        ConfiguredObject<?> target = getTarget(requestInfo, request, response);
+        if (target == null)
+        {
+            return;
+        }
         String operationName = requestInfo.getOperationName();
         final Map<String, ConfiguredObjectOperation<?>> availableOperations =
-                getBroker().getModel().getTypeRegistry().getOperations(subject.getClass());
+                getBroker().getModel().getTypeRegistry().getOperations(target.getClass());
         ConfiguredObjectOperation operation = availableOperations.get(operationName);
         Map<String, Object> operationArguments;
 
@@ -698,7 +744,7 @@ public class RestServlet extends Abstrac
                     return;
             }
         }
-        Object returnVal = operation.perform(subject, operationArguments);
+        Object returnVal = operation.perform(target, operationArguments);
         if(returnVal instanceof Content)
         {
             Content content = (Content)returnVal;
@@ -740,7 +786,43 @@ public class RestServlet extends Abstrac
             }
             sendJsonResponse(returnVal, request, response);
         }
+    }
 
+    private ConfiguredObject<?> getTarget(final RequestInfo requestInfo,
+                                          final HttpServletRequest request,
+                                          final HttpServletResponse response) throws IOException
+    {
+        final ConfiguredObject<?> target;
+        final List<String> names = requestInfo.getModelParts();
+
+        if (names.isEmpty() && _hierarchy.length == 0)
+        {
+            target = getBroker();
+        }
+        else
+        {
+            ConfiguredObject theParent = getBroker();
+            ConfiguredObject[] otherParents = null;
+            if (_hierarchy.length > 1)
+            {
+                List<ConfiguredObject> parents = findAllObjectParents(names);
+                theParent = parents.remove(0);
+                otherParents = parents.toArray(new ConfiguredObject[parents.size()]);
+            }
+            Class<? extends ConfiguredObject> objClass = getConfiguredClass();
+            Map<String, Object> objectName =
+                    Collections.<String, Object>singletonMap("name", names.get(names.size() - 1));
+            target = findObjectToUpdateInParent(objClass, objectName, theParent, otherParents);
+            if (target == null)
+            {
+
+                final String errorMessage = String.format("%s '%s' not found",
+                                                          getConfiguredClass().getSimpleName(),
+                                                          Joiner.on("/").join(names));
+                throw new NotFoundException(errorMessage);
+            }
+        }
+        return target;
     }
 
     private boolean returnsCollectionOfConfiguredObjects(ConfiguredObjectOperation operation)
@@ -868,23 +950,30 @@ public class RestServlet extends Abstrac
         return parents;
     }
 
-    private Map<String, Object> getRequestProvidedObject(HttpServletRequest request) throws IOException, ServletException
+    private Map<String, Object> getRequestProvidedObject(HttpServletRequest request)
+            throws IOException, ServletException
     {
-        Map<String, Object> providedObject;
+        return getRequestProvidedObject(request, LinkedHashMap.class);
+    }
+
+    private <T> T getRequestProvidedObject(HttpServletRequest request, Class<T> expectedClass)
+            throws IOException, ServletException
+    {
+        T providedObject;
 
         ArrayList<String> headers = Collections.list(request.getHeaderNames());
         ObjectMapper mapper = new ObjectMapper();
 
-        if(headers.contains("Content-Type") && request.getHeader("Content-Type").startsWith("multipart/form-data"))
+        if (headers.contains("Content-Type") && request.getHeader("Content-Type").startsWith("multipart/form-data"))
         {
-            providedObject = new HashMap<>();
-            Map<String,String> fileUploads = new HashMap<>();
+            providedObject = (T) new LinkedHashMap<>();
+            Map<String, String> fileUploads = new HashMap<>();
             Collection<Part> parts = request.getParts();
-            for(Part part : parts)
+            for (Part part : parts)
             {
-                if("data".equals(part.getName()) && "application/json".equals(part.getContentType()))
+                if ("data".equals(part.getName()) && "application/json".equals(part.getContentType()))
                 {
-                    providedObject = mapper.readValue(part.getInputStream(), LinkedHashMap.class);
+                    providedObject = (T) mapper.readValue(part.getInputStream(), LinkedHashMap.class);
                 }
                 else
                 {
@@ -894,13 +983,13 @@ public class RestServlet extends Abstrac
                     fileUploads.put(part.getName(), inlineURL);
                 }
             }
-            providedObject.putAll(fileUploads);
+            ((Map<String, Object>) providedObject).putAll(fileUploads);
         }
         else
         {
             try
             {
-                providedObject = mapper.readValue(request.getInputStream(), LinkedHashMap.class);
+                providedObject = mapper.readValue(request.getInputStream(), expectedClass);
             }
             catch (JsonParseException e)
             {
@@ -956,7 +1045,8 @@ public class RestServlet extends Abstrac
         return true;
     }
 
-    private void setResponseStatus(HttpServletRequest request, HttpServletResponse response, Throwable e) throws IOException
+    private void setResponseStatus(HttpServletRequest request, HttpServletResponse response, Throwable e)
+            throws IOException
     {
         if (e instanceof AccessControlException)
         {
@@ -968,18 +1058,26 @@ public class RestServlet extends Abstrac
             int responseCode = HttpServletResponse.SC_BAD_REQUEST;
             String message = e.getMessage();
             if (e instanceof AbstractConfiguredObject.DuplicateIdException
-                    || e instanceof AbstractConfiguredObject.DuplicateNameException
-                    || e instanceof IntegrityViolationException
-                    || e instanceof IllegalStateTransitionException)
+                || e instanceof AbstractConfiguredObject.DuplicateNameException
+                || e instanceof IntegrityViolationException
+                || e instanceof IllegalStateTransitionException)
             {
                 responseCode = HttpServletResponse.SC_CONFLICT;
             }
+            else if (e instanceof NotFoundException)
+            {
+                if (LOGGER.isTraceEnabled())
+                {
+                    LOGGER.trace(e.getClass().getSimpleName() + " processing request", e);
+                }
+                responseCode = HttpServletResponse.SC_NOT_FOUND;
+            }
             else if (e instanceof IllegalConfigurationException || e instanceof IllegalArgumentException)
             {
                 LOGGER.warn(e.getClass().getSimpleName() + " processing request : " + message);
                 Throwable t = e;
                 int maxDepth = 10;
-                while((t = t.getCause())!=null && maxDepth-- != 0)
+                while ((t = t.getCause()) != null && maxDepth-- != 0)
                 {
                     LOGGER.warn("... caused by " + t.getClass().getSimpleName() + "  : " + t.getMessage());
                 }
@@ -999,11 +1097,11 @@ public class RestServlet extends Abstrac
                 // This should not happen
                 if (e instanceof RuntimeException)
                 {
-                    throw (RuntimeException)e;
+                    throw (RuntimeException) e;
                 }
                 else if (e instanceof Error)
                 {
-                    throw (Error)e;
+                    throw (Error) e;
                 }
                 else
                 {
@@ -1012,7 +1110,6 @@ public class RestServlet extends Abstrac
             }
 
             sendJsonErrorResponse(request, response, responseCode, message);
-
         }
     }
 
@@ -1020,21 +1117,38 @@ public class RestServlet extends Abstrac
     protected void doDeleteWithSubjectAndActor(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
     {
         RequestInfo requestInfo = _requestInfoParser.parse(request);
-
         Collection<ConfiguredObject<?>> allObjects = getObjects(request, requestInfo);
-        if(allObjects != null)
+        if (allObjects == null)
         {
-            for (ConfiguredObject o : allObjects)
+            throw new NotFoundException("Not Found");
+        }
+
+        switch (requestInfo.getType())
+        {
+            case MODEL_OBJECT:
+            {
+                for (ConfiguredObject o : allObjects)
+                {
+                    o.delete();
+                }
+
+                sendCachingHeadersOnResponse(response);
+                response.setStatus(HttpServletResponse.SC_OK);
+                break;
+            }
+            case USER_PREFERENCES:
             {
-                o.delete();
+                for (ConfiguredObject o : allObjects)
+                {
+                    new RestUserPreferenceHandler().handleDELETE(o.getUserPreferences(), requestInfo);
+                }
+                break;
             }
 
-            sendCachingHeadersOnResponse(response);
-            response.setStatus(HttpServletResponse.SC_OK);
-        }
-        else
-        {
-            sendJsonErrorResponse(request, response, HttpServletResponse.SC_NOT_FOUND, "Not Found");
+            default:
+            {
+                sendJsonErrorResponse(request, response, HttpServletResponse.SC_BAD_REQUEST, "Unsupported delete call");
+            }
         }
     }
 



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@qpid.apache.org
For additional commands, e-mail: commits-help@qpid.apache.org


Mime
View raw message