abdera-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jmsn...@apache.org
Subject svn commit: r510127 - in /incubator/abdera/java/trunk: examples/src/main/java/org/apache/abdera/examples/appserver/ protocol/src/main/java/org/apache/abdera/protocol/ server/src/main/java/org/apache/abdera/protocol/server/provider/ server/src/main/java...
Date Wed, 21 Feb 2007 18:05:43 GMT
Author: jmsnell
Date: Wed Feb 21 10:05:42 2007
New Revision: 510127

URL: http://svn.apache.org/viewvc?view=rev&rev=510127
Log:
* eliminate Provider.getInfo method. not necessary
* do not do http request precondition checking in the default request handler. 
  easier to do this in the Provider
* eliminate the "full" param on the Provider methods. Not useful
* refactor the default request handler to shift out some unused, likely not useful code
* eliminate the ResponseInfo interface. not used any more

Added:
    incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/servlet/TypeRequestHandler.java
Removed:
    incubator/abdera/java/trunk/protocol/src/main/java/org/apache/abdera/protocol/ResponseInfo.java
Modified:
    incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/appserver/SimpleProvider.java
    incubator/abdera/java/trunk/protocol/src/main/java/org/apache/abdera/protocol/Response.java
    incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/provider/Provider.java
    incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/servlet/AbstractRequestHandler.java
    incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/servlet/DefaultRequestHandler.java
    incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/servlet/HttpServletRequestContext.java

Modified: incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/appserver/SimpleProvider.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/appserver/SimpleProvider.java?view=diff&rev=510127&r1=510126&r2=510127
==============================================================================
--- incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/appserver/SimpleProvider.java
(original)
+++ incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/appserver/SimpleProvider.java
Wed Feb 21 10:05:42 2007
@@ -34,7 +34,6 @@
 import org.apache.abdera.parser.ParseException;
 import org.apache.abdera.parser.Parser;
 import org.apache.abdera.protocol.EntityTag;
-import org.apache.abdera.protocol.ResponseInfo;
 import org.apache.abdera.protocol.server.provider.AbstractResponseContext;
 import org.apache.abdera.protocol.server.provider.BaseResponseContext;
 import org.apache.abdera.protocol.server.provider.EmptyResponseContext;
@@ -94,41 +93,25 @@
   }
   
   public ResponseContext getService(
-    RequestContext request, 
-    boolean full) {
+    RequestContext request) {
       Abdera abdera = request.getServiceContext().getAbdera();
       Document<Service> service = get_service_doc(abdera);
       AbstractResponseContext rc; 
-      rc = (full) ? 
-        new BaseResponseContext<Document<Service>>(service) : 
-        new EmptyResponseContext(200);
+      rc = new BaseResponseContext<Document<Service>>(service); 
       rc.setEntityTag(service_etag);
       return rc;
   }
   
   public ResponseContext getFeed(
-    RequestContext request, 
-    boolean full) {
+    RequestContext request) {
       Abdera abdera = request.getServiceContext().getAbdera();
       Document<Feed> feed = get_feed_doc(abdera);
       AbstractResponseContext rc; 
-      rc = (full) ? 
-        new BaseResponseContext<Document<Feed>>(feed) : 
-        new EmptyResponseContext(200);
+      rc = new BaseResponseContext<Document<Feed>>(feed);
       rc.setEntityTag(calculateEntityTag(feed.getRoot()));
       return rc;
   }
 
-  public ResponseInfo getInfo(
-    RequestContext request) {
-      TargetType type = request.getTarget().getType();
-      if (type == TargetType.TYPE_SERVICE) return getService(request, false);
-      if (type == TargetType.TYPE_COLLECTION) return getFeed(request, false);
-      if (type == TargetType.TYPE_ENTRY) return getEntry(request, false);
-      return null;
-  }
-
-  
   @SuppressWarnings("unchecked")
   public ResponseContext createEntry(
     RequestContext request) {
@@ -178,24 +161,21 @@
   
   public ResponseContext deleteEntry(
     RequestContext request) {
-      Entry entry = getEntry(request);
+      Entry entry = getAbderaEntry(request);
       if (entry != null)
         entry.discard();
       return new EmptyResponseContext(204);
   }
 
   public ResponseContext getEntry(
-    RequestContext request, 
-    boolean full) {
-      Entry entry = (Entry) getEntry(request);
+    RequestContext request) {
+      Entry entry = (Entry) getAbderaEntry(request);
       if (entry != null) {
         Feed feed = entry.getParentElement();
         entry = (Entry) entry.clone();
         entry.setSource(feed.getAsSource());
         Document<Entry> entry_doc = entry.getDocument();
-        AbstractResponseContext rc = (full) ?
-          new BaseResponseContext<Document<Entry>>(entry_doc) :
-          new EmptyResponseContext(200);
+        AbstractResponseContext rc = new BaseResponseContext<Document<Entry>>(entry_doc);
         rc.setEntityTag(calculateEntityTag(entry));
         return rc;
       } else {
@@ -209,7 +189,7 @@
       Abdera abdera = request.getServiceContext().getAbdera();
       Parser parser = abdera.getParser();
       Factory factory = abdera.getFactory();
-      Entry orig_entry = getEntry(request);
+      Entry orig_entry = getAbderaEntry(request);
       if (orig_entry != null) {
         try {
           MimeType contentType = request.getContentType();
@@ -268,7 +248,7 @@
     return null;
   }
   
-  private Entry getEntry(RequestContext request) {
+  private Entry getAbderaEntry(RequestContext request) {
     Abdera abdera = request.getServiceContext().getAbdera();
     String entry_id = getEntryID(request);
     Document<Feed> feed = get_feed_doc(abdera);
@@ -302,8 +282,7 @@
   }
   
   public ResponseContext getMedia(
-    RequestContext request, 
-    boolean full) {
+    RequestContext request) {
       throw new UnsupportedOperationException();
   }
   
@@ -329,7 +308,7 @@
     return true;
   }
 
-  public ResponseContext getCategories(RequestContext request, boolean full) {
+  public ResponseContext getCategories(RequestContext request) {
     return null;
   }
 

Modified: incubator/abdera/java/trunk/protocol/src/main/java/org/apache/abdera/protocol/Response.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/protocol/src/main/java/org/apache/abdera/protocol/Response.java?view=diff&rev=510127&r1=510126&r2=510127
==============================================================================
--- incubator/abdera/java/trunk/protocol/src/main/java/org/apache/abdera/protocol/Response.java
(original)
+++ incubator/abdera/java/trunk/protocol/src/main/java/org/apache/abdera/protocol/Response.java
Wed Feb 21 10:05:42 2007
@@ -29,7 +29,7 @@
 import org.apache.abdera.util.iri.IRISyntaxException;
 
 public interface Response
-  extends ResponseInfo, ProtocolConstants {
+  extends ProtocolConstants {
 
   public static enum ResponseType {
     SUCCESS, REDIRECTION, CLIENT_ERROR, SERVER_ERROR, UNKNOWN;
@@ -44,6 +44,8 @@
     
   }
   
+  public EntityTag getEntityTag();
+    
   public ResponseType getType();
   
   public int getStatus();

Modified: incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/provider/Provider.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/provider/Provider.java?view=diff&rev=510127&r1=510126&r2=510127
==============================================================================
--- incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/provider/Provider.java
(original)
+++ incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/provider/Provider.java
Wed Feb 21 10:05:42 2007
@@ -17,12 +17,8 @@
 */
 package org.apache.abdera.protocol.server.provider;
 
-import org.apache.abdera.protocol.ResponseInfo;
-
 public interface Provider {
 
-  ResponseInfo getInfo(RequestContext request);
-  
   ResponseContext createEntry(RequestContext request);
   
   ResponseContext deleteEntry(RequestContext request);
@@ -33,15 +29,15 @@
   
   ResponseContext updateMedia(RequestContext request);
   
-  ResponseContext getService(RequestContext request, boolean full);
+  ResponseContext getService(RequestContext request);
   
-  ResponseContext getFeed(RequestContext request, boolean full);
+  ResponseContext getFeed(RequestContext request);
   
-  ResponseContext getEntry(RequestContext request, boolean full);
+  ResponseContext getEntry(RequestContext request);
   
-  ResponseContext getMedia(RequestContext request, boolean full);
+  ResponseContext getMedia(RequestContext request);
   
-  ResponseContext getCategories(RequestContext request, boolean full);
+  ResponseContext getCategories(RequestContext request);
   
   ResponseContext entryPost(RequestContext request);
   

Modified: incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/servlet/AbstractRequestHandler.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/servlet/AbstractRequestHandler.java?view=diff&rev=510127&r1=510126&r2=510127
==============================================================================
--- incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/servlet/AbstractRequestHandler.java
(original)
+++ incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/servlet/AbstractRequestHandler.java
Wed Feb 21 10:05:42 2007
@@ -28,8 +28,6 @@
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 
-import org.apache.abdera.protocol.EntityTag;
-import org.apache.abdera.protocol.ResponseInfo;
 import org.apache.abdera.protocol.server.ServiceContext;
 import org.apache.abdera.protocol.server.provider.EmptyResponseContext;
 import org.apache.abdera.protocol.server.provider.Provider;
@@ -97,12 +95,6 @@
         getAllowedMethods(target.getType()));
       return false;
     }
-    // Check The Conditions
-    ResponseInfo info = provider.getInfo(request);
-    switch(checkConditions(info, request)) {
-      case 412: preconditionfailed(response); return false;
-      case 304: notmodified(response); return false;
-    }
     return true;
   }
   
@@ -164,44 +156,6 @@
     ServiceContext context, 
     HttpServletRequest request) {
       return new HttpServletRequestContext(context, request);
-  }
-  
-  protected int checkConditions(
-    ResponseInfo info, 
-    RequestContext request) {
-      EntityTag entity_tag = (info != null) ? info.getEntityTag() : null;
-      Date last_mod = (info != null) ? info.getLastModified() : null;
-      if (entity_tag != null) {
-        String ifmatch = request.getIfMatch();
-        if (ifmatch != null && 
-           (entity_tag == null || 
-            !EntityTag.matches(entity_tag,ifmatch))) {
-          return 412;
-        }
-        String ifnonematch = request.getIfNoneMatch();
-        if (ifnonematch != null && 
-            entity_tag != null && 
-            EntityTag.matches(entity_tag,ifnonematch)) {
-          return 304;
-        }
-      }
-      if (last_mod != null) {
-        Date ifmodsince = request.getIfModifiedSince();
-        if (ifmodsince != null && 
-            last_mod.getTime() <= ifmodsince.getTime()) return 304;
-        Date ifunmodsince = request.getIfUnmodifiedSince();
-        if (ifunmodsince != null && 
-            last_mod.getTime() > ifunmodsince.getTime()) return 412;
-      }
-      return 0;
-  }
-  
-  protected void preconditionfailed(HttpServletResponse response) throws IOException {
-    response.sendError(412, "Failed");
-  }
-  
-  protected void notmodified(HttpServletResponse response) throws IOException {
-    response.sendError(304, "Not Modified");
   }
   
   protected void noprovider(HttpServletResponse response) throws IOException {

Modified: incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/servlet/DefaultRequestHandler.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/servlet/DefaultRequestHandler.java?view=diff&rev=510127&r1=510126&r2=510127
==============================================================================
--- incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/servlet/DefaultRequestHandler.java
(original)
+++ incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/servlet/DefaultRequestHandler.java
Wed Feb 21 10:05:42 2007
@@ -17,9 +17,6 @@
 */
 package org.apache.abdera.protocol.server.servlet;
 
-import java.util.HashMap;
-import java.util.Map;
-
 import org.apache.abdera.protocol.server.provider.AbstractResponseContext;
 import org.apache.abdera.protocol.server.provider.EmptyResponseContext;
 import org.apache.abdera.protocol.server.provider.Provider;
@@ -31,153 +28,80 @@
   extends AbstractRequestHandler
   implements RequestHandler {
 
-  public static interface TypeHandler {
-    ResponseContext process(Provider provider, RequestContext request);
-  }
-  
-  private static class TypeMethod {
-    private TargetType type;
-    private String method;
-    public int hashCode() {
-      final int PRIME = 31;
-      int result = 1;
-      result = PRIME * result + ((method == null) ? 0 : method.hashCode());
-      result = PRIME * result + ((type == null) ? 0 : type.hashCode());
-      return result;
-    }
-    public boolean equals(Object obj) {
-      if (this == obj)
-        return true;
-      if (obj == null)
-        return false;
-      if (getClass() != obj.getClass())
-        return false;
-      final TypeMethod other = (TypeMethod) obj;
-      if (method == null) {
-        if (other.method != null)
-          return false;
-      } else if (!method.equals(other.method))
-        return false;
-      if (type == null) {
-        if (other.type != null)
-          return false;
-      } else if (!type.equals(other.type))
-        return false;
-      return true;
-    }
-  }
-  
-  private final Map<TargetType,TypeHandler> typehandlers = new HashMap<TargetType,TypeHandler>();
-  private final Map<TypeMethod,TypeHandler> typemethodhandlers = new HashMap<TypeMethod,TypeHandler>();
-  
-  private TypeMethod getTypeMethod(TargetType type, String method) {
-    TypeMethod tm = new TypeMethod();
-    tm.type = type;
-    tm.method = method;
-    return tm;
-  }
-  
-  protected synchronized void setTypeMethodHandler(TargetType type, String method, TypeHandler
handler) {
-    typemethodhandlers.put(getTypeMethod(type,method), handler);
-  }
-  
-  protected synchronized void setTypeHandler(TargetType type, TypeHandler handler) {
-    typehandlers.put(type,handler);
-  }
-  
-  private TypeHandler getHandler(TargetType type) {
-    return typehandlers.get(type);
-  }
-  
-  private TypeHandler getMethodHandler(TargetType type, String method) {
-    return typemethodhandlers.get(getTypeMethod(type, method));
-  }
-  
   protected ResponseContext process(
     Provider provider, 
     RequestContext request) {
       
       TargetType type = request.getTarget().getType();    
-      TypeHandler handler = getHandler(type);
-      if (handler == null) {
-        String method = request.getMethod().intern();
-        
-        handler = getMethodHandler(type,method);
+      String method = request.getMethod();
         
-        if (handler == null) {
-          if (method == "GET") {
-            if (type == TargetType.TYPE_SERVICE) {
-              return provider.getService(request, true);
-            }
-            if (type == TargetType.TYPE_COLLECTION) {
-              return provider.getFeed(request, true);
-            }
-            if (type == TargetType.TYPE_ENTRY) {
-              return provider.getEntry(request, true);
-            }
-            if (type == TargetType.TYPE_MEDIA) {
-              return provider.getMedia(request, true);
-            }
-            if (type == TargetType.TYPE_CATEGORIES) {
-              return provider.getCategories(request, true);
-            }
-          }
-          else if (method == "HEAD") {
-            if (type == TargetType.TYPE_SERVICE) {
-              return provider.getService(request, false);
-            }
-            if (type == TargetType.TYPE_COLLECTION) {
-              return provider.getFeed(request, false);
-            }
-            if (type == TargetType.TYPE_ENTRY) {
-              return provider.getEntry(request, false);
-            }
-            if (type == TargetType.TYPE_MEDIA) {
-              return provider.getMedia(request, false);
-            }
-            if (type == TargetType.TYPE_CATEGORIES) {
-              return provider.getCategories(request, false);
-            }
-          }
-          else if (method == "POST") {
-            if (type == TargetType.TYPE_COLLECTION) {
-              return provider.createEntry(request);
-            }
-            if (type == TargetType.TYPE_ENTRY) {
-              return provider.entryPost(request);
-            }
-            if (type == TargetType.TYPE_MEDIA) {
-              return provider.mediaPost(request);
-            }
-          }
-          else if (method == "PUT") {
-            if (type == TargetType.TYPE_ENTRY) {
-              return provider.updateEntry(request);
-            }
-            if (type == TargetType.TYPE_MEDIA) {
-              return provider.updateMedia(request);
-            }
-          }
-          else if (method == "DELETE") {
-            if (type == TargetType.TYPE_ENTRY) {
-              return provider.deleteEntry(request);
-            }
-            if (type == TargetType.TYPE_MEDIA) {
-              return provider.deleteMedia(request);
-            }
-          } 
-          else if (method == "OPTIONS") {
-            AbstractResponseContext rc = new EmptyResponseContext(200);
-            rc.addHeader("Allow", combine(getAllowedMethods(type)));
-            return rc;
-          }
-        } else {
-          return handler.process(provider, request);
-        }
-        return null;
-      } else {
-        return handler.process(provider, request);
+      if (method.equals("GET")) {
+        if (type == TargetType.TYPE_SERVICE) {
+          return provider.getService(request);
+        }
+        if (type == TargetType.TYPE_COLLECTION) {
+          return provider.getFeed(request);
+        }
+        if (type == TargetType.TYPE_ENTRY) {
+          return provider.getEntry(request);
+        }
+        if (type == TargetType.TYPE_MEDIA) {
+          return provider.getMedia(request);
+        }
+        if (type == TargetType.TYPE_CATEGORIES) {
+          return provider.getCategories(request);
+        }
+      }
+      else if (method.equals("HEAD")) {
+        if (type == TargetType.TYPE_SERVICE) {
+          return provider.getService(request);
+        }
+        if (type == TargetType.TYPE_COLLECTION) {
+          return provider.getFeed(request);
+        }
+        if (type == TargetType.TYPE_ENTRY) {
+          return provider.getEntry(request);
+        }
+        if (type == TargetType.TYPE_MEDIA) {
+          return provider.getMedia(request);
+        }
+        if (type == TargetType.TYPE_CATEGORIES) {
+          return provider.getCategories(request);
+        }
+      }
+      else if (method.equals("POST")) {
+        if (type == TargetType.TYPE_COLLECTION) {
+          return provider.createEntry(request);
+        }
+        if (type == TargetType.TYPE_ENTRY) {
+          return provider.entryPost(request);
+        }
+        if (type == TargetType.TYPE_MEDIA) {
+          return provider.mediaPost(request);
+        }
+      }
+      else if (method.equals("PUT")) {
+        if (type == TargetType.TYPE_ENTRY) {
+          return provider.updateEntry(request);
+        }
+        if (type == TargetType.TYPE_MEDIA) {
+          return provider.updateMedia(request);
+        }
+      }
+      else if (method.equals("DELETE")) {
+        if (type == TargetType.TYPE_ENTRY) {
+          return provider.deleteEntry(request);
+        }
+        if (type == TargetType.TYPE_MEDIA) {
+          return provider.deleteMedia(request);
+        }
+      } 
+      else if (method.equals("OPTIONS")) {
+        AbstractResponseContext rc = new EmptyResponseContext(200);
+        rc.addHeader("Allow", combine(getAllowedMethods(type)));
+        return rc;
       }
+      return null;
   }
   
   protected String[] getAllowedMethods(TargetType type) {

Modified: incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/servlet/HttpServletRequestContext.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/servlet/HttpServletRequestContext.java?view=diff&rev=510127&r1=510126&r2=510127
==============================================================================
--- incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/servlet/HttpServletRequestContext.java
(original)
+++ incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/servlet/HttpServletRequestContext.java
Wed Feb 21 10:05:42 2007
@@ -168,62 +168,62 @@
   }
   
   private static String getHost(
-      ServiceContext context, 
-      HttpServletRequest request) {
-        Abdera abdera = context.getAbdera();
-        String host = abdera.getConfiguration().getConfigurationOption(
-          "org.apache.abdera.protocol.server.Host");
-        return (host != null) ? 
-          host : 
-          request.getServerName();
-    }
-    
-    private static int getPort(
-      ServiceContext context, 
-      HttpServletRequest request) {
+    ServiceContext context, 
+    HttpServletRequest request) {
       Abdera abdera = context.getAbdera();
-        String port = abdera.getConfiguration().getConfigurationOption(
-          "org.apache.abdera.protocol.server.Port");
-        return (port != null) ? 
-          Integer.parseInt(port) : 
-          request.getLocalPort();
-    }
-    
-    private static IRI initBaseUri(
-      ServiceContext context, 
-      HttpServletRequest request) {
-        StringBuffer buffer = 
-          new StringBuffer(
-            (request.isSecure())?
-              "https":"http");
-        buffer.append("://");
-        buffer.append(getHost(context,request));
-        int port = getPort(context,request);
-        if (port != 80) {
-          buffer.append(":");
-          buffer.append(port);
-        }
-        buffer.append(request.getContextPath());
-        // So that .resolve() works appropriately.
-        buffer.append("/");
-        try {
-          return new IRI(buffer.toString());
-        } catch (IRISyntaxException e) {
-          throw new RuntimeException(e);
-        }
-    }
-    
-    private static IRI initRequestUri(HttpServletRequest request) {
-      IRI uri = null;
+      String host = abdera.getConfiguration().getConfigurationOption(
+        "org.apache.abdera.protocol.server.Host");
+      return (host != null) ? 
+        host : 
+        request.getServerName();
+  }
+  
+  private static int getPort(
+    ServiceContext context, 
+    HttpServletRequest request) {
+    Abdera abdera = context.getAbdera();
+      String port = abdera.getConfiguration().getConfigurationOption(
+        "org.apache.abdera.protocol.server.Port");
+      return (port != null) ? 
+        Integer.parseInt(port) : 
+        request.getLocalPort();
+  }
+  
+  private static IRI initBaseUri(
+    ServiceContext context, 
+    HttpServletRequest request) {
+      StringBuffer buffer = 
+        new StringBuffer(
+          (request.isSecure())?
+            "https":"http");
+      buffer.append("://");
+      buffer.append(getHost(context,request));
+      int port = getPort(context,request);
+      if (port != 80) {
+        buffer.append(":");
+        buffer.append(port);
+      }
+      buffer.append(request.getContextPath());
+      // So that .resolve() works appropriately.
+      buffer.append("/");
       try {
-        StringBuffer buf = 
-          new StringBuffer(
-            request.getRequestURI());
-        String qs = request.getQueryString();
-        if (qs != null && qs.length() != 0)
-          buf.append("?" + request.getQueryString());
-        uri = new IRI(buf.toString());
-      } catch (IRISyntaxException e) {}
-      return uri;
-    }
+        return new IRI(buffer.toString());
+      } catch (IRISyntaxException e) {
+        throw new RuntimeException(e);
+      }
+  }
+  
+  private static IRI initRequestUri(HttpServletRequest request) {
+    IRI uri = null;
+    try {
+      StringBuffer buf = 
+        new StringBuffer(
+          request.getRequestURI());
+      String qs = request.getQueryString();
+      if (qs != null && qs.length() != 0)
+        buf.append("?" + request.getQueryString());
+      uri = new IRI(buf.toString());
+    } catch (IRISyntaxException e) {}
+    return uri;
+  }
 }

Added: incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/servlet/TypeRequestHandler.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/servlet/TypeRequestHandler.java?view=auto&rev=510127
==============================================================================
--- incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/servlet/TypeRequestHandler.java
(added)
+++ incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/servlet/TypeRequestHandler.java
Wed Feb 21 10:05:42 2007
@@ -0,0 +1,133 @@
+/*
+* 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.protocol.server.servlet;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.abdera.protocol.server.provider.Provider;
+import org.apache.abdera.protocol.server.provider.RequestContext;
+import org.apache.abdera.protocol.server.provider.ResponseContext;
+import org.apache.abdera.protocol.server.provider.TargetType;
+
+/**
+ * Extension of the Default Request Handler that allows subclasses
+ * to plug in alternate implementations of request and method handlers.
+ * This was originally in DefaultRequestHandler but it just didn't 
+ * prove to be as useful as I had originally imagined it could be.  
+ * Separating this out into it's own class makes it easier for us to 
+ * drop it later if it proves to be completely useless :-)  
+ */
+public abstract class TypeRequestHandler
+  extends DefaultRequestHandler 
+  implements RequestHandler {
+
+  public static interface TypeHandler {
+    ResponseContext process(
+      Provider provider, 
+      RequestContext request);
+  }
+  
+  private static class TypeMethod {
+    private TargetType type;
+    private String method;
+    public int hashCode() {
+      final int PRIME = 31;
+      int result = 1;
+      result = PRIME * result + ((method == null) ? 0 : method.hashCode());
+      result = PRIME * result + ((type == null) ? 0 : type.hashCode());
+      return result;
+    }
+    public boolean equals(Object obj) {
+      if (this == obj)
+        return true;
+      if (obj == null)
+        return false;
+      if (getClass() != obj.getClass())
+        return false;
+      final TypeMethod other = (TypeMethod) obj;
+      if (method == null) {
+        if (other.method != null)
+          return false;
+      } else if (!method.equals(other.method))
+        return false;
+      if (type == null) {
+        if (other.type != null)
+          return false;
+      } else if (!type.equals(other.type))
+        return false;
+      return true;
+    }
+  }
+  
+  private final Map<TargetType,TypeHandler> typehandlers = 
+    new HashMap<TargetType,TypeHandler>();
+  private final Map<TypeMethod,TypeHandler> typemethodhandlers = 
+    new HashMap<TypeMethod,TypeHandler>();
+  
+  private TypeMethod getTypeMethod(TargetType type, String method) {
+    TypeMethod tm = new TypeMethod();
+    tm.type = type;
+    tm.method = method;
+    return tm;
+  }
+  
+  protected synchronized void setTypeMethodHandler(
+    TargetType type, 
+    String method, 
+    TypeHandler handler) {
+      typemethodhandlers.put(getTypeMethod(type,method), handler);
+  }
+  
+  protected synchronized void setTypeHandler(
+    TargetType type, 
+    TypeHandler handler) {
+      typehandlers.put(type,handler);
+  }
+  
+  private TypeHandler getHandler(
+    TargetType type) {
+      return typehandlers.get(type);
+  }
+  
+  private TypeHandler getMethodHandler(
+    TargetType type, 
+    String method) {
+      return typemethodhandlers.get(getTypeMethod(type, method));
+  }
+
+  @Override
+  protected ResponseContext process(
+    Provider provider, 
+    RequestContext request) {
+      TargetType type = request.getTarget().getType();    
+      TypeHandler handler = getHandler(type);
+      if (handler == null) {
+        String method = request.getMethod();
+        handler = getMethodHandler(type,method);
+        if (handler == null) {
+          return super.process(provider, request);
+        } else {
+          return handler.process(provider, request);
+        }
+      } else {
+        return handler.process(provider, request);
+      }
+  }
+
+}



Mime
View raw message