abdera-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jmsn...@apache.org
Subject svn commit: r571820 - in /incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server: ./ impl/ servlet/
Date Sat, 01 Sep 2007 16:48:28 GMT
Author: jmsnell
Date: Sat Sep  1 09:48:27 2007
New Revision: 571820

URL: http://svn.apache.org/viewvc?rev=571820&view=rev
Log:
Applying Dan Diephouse's HttpResponse patch. Thanks!

Added:
    incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/HttpResponse.java
    incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/servlet/HttpResponseServletAdapter.java
Modified:
    incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/RequestHandler.java
    incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/impl/AbstractRequestHandler.java
    incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/impl/DefaultServiceContext.java
    incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/servlet/AbderaServlet.java

Added: incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/HttpResponse.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/HttpResponse.java?rev=571820&view=auto
==============================================================================
--- incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/HttpResponse.java
(added)
+++ incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/HttpResponse.java
Sat Sep  1 09:48:27 2007
@@ -0,0 +1,18 @@
+package org.apache.abdera.protocol.server;
+
+import java.io.IOException;
+import java.io.OutputStream;
+
+public interface HttpResponse {
+
+  void setStatus(int status);
+
+  void setHeader(String name, String value);
+
+  void setContentType(String contentType);
+
+  void setDateHeader(String key, long time);
+
+  OutputStream getOutputStream() throws IOException;
+
+}

Modified: incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/RequestHandler.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/RequestHandler.java?rev=571820&r1=571819&r2=571820&view=diff
==============================================================================
--- incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/RequestHandler.java
(original)
+++ incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/RequestHandler.java
Sat Sep  1 09:48:27 2007
@@ -19,7 +19,6 @@
 
 import java.io.IOException;
 
-import javax.servlet.http.HttpServletResponse;
 
 
 public interface RequestHandler {
@@ -27,6 +26,6 @@
   void process(
     ServiceContext context, 
     RequestContext request,
-    HttpServletResponse response) throws IOException;
+    HttpResponse output) throws IOException;
   
 }

Modified: incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/impl/AbstractRequestHandler.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/impl/AbstractRequestHandler.java?rev=571820&r1=571819&r2=571820&view=diff
==============================================================================
--- incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/impl/AbstractRequestHandler.java
(original)
+++ incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/impl/AbstractRequestHandler.java
Sat Sep  1 09:48:27 2007
@@ -19,6 +19,8 @@
 
 import java.io.IOException;
 import java.io.OutputStream;
+import java.io.OutputStreamWriter;
+import java.io.UnsupportedEncodingException;
 import java.util.Date;
 import java.util.List;
 import java.util.Map;
@@ -28,6 +30,7 @@
 import javax.servlet.http.HttpServletResponse;
 
 import org.apache.abdera.protocol.ItemManager;
+import org.apache.abdera.protocol.server.HttpResponse;
 import org.apache.abdera.protocol.server.Provider;
 import org.apache.abdera.protocol.server.RequestContext;
 import org.apache.abdera.protocol.server.RequestHandler;
@@ -46,7 +49,7 @@
   public void process(
     ServiceContext context, 
     RequestContext request,
-    HttpServletResponse response) 
+    HttpResponse response) 
       throws IOException {
     
     log.debug(Messages.get("PROCESSING.REQUEST"));
@@ -55,15 +58,15 @@
     log.debug(Messages.format("USING.PROVIDER",provider));
     try {
       if (preconditions(provider, request, response)) {
-        output(request, response,provider.request(request));
+        response(request, response,provider.request(request));
       }
     } catch (Throwable e) {
       log.error(Messages.get("OUTPUT.ERROR"), e);
       try {
-        output(request,response,new EmptyResponseContext(500));
+        response(request,response,new EmptyResponseContext(500));
       } catch (Exception ex) {
         log.error(Messages.get("OUTPUT.ERROR"), ex);
-        response.sendError(500);
+        internalServerError(request, response);
       }
     } finally {
       log.debug(Messages.format("RELEASING.PROVIDER", provider));
@@ -74,22 +77,23 @@
   protected boolean preconditions(
     Provider provider, 
     RequestContext request, 
-    HttpServletResponse response)
+    HttpResponse response)
       throws IOException {
     // Check The Provider    
     if (provider == null) { 
-      noprovider(response); 
+      noprovider(request, response); 
       return false;
     }
     // Check The Target
     Target target = request.getTarget();
     if (target == null) { 
-      notfound(response); 
+      notfound(request, response); 
       return false;
     }
     // Check The Method
     if (!checkMethod(provider,request)) {
       notallowed(
+        request, 
         response, 
         request.getMethod(), 
         provider.getAllowedMethods(target.getType()));
@@ -98,9 +102,9 @@
     return true;
   }
   
-  protected void output(
+  protected void response(
     RequestContext request,
-    HttpServletResponse response, 
+    HttpResponse response, 
     ResponseContext context) 
       throws IOException, ServletException {
     if (context != null) {
@@ -133,9 +137,28 @@
         out.close();
       }  
     } else {
-      response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
+      internalServerError(request, response);
     }
   }
+
+  private void internalServerError(RequestContext request, HttpResponse response)
+    throws UnsupportedEncodingException, IOException {
+    sendError(request, response, HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
+              "There was an error fulfilling your request.");
+  }
+
+  private void sendError(RequestContext request, 
+                         HttpResponse response, int code, 
+                         String message) throws UnsupportedEncodingException,
+    IOException {
+    response.setStatus(code);
+    OutputStream out = response.getOutputStream();
+    OutputStreamWriter writer = new OutputStreamWriter(out, request.getAcceptCharset());
+    // TODO: should we wrap this in some nice HTML?
+    writer.write(message);
+    writer.close();
+    out.close();
+  }
   
   protected boolean checkMethod(
     Provider provider,
@@ -148,20 +171,22 @@
     return (java.util.Arrays.binarySearch(methods, method) >= 0);
   }
     
-  protected void noprovider(HttpServletResponse response) throws IOException {
-    response.sendError(500, Messages.get("NO.PROVIDER"));
+  protected void noprovider(RequestContext request, HttpResponse response) throws IOException
{
+    sendError(request, response, 500, Messages.get("NO.PROVIDER"));
   }
   
-  protected void notfound(HttpServletResponse response) throws IOException {
-    response.sendError(404, Messages.get("NOT.FOUND"));
+  protected void notfound(RequestContext request, HttpResponse response) throws IOException
{
+    sendError(request, response, 404,
+              Messages.get("NOT.FOUND"));
   }
   
   protected void notallowed(
-    HttpServletResponse response,
+    RequestContext request, 
+    HttpResponse response,
     String method, 
     String[] methods) 
       throws IOException {
-    response.sendError(405, Messages.format("METHOD.NOT.ALLOWED", method));
+    sendError(request, response, 405, Messages.format("METHOD.NOT.ALLOWED", method));
     response.setHeader("Allow", combine(methods));;
   }
   

Modified: incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/impl/DefaultServiceContext.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/impl/DefaultServiceContext.java?rev=571820&r1=571819&r2=571820&view=diff
==============================================================================
--- incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/impl/DefaultServiceContext.java
(original)
+++ incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/impl/DefaultServiceContext.java
Sat Sep  1 09:48:27 2007
@@ -50,13 +50,6 @@
     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);
-    logger.debug(Messages.format("RETURNING.INSTANCE", obj, id));
-    return obj;
-  }
-  
   @SuppressWarnings("unchecked")
   public synchronized ItemManager<Provider> getProviderManager() {
     if (providerManager == null) {

Modified: incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/servlet/AbderaServlet.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/servlet/AbderaServlet.java?rev=571820&r1=571819&r2=571820&view=diff
==============================================================================
--- incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/servlet/AbderaServlet.java
(original)
+++ incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/servlet/AbderaServlet.java
Sat Sep  1 09:48:27 2007
@@ -96,7 +96,7 @@
     RequestHandler handler = manager.get(reqcontext);
     log.debug("Handler - " + handler);
     try {
-      handler.process(context, reqcontext, response);
+      handler.process(context, reqcontext, new HttpResponseServletAdapter(response));
     } catch (Throwable t) {
       error("Error servicing request", t, response);
       return;

Added: incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/servlet/HttpResponseServletAdapter.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/servlet/HttpResponseServletAdapter.java?rev=571820&view=auto
==============================================================================
--- incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/servlet/HttpResponseServletAdapter.java
(added)
+++ incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/servlet/HttpResponseServletAdapter.java
Sat Sep  1 09:48:27 2007
@@ -0,0 +1,38 @@
+package org.apache.abdera.protocol.server.servlet;
+
+import java.io.IOException;
+import java.io.OutputStream;
+
+import javax.servlet.http.HttpServletResponse;
+
+import org.apache.abdera.protocol.server.HttpResponse;
+
+public class HttpResponseServletAdapter implements HttpResponse {
+
+  private HttpServletResponse response;
+
+  public HttpResponseServletAdapter(HttpServletResponse response) {
+    this.response = response;
+  }
+
+  public OutputStream getOutputStream() throws IOException {
+    return response.getOutputStream();
+  }
+
+  public void setContentType(String contentType) {
+    response.setContentType(contentType);
+  }
+
+  public void setDateHeader(String key, long time) {
+    response.setDateHeader(key, time);
+  }
+
+  public void setHeader(String name, String value) {
+    response.setHeader(name, value);
+  }
+
+  public void setStatus(int status) {
+    response.setStatus(status);
+  }
+
+}



Mime
View raw message