use of javax.jcr.observation.ObservationManager in project sling by apache.
the class PostServletPrivilegesUpdateTest method testUpdatePropertyPrivilegesAndEvents.
/**
* Test for SLING-897 fix:
* 1. Updating a property requires jcr:modifyProperties privilege on node.
* 2. When changing an existing property observers should receive a PROPERTY_CHANGED event instead
* of a PROPERTY_REMOVED event and a PROPERTY_ADDED event
*/
@Test
// TODO fails on jackrabbit 2.6.5 and on Oak
@Ignore
public void testUpdatePropertyPrivilegesAndEvents() throws IOException, JsonException, RepositoryException, InterruptedException {
//1. Create user as admin (OK)
// curl -F:name=myuser -Fpwd=password -FpwdConfirm=password http://admin:admin@localhost:8080/system/userManager/user.create.html
testUserId = H.createTestUser();
//2. Create node as admin (OK)
// curl -F:nameHint=node -FpropOne=propOneValue1 -FpropOne=propOneValue2 -FpropTwo=propTwoValue http://admin:admin@localhost:8080/test/
final String createTestNodeUrl = postUrl + SlingPostConstants.DEFAULT_CREATE_SUFFIX;
NameValuePairList clientNodeProperties = new NameValuePairList();
clientNodeProperties.add(SlingPostConstants.RP_NODE_NAME_HINT, testName.getMethodName());
clientNodeProperties.add("propOne", "propOneValue1");
clientNodeProperties.add("propOne", "propOneValue2");
clientNodeProperties.add("propTwo", "propTwoValue");
String testNodeUrl = H.getTestClient().createNode(createTestNodeUrl, clientNodeProperties, null, false);
String content = H.getContent(testNodeUrl + ".json", HttpTest.CONTENT_TYPE_JSON);
JsonObject json = JsonUtil.parseObject(content);
Object propOneObj = json.get("propOne");
assertTrue(propOneObj instanceof JsonArray);
assertEquals(2, ((JsonArray) propOneObj).size());
assertEquals("propOneValue1", ((JsonArray) propOneObj).getString(0));
assertEquals("propOneValue2", ((JsonArray) propOneObj).getString(1));
Object propTwoObj = json.get("propTwo");
assertTrue(propTwoObj instanceof JsonString);
assertEquals("propTwoValue", ((JsonString) propTwoObj).getString());
//3. Attempt to update property of node as testUser (500: javax.jcr.AccessDeniedException: /test/node/propOne: not allowed to add or modify item)
// curl -FpropOne=propOneValueChanged -FpropTwo=propTwoValueChanged1 -FpropTwo=propTwoValueChanged2 http://myuser:password@localhost:8080/test/node
List<NameValuePair> postParams = new ArrayList<NameValuePair>();
postParams.add(new NameValuePair("propOne", "propOneValueChanged"));
postParams.add(new NameValuePair("propTwo", "propTwoValueChanged1"));
postParams.add(new NameValuePair("propTwo", "propTwoValueChanged2"));
Credentials testUserCreds = new UsernamePasswordCredentials(testUserId, "testPwd");
String expectedMessage = "Expected javax.jcr.AccessDeniedException";
H.assertAuthenticatedPostStatus(testUserCreds, testNodeUrl, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, postParams, expectedMessage);
//4. Grant jcr:modifyProperties rights to testUser as admin (OK)
// curl -FprincipalId=myuser -Fprivilege@jcr:modifyProperties=granted http://admin:admin@localhost:8080/test/node.modifyAce.html
Map<String, String> nodeAceProperties = new HashMap<String, String>();
nodeAceProperties.put("principalId", testUserId);
nodeAceProperties.put("privilege@jcr:modifyProperties", "granted");
H.getTestClient().createNode(testNodeUrl + ".modifyAce.html", nodeAceProperties);
//use a davex session to verify the correct JCR events are delivered
Repository repository = JcrUtils.getRepository(HttpTest.HTTP_BASE_URL + "/server/");
Session jcrSession = null;
TestEventListener listener = new TestEventListener();
ObservationManager observationManager = null;
try {
jcrSession = repository.login(new SimpleCredentials("admin", "admin".toCharArray()));
observationManager = jcrSession.getWorkspace().getObservationManager();
String testNodePath = testNodeUrl.substring(HttpTest.HTTP_BASE_URL.length());
observationManager.addEventListener(listener, //event types
Event.PROPERTY_ADDED | Event.PROPERTY_CHANGED | Event.PROPERTY_REMOVED, //absPath
testNodePath, //isDeep
true, //uuid
null, //nodeTypeName
null, //noLocal
false);
//5. Attempt to update properties of node (OK)
// curl -FpropOne=propOneValueChanged -FpropTwo=propTwoValueChanged1 -FpropTwo=propTwoValueChanged2 http://myuser:password@localhost:8080/test/node
H.assertAuthenticatedPostStatus(testUserCreds, testNodeUrl, HttpServletResponse.SC_OK, postParams, expectedMessage);
//verify the change happened
String afterUpdateContent = H.getContent(testNodeUrl + ".json", HttpTest.CONTENT_TYPE_JSON);
JsonObject afterUpdateJson = JsonUtil.parseObject(afterUpdateContent);
Object afterUpdatePropOneObj = afterUpdateJson.get("propOne");
assertTrue(afterUpdatePropOneObj instanceof JsonArray);
assertEquals(1, ((JsonArray) afterUpdatePropOneObj).size());
assertEquals("propOneValueChanged", ((JsonArray) afterUpdatePropOneObj).getString(0));
Object afterUpdatePropTwoObj = afterUpdateJson.get("propTwo");
assertTrue(afterUpdatePropTwoObj instanceof JsonArray);
assertEquals(2, ((JsonArray) afterUpdatePropTwoObj).size());
assertEquals("propTwoValueChanged1", ((JsonArray) afterUpdatePropTwoObj).getString(0));
assertEquals("propTwoValueChanged2", ((JsonArray) afterUpdatePropTwoObj).getString(1));
//wait for the expected JCR events to be delivered
for (int second = 0; second < 15; second++) {
if (listener.getEventBundlesProcessed() > 0) {
break;
}
Thread.sleep(1000);
}
assertEquals("One property added event was expected: " + listener.toString(), 1, listener.addedProperties.size());
assertEquals(testNodePath + "/propTwo", listener.addedProperties.get(0));
assertEquals("One property removed event was expected: " + listener.toString(), 1, listener.removedProperties.size());
assertEquals(testNodePath + "/propTwo", listener.removedProperties.get(0));
assertEquals("One property changed event was expected: " + listener.toString(), 1, listener.changedProperties.size());
assertEquals(testNodePath + "/propOne", listener.changedProperties.get(0));
} finally {
//cleanup
if (observationManager != null) {
observationManager.removeEventListener(listener);
}
jcrSession.logout();
repository = null;
}
}
use of javax.jcr.observation.ObservationManager in project jackrabbit by apache.
the class SessionImpl method removeRegisteredEventListeners.
/**
* Utility method that removes all registered event listeners.
*/
private void removeRegisteredEventListeners() {
try {
ObservationManager manager = getWorkspace().getObservationManager();
// Use a copy to avoid modifying the set of registered listeners
// while iterating over it
Collection<EventListener> listeners = IteratorUtils.toList(manager.getRegisteredEventListeners());
for (EventListener listener : listeners) {
try {
manager.removeEventListener(listener);
} catch (RepositoryException e) {
log.warn("Error removing event listener: " + listener, e);
}
}
} catch (RepositoryException e) {
log.warn("Error removing event listeners", e);
}
}
use of javax.jcr.observation.ObservationManager in project jackrabbit by apache.
the class VersionEventsTest method testCheckinOtherWorkspace.
/**
* Test if checkin creates events also on a different workspace than the one
* where the checkin was executed.
*/
public void testCheckinOtherWorkspace() throws RepositoryException {
// create versionable node
Node n1 = testRootNode.addNode(nodeName1);
n1.addMixin(mixVersionable);
testRootNode.save();
Session s = getHelper().getReadOnlySession(workspaceName);
try {
EventResult listener = new EventResult(log);
ObservationManager obsMgr = s.getWorkspace().getObservationManager();
obsMgr.addEventListener(listener, Event.NODE_ADDED, "/", true, null, null, false);
Version v = n1.checkin();
obsMgr.removeEventListener(listener);
Event[] events = listener.getEvents(1000);
Set paths = new HashSet();
for (int i = 0; i < events.length; i++) {
paths.add(events[i].getPath());
}
assertTrue("missing 'node removed': " + v.getPath(), paths.contains(v.getPath()));
String frozenPath = v.getPath() + "/" + jcrFrozenNode;
assertTrue("missing 'node removed': " + frozenPath, paths.contains(frozenPath));
} finally {
s.logout();
}
}
use of javax.jcr.observation.ObservationManager in project jackrabbit by apache.
the class ServerWorkspace method getObservationManager.
/** {@inheritDoc} */
public RemoteObservationManager getObservationManager() throws RepositoryException, RemoteException {
try {
if (remoteObservationManager == null) {
ObservationManager observationManager = workspace.getObservationManager();
remoteObservationManager = getFactory().getRemoteObservationManager(observationManager);
}
return remoteObservationManager;
} catch (RepositoryException ex) {
throw getRepositoryException(ex);
}
}
use of javax.jcr.observation.ObservationManager in project jackrabbit by apache.
the class RepositoryImpl method getSystemSearchManager.
/**
* Returns the system search manager or <code>null</code> if none is
* configured.
*/
protected SearchManager getSystemSearchManager(String wspName) throws RepositoryException {
if (systemSearchMgr == null) {
if (repConfig.isSearchEnabled()) {
systemSearchMgr = new SearchManager(null, context, repConfig, getWorkspaceInfo(wspName).itemStateMgr, context.getInternalVersionManager().getPersistenceManager(), SYSTEM_ROOT_NODE_ID, null, null);
SystemSession defSysSession = getSystemSession(wspName);
ObservationManager obsMgr = defSysSession.getWorkspace().getObservationManager();
obsMgr.addEventListener(systemSearchMgr, Event.NODE_ADDED | Event.NODE_REMOVED | Event.PROPERTY_ADDED | Event.PROPERTY_CHANGED | Event.PROPERTY_REMOVED, "/" + defSysSession.getJCRName(NameConstants.JCR_SYSTEM), true, null, null, false);
}
}
return systemSearchMgr;
}
Aggregations