abdera-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jmsn...@apache.org
Subject svn commit: r449861 - in /incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server: provider/ servlet/ util/
Date Mon, 25 Sep 2006 23:33:26 GMT
Author: jmsnell
Date: Mon Sep 25 16:33:26 2006
New Revision: 449861

URL: http://svn.apache.org/viewvc?view=rev&rev=449861
Log:
Variety of fixes and improvements to the server code.

Modified:
    incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/provider/AbstractResponseContext.java
    incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/provider/AbstractTarget.java
    incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/provider/BaseResponseContext.java
    incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/provider/EmptyResponseContext.java
    incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/provider/ResponseContext.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/util/RegexTargetResolver.java
    incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/util/ServerUtils.java

Modified: incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/provider/AbstractResponseContext.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/provider/AbstractResponseContext.java?view=diff&rev=449861&r1=449860&r2=449861
==============================================================================
--- incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/provider/AbstractResponseContext.java
(original)
+++ incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/provider/AbstractResponseContext.java
Mon Sep 25 16:33:26 2006
@@ -17,6 +17,8 @@
 */
 package org.apache.abdera.protocol.server.provider;
 
+import java.io.IOException;
+import java.io.OutputStream;
 import java.util.ArrayList;
 import java.util.Date;
 import java.util.HashMap;
@@ -27,6 +29,7 @@
 import org.apache.abdera.protocol.server.provider.ResponseContext;
 import org.apache.abdera.protocol.util.AbstractResponse;
 import org.apache.abdera.protocol.util.EncodingUtil;
+import org.apache.abdera.writer.Writer;
 
 public abstract class AbstractResponseContext
   extends AbstractResponse
@@ -36,6 +39,7 @@
   
   protected int status = 0;
   protected String status_text = null;
+  protected Writer writer = null;
   
   protected Map<String,List<Object>> headers = null;
 
@@ -291,4 +295,9 @@
     }
     setAllow(buf.toString());
   }
+  
+  public void setWriter(Writer writer) {
+    this.writer = writer;
+  }
+  
 }

Modified: incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/provider/AbstractTarget.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/provider/AbstractTarget.java?view=diff&rev=449861&r1=449860&r2=449861
==============================================================================
--- incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/provider/AbstractTarget.java
(original)
+++ incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/provider/AbstractTarget.java
Mon Sep 25 16:33:26 2006
@@ -39,7 +39,8 @@
   }
 
   public String[] getParameterNames() {
-    return context.getParameterNames();
+    String[] pn = context.getParameterNames();
+    return (pn != null) ? pn : new String[0];
   }
 
   public TargetType getType() {

Modified: incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/provider/BaseResponseContext.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/provider/BaseResponseContext.java?view=diff&rev=449861&r1=449860&r2=449861
==============================================================================
--- incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/provider/BaseResponseContext.java
(original)
+++ incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/provider/BaseResponseContext.java
Mon Sep 25 16:33:26 2006
@@ -26,6 +26,7 @@
 
 import org.apache.abdera.model.Base;
 import org.apache.abdera.util.MimeTypeHelper;
+import org.apache.abdera.writer.Writer;
 
 public class BaseResponseContext<T extends Base>
   extends AbstractResponseContext {
@@ -52,8 +53,18 @@
     return (base != null);
   }
 
+  public void writeTo(java.io.Writer javaWriter) throws IOException {
+    if (hasEntity()) {
+      if (writer == null) base.writeTo(javaWriter);
+      else writeTo(javaWriter,writer);
+    }
+  }
+  
   public void writeTo(OutputStream out) throws IOException {
-    if (hasEntity()) base.writeTo(out);
+    if (hasEntity()) {
+      if (writer == null) base.writeTo(out);
+      else writeTo(out,writer);
+    }
   }
 
   @Override
@@ -80,4 +91,19 @@
     }
     return len;
   }
+  
+  public void writeTo(
+    OutputStream out, 
+    Writer writer) 
+      throws IOException {
+    writer.writeTo(base, out);
+  }
+    
+  public void writeTo(
+    java.io.Writer javaWriter, 
+    Writer abderaWriter) 
+      throws IOException {
+    abderaWriter.writeTo(base, javaWriter);
+  }
+    
 }

Modified: incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/provider/EmptyResponseContext.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/provider/EmptyResponseContext.java?view=diff&rev=449861&r1=449860&r2=449861
==============================================================================
--- incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/provider/EmptyResponseContext.java
(original)
+++ incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/provider/EmptyResponseContext.java
Mon Sep 25 16:33:26 2006
@@ -20,6 +20,8 @@
 import java.io.IOException;
 import java.io.OutputStream;
 
+import org.apache.abdera.writer.Writer;
+
 public final class EmptyResponseContext 
   extends AbstractResponseContext {
 
@@ -33,5 +35,11 @@
 
   public void writeTo(OutputStream out) 
     throws IOException {}
+
+  public void writeTo(OutputStream out, Writer writer) throws IOException {}
+
+  public void writeTo(java.io.Writer javaWriter, Writer abderaWriter) throws IOException
{}
+
+  public void writeTo(java.io.Writer javaWriter) throws IOException {}
 
 }

Modified: incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/provider/ResponseContext.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/provider/ResponseContext.java?view=diff&rev=449861&r1=449860&r2=449861
==============================================================================
--- incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/provider/ResponseContext.java
(original)
+++ incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/provider/ResponseContext.java
Mon Sep 25 16:33:26 2006
@@ -21,6 +21,7 @@
 import java.io.OutputStream;
 
 import org.apache.abdera.protocol.Response;
+import org.apache.abdera.writer.Writer;
 
 public interface ResponseContext 
   extends Response {
@@ -29,4 +30,11 @@
   
   public void writeTo(OutputStream out) throws IOException;
   
+  public void writeTo(java.io.Writer javaWriter) throws IOException;
+  
+  public void writeTo(OutputStream out, Writer writer) throws IOException;
+  
+  public void writeTo(java.io.Writer javaWriter, Writer abderaWriter) throws IOException;
+  
+  public void setWriter(Writer writer);
 }

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=449861&r1=449860&r2=449861
==============================================================================
--- 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
Mon Sep 25 16:33:26 2006
@@ -17,6 +17,9 @@
 */
 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;
@@ -28,78 +31,153 @@
   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) {
-      String method = request.getMethod().intern();
-      TargetType type = request.getTarget().getType();
-      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;
+      
+      TargetType type = request.getTarget().getType();    
+      TypeHandler handler = getHandler(type);
+      if (handler == null) {
+        String method = request.getMethod().intern();
+        
+        handler = getMethodHandler(type,method);
+        
+        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);
       }
-      return null;
   }
   
   protected String[] getAllowedMethods(TargetType type) {

Modified: incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/util/RegexTargetResolver.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/util/RegexTargetResolver.java?view=diff&rev=449861&r1=449860&r2=449861
==============================================================================
--- incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/util/RegexTargetResolver.java
(original)
+++ incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/util/RegexTargetResolver.java
Mon Sep 25 16:33:26 2006
@@ -100,9 +100,9 @@
     implements Target {
     
     private static final long serialVersionUID = 165211244926064449L;
-    transient Matcher matcher;
+    protected transient Matcher matcher;
     
-    RegexTarget(
+    protected RegexTarget(
       TargetType type, 
       RequestContext context, 
       Matcher matcher) {

Modified: incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/util/ServerUtils.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/util/ServerUtils.java?view=diff&rev=449861&r1=449860&r2=449861
==============================================================================
--- incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/util/ServerUtils.java
(original)
+++ incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/util/ServerUtils.java
Mon Sep 25 16:33:26 2006
@@ -110,4 +110,10 @@
       return null;
   }
   
+  public static NamedWriter getNamedWriter(Abdera abdera, String mediatype) {
+    WriterFactory factory = abdera.getWriterFactory();
+    if (factory == null) return null;
+    NamedWriter writer = (NamedWriter) factory.getWriterByMediaType(mediatype);
+    return writer;
+  }
 }



Mime
View raw message