abdera-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jmsn...@apache.org
Subject svn commit: r517481 - in /incubator/abdera/java/trunk: client/src/test/java/org/apache/abdera/test/client/app/ core/src/main/java/org/apache/abdera/util/ examples/src/main/java/org/apache/abdera/examples/appserver/ server/src/main/java/org/apache/abder...
Date Tue, 13 Mar 2007 00:48:24 GMT
Author: jmsnell
Date: Mon Mar 12 17:48:23 2007
New Revision: 517481

URL: http://svn.apache.org/viewvc?view=rev&rev=517481
Log:
Add a new mediaresponsecontext implementation
Pass the servlet context path in to the target resolver
Fix up more entity tag stuff
Fix some test cases

Added:
    incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/provider/MediaResponseContext.java
Modified:
    incubator/abdera/java/trunk/client/src/test/java/org/apache/abdera/test/client/app/AppTest.java
    incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/ServiceUtil.java
    incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/appserver/SimpleTargetResolver.java
    incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/DefaultServiceContext.java
    incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/ServiceContext.java
    incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/provider/AbstractProvider.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/TargetResolver.java
    incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/servlet/HttpServletRequestContext.java
    incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/util/RegexTargetResolver.java
    incubator/abdera/java/trunk/server/src/test/java/org/apache/abdera/test/server/UtilityTest.java

Modified: incubator/abdera/java/trunk/client/src/test/java/org/apache/abdera/test/client/app/AppTest.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/client/src/test/java/org/apache/abdera/test/client/app/AppTest.java?view=diff&rev=517481&r1=517480&r2=517481
==============================================================================
--- incubator/abdera/java/trunk/client/src/test/java/org/apache/abdera/test/client/app/AppTest.java
(original)
+++ incubator/abdera/java/trunk/client/src/test/java/org/apache/abdera/test/client/app/AppTest.java
Mon Mar 12 17:48:23 2007
@@ -598,9 +598,7 @@
     assertFalse(tag1.isWeak());
     assertTrue(tag2.isWeak());
     assertFalse(EntityTag.matches(tag1, tag2));
-    assertTrue(EntityTag.matches(tag1, tag2, true));
     assertFalse(EntityTag.matchesAny(tag1, new EntityTag[] {tag2}));
-    assertTrue(EntityTag.matchesAny(tag1, new EntityTag[] {tag2}, true));
     assertEquals(tag1.toString(), "\"tag\"");
     assertEquals(tag2.toString(), "W/\"tag\"");
     tag1 = EntityTag.parse("\"tag\"");

Modified: incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/ServiceUtil.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/ServiceUtil.java?view=diff&rev=517481&r1=517480&r2=517481
==============================================================================
--- incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/ServiceUtil.java
(original)
+++ incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/ServiceUtil.java
Mon Mar 12 17:48:23 2007
@@ -56,6 +56,18 @@
   public static Object newInstance(String id, String _default, Abdera abdera) {
     return locate(id, _default, abdera);
   }
+  
+  /**
+   * Returns a new instance of the identified object class.  This will use
+   * the Abdera configuration mechanism to look up the implementation class
+   * for the specified id.  Several places will be checked: the abdera.properties
+   * file, the /META-INF/services directory, and the System properties.  If 
+   * no instance is configured, the default class name will be used.  Returns
+   * null if no instance can be created.
+   */
+  public static Object newInstance(String id, String _default, Abdera abdera, Object... args)
{
+    return locate(id, _default, abdera, args);
+  }
 
   /**
    * Utility method for returning an instance of the default Abdera XPath instance
@@ -125,6 +137,18 @@
       }
       return object;
   }
+  
+  public static Object locate(
+    String id, 
+    String _default, 
+    Abdera abdera,
+    Object... args) {
+      Object object = locate(id, abdera);
+      if (object == null && _default != null) {
+        object = locateInstance(getClassLoader(), _default, abdera, args);
+      }
+      return object;
+  }
 
   /**
    * Locate a class instance for the given id
@@ -155,6 +179,38 @@
   }
   
   @SuppressWarnings("unchecked")
+  private static Object _create(Class _class, Abdera abdera, Object... args) {
+    Class[] types = null;
+    Object[] values = null;
+    if (_class == null) return null;
+    try {
+      if (abdera != null) {
+        types = new Class[args.length + 1];
+        values = new Object[args.length + 1];
+        types[0] = Abdera.class;
+        values[0] = abdera;
+        for (int n = 0; n < args.length; n++) {
+          types[n+1] = args[n].getClass();
+          values[n+1] = args[n];
+        }
+        Constructor c = _class.getConstructor(types);
+        return c.newInstance(values);
+      }
+    } catch (Exception e) {
+      // Nothing
+    }
+    try {
+      types = new Class[args.length];
+      for (int n = 0; n < args.length; n++)
+        types[n] = args[n].getClass();
+      _class.getConstructor(types).newInstance(args);
+    } catch (Exception e) {
+      // Nothing
+    }
+    return null;
+  }
+  
+  @SuppressWarnings("unchecked")
   public static Object locateInstance(ClassLoader loader, String id, Abdera abdera) {
     try {
       Class _class = loader.loadClass(id);
@@ -165,6 +221,23 @@
     try {
       Class _class = ClassLoader.getSystemClassLoader().loadClass(id);
       return _create(_class, abdera);
+    } catch (Exception e) {
+      // Nothing
+    }
+    return null;
+  }
+  
+  @SuppressWarnings("unchecked")
+  public static Object locateInstance(ClassLoader loader, String id, Abdera abdera, Object...
args) {
+    try {
+      Class _class = loader.loadClass(id);
+      return _create(_class, abdera);
+    } catch (Exception e) {
+      // Nothing
+    }
+    try {
+      Class _class = ClassLoader.getSystemClassLoader().loadClass(id);
+      return _create(_class, abdera, args);
     } catch (Exception e) {
       // Nothing
     }

Modified: incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/appserver/SimpleTargetResolver.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/appserver/SimpleTargetResolver.java?view=diff&rev=517481&r1=517480&r2=517481
==============================================================================
--- incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/appserver/SimpleTargetResolver.java
(original)
+++ incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/appserver/SimpleTargetResolver.java
Mon Mar 12 17:48:23 2007
@@ -23,10 +23,11 @@
 public class SimpleTargetResolver 
   extends RegexTargetResolver {
 
-  public SimpleTargetResolver() {
-    setPattern("/appserver/atom(\\?[^#]*)?", TargetType.TYPE_SERVICE);
-    setPattern("/appserver/atom/feed(\\?[^#]*)?", TargetType.TYPE_COLLECTION);
-    setPattern("/appserver/atom/feed/([^/#?]+)(\\?[^#]*)?", TargetType.TYPE_ENTRY);
+  public SimpleTargetResolver(String contextPath) {
+    super(contextPath);
+    setPattern("/atom(\\?[^#]*)?", TargetType.TYPE_SERVICE);
+    setPattern("/atom/feed(\\?[^#]*)?", TargetType.TYPE_COLLECTION);
+    setPattern("/atom/feed/([^/#?]+)(\\?[^#]*)?", TargetType.TYPE_ENTRY);
   }
   
 }

Modified: incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/DefaultServiceContext.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/DefaultServiceContext.java?view=diff&rev=517481&r1=517480&r2=517481
==============================================================================
--- incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/DefaultServiceContext.java
(original)
+++ incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/DefaultServiceContext.java
Mon Mar 12 17:48:23 2007
@@ -44,6 +44,15 @@
     return obj;
   }
   
+  private Object instance(String id, String _default, Object... args) {
+    String instance = getProperty(id);
+    Object obj =  ServiceUtil.newInstance( id, (instance!=null)?instance:_default, abdera,
args);
+    if (logger.isDebugEnabled()) {
+      logger.debug("Returning " + obj + " as instance of '" + id + "'.");
+    }
+    return obj;
+  }
+  
   public synchronized ProviderManager getProviderManager() {
     if (providerManager == null) {
       providerManager = (ProviderManager) instance(
@@ -80,10 +89,11 @@
     return SimpleSubjectResolver.class.getName();
   }
 
-  public TargetResolver getTargetResolver() {
+  public TargetResolver getTargetResolver(String contextPath) {
     if (targetResolver == null) {
       targetResolver = (TargetResolver) instance(
-        TARGET_RESOLVER, getDefaultTargetResolver());
+        TARGET_RESOLVER, getDefaultTargetResolver(), 
+        contextPath);
     }
     return targetResolver;
   }

Modified: incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/ServiceContext.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/ServiceContext.java?view=diff&rev=517481&r1=517480&r2=517481
==============================================================================
--- incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/ServiceContext.java
(original)
+++ incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/ServiceContext.java
Mon Mar 12 17:48:23 2007
@@ -37,7 +37,7 @@
   
   SubjectResolver getSubjectResolver();
   
-  TargetResolver getTargetResolver();
+  TargetResolver getTargetResolver(String contextPath);
   
   String getProperty(String name);
   

Modified: incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/provider/AbstractProvider.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/provider/AbstractProvider.java?view=diff&rev=517481&r1=517480&r2=517481
==============================================================================
--- incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/provider/AbstractProvider.java
(original)
+++ incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/provider/AbstractProvider.java
Mon Mar 12 17:48:23 2007
@@ -32,6 +32,7 @@
 import org.apache.abdera.model.Element;
 import org.apache.abdera.model.Entry;
 import org.apache.abdera.model.ExtensibleElement;
+import org.apache.abdera.protocol.util.EncodingUtil;
 import org.apache.abdera.util.MimeTypeHelper;
 import org.apache.abdera.util.iri.IRI;
 
@@ -45,6 +46,10 @@
   private static final QName MESSAGE       = new QName(NS, "message", PFX);
   private static final QName TRACE         = new QName(NS, "trace", PFX);
   
+  protected boolean isDebug() {
+    return false;
+  }
+  
   protected Document createErrorDocument(
     Abdera abdera, 
     int code, 
@@ -55,10 +60,12 @@
         (ExtensibleElement) abdera.getFactory().newElement(ERROR, doc);
       root.addSimpleExtension(CODE, (code != -1) ? String.valueOf(code) : "");
       root.addSimpleExtension(MESSAGE, (message != null) ? message : "");
-      if (e != null) {
-        CharArrayWriter out = new CharArrayWriter();
-        e.printStackTrace(new PrintWriter(out));
-        root.addSimpleExtension(TRACE, out.toString());
+      if (isDebug()) {
+        if (e != null) {
+          CharArrayWriter out = new CharArrayWriter();
+          e.printStackTrace(new PrintWriter(out));
+          root.addSimpleExtension(TRACE, out.toString());
+        }
       }
       return doc;
   }
@@ -213,7 +220,7 @@
    */
   protected String sanitizeSlug(String slug) {
     if (slug == null) throw new IllegalArgumentException("Slug cannot be null");
-    return slug.replaceAll("[^a-zA-Z0-9]", "_");
+    return EncodingUtil.sanitize(slug,"",true,true);
   }
 
   protected abstract int getDefaultPageSize();

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=517481&r1=517480&r2=517481
==============================================================================
--- 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 Mar 12 17:48:23 2007
@@ -43,6 +43,9 @@
     setStatus(200);
     setStatusText("OK");
     this.chunked = chunked;
+    try {
+      setContentType(getContentType().toString());
+    } catch (Exception e) {}
   }
   
   public T getBase() {

Added: incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/provider/MediaResponseContext.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/provider/MediaResponseContext.java?view=auto&rev=517481
==============================================================================
--- incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/provider/MediaResponseContext.java
(added)
+++ incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/provider/MediaResponseContext.java
Mon Mar 12 17:48:23 2007
@@ -0,0 +1,89 @@
+/*
+* 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.provider;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.OutputStream;
+import java.io.Writer;
+import java.util.Date;
+
+/**
+ * ResponseContext implementation for arbitrary media resources
+ */
+public class MediaResponseContext
+  extends AbstractResponseContext {
+
+  private InputStream in;
+  
+  public MediaResponseContext(
+    InputStream in, 
+    Date lastmodified, 
+    int status) {
+      this.in = in;
+      this.status = status;
+      setLastModified(lastmodified);
+  }
+  
+  public boolean hasEntity() {
+    return in != null;
+  }
+
+  public void writeTo(
+    OutputStream out) 
+      throws IOException {
+    if (in != null) {
+      byte[] buf = new byte[100];
+      int r = -1;
+      while ((r = in.read(buf)) != -1) {
+        out.write(buf,0,r);
+        buf = new byte[100];
+      }
+    }
+  } 
+
+  public void writeTo(
+    Writer out) 
+      throws IOException {
+    if (in != null) {
+      InputStreamReader rdr = new InputStreamReader(in);
+      char[] buf = new char[100];
+      int r = -1;
+      while ((r = rdr.read(buf)) != -1) {
+        out.write(buf,0,r);
+        buf = new char[100];
+      }
+    }
+  }
+
+  public void writeTo(
+    OutputStream out, 
+    org.apache.abdera.writer.Writer writer) 
+      throws IOException {
+    throw new UnsupportedOperationException();
+  }
+
+  public void writeTo(
+    Writer javaWriter, 
+    org.apache.abdera.writer.Writer abderaWriter) 
+      throws IOException {
+    throw new UnsupportedOperationException();
+  }
+
+}

Modified: incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/provider/TargetResolver.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/provider/TargetResolver.java?view=diff&rev=517481&r1=517480&r2=517481
==============================================================================
--- incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/provider/TargetResolver.java
(original)
+++ incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/provider/TargetResolver.java
Mon Mar 12 17:48:23 2007
@@ -21,4 +21,6 @@
 
   Target resolve(RequestContext context);
   
+  void setContextPath(String contextPath);
+  
 }

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=517481&r1=517480&r2=517481
==============================================================================
--- 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
Mon Mar 12 17:48:23 2007
@@ -59,7 +59,9 @@
       subject = (subjectResolver != null)? 
         subjectResolver.resolve((Principal)getProperty(Property.PRINCIPAL)) : null;
       
-      TargetResolver targetResolver = context.getTargetResolver();
+      TargetResolver targetResolver = 
+        context.getTargetResolver(
+          request.getContextPath());
       target = (targetResolver != null) ? 
         targetResolver.resolve(this) : null;
   }

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=517481&r1=517480&r2=517481
==============================================================================
--- 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 Mar 12 17:48:23 2007
@@ -57,12 +57,15 @@
   implements TargetResolver {
 
   private final Map<Pattern, TargetType> patterns;
+  private String contextPath;
   
-  public RegexTargetResolver() {
+  public RegexTargetResolver(String contextPath) {
+    this.contextPath = contextPath;
     this.patterns = new HashMap<Pattern, TargetType>();
   }
   
-  public RegexTargetResolver(Map<String, TargetType> patterns) {
+  public RegexTargetResolver(String contextPath, Map<String, TargetType> patterns)
{
+    this.contextPath = contextPath;
     this.patterns = new HashMap<Pattern, TargetType>();
     for (String p : patterns.keySet()) {
       TargetType type = patterns.get(p);
@@ -72,7 +75,8 @@
   }
   
   public synchronized void setPattern(String pattern, TargetType type) {
-    Pattern p = Pattern.compile(pattern);
+    String root = getContextPath();
+    Pattern p = Pattern.compile(getContextPath() + pattern);
     this.patterns.put(p,type);
   }
   
@@ -174,6 +178,14 @@
       return buf.toString();
     }
     
+  }
+
+  public void setContextPath(String contextPath) {
+    this.contextPath = contextPath;
+  }
+  
+  protected String getContextPath() {
+    return this.contextPath;
   }
   
 }

Modified: incubator/abdera/java/trunk/server/src/test/java/org/apache/abdera/test/server/UtilityTest.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/server/src/test/java/org/apache/abdera/test/server/UtilityTest.java?view=diff&rev=517481&r1=517480&r2=517481
==============================================================================
--- incubator/abdera/java/trunk/server/src/test/java/org/apache/abdera/test/server/UtilityTest.java
(original)
+++ incubator/abdera/java/trunk/server/src/test/java/org/apache/abdera/test/server/UtilityTest.java
Mon Mar 12 17:48:23 2007
@@ -54,22 +54,19 @@
     assertTrue(etag2.isWeak());
     
     assertFalse(EntityTag.matches(etag1, etag2));
-    assertTrue(EntityTag.matches(etag1, etag2, true));
     
     EntityTag[] tags = EntityTag.parseTags("\"foo\", W/\"bar\"");
     assertTrue(EntityTag.matchesAny(etag1, tags));
     
-    assertTrue(EntityTag.matchesAny("\"bar\"", "\"foo\", W/\"bar\"", true));
-    
     assertEquals(etag1.toString(), "\"foo\"");
     assertEquals(etag2.toString(), "W/\"foo\"");
   }
   
   public static void testRegexTargetResolver() throws Exception {
     
-    RegexTargetResolver r = new RegexTargetResolver();
-    r.setPattern("/test", TargetType.TYPE_SERVICE);
-    r.setPattern("/test/([^/?#]+)", TargetType.TYPE_COLLECTION);
+    RegexTargetResolver r = new RegexTargetResolver("/test");
+    r.setPattern("", TargetType.TYPE_SERVICE);
+    r.setPattern("/([^/?#]+)", TargetType.TYPE_COLLECTION);
     
     DummyRequestContext drc = null;
     Target target = null;



Mime
View raw message