[Yanel-commits] rev 59474 - public/yanel/trunk/src/core/java/org/wyona/yanel/core

michi at wyona.com michi at wyona.com
Tue Jul 19 16:52:54 CEST 2011


Author: michi
Date: 2011-07-19 16:52:54 +0200 (Tue, 19 Jul 2011)
New Revision: 59474

Added:
   public/yanel/trunk/src/core/java/org/wyona/yanel/core/ResourceConfigurationMapV2.java
   public/yanel/trunk/src/core/java/org/wyona/yanel/core/ResourceTypeMatcherV1ImplV2.java
Log:
new versions of request matcher started

Added: public/yanel/trunk/src/core/java/org/wyona/yanel/core/ResourceConfigurationMapV2.java
===================================================================
--- public/yanel/trunk/src/core/java/org/wyona/yanel/core/ResourceConfigurationMapV2.java	                        (rev 0)
+++ public/yanel/trunk/src/core/java/org/wyona/yanel/core/ResourceConfigurationMapV2.java	2011-07-19 14:52:54 UTC (rev 59474)
@@ -0,0 +1,104 @@
+/*
+ * Copyright 2006 Wyona
+ *
+ *  Licensed 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.wyona.org/licenses/APACHE-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.wyona.yanel.core;
+
+import java.io.InputStream;
+
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.XMLStreamReader;
+import javax.xml.stream.XMLInputFactory;
+import javax.xml.stream.XMLStreamConstants;
+import org.apache.log4j.Logger;
+import org.wyona.yanel.core.map.Realm;
+import org.wyona.yanel.core.util.WildcardMatcherHelper;
+
+/**
+ * Implements a Chain of Responsibility (http://en.wikipedia.org/wiki/Chain-of-responsibility_pattern) to get the path for the .yanel-rc file by looking up for a match of path in map.rc-map
+ * 
+ * the map file needs to be located at YOUR-RTI-REPO/map.rc-map (TODO: make this configurable via realm)
+ * example of a map.rc-map which maps every request which ends with .html to a html.yanel-rc
+ * (whereas in this example STAR means * which is not possible to use with a slash in javadoc)
+ * <pre>
+ * <?xml version="1.0"?>
+ * <rc-map>
+ *   <matcher pattern"/STARSTAR.html" rcpath="/html.yanel-rc"/>
+ * </rc-map>
+ * </pre>
+ */
+public class ResourceConfigurationMapV2 {
+
+    private static Logger log = Logger.getLogger(ResourceConfigurationMapV2.class);
+
+    /**
+     * Get path of resource configuration which matched the path pattern within the resource configuration map
+     * @param realm Realm
+     * @param path Path of request
+     * @return String which contains the path of a rc file
+     */
+    public static String getRCPath(Realm realm, String path, String queryString) {
+        log.warn("DEBUG: Try to get resource configuration for request: " + path);
+
+        InputStream rcMapIS = getRCMap(realm);
+        if (rcMapIS == null) {
+            log.warn("No resource configuration map seems to exists for realm: " + realm.getID());
+            return null;
+        }
+
+        XMLInputFactory factory = XMLInputFactory.newInstance();
+        try {
+            XMLStreamReader parser = factory.createXMLStreamReader(rcMapIS);
+            while (true) {
+                int event = parser.next();
+                if (event == XMLStreamConstants.END_DOCUMENT) {
+                    parser.close();
+                    break;
+                }
+                if (event == XMLStreamConstants.START_ELEMENT) {
+                    if (parser.getLocalName().equals("matcher")) {
+                        String pattern = parser.getAttributeValue("", "pattern");
+                        if (WildcardMatcherHelper.match(pattern, path) != null) {
+                            if (log.isDebugEnabled()) {
+                                log.debug("CoR pattern: '" + pattern + "' matched with path: '" + path + "'. will use following path in the RTIRepository to reach the rc: " + parser.getAttributeValue("", "rcpath"));
+                            }
+                            log.warn("DEBUG: CoR pattern: '" + pattern + "' matched with path: '" + path + "'. will use following path in the RTIRepository to reach the rc: " + parser.getAttributeValue("", "rcpath"));
+                            return parser.getAttributeValue("", "rcpath");
+                        }
+                    }
+                }
+            }
+        } catch (XMLStreamException e) {
+            log.error("error while reading the rc map", e);
+            return null;
+        }
+        return null;
+    }
+
+    /**
+     * Get resource configuration map
+     * @param realm Realm containing resource configuration map
+     * @return InputStream of RTIRepository/map.rc-map
+     */
+    private static InputStream getRCMap(Realm realm) {
+        try {
+            //TODO: make this configurable via realm
+            return realm.getRTIRepository().getNode("/map.rc-map").getInputStream();
+        } catch (Exception e) {
+            log.error("Could not get InputStream of rc-map",e);
+            return null;
+        }
+    }
+}

Added: public/yanel/trunk/src/core/java/org/wyona/yanel/core/ResourceTypeMatcherV1ImplV2.java
===================================================================
--- public/yanel/trunk/src/core/java/org/wyona/yanel/core/ResourceTypeMatcherV1ImplV2.java	                        (rev 0)
+++ public/yanel/trunk/src/core/java/org/wyona/yanel/core/ResourceTypeMatcherV1ImplV2.java	2011-07-19 14:52:54 UTC (rev 59474)
@@ -0,0 +1,92 @@
+/*
+ * Copyright 2011 Wyona
+ *
+ *  Licensed 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.wyona.org/licenses/APACHE-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.wyona.yanel.core;
+
+import org.apache.log4j.Logger;
+
+import org.wyona.yanel.core.api.ResourceTypeMatcherV1;
+import org.wyona.yanel.core.map.Realm;
+import org.wyona.yanel.core.util.PathUtil;
+
+import org.wyona.yarep.core.NoSuchNodeException;
+
+import java.io.Reader;
+
+/**
+ * Request matcher implementation
+ */
+public class ResourceTypeMatcherV1ImplV2 implements ResourceTypeMatcherV1 {
+
+    private static Logger log = Logger.getLogger(ResourceTypeMatcherV1ImplV2.class);
+    
+    /**
+     * Get resource configuration for a given realm and for a given path.
+     * The chain of responsibility is as follows:
+     *  1) One-to-one mapping (.yanel-rc and then deprecated .yanel-rti)
+     *  2) RC map
+     *  3) file/node resource type
+     *
+     * @param environment Yanel environment containing request, response, etc.
+     * @param realm Realm for which resource/controller should handle/process request
+     * @param path Path starting at root of realm (e.g. yanel.getMap().getPath(realm, request.getServletPath()))
+     */
+    public ResourceConfiguration getResourceConfiguration(Environment environment, Realm realm, String path) throws Exception {
+        // Check first if a one-to-one mapping exists
+        if (realm.getRTIRepository().existsNode(PathUtil.getRCPath(path))) {
+            ResourceConfiguration rc = new ResourceConfiguration(realm.getRTIRepository().getNode(PathUtil.getRCPath(path)));
+            if (rc != null) return rc;
+        }
+
+        // Fallback to deprecated RTI (one-to-one mapping)
+        if (realm.getRTIRepository().existsNode(PathUtil.getRTIPath(path))) {
+            log.warn("DEPRECATED: RTI should be replaced by ResourceConfiguration: " + realm + ", " + path);
+            ResourceTypeIdentifier rti = getResourceTypeIdentifier(realm, path);
+            ResourceConfiguration rc = new RTIbasedResourceConfiguration(rti);
+            return rc;
+        } 
+
+        // Check on resource configuration map
+        String rcPath = ResourceConfigurationMapV2.getRCPath(realm, path, environment.getRequest().getQueryString());
+        if (rcPath != null) {
+            if (realm.getRTIRepository().existsNode(rcPath)) {
+                ResourceConfiguration rc = new ResourceConfiguration(realm.getRTIRepository().getNode(rcPath));
+                if (rc != null) return rc;
+            } else {
+                throw new Exception("Request did match within resource configuration map of realm '" + realm.getName() + "', but no such resource type configuration node exist: " + rcPath);
+            }
+        } 
+        
+        //log.debug("No resource configuration for request: " + path + ", " + realm.getID());
+        return null;
+    }
+
+    /**
+     * Returns the abstraction of the rti file for the given path in the realm.
+     * TODO: move this method to some RTIManager class ?
+     * @deprecated
+     */
+    private ResourceTypeIdentifier getResourceTypeIdentifier(Realm realm, String path) throws Exception {
+        log.debug("Original path: " + path);
+        try {
+            Reader reader = realm.getRTIRepository().getReader(new Path(PathUtil.getRTIPath(path)));
+            return new ResourceTypeIdentifier(reader);
+        } catch(NoSuchNodeException e) {
+            log.warn(e.getMessage());
+            log.warn("TODO: Implement chain of responsibility ...");
+            return new ResourceTypeIdentifier("<{http://www.wyona.org/yanel/resource/1.0}file/>", null);
+        }
+    }
+}



More information about the Yanel-commits mailing list