You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@avalon.apache.org by ni...@apache.org on 2004/07/26 16:11:01 UTC

svn commit: rev 30715 - in avalon/trunk/planet/facilities/http: api/src/main/org/apache/avalon/http impl/src/etc impl/src/main/org/apache/avalon/http/impl test/src/main/test/http

Author: niclas
Date: Mon Jul 26 07:10:59 2004
New Revision: 30715

Modified:
   avalon/trunk/planet/facilities/http/api/src/main/org/apache/avalon/http/HttpRequestHandler.java
   avalon/trunk/planet/facilities/http/api/src/main/org/apache/avalon/http/HttpRequestHandlerException.java
   avalon/trunk/planet/facilities/http/impl/src/etc/default-server.xml
   avalon/trunk/planet/facilities/http/impl/src/main/org/apache/avalon/http/impl/ModelHandler.java
   avalon/trunk/planet/facilities/http/impl/src/main/org/apache/avalon/http/impl/ResourceHandler.java
   avalon/trunk/planet/facilities/http/test/src/main/test/http/TestComponent.java
   avalon/trunk/planet/facilities/http/test/src/main/test/http/TestComponent.xprofile
Log:
ModelHandler first working cut in place.

Modified: avalon/trunk/planet/facilities/http/api/src/main/org/apache/avalon/http/HttpRequestHandler.java
==============================================================================
--- avalon/trunk/planet/facilities/http/api/src/main/org/apache/avalon/http/HttpRequestHandler.java	(original)
+++ avalon/trunk/planet/facilities/http/api/src/main/org/apache/avalon/http/HttpRequestHandler.java	Mon Jul 26 07:10:59 2004
@@ -19,8 +19,8 @@
 
 import java.io.IOException;
 
-import javax.servlet.ServletRequest;
-import javax.servlet.ServletResponse;
+import org.mortbay.http.HttpRequest;
+import org.mortbay.http.HttpResponse;
 
 /**
  * Defines methods that all http handlers must implement.
@@ -30,7 +30,6 @@
  * response generation.</p>
  *
  * @author <a href="mailto:dev@avalon.apache.org">Avalon Development Team</a>
- * @version $Revision: 1.1 $ $Date: 2004/04/04 15:00:56 $
  */
 public interface HttpRequestHandler
 {
@@ -51,6 +50,7 @@
      * @exception IOException if an input or output exception occurs
      *
      */
-    public void service( ServletRequest request, ServletResponse response )
+    public void handle( String pathInContext, String pathParms,
+                        HttpRequest request, HttpResponse response )
         throws HttpRequestHandlerException, IOException;
 }

Modified: avalon/trunk/planet/facilities/http/api/src/main/org/apache/avalon/http/HttpRequestHandlerException.java
==============================================================================
--- avalon/trunk/planet/facilities/http/api/src/main/org/apache/avalon/http/HttpRequestHandlerException.java	(original)
+++ avalon/trunk/planet/facilities/http/api/src/main/org/apache/avalon/http/HttpRequestHandlerException.java	Mon Jul 26 07:10:59 2004
@@ -23,7 +23,7 @@
  * @author <a href="mailto:dev@avalon.apache.org">Avalon Development Team</a>
  * @version $Revision: 1.1 $ $Date: 2004/04/04 15:00:56 $
  */
-public class HttpRequestHandlerException extends Exception
+public class HttpRequestHandlerException extends java.io.IOException
 {
 
      private final Throwable m_cause;

Modified: avalon/trunk/planet/facilities/http/impl/src/etc/default-server.xml
==============================================================================
--- avalon/trunk/planet/facilities/http/impl/src/etc/default-server.xml	(original)
+++ avalon/trunk/planet/facilities/http/impl/src/etc/default-server.xml	Mon Jul 26 07:10:59 2004
@@ -16,9 +16,16 @@
       <artifact>jar:avalon/logkit/avalon-logkit#2.0.0</artifact>
       <artifact>jar:avalon/http/avalon-http-impl#SNAPSHOT</artifact>
       <artifact>jar:commons-logging/commons-logging#SNAPSHOT</artifact>
+      <artifact>jar:avalon/http/avalon-http-test</artifact>
     </classpath>
   </classloader>
 
+<!-- Test components for the Facility  -->  
+  <component name="primary" class="test.http.TestComponent" profile="primary"/>
+
+  <component name="secondary" class="test.http.TestComponent" profile="secondary"/>
+
+<!-- Actual Facility  -->  
   <component name="socketlistener" class="org.apache.avalon.http.impl.SocketListenerComponent">
     <parameters>
       <parameter name="port" value="8088"/>
@@ -36,30 +43,41 @@
     </configuration>
   </component>
   
-  
-<!--  
   <component name="security-handler" class="org.apache.avalon.http.impl.SecurityHandler" >
     <parameters>
     </parameters>
   </component>
   
-  <component name="model-handler"    class="org.apache.avalon.http.impl.ModelHandler" >
+  <component name="model-handler-primary"    
+             class="org.apache.avalon.http.impl.ModelHandler" 
+  >
     <parameters>
+      <parameter name="context-path" value="/primary" />
+      <parameter name="target" value="/http/primary" />
     </parameters>
   </component>
--->  
+  
+  <component name="model-handler-secondary"
+             class="org.apache.avalon.http.impl.ModelHandler" 
+  >
+    <parameters>
+      <parameter name="context-path" value="/secondary" />
+      <parameter name="target" value="/http/secondary" />
+    </parameters>
+  </component>
+
   <component name="resource-handler" class="org.apache.avalon.http.impl.ResourceHandler" >
     <parameters>
       <parameter name="allow-directory" value="true" />
       <parameter name="allow-methods" value="GET" />
     </parameters>
   </component>
-<!--  
+
   <component name="notfound-handler" class="org.apache.avalon.http.impl.NotFoundHandler" >
     <parameters>
     </parameters>
   </component>
-
+<!--  
   <component name="errorpage-handler" class="org.apache.avalon.http.impl.ErrorPageHandler" >
     <parameters>
     </parameters>

Modified: avalon/trunk/planet/facilities/http/impl/src/main/org/apache/avalon/http/impl/ModelHandler.java
==============================================================================
--- avalon/trunk/planet/facilities/http/impl/src/main/org/apache/avalon/http/impl/ModelHandler.java	(original)
+++ avalon/trunk/planet/facilities/http/impl/src/main/org/apache/avalon/http/impl/ModelHandler.java	Mon Jul 26 07:10:59 2004
@@ -17,12 +17,15 @@
 
 package org.apache.avalon.http.impl;
 
+import java.io.IOException;
+
 import org.apache.avalon.composition.event.CompositionEvent;
 import org.apache.avalon.composition.event.CompositionListener;
 import org.apache.avalon.composition.model.ContainmentModel;
 import org.apache.avalon.composition.model.ComponentModel;
 import org.apache.avalon.composition.model.DeploymentModel;
 
+import org.apache.avalon.framework.activity.Initializable;
 import org.apache.avalon.framework.activity.Startable;
 
 import org.apache.avalon.framework.context.Context;
@@ -56,7 +59,7 @@
  */
 public class ModelHandler
     implements Serviceable, Parameterizable, Contextualizable, LogEnabled,
-               HttpHandler, CompositionListener, Startable
+               HttpHandler, CompositionListener, Startable, Initializable
 {
     private Logger              m_Logger;
     private ContainmentModel    m_Model;
@@ -64,6 +67,11 @@
     private String              m_Name;
     private boolean             m_Started;
     private int                 m_Index;
+    private String              m_ContextPath;
+    private String              m_ComponentPath;
+    
+    private HttpRequestHandler  m_HandlerComponent;
+    private ContainmentModel    m_Container;
         
     public ModelHandler()
     {
@@ -121,6 +129,10 @@
         throws ParameterException
     {
         m_Index = params.getParameterAsInteger( "handler-index", -1 );
+        
+        m_ContextPath = params.getParameter( "context-path", "/" );
+        
+        m_ComponentPath = params.getParameter( "target" );
     }
     
     /* HttpHandler interface */
@@ -137,23 +149,62 @@
 
     public void handle( String pathInContext, String pathParams, 
                         HttpRequest request, HttpResponse response ) 
-        throws HttpException
+        throws IOException
     {
-        getLogger().info( "Request: " + pathInContext + ", " + pathParams );
+        getLogger().info( "ModelHandler arg1: " + pathInContext + ", " + pathParams );
+        getLogger().info( "ModelHandler arg2: " + request.getPath() );
+        
+        getLogger().info( "ModelHandler ContextPath: " + m_ContextPath );
+        getLogger().info( "ModelHandler ComponentPath: " + m_ComponentPath );
+        getLogger().info( "ModelHandler Container: " + m_Container );
+        getLogger().info( "ModelHandler Component: " + m_HandlerComponent );
+        
+        if( pathInContext.startsWith( m_ContextPath ) )
+        {
+            pathInContext = pathInContext.substring( m_ContextPath.length() );
+            m_HandlerComponent.handle( pathInContext, pathParams, request, response );
+            request.setHandled( true );
+            response.getOutputStream().close();
+        }
     }
 
+    public void initialize()
+        throws Exception
+    {
+        ContainmentModel root = (ContainmentModel) m_Model;
+        
+        int pos = m_ComponentPath.lastIndexOf( "/" );
+        String containerName = m_ComponentPath.substring( 0, pos );
+        if( "".equals( containerName ) )
+            containerName = "/";
+        String componentName = m_ComponentPath.substring( pos + 1 );
+        
+        m_Container = (ContainmentModel) root.getModel( containerName );
+        ComponentModel component = (ComponentModel) m_Container.getModel( componentName );
+
+        m_Container.addCompositionListener( this );              
+        m_HandlerComponent = (HttpRequestHandler) component.resolve();
+    }
+    
+    public void dispose()
+    {
+        m_Container.removeCompositionListener( this );              
+    }
+    
+    /* Jetty LifeCycle interface */
+    
     public void initialize( HttpContext context )
     {
         m_Logger.warn( "unhandled:  initialize( " + context + " );" );
     }
 
-    /* Jetty LifeCycle interface */
-    
     public boolean isStarted()
     {
         return m_Started;
     }
     
+    /* Combined Avalon and Jetty LifeCycle interface */
+    
     public void start()
     {
         if( m_Logger.isDebugEnabled() )
@@ -180,11 +231,32 @@
     */
     public void modelAdded( CompositionEvent event )
     {
-        DeploymentModel model = event.getChild();
-        if( ! ( model instanceof HttpRequestHandler ) )
+        if( m_Logger.isDebugEnabled() )
+            m_Logger.debug( "modelRemoved( " + event + " );" );
+        
+        DeploymentModel dmodel = event.getChild();
+        
+        if( ! ( dmodel instanceof ComponentModel ) )
+            return;
+        ComponentModel cmodel = (ComponentModel) dmodel;
+        
+        String path = cmodel.getPath();
+        if( ! path.equals( m_ComponentPath ) )
             return;
             
-        // TODO:
+        if( m_HandlerComponent != null )
+        {
+            getLogger().warn( "Internal error. New component added at the same path, without a modelRemoved() event: " + path );
+        }
+        
+        try
+        {
+            m_HandlerComponent = (HttpRequestHandler) cmodel.resolve();
+            getLogger().info( "HttpRequestHandler added: " + path );
+        } catch( Exception e )
+        {
+            getLogger().error( "Unable to resolve " + path, e );
+        }
     }
 
    /**
@@ -192,10 +264,21 @@
     */
     public void modelRemoved( CompositionEvent event )
     {
-        DeploymentModel model = event.getChild();
-        if( ! ( model instanceof HttpRequestHandler ) )
-            return;
+        if( m_Logger.isDebugEnabled() )
+            m_Logger.debug( "modelRemoved( " + event + " );" );
             
-        // TODO:
+        if( m_HandlerComponent == null)  // No model bound to this handler.
+            return;    
+        
+        DeploymentModel dmodel = event.getChild();
+        if( ! ( dmodel instanceof ComponentModel ) )
+            return;
+        
+        String path = dmodel.getPath();
+        if( path.equals( m_ComponentPath ) )
+        {
+            m_HandlerComponent = null;
+            getLogger().info( "HttpRequestHandler removed: " + path );
+        }
     }
 } 

Modified: avalon/trunk/planet/facilities/http/impl/src/main/org/apache/avalon/http/impl/ResourceHandler.java
==============================================================================
--- avalon/trunk/planet/facilities/http/impl/src/main/org/apache/avalon/http/impl/ResourceHandler.java	(original)
+++ avalon/trunk/planet/facilities/http/impl/src/main/org/apache/avalon/http/impl/ResourceHandler.java	Mon Jul 26 07:10:59 2004
@@ -148,23 +148,4 @@
         super.stop();
         m_Context.removeHandler( this );
     }
-   
-    public void handle( String pathInContext, String pathParams, 
-                        HttpRequest request, HttpResponse response ) 
-        throws HttpException, IOException
-    {
-        getLogger().info( "(ResourceHandler): " + pathInContext + ", " + pathParams );
-        super.handle( pathInContext, pathParams, request, response );
-    }
-
-    public void handleGet( HttpRequest request, HttpResponse response,
-                           String pathInContext, String pathParams,
-                           Resource resource 
-                         ) 
-        throws IOException
-    {
-        getLogger().info( "(ResourceHandler)/GET: " + pathInContext + ", " + pathParams + ", " + resource);
-        super.handleGet( request, response, pathInContext, pathParams, resource );
-    }
-
 }

Modified: avalon/trunk/planet/facilities/http/test/src/main/test/http/TestComponent.java
==============================================================================
--- avalon/trunk/planet/facilities/http/test/src/main/test/http/TestComponent.java	(original)
+++ avalon/trunk/planet/facilities/http/test/src/main/test/http/TestComponent.java	Mon Jul 26 07:10:59 2004
@@ -18,6 +18,8 @@
 package test.http;
 
 import java.io.IOException;
+import java.io.OutputStream;
+import java.io.OutputStreamWriter;
 import java.io.PrintWriter;
 import java.util.Date;
 
@@ -30,24 +32,21 @@
 import org.apache.avalon.framework.configuration.ConfigurationException;
 import org.apache.avalon.framework.configuration.Configurable;
 
-import javax.servlet.ServletException;
-import javax.servlet.http.HttpServlet;
-import javax.servlet.http.HttpServletRequest;
-import javax.servlet.http.HttpServletResponse;
+import org.apache.avalon.http.HttpRequestHandler;
 
-import org.apache.avalon.http.HttpRequestHandlerException;
-import org.apache.avalon.http.util.AbstractHttpRequestHandler;
+import org.mortbay.http.HttpRequest;
+import org.mortbay.http.HttpResponse;
 
 /**
- * HTTP Handler component that receives and processes http service 
- * requests.
+ * HTTP Handler component that receives and processes http requests.
  * 
  * @avalon.component name="test" lifestyle="thread"
  * @avalon.service type="org.apache.avalon.http.HttpRequestHandler"
+ *
  * @author <a href="mailto:dev@avalon.apache.org">Avalon Development Team</a>
  */
-public class TestComponent extends AbstractHttpRequestHandler 
-    implements LogEnabled, Serviceable, Configurable
+public class TestComponent
+    implements LogEnabled, Serviceable, Configurable, HttpRequestHandler
 {
     //----------------------------------------------------------
     // state
@@ -97,31 +96,22 @@
         m_name = config.getChild( "name" ).getValue();
     }
 
-    //----------------------------------------------------------
-    // Handler
-    //----------------------------------------------------------
-
-    /**
-     * Respond to a GET request for the content produced by
-     * this servlet.  This method should be overidden in a
-     *
-     * @param request The servlet request we are processing
-     * @param response The servlet response we are producing
-     *
-     * @exception IOException if an input/output error occurs
-     * @exception ServletException if a servlet error occurs
-     */
-    public void doGet(HttpServletRequest request,
-                      HttpServletResponse response)
-      throws IOException, HttpRequestHandlerException {
 
+    public void handle( String path, String params, 
+                        HttpRequest request, HttpResponse response )
+        throws IOException
+    {
+        m_logger.debug( "TestComponent - Handling HTTP: " + path );
+        
         int count = m_counter.increment();
         
         m_count++;
 
         response.setContentType("text/html");
-        PrintWriter writer = response.getWriter();
-        String context = request.getContextPath();
+        OutputStream out = response.getOutputStream();
+        OutputStreamWriter osw = new OutputStreamWriter( out, "ISO8859-1" );
+        PrintWriter writer = new PrintWriter( osw );
+        
         writer.println("<html>");
         writer.println("<head>");
         writer.println("<title>" + m_name + "</title>" );
@@ -143,5 +133,6 @@
         writer.println("<hr/>");
         writer.println("</body>");
         writer.println("</html>");
+        writer.flush();
     }
 }

Modified: avalon/trunk/planet/facilities/http/test/src/main/test/http/TestComponent.xprofile
==============================================================================
--- avalon/trunk/planet/facilities/http/test/src/main/test/http/TestComponent.xprofile	(original)
+++ avalon/trunk/planet/facilities/http/test/src/main/test/http/TestComponent.xprofile	Mon Jul 26 07:10:59 2004
@@ -1,19 +1,19 @@
-<profiles>
-
-  <profile name="primary">
-    <configuration>
-      <name>Primary</name>
-      <message>I'm an avalon component.</message>
-      <link>/test/secondary</link>
-    </configuration>
-  </profile>
-
-  <profile name="secondary">
-    <configuration>
-      <name>Secondary</name>
-      <message>I'm an avalon component too!</message>
-      <link>/test/primary</link>
-    </configuration>
-  </profile>
-
-</profiles>
\ No newline at end of file
+<profiles>
+
+  <profile name="primary">
+    <configuration>
+      <name>Primary</name>
+      <message>I'm an avalon component.</message>
+      <link>/niclas/testing/secondary</link>
+    </configuration>
+  </profile>
+
+  <profile name="secondary">
+    <configuration>
+      <name>Secondary</name>
+      <message>I'm an avalon component too!</message>
+      <link>/niclas/testing/primary</link>
+    </configuration>
+  </profile>
+
+</profiles>

---------------------------------------------------------------------
To unsubscribe, e-mail: cvs-unsubscribe@avalon.apache.org
For additional commands, e-mail: cvs-help@avalon.apache.org