jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mreut...@apache.org
Subject svn commit: r483369 - in /jackrabbit/trunk: jackrabbit-jcr-server/src/main/java/org/apache/jackrabbit/webdav/jcr/search/ jackrabbit-webdav/src/main/java/org/apache/jackrabbit/webdav/search/ jackrabbit-webdav/src/main/java/org/apache/jackrabbit/webdav/xml/
Date Thu, 07 Dec 2006 08:17:12 GMT
Author: mreutegg
Date: Thu Dec  7 00:17:09 2006
New Revision: 483369

URL: http://svn.apache.org/viewvc?view=rev&rev=483369
Log:
Use namespace prefixes supplied with request to execute the query.

Modified:
    jackrabbit/trunk/jackrabbit-jcr-server/src/main/java/org/apache/jackrabbit/webdav/jcr/search/SearchResourceImpl.java
    jackrabbit/trunk/jackrabbit-webdav/src/main/java/org/apache/jackrabbit/webdav/search/SearchInfo.java
    jackrabbit/trunk/jackrabbit-webdav/src/main/java/org/apache/jackrabbit/webdav/xml/DomUtil.java

Modified: jackrabbit/trunk/jackrabbit-jcr-server/src/main/java/org/apache/jackrabbit/webdav/jcr/search/SearchResourceImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-jcr-server/src/main/java/org/apache/jackrabbit/webdav/jcr/search/SearchResourceImpl.java?view=diff&rev=483369&r1=483368&r2=483369
==============================================================================
--- jackrabbit/trunk/jackrabbit-jcr-server/src/main/java/org/apache/jackrabbit/webdav/jcr/search/SearchResourceImpl.java
(original)
+++ jackrabbit/trunk/jackrabbit-jcr-server/src/main/java/org/apache/jackrabbit/webdav/jcr/search/SearchResourceImpl.java
Thu Dec  7 00:17:09 2006
@@ -36,12 +36,15 @@
 import javax.jcr.RepositoryException;
 import javax.jcr.Session;
 import javax.jcr.Value;
+import javax.jcr.NamespaceRegistry;
 import javax.jcr.query.InvalidQueryException;
 import javax.jcr.query.Query;
 import javax.jcr.query.QueryManager;
 import javax.jcr.query.QueryResult;
 import javax.jcr.query.Row;
 import javax.jcr.query.RowIterator;
+import java.util.Map;
+import java.util.Iterator;
 
 /**
  * <code>SearchResourceImpl</code>...
@@ -115,7 +118,9 @@
     private Query getQuery(SearchInfo sInfo)
             throws InvalidQueryException, RepositoryException, DavException {
 
-        Node rootNode = getRepositorySession().getRootNode();
+        Session session = getRepositorySession();
+        NamespaceRegistry nsReg = session.getWorkspace().getNamespaceRegistry();
+        Node rootNode = session.getRootNode();
         QueryManager qMgr = getRepositorySession().getWorkspace().getQueryManager();
 
         // test if query is defined by requested repository node
@@ -132,7 +137,26 @@
 
         Query q;
         if (sInfo != null) {
-            q = qMgr.createQuery(sInfo.getQuery(), sInfo.getLanguageName());
+            // apply namespace mappings to session
+            Map namespaces = sInfo.getNamespaces();
+            try {
+                for (Iterator it = namespaces.keySet().iterator(); it.hasNext(); ) {
+                    String prefix = (String) it.next();
+                    String uri = (String) namespaces.get(prefix);
+                    session.setNamespacePrefix(prefix, uri);
+                }
+                q = qMgr.createQuery(sInfo.getQuery(), sInfo.getLanguageName());
+            } finally {
+                // reset namespace mappings
+                for (Iterator it = namespaces.values().iterator(); it.hasNext(); ) {
+                    String uri = (String) it.next();
+                    try {
+                        session.setNamespacePrefix(nsReg.getPrefix(uri), uri);
+                    } catch (RepositoryException e) {
+                        log.warn("Unable to reset mapping of namespace: " + uri);
+                    }
+                }
+            }
         } else {
             throw new DavException(DavServletResponse.SC_BAD_REQUEST, locator.getResourcePath()
+ " is not a nt:query node -> searchRequest body required.");
         }

Modified: jackrabbit/trunk/jackrabbit-webdav/src/main/java/org/apache/jackrabbit/webdav/search/SearchInfo.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-webdav/src/main/java/org/apache/jackrabbit/webdav/search/SearchInfo.java?view=diff&rev=483369&r1=483368&r2=483369
==============================================================================
--- jackrabbit/trunk/jackrabbit-webdav/src/main/java/org/apache/jackrabbit/webdav/search/SearchInfo.java
(original)
+++ jackrabbit/trunk/jackrabbit-webdav/src/main/java/org/apache/jackrabbit/webdav/search/SearchInfo.java
Thu Dec  7 00:17:09 2006
@@ -18,6 +18,7 @@
 
 import org.apache.jackrabbit.webdav.DavException;
 import org.apache.jackrabbit.webdav.DavServletResponse;
+import org.apache.jackrabbit.webdav.DavConstants;
 import org.apache.jackrabbit.webdav.xml.DomUtil;
 import org.apache.jackrabbit.webdav.xml.Namespace;
 import org.apache.jackrabbit.webdav.xml.XmlSerializable;
@@ -25,6 +26,14 @@
 import org.slf4j.LoggerFactory;
 import org.w3c.dom.Document;
 import org.w3c.dom.Element;
+import org.w3c.dom.Attr;
+
+import java.util.Map;
+import java.util.HashMap;
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.Set;
+import java.util.HashSet;
 
 /**
  * <code>SearchInfo</code> parses the 'searchrequest' element of a SEARCH
@@ -53,9 +62,23 @@
 
     private static Logger log = LoggerFactory.getLogger(SearchInfo.class);
 
+    /**
+     * Set of namespace uri String which are ignored in the search request.
+     */
+    private static final Set IGNORED_NAMESPACES;
+
+    static {
+        Set s = new HashSet();
+        s.add(Namespace.XMLNS_NAMESPACE.getURI());
+        s.add(Namespace.XML_NAMESPACE.getURI());
+        s.add(DavConstants.NAMESPACE.getURI());
+        IGNORED_NAMESPACES = Collections.unmodifiableSet(s);
+    }
+
     private final String language;
     private final Namespace languageNamespace;
     private final String query;
+    private final Map namespaces;
 
     /**
      * Create a new <code>SearchInfo</code> instance.
@@ -63,11 +86,24 @@
      * @param language
      * @param languageNamespace
      * @param query
+     * @param namespaces the re-mapped namespaces. Key=prefix, value=uri.
      */
-    public SearchInfo(String language, Namespace languageNamespace, String query) {
+    public SearchInfo(String language, Namespace languageNamespace, String query, Map namespaces)
{
         this.language = language;
         this.languageNamespace = languageNamespace;
         this.query = query;
+        this.namespaces = Collections.unmodifiableMap(new HashMap(namespaces));
+    }
+
+    /**
+     * Create a new <code>SearchInfo</code> instance.
+     *
+     * @param language
+     * @param languageNamespace
+     * @param query
+     */
+    public SearchInfo(String language, Namespace languageNamespace, String query) {
+        this(language,  languageNamespace, query, Collections.EMPTY_MAP);
     }
 
     /**
@@ -98,6 +134,15 @@
     }
 
     /**
+     * Returns the namespaces that have been re-mapped by the user.
+     *
+     * @return map of namespace to prefix mappings. Key=prefix, value=uri.
+     */
+    public Map getNamespaces() {
+        return namespaces;
+    }
+
+    /**
      * Return the xml representation of this <code>SearchInfo</code> instance.
      *
      * @return xml representation
@@ -105,6 +150,11 @@
      */
     public Element toXml(Document document) {
         Element sRequestElem = DomUtil.createElement(document, XML_SEARCHREQUEST, NAMESPACE);
+        for (Iterator it = namespaces.keySet().iterator(); it.hasNext(); ) {
+            String prefix = (String) it.next();
+            String uri = (String) namespaces.get(prefix);
+            DomUtil.setNamespaceAttribute(sRequestElem, prefix, uri);
+        }
         DomUtil.addChildElement(sRequestElem, language, languageNamespace, query);
         return sRequestElem;
     }
@@ -124,8 +174,16 @@
             throw new DavException(DavServletResponse.SC_BAD_REQUEST);
         }
         Element first = DomUtil.getFirstChildElement(searchRequest);
+        Attr[] nsAttributes = DomUtil.getNamespaceAttributes(searchRequest);
+        Map namespaces = new HashMap();
+        for (int i = 0; i < nsAttributes.length; i++) {
+            // filter out xmlns namespace and DAV namespace
+            if (!IGNORED_NAMESPACES.contains(nsAttributes[i].getValue())) {
+                namespaces.put(nsAttributes[i].getLocalName(), nsAttributes[i].getValue());
+            }
+        }
         if (first != null) {
-            return new SearchInfo(first.getLocalName(), DomUtil.getNamespace(first), DomUtil.getText(first));
+            return new SearchInfo(first.getLocalName(), DomUtil.getNamespace(first), DomUtil.getText(first),
namespaces);
         } else {
             log.warn("A single child element is expected with the 'DAV:searchrequest'.");
             throw new DavException(DavServletResponse.SC_BAD_REQUEST);

Modified: jackrabbit/trunk/jackrabbit-webdav/src/main/java/org/apache/jackrabbit/webdav/xml/DomUtil.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-webdav/src/main/java/org/apache/jackrabbit/webdav/xml/DomUtil.java?view=diff&rev=483369&r1=483368&r2=483369
==============================================================================
--- jackrabbit/trunk/jackrabbit-webdav/src/main/java/org/apache/jackrabbit/webdav/xml/DomUtil.java
(original)
+++ jackrabbit/trunk/jackrabbit-webdav/src/main/java/org/apache/jackrabbit/webdav/xml/DomUtil.java
Thu Dec  7 00:17:09 2006
@@ -26,6 +26,7 @@
 import org.w3c.dom.Node;
 import org.w3c.dom.NodeList;
 import org.w3c.dom.Text;
+import org.w3c.dom.NamedNodeMap;
 
 import javax.xml.parsers.DocumentBuilderFactory;
 import java.util.ArrayList;
@@ -77,6 +78,24 @@
     }
 
     /**
+     * Returns the namespace attributes of the given element.
+     *
+     * @param element
+     * @return the namespace attributes.
+     */
+    public static Attr[] getNamespaceAttributes(Element element) {
+        NamedNodeMap attributes = element.getAttributes();
+        List nsAttr = new ArrayList();
+        for (int i = 0; i < attributes.getLength(); i++) {
+            Attr attr = (Attr) attributes.item(i);
+            if (Namespace.XMLNS_NAMESPACE.getURI().equals(attr.getNamespaceURI())) {
+                nsAttr.add(attr);
+            }
+        }
+        return (Attr[]) nsAttr.toArray(new Attr[nsAttr.size()]);
+    }
+
+    /**
      * Concatenates the values of all child nodes of type 'Text' or 'CDATA'/
      *
      * @param element
@@ -493,6 +512,17 @@
             attr.setValue(attrValue);
             element.setAttributeNodeNS(attr);
         }
+    }
+
+    /**
+     * Adds a namespace attribute on the given element.
+     *
+     * @param element
+     * @param prefix
+     * @param uri
+     */
+    public static void setNamespaceAttribute(Element element, String prefix, String uri)
{
+        setAttribute(element, prefix, Namespace.XMLNS_NAMESPACE, uri);
     }
 
     /**



Mime
View raw message