You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jackrabbit.apache.org by re...@apache.org on 2017/09/04 15:22:04 UTC

svn commit: r1807244 - in /jackrabbit/trunk/jackrabbit-jcr-server: ./ src/main/java/org/apache/jackrabbit/webdav/jcr/observation/ src/test/java/org/apache/jackrabbit/webdav/jcr/observation/

Author: reschke
Date: Mon Sep  4 15:22:04 2017
New Revision: 1807244

URL: http://svn.apache.org/viewvc?rev=1807244&view=rev
Log:
JCR-4173: Unable to receive observation events when connecting via DavEx

make sure we do not produce broken XML, add a minimal test case

Added:
    jackrabbit/trunk/jackrabbit-jcr-server/src/test/java/org/apache/jackrabbit/webdav/jcr/observation/
    jackrabbit/trunk/jackrabbit-jcr-server/src/test/java/org/apache/jackrabbit/webdav/jcr/observation/InfoMapTest.java   (with props)
Modified:
    jackrabbit/trunk/jackrabbit-jcr-server/pom.xml
    jackrabbit/trunk/jackrabbit-jcr-server/src/main/java/org/apache/jackrabbit/webdav/jcr/observation/SubscriptionImpl.java

Modified: jackrabbit/trunk/jackrabbit-jcr-server/pom.xml
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-jcr-server/pom.xml?rev=1807244&r1=1807243&r2=1807244&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-jcr-server/pom.xml (original)
+++ jackrabbit/trunk/jackrabbit-jcr-server/pom.xml Mon Sep  4 15:22:04 2017
@@ -261,6 +261,11 @@
       <artifactId>logback-classic</artifactId>
       <scope>test</scope>
     </dependency>
+    <dependency>
+      <groupId>org.mockito</groupId>
+      <artifactId>mockito-core</artifactId>
+      <scope>test</scope>
+    </dependency>
   </dependencies>
 
 </project>

Modified: jackrabbit/trunk/jackrabbit-jcr-server/src/main/java/org/apache/jackrabbit/webdav/jcr/observation/SubscriptionImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-jcr-server/src/main/java/org/apache/jackrabbit/webdav/jcr/observation/SubscriptionImpl.java?rev=1807244&r1=1807243&r2=1807244&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-jcr-server/src/main/java/org/apache/jackrabbit/webdav/jcr/observation/SubscriptionImpl.java (original)
+++ jackrabbit/trunk/jackrabbit-jcr-server/src/main/java/org/apache/jackrabbit/webdav/jcr/observation/SubscriptionImpl.java Mon Sep  4 15:22:04 2017
@@ -535,18 +535,8 @@ public class SubscriptionImpl implements
                     log.error("Internal error while retrieving event identifier. {}", e.getMessage());
                 }
                 // info
-                Element info = DomUtil.addChildElement(eventElem, XML_EVENTINFO, NAMESPACE);
                 try {
-                    Map<?, ?> m = event.getInfo();
-                    for (Map.Entry<?, ?> entry : m.entrySet()) {
-                        String key = entry.getKey().toString();
-                        Object value = entry.getValue();
-                        if (value != null) {
-                            DomUtil.addChildElement(info, key, Namespace.EMPTY_NAMESPACE, value.toString());
-                        } else {
-                            DomUtil.addChildElement(info, key, Namespace.EMPTY_NAMESPACE);
-                        }
-                    }
+                    serializeInfoMap(eventElem, session, event.getInfo());
                 } catch (RepositoryException e) {
                     log.error("Internal error while retrieving event info. {}", e.getMessage());
                 }
@@ -555,6 +545,33 @@ public class SubscriptionImpl implements
         }
     }
 
+    protected static void serializeInfoMap(Element eventElem, Session session, Map<?, ?> map) {
+        // info
+        Element info = DomUtil.addChildElement(eventElem, XML_EVENTINFO, NAMESPACE);
+        Map<?, ?> m = map;
+        for (Map.Entry<?, ?> entry : m.entrySet()) {
+            try {
+                String key = entry.getKey().toString();
+                Namespace ns = Namespace.EMPTY_NAMESPACE;
+                int colon = key.indexOf(':');
+                if (colon >= 0) {
+                    String prefix = key.substring(0, colon);
+                    String localname = key.substring(colon + 1);
+                    ns = Namespace.getNamespace(prefix, session.getNamespaceURI(prefix));
+                    key = localname;
+                }
+                Object value = entry.getValue();
+                if (value != null) {
+                    DomUtil.addChildElement(info, key, ns, value.toString());
+                } else {
+                    DomUtil.addChildElement(info, key, ns);
+                }
+            } catch (RepositoryException nse) {
+                log.error("Internal error while getting namespaceUri, info map field skipped for {}", entry.getKey());
+            }
+        }
+    }    
+    
     //----------------------------< TransactionEvent >------------------------
 
     /**

Added: jackrabbit/trunk/jackrabbit-jcr-server/src/test/java/org/apache/jackrabbit/webdav/jcr/observation/InfoMapTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-jcr-server/src/test/java/org/apache/jackrabbit/webdav/jcr/observation/InfoMapTest.java?rev=1807244&view=auto
==============================================================================
--- jackrabbit/trunk/jackrabbit-jcr-server/src/test/java/org/apache/jackrabbit/webdav/jcr/observation/InfoMapTest.java (added)
+++ jackrabbit/trunk/jackrabbit-jcr-server/src/test/java/org/apache/jackrabbit/webdav/jcr/observation/InfoMapTest.java Mon Sep  4 15:22:04 2017
@@ -0,0 +1,71 @@
+/*
+ * 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.jackrabbit.webdav.jcr.observation;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.jcr.RepositoryException;
+import javax.jcr.Session;
+import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.transform.TransformerException;
+
+import org.apache.jackrabbit.webdav.observation.ObservationConstants;
+import org.apache.jackrabbit.webdav.xml.DomUtil;
+import org.apache.jackrabbit.webdav.xml.Namespace;
+import org.mockito.Mockito;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.xml.sax.SAXException;
+
+import junit.framework.TestCase;
+
+public class InfoMapTest extends TestCase {
+
+    public void testInfoMap()
+            throws ParserConfigurationException, TransformerException, SAXException, IOException, RepositoryException {
+
+        Session s = Mockito.mock(Session.class);
+        Mockito.when(s.getNamespaceURI("jcr")).thenReturn("http://www.jcp.org/jcr/1.0");
+
+        Map<String, String> map = new HashMap<String, String>();
+        // mandated by JCR 2.0
+        map.put("srcChildRelPath", "/x");
+        // OAK extension, see https://issues.apache.org/jira/browse/OAK-1669
+        map.put("jcr:primaryType", "nt:unstructured");
+        Document doc = DomUtil.createDocument();
+        Element container = DomUtil.createElement(doc, "x", null);
+        doc.appendChild(container);
+        SubscriptionImpl.serializeInfoMap(container, s, map);
+        ByteArrayOutputStream xml = new ByteArrayOutputStream();
+        DomUtil.transformDocument(doc, xml);
+
+        // reparse
+        Document tripped = DomUtil.parseDocument(new ByteArrayInputStream(xml.toByteArray()));
+        Element top = tripped.getDocumentElement();
+        assertEquals("x", top.getLocalName());
+        Element emap = DomUtil.getChildElement(top, ObservationConstants.N_EVENTINFO);
+        assertNotNull(emap);
+        Element path = DomUtil.getChildElement(emap, "srcChildRelPath", null);
+        assertNotNull(path);
+        Element type = DomUtil.getChildElement(emap, "primaryType", Namespace.getNamespace("http://www.jcp.org/jcr/1.0"));
+        assertNotNull(type);
+    }
+}

Propchange: jackrabbit/trunk/jackrabbit-jcr-server/src/test/java/org/apache/jackrabbit/webdav/jcr/observation/InfoMapTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jackrabbit/trunk/jackrabbit-jcr-server/src/test/java/org/apache/jackrabbit/webdav/jcr/observation/InfoMapTest.java
------------------------------------------------------------------------------
    svn:executable = *