Package com.rimfaxe.webserver

Source Code of com.rimfaxe.webserver.Handler

/*
* Handler.java
*
*
* Copyright (c) 2003 Rimfaxe ApS  (www.rimfaxe.com). 
* All rights reserved.
*
* This package is written by Lars Andersen <lars@rimfaxe.com>
* and licensed by Rimfaxe ApS.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
*    notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
*    notice, this list of conditions and the following disclaimer in
*    the documentation and/or other materials provided with the
*    distribution.
*
* 3. The end-user documentation included with the redistribution, if
*    any, must include the following acknowlegement:
*       "This product includes software developed by Rimfaxe ApS
          (www.rimfaxe.com)"
*    Alternately, this acknowlegement may appear in the software itself,
*    if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "Rimfaxe", "Rimfaxe Software", "Lars Andersen" and
*    "Rimfaxe WebServer" must not be used to endorse or promote products
*    derived from this software without prior written permission. For written
*    permission, please contact info@rimfaxe.com
*
* 5. Products derived from this software may not be called "Rimfaxe"
*    nor may "Rimfaxe" appear in their names without prior written
*    permission of the Rimfaxe ApS.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
*/

package com.rimfaxe.webserver;

import seda.sandStorm.core.*;
import seda.sandStorm.lib.http.httpRequest;

import com.rimfaxe.webserver.seda.SedaHttpResponse;
import com.rimfaxe.webserver.webapp.*;
import com.rimfaxe.webserver.servletapi.RimfaxeFilterChain;

import java.util.*;

/**
*
* @author  Lars Andersen
*/
public class Handler
{
  com.rimfaxe.webserver.servletapi.ServletFactory factory = com.rimfaxe.webserver.servletapi.ServletFactory.getInstance();
  Configuration conf = null
  long age;
 
  StringBuffer error_log = null;
 
  /** Creates a new instance of Handler */
  public Handler(Configuration conf)
  {
    this.age = System.currentTimeMillis()
    this.conf = conf; 
  }
 
  public void setErrorLog(StringBuffer buf)
  {
    this.error_log = buf;  
  }
 
  public VirtualHost lookupVirtualHost(httpRequest req)
  {
   
    String urlpath = req.getURL()
    String host = req.getHeader("Host");
    String port = ""+req.getConnection().getConnection().getServerSocket().getLocalPort();
    String hostname = host;
   
    if (host==null) host="default:"+port;
   
    if (error_log!=null) error_log.append("Handler, lookupVirtualHost, host="+host+"\n");
   
   
    int colon = host.indexOf(':');
     
    if (colon>0)
    {
      hostname = host.substring(0,colon)
      port = host.substring(colon+1);
    }
    else
    {
      hostname = host; 
      String tmp = "http";
      if (tmp.equalsIgnoreCase("http")) port = "80";
      if (tmp.equalsIgnoreCase("https")) port = "443";
    }
   
    VirtualHost vhost = conf.getVirtualHost( hostname.toString().trim()+":"+port );
   
    if (vhost==null)
    {   
      port = ""+req.getConnection().getConnection().getServerSocket().getLocalPort();
     
      vhost = conf.getVirtualHost("default:"+port)
    }
  
    if (vhost==null)
    {
      if (error_log!=null) error_log.append("Handler, no vhost??\n")
      
      return null;
   
    return vhost;
  }
 
  public boolean isCacheable(httpRequest req)
  {
 
    if (req.getURL().startsWith("/RWS_ADMIN")) return false;
    if (!req.getMethod().equalsIgnoreCase("GET")) return false;
    if (req.hasHeader("If-Modified-Since")) return false;
    if (req.hasHeader("If-Range")) return false;
   
    String urlpath = req.getURL();    
    VirtualHost vhost = lookupVirtualHost(req);
    
    if (vhost!=null)
    {
       WebContext webcontext = vhost.getWebContext(urlpath);
      
       req.setSpecial("ROOT_DIR", webcontext.getRoot());
       req.setSpecial("WEBAPP_ROOT", webcontext.getUrlpathPrefix());
      
      
       return webcontext.isStatic(req);     
    }
    else
    {
      return false;
    }
     
  }

  public SedaHttpResponse perform(httpRequest req)
  {
   
   
    String urlpath = req.getURL()
   
    //if (urlpath.startsWith("/servlet"))
    //{
    //   System.out.println("Request dump \n"+req);
    //}
   
    VirtualHost vhost = lookupVirtualHost(req)
   
   
   
    if (vhost!=null)
    {
       
      
      
       WebContext webcontext = vhost.getWebContext(urlpath);
      
      
       req.setURL( webcontext.processUrlPath(req.getURL()) );
      
    
       webcontext.getWebApp().reload();
      
      
       if (vhost.getDebug())
       {
         com.rimfaxe.webserver.DebugLog.log("Handler", 192, "Debug messages",
                                            ""+req+"\n"+error_log+"\n");
       }
      
       return getResponse(req,vhost,webcontext);
    }
    else
    {
       if (error_log!=null) error_log.append("Handler, no virtualhost defined?\n");      
       return null;
    }
   
  }
 
  public SedaHttpResponse getResponse(httpRequest req, VirtualHost vh, WebContext webcontext)
  {
  
       
    String urlpath = req.getURL();
     
    
   
   
   
    RimfaxeFilterChain rfc = new RimfaxeFilterChain(req, vh, webcontext, conf, factory)
   
  
  
   
    SedaHttpResponse response = null;
    try
   
      
      response = rfc.runservice();
    }
    catch (javax.servlet.ServletException se)
    {
      // TODO
      if (error_log!=null) error_log.append("Handler, ServletEception\n");  
    }
    catch (java.io.IOException ioe)
    {
      // TODO
      if (error_log!=null) error_log.append("Handler, IOException\n");    
    }
   
   
   
  
    return response;
  }
 
 
  public SedaHttpResponse callServlet(String name, httpRequest req, VirtualHost vh, WebContext webcontext)
  {
   
   
    String urlpath = req.getURL();
     
    Servlet s = webcontext.getWebApp().getServlet(name);
    if (s==null) s = conf.getWebApp().getServlet(name);
   
    String cls = s.getClassName();
   
  
   
    com.rimfaxe.webserver.servletapi.ServletWrapper swrap = factory.getWrapper(vh, webcontext, name,cls,urlpath,s);
   
    SedaHttpResponse response = null;
    try
    {
     
      response = swrap.runservice(req,webcontext);
    }
    catch (javax.servlet.ServletException se)
    {
      se.printStackTrace(System.out)
    }
    catch (java.io.IOException ioe)
    {
      ioe.printStackTrace(System.out)
    }
   
   
    return response;
  }
 
  public long getAge()
  {
    return System.currentTimeMillis()-age;
  }

  public void reset() { }
}
TOP

Related Classes of com.rimfaxe.webserver.Handler

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.