You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@abdera.apache.org by jm...@apache.org on 2006/08/29 03:06:31 UTC

svn commit: r437896 - in /incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/server: impl/BaseRequestHandler.java util/ResourceType.java util/Target.java

Author: jmsnell
Date: Mon Aug 28 18:06:31 2006
New Revision: 437896

URL: http://svn.apache.org/viewvc?rev=437896&view=rev
Log:
* Utility class for helping with determining which resource the client is requesting.
  Specifically, regex patterns are specified for the uris of each of the various 
  Atom resource types (e.g. collection, entry, service doc, etc).  The target class
  checks the passed in request uri and returns an appropriate TargetMatcher for the
  type of resource.

  e.g.,

    Target target = new Target();
    target.addPattern(ResourceType.INTROSPECTION, "/atom");
    target.addPattern(ResourceType.COLLECTION, "/atom/([^/#?]*)");
    target.addPattern(ResourceType.ENTRY, "/atom/([^/#?]*)/([^/#?]*)");
    target.addPattern(ResourceType.ENTRY_EDIT, "/atom/([^/#?]*)/([^/#?]*)\\?edit");
    target.addPattern(ResourceType.MEDIA,"/atom/([^/#?]*)/([^/#?]*)\\?media");
    target.addPattern(ResourceType.MEDIA_EDIT,"/atom/([^/#?]*)/([^/#?]*)\\?edit-media");
    
    TargetMatcher tm = target.getTargetMatcher("/atom");
    System.out.println(tm.getType());
    System.out.println(tm.getToken(2));

* Move the resource type enum that was in BaseRequestHandler out to it's own class so
  it can be easily reused without depending on BaseRequestHandler.

Added:
    incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/server/util/ResourceType.java
    incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/server/util/Target.java
Modified:
    incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/server/impl/BaseRequestHandler.java

Modified: incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/server/impl/BaseRequestHandler.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/server/impl/BaseRequestHandler.java?rev=437896&r1=437895&r2=437896&view=diff
==============================================================================
--- incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/server/impl/BaseRequestHandler.java (original)
+++ incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/server/impl/BaseRequestHandler.java Mon Aug 28 18:06:31 2006
@@ -23,15 +23,12 @@
 import org.apache.abdera.server.cache.Cache;
 import org.apache.abdera.server.cache.CacheEntry;
 import org.apache.abdera.server.exceptions.AbderaServerException;
+import org.apache.abdera.server.util.ResourceType;
 
 public abstract class BaseRequestHandler 
   implements RequestHandler {
 
-  public enum Type {
-    UNKNOWN, INTROSPECTION, COLLECTION, ENTRY, ENTRY_EDIT, MEDIA, MEDIA_EDIT
-  };
-  
-  protected Type resourceType = Type.UNKNOWN;
+  protected ResourceType resourceType = ResourceType.UNKNOWN;
   
   public BaseRequestHandler() {}
   
@@ -91,7 +88,7 @@
   /**
    * Returns the type of the requested resource
    */
-  protected abstract Type getResourceType(RequestContext requestContext);
+  protected abstract ResourceType getResourceType(RequestContext requestContext);
   
   /**
    * Check that the requested resource exists.  If not, the method
@@ -138,7 +135,7 @@
    * Implementations should override this to specify customizations to the
    * allowable methods on a particular type of resource
    */
-  protected String[] getAllowedMethods(Type type) {
+  protected String[] getAllowedMethods(ResourceType type) {
     switch (type) {
       case COLLECTION:    return new String[] { "GET", "POST", "HEAD", "OPTIONS" };
       case ENTRY:         return new String[] { "GET", "HEAD", "OPTIONS" };

Added: incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/server/util/ResourceType.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/server/util/ResourceType.java?rev=437896&view=auto
==============================================================================
--- incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/server/util/ResourceType.java (added)
+++ incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/server/util/ResourceType.java Mon Aug 28 18:06:31 2006
@@ -0,0 +1,30 @@
+/*
+* Licensed to the Apache Software Foundation (ASF) under one or more
+* contributor license agreements.  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.  For additional information regarding
+* copyright in this work, please see the NOTICE file in the top level
+* directory of this distribution.
+*/
+package org.apache.abdera.server.util;
+
+public enum ResourceType {
+
+  UNKNOWN, 
+  INTROSPECTION, 
+  COLLECTION, 
+  ENTRY, 
+  ENTRY_EDIT, 
+  MEDIA, 
+  MEDIA_EDIT
+  
+}

Added: incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/server/util/Target.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/server/util/Target.java?rev=437896&view=auto
==============================================================================
--- incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/server/util/Target.java (added)
+++ incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/server/util/Target.java Mon Aug 28 18:06:31 2006
@@ -0,0 +1,108 @@
+/*
+* Licensed to the Apache Software Foundation (ASF) under one or more
+* contributor license agreements.  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.  For additional information regarding
+* copyright in this work, please see the NOTICE file in the top level
+* directory of this distribution.
+*/
+package org.apache.abdera.server.util;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+/**
+ * <p>Provides a utility class helpful for determining which type of resource
+ * the client is requesting.  Each resource type (e.g. service doc, collection,
+ * entry, edit uri, media resource, etc) is assigned a regex pattern.  Given
+ * the request URI (path and querystring), this will determine which resource
+ * was selected and return an appropriate TargetMatcher.  TargetMatcher is 
+ * essentially just a simplified version of the java.util.regex.Matcher that
+ * also specifies the Resource Type.</p>
+ * 
+ * <pre>
+ *  Target target = new Target();
+ *  target.addPattern(ResourceType.INTROSPECTION, "/atom");
+ *  target.addPattern(ResourceType.COLLECTION, "/atom/([^/#?]+)");
+ *  target.addPattern(ResourceType.ENTRY, "/atom/([^/#?]+)/([^/#?]+)");
+ *  target.addPattern(ResourceType.ENTRY_EDIT, "/atom/([^/#?]+)/([^/#?]+)\\?edit");
+ *  target.addPattern(ResourceType.MEDIA,"/atom/([^/#?]+)/([^/#?]+)\\?media");
+ *  target.addPattern(ResourceType.MEDIA_EDIT,"/atom/([^/#?]+)/([^/#?]+)\\?edit-media");
+ *  
+ *  TargetMatcher tm = target.getTargetMatcher("/atom/foo");
+ *  System.out.println(tm.getType());
+ *  System.out.println(tm.getToken(1));  // foo
+ * </pre>
+ *  
+ */
+public class Target {
+
+  private Map<ResourceType,Pattern> patterns = null;
+  
+  public Target() {
+    this.patterns = new HashMap<ResourceType,Pattern>();
+  }
+  
+  public Target(Map<ResourceType,String> patterns) {
+    this.patterns = new HashMap<ResourceType,Pattern>();
+    for (ResourceType type : patterns.keySet()) {
+      String p = patterns.get(type);
+      Pattern pattern = Pattern.compile(p);
+      this.patterns.put(type, pattern);
+    }
+  }
+  
+  public void addPattern(ResourceType type, String pattern) {
+    Pattern p = Pattern.compile(pattern);
+    this.patterns.put(type, p);
+  }
+  
+  public TargetMatcher getTargetMatcher(String path_info) {
+    if (patterns == null) return null;
+    for (ResourceType type : patterns.keySet()) {
+      Pattern pattern = patterns.get(type);
+      Matcher matcher = pattern.matcher(path_info);
+      if (matcher.matches()) return new TargetMatcher(type, matcher);
+    }
+    return null;
+  }
+  
+  public static class TargetMatcher {
+    
+    Matcher matcher = null;
+    ResourceType type = ResourceType.UNKNOWN;
+    
+    TargetMatcher(ResourceType type, Matcher matcher) {
+      this.type = type;
+      this.matcher = matcher;
+    }
+    
+    public ResourceType getType() {
+      return this.type;
+    }
+    
+    public String getToken(int token) {
+      try {
+        return this.matcher.group(token);
+      } catch (IndexOutOfBoundsException e) {
+        return null;
+      }
+    }
+    
+    public boolean hasToken(int token) {
+      return getToken(token) != null;
+    }
+  }
+  
+}