Package net.jr.fastcgi.spring

Source Code of net.jr.fastcgi.spring.RequestHandler

/*
* (c) 2009 Julien Rialland, and the jFastCGI project developpers.
*
* Released under BSD License (see license.txt)
*   $Id$
*/
package net.jr.fastcgi.spring;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.jr.fastcgi.ConnectionFactory;
import net.jr.fastcgi.impl.FastCGIHandler;
import net.jr.fastcgi.impl.ServletRequestAdapter;
import net.jr.fastcgi.impl.ServletResponseAdapter;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.util.Assert;
import org.springframework.web.HttpRequestHandler;

/**
* Sample configuration :
*
* <h3>web.xml</h3>
* <pre>  &lt;servlet&gt;
        &lt;servlet-name&gt;jfastcgi&lt;/servlet-name&gt;
        &lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet&lt;/servlet-class&gt;
        &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
    &lt;/servlet&gt;

    &lt;servlet-mapping&gt;
        &lt;servlet-name&gt;jfastcgi&lt;/servlet-name&gt;
        &lt;url-pattern&gt;*.php&lt;/url-pattern&gt;
    &lt;/servlet-mapping&gt;</pre>
*
* <h3>Spring xml configuration</h3>
* <pre>  &lt;bean class=&quot;org.springframework.web.servlet.handler.SimpleUrlHandlerMapping&quot;&gt;
        &lt;property name=&quot;mappings&quot;&gt;
            &lt;value&gt;
                /&amp;#42;/&amp;#42;.php=fastCGIRequestHandler
            &lt;/value&gt;
        &lt;/property&gt;
    &lt;/bean&gt;
   
    &lt;bean id=&quot;fastCGIRequestHandler&quot; class=&quot;net.jr.fastcgi.spring.RequestHandler&quot;&gt;
      &lt;property name=&quot;connectionFactory&quot; ref=&quot;connectionFactory&quot; /&gt;
    &lt;/bean&gt;
   
    &lt;bean id=&quot;connectionFactory&quot; class=&quot;net.jr.fastcgi.impl.SingleConnectionFactory&quot;&gt;
       &lt;constructor-arg value=&quot;localhost:9763&quot;/&gt;
    &lt;/bean&gt;
</pre>
* @author julien
*
*/
public class RequestHandler implements HttpRequestHandler, InitializingBean {

  private ConnectionFactory connectionFactory = null;
 
  private FastCGIHandler fastCGIHandler = null;
 
  public void handleRequest(HttpServletRequest request,
      HttpServletResponse response) throws ServletException, IOException {

    ServletRequestAdapter requestAdapter = new ServletRequestAdapter(
        request);
    ServletResponseAdapter responseAdapter = new ServletResponseAdapter(
        response);
    getFastCGIHandler().service(requestAdapter, responseAdapter);
  }

  public void afterPropertiesSet() throws Exception {
    Assert.isTrue(getFastCGIHandler() != null,"connectionFactory or fastCgiHandler property should be set.");
  }

  public FastCGIHandler getFastCGIHandler() {
    return fastCGIHandler;
  }

  public void setFastCGIHandler(FastCGIHandler fastCGIHandler) {
    this.fastCGIHandler = fastCGIHandler;
  }

  public ConnectionFactory getConnectionFactory() {
    return connectionFactory;
  }

  public void setConnectionFactory(ConnectionFactory connectionFactory) {
    this.connectionFactory = connectionFactory;
    if(getFastCGIHandler() == null)
    {
      setFastCGIHandler(new FastCGIHandler());
    }
    getFastCGIHandler().setConnectionFactory(connectionFactory);
  }

}
TOP

Related Classes of net.jr.fastcgi.spring.RequestHandler

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.