Last active
August 29, 2015 14:09
-
-
Save d6y/53b060e0e3305536ec9f to your computer and use it in GitHub Desktop.
Re-writing in the container
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
package code.filter | |
import javax.servlet._ | |
import http._ | |
import java.util.regex._ | |
// A wrapper around an HTTP Servlet Request which removes any /cached/:id from a URI | |
case class ResouceRequest(req: HttpServletRequest) extends HttpServletRequestWrapper(req) { | |
// The URI without the /cached/:id part: | |
lazy val rewritten: Option[String] = { | |
val pattern = "^/cache/[^/]+(/.+)$".r | |
pattern.findFirstMatchIn(req.getRequestURI).map(_.group(1)) | |
} | |
// Lift (currently) uses the servlet getRequetURI, allowing us to jump in | |
// and change the URI as far as Lift is concerned: | |
override def getRequestURI(): String = | |
rewritten getOrElse req.getRequestURI | |
} | |
class CustomResourceFilter extends Filter { | |
def init(config: FilterConfig): Unit = {} | |
def doFilter( | |
request: ServletRequest, | |
response: ServletResponse, | |
chain: FilterChain): Unit = { | |
// Containers can support non-HTTP requests, but we just pick out | |
// the HTTP ones and re-write them: | |
val rewritten = request match { | |
case httpReq: HttpServletRequest => ResouceRequest(httpReq) | |
case _ => request | |
} | |
// Pass our (possibly modified) request on to Lift: | |
chain.doFilter(rewritten, response) | |
} | |
def destroy(): Unit = {} | |
} |
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
<?xml version="1.0" encoding="ISO-8859-1"?> | |
<!DOCTYPE web-app | |
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" | |
"http://java.sun.com/dtd/web-app_2_3.dtd"> | |
<web-app> | |
<filter> | |
<filter-name>CustomResourceFilter</filter-name> | |
<display-name>Resource Filter</display-name> | |
<description>sRemoves anti-caching path from URLs</description> | |
<filter-class>code.filter.CustomResourceFilter</filter-class> | |
</filter> | |
<filter> | |
<filter-name>LiftFilter</filter-name> | |
<display-name>Lift Filter</display-name> | |
<description>The Filter that intercepts lift calls</description> | |
<filter-class>net.liftweb.http.LiftFilter</filter-class> | |
</filter> | |
<filter-mapping> | |
<filter-name>CustomResourceFilter</filter-name> | |
<url-pattern>/*</url-pattern> | |
</filter-mapping> | |
<filter-mapping> | |
<filter-name>LiftFilter</filter-name> | |
<url-pattern>/*</url-pattern> | |
</filter-mapping> | |
</web-app> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment