Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix #53: Allow to use jruby-rack in embedded environments
Signed-off-by: David Calavera <david.calavera@gmail.com>
- Loading branch information
1 parent
ec660bb
commit 9d2899c
Showing
28 changed files
with
814 additions
and
367 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package org.jruby.rack; | ||
|
||
import java.io.IOException; | ||
|
||
import javax.servlet.Filter; | ||
import javax.servlet.FilterChain; | ||
import javax.servlet.ServletException; | ||
import javax.servlet.ServletRequest; | ||
import javax.servlet.ServletResponse; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
|
||
import org.jruby.rack.servlet.ServletRackEnvironment; | ||
import org.jruby.rack.servlet.ServletRackResponseEnvironment; | ||
|
||
public abstract class AbstractFilter implements Filter { | ||
|
||
public final void doFilter(ServletRequest req, ServletResponse resp, | ||
FilterChain chain) throws IOException, ServletException { | ||
|
||
HttpServletRequest httpReq = (HttpServletRequest) req; | ||
HttpServletResponse httpResp = (HttpServletResponse) resp; | ||
|
||
RackEnvironment env = new ServletRackEnvironment(httpReq, httpResp, getContext()); | ||
RackResponseEnvironment respEnv = new ServletRackResponseEnvironment(httpResp); | ||
|
||
if (isDoDispatch(httpReq, httpResp, chain, env, respEnv)) { | ||
getDispatcher().process(env, respEnv); | ||
} | ||
|
||
} | ||
|
||
/** | ||
* Some filters may want to by-pass the rack application. By default, all | ||
* requests are given to the {@link RackDispatcher}, but you can extend | ||
* this method and return false if you want to signal that you don't want | ||
* the {@link RackDispatcher} to see the request. | ||
* @return true if the dispatcher should handle the request, false if it | ||
* shouldn't. | ||
* @throws IOException | ||
* @throws ServletException | ||
*/ | ||
protected boolean isDoDispatch(HttpServletRequest req, HttpServletResponse resp, | ||
FilterChain chain, RackEnvironment env, RackResponseEnvironment respEnv) throws IOException, ServletException { | ||
return true; | ||
} | ||
|
||
protected abstract RackContext getContext(); | ||
protected abstract RackDispatcher getDispatcher(); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
* Copyright (c) 2010-2011 Engine Yard, Inc. | ||
* Copyright (c) 2007-2009 Sun Microsystems, Inc. | ||
* This source code is available under the MIT license. | ||
* See the file LICENSE.txt for details. | ||
*/ | ||
|
||
package org.jruby.rack; | ||
|
||
import java.io.IOException; | ||
|
||
/** | ||
* | ||
* @author nicksieger | ||
*/ | ||
public abstract class AbstractRackDispatcher implements RackDispatcher { | ||
private RackContext context; | ||
|
||
public AbstractRackDispatcher(RackContext rackContext) { | ||
this.context = rackContext; | ||
} | ||
|
||
public void process(RackEnvironment request, RackResponseEnvironment response) | ||
throws IOException { | ||
|
||
RackApplication app = null; | ||
try { | ||
app = getApplication(this.context); | ||
app.call(request).respond(response); | ||
} catch (Exception re) { | ||
handleException(re, request, response); | ||
} finally { | ||
this.afterProcess(app); | ||
} | ||
} | ||
|
||
private void handleException(Exception re, RackEnvironment request, | ||
RackResponseEnvironment response) | ||
throws IOException { | ||
if (response.isCommitted()) { | ||
context.log("Error: Couldn't handle error: response committed", re); | ||
return; | ||
} | ||
response.reset(); | ||
context.log("Application Error", re); | ||
|
||
afterException(request, re, response); | ||
} | ||
|
||
abstract protected void afterProcess(RackApplication app) throws IOException; | ||
abstract protected RackApplication getApplication(RackContext context) throws RackInitializationException; | ||
protected abstract void afterException(RackEnvironment request, Exception re, | ||
RackResponseEnvironment response) throws IOException; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* | ||
* Copyright (c) 2010-2011 Engine Yard, Inc. | ||
* Copyright (c) 2007-2009 Sun Microsystems, Inc. | ||
* This source code is available under the MIT license. | ||
* See the file LICENSE.txt for details. | ||
*/ | ||
|
||
package org.jruby.rack; | ||
|
||
import org.jruby.rack.servlet.ServletRackEnvironment; | ||
import org.jruby.rack.servlet.ServletRackResponseEnvironment; | ||
|
||
import javax.servlet.ServletConfig; | ||
import javax.servlet.ServletException; | ||
import javax.servlet.ServletRequest; | ||
import javax.servlet.ServletResponse; | ||
import javax.servlet.http.HttpServlet; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
import java.io.IOException; | ||
|
||
/** | ||
* | ||
* @author nicksieger | ||
*/ | ||
public abstract class AbstractServlet extends HttpServlet { | ||
|
||
/** Default ctor, used by servlet container */ | ||
public AbstractServlet() { | ||
} | ||
|
||
@Override | ||
public void service(HttpServletRequest request, HttpServletResponse response) | ||
throws ServletException, IOException { | ||
getDispatcher().process(new ServletRackEnvironment((HttpServletRequest) request, (HttpServletResponse) response, getContext()), | ||
new ServletRackResponseEnvironment(response)); | ||
} | ||
|
||
@Override | ||
public void service(ServletRequest request, ServletResponse response) | ||
throws ServletException, IOException { | ||
service((HttpServletRequest) request, (HttpServletResponse) response); | ||
} | ||
|
||
@Override | ||
public void destroy() { | ||
super.destroy(); | ||
getDispatcher().destroy(); | ||
} | ||
|
||
protected abstract RackDispatcher getDispatcher(); | ||
protected abstract RackContext getContext(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,63 +1,54 @@ | ||
/* | ||
* Copyright (c) 2010-2011 Engine Yard, Inc. | ||
* Copyright (c) 2007-2009 Sun Microsystems, Inc. | ||
* This source code is available under the MIT license. | ||
* See the file LICENSE.txt for details. | ||
*/ | ||
|
||
package org.jruby.rack; | ||
|
||
import java.io.IOException; | ||
|
||
import org.jruby.rack.servlet.ServletRackContext; | ||
|
||
/** | ||
* Dispatcher suited for use in a servlet container | ||
* @author nick | ||
* | ||
* @author nicksieger | ||
*/ | ||
public class DefaultRackDispatcher implements RackDispatcher { | ||
private RackContext context; | ||
|
||
public DefaultRackDispatcher(RackContext rackContext) { | ||
this.context = rackContext; | ||
public class DefaultRackDispatcher extends AbstractRackDispatcher { | ||
|
||
private final ServletRackContext servletRackContext; | ||
|
||
public DefaultRackDispatcher(RackContext rackContext) { | ||
super(rackContext); | ||
this.servletRackContext = (ServletRackContext) rackContext; | ||
} | ||
|
||
@Override | ||
protected RackApplication getApplication(RackContext context) throws RackInitializationException { | ||
return getRackFactory().getApplication(); | ||
} | ||
|
||
@Override | ||
protected void afterException(RackEnvironment request, Exception re, | ||
RackResponseEnvironment response) throws IOException { | ||
try { | ||
RackApplication errorApp = getRackFactory().getErrorApplication(); | ||
request.setAttribute(RackEnvironment.EXCEPTION, re); | ||
errorApp.call(request).respond(response); | ||
} catch (Exception e) { | ||
servletRackContext.log("Error: Couldn't handle error", e); | ||
response.sendError(500); | ||
} | ||
} | ||
|
||
public void process(RackEnvironment request, RackResponseEnvironment response) | ||
throws IOException { | ||
final RackApplicationFactory rackFactory = context.getRackFactory(); | ||
RackApplication app = null; | ||
try { | ||
app = rackFactory.getApplication(); | ||
app.call(request).respond(response); | ||
} catch (Exception re) { | ||
handleException(re, rackFactory, request, response); | ||
} finally { | ||
if (app != null) { | ||
rackFactory.finishedWithApplication(app); | ||
} | ||
} | ||
} | ||
|
||
private void handleException(Exception re, RackApplicationFactory rackFactory, | ||
RackEnvironment request, RackResponseEnvironment response) | ||
throws IOException { | ||
if (response.isCommitted()) { | ||
context.log("Error: Couldn't handle error: response committed", re); | ||
return; | ||
} | ||
response.reset(); | ||
context.log("Application Error", re); | ||
|
||
try { | ||
RackApplication errorApp = rackFactory.getErrorApplication(); | ||
request.setAttribute(RackEnvironment.EXCEPTION, re); | ||
errorApp.call(request).respond(response); | ||
} catch (Exception e) { | ||
context.log("Error: Couldn't handle error", e); | ||
response.sendError(500); | ||
} | ||
} | ||
public void destroy() { | ||
final RackApplicationFactory rackFactory = servletRackContext.getRackFactory(); | ||
rackFactory.destroy(); | ||
} | ||
|
||
protected RackApplicationFactory getRackFactory() { | ||
return servletRackContext.getRackFactory(); | ||
} | ||
|
||
@Override | ||
protected void afterProcess(RackApplication app) { | ||
getRackFactory().finishedWithApplication(app); | ||
} | ||
|
||
public void destroy() { | ||
final RackApplicationFactory rackFactory = context.getRackFactory(); | ||
rackFactory.destroy(); | ||
} | ||
} |
Oops, something went wrong.