Permalink
Browse files

add Rubble

  • Loading branch information...
earldouglas committed Nov 26, 2012
1 parent d6ba16a commit d259616e595cca3455263363de46faf37dcbbde2
View
@@ -124,3 +124,9 @@ weighttp -n 500000 -c 20 -t 4 -k http://localhost:9000/fast
```
(cd scamper/finatra ; sbt run)
```
+
+### [Rubble](https://github.com/JamesEarlDouglas/rubble)
+
+```
+(cd scamper/rubble ; sbt run)
+```
View
@@ -8,6 +8,7 @@
"Pinky" 4249
"Play 2" 16184
"Play 2 mini" 16715
+"Rubble" 58726
"Scalatra" 18833
-"Servlet 3.0" 54123
-"spray-can" 54996
+"Servlet 3.0" 57153
+"spray-can" 58927
View
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
@@ -0,0 +1,17 @@
+name := "rubble"
+
+organization := "com.earldouglas"
+
+version := "0.1-SNAPSHOT"
+
+scalaVersion := "2.9.2"
+
+classpathTypes ~= (_ + "orbit")
+
+libraryDependencies += "org.eclipse.jetty.orbit" % "javax.servlet" % "3.0.0.v201112011016" % "provided" artifacts (Artifact("javax.servlet", "jar", "jar"))
+
+libraryDependencies ++= Seq(
+ "org.eclipse.jetty.orbit" % "javax.servlet" % "3.0.0.v201112011016" artifacts (Artifact("javax.servlet", "jar", "jar"))
+ , "org.eclipse.jetty.orbit" % "javax.servlet" % "3.0.0.v201112011016" artifacts (Artifact("javax.servlet", "jar", "jar"))
+ , "org.eclipse.jetty" % "jetty-webapp" % "8.1.4.v20120524" artifacts (Artifact("jetty-webapp", "jar", "jar"))
+)
@@ -0,0 +1,66 @@
+package com.earldouglas.rubble
+
+import scala.collection.JavaConversions._
+import scala.collection.JavaConverters._
+import java.io.InputStream
+import scala.util.matching.Regex
+import javax.servlet.http.HttpServletRequest
+import javax.servlet.http.HttpServletResponse
+import javax.servlet.http.HttpServlet
+import scala.xml.NodeSeq
+
+object Rubble {
+
+ implicit def richRequest(req: HttpServletRequest) = new RichRequest(req)
+ implicit def stringBody(body: String) = StringBody(body)
+ implicit def streamBody(body: InputStream) = StreamBody(body)
+ implicit def xmlBody(body: NodeSeq) = XmlBody(body)
+ implicit def richServlet(servlet: HttpServlet) = new RichServlet(servlet)
+ implicit def richResponse(res: HttpServletResponse) = new RichResponse(res)
+ implicit def singleHeader(header: Header) = Seq(header)
+
+ class RichRequest(req: HttpServletRequest) {
+ val context = req.getContextPath + req.getServletPath
+ def uri: String = req.getRequestURI() match {
+ case x if x startsWith context =>
+ x.substring(context.length())
+ case x => x
+ }
+ def params: Map[String, String] = req.getParameterNames().asScala.map(name => (name -> req.getParameter(name))).toMap
+ }
+
+ class RichServlet(servlet: HttpServlet) {
+ def initParam(name: String): Option[String] = Option(servlet.getInitParameter(name))
+ }
+
+ case class Header(name: String, value: String)
+
+ sealed trait Body
+ case object NoBody extends Body
+ case class StringBody(body: String) extends Body
+ case class StreamBody(body: InputStream) extends Body
+ case class XmlBody(body: NodeSeq) extends Body
+
+ class RichResponse(res: HttpServletResponse) {
+ def respond(status: Int = 200, headers: Seq[Header] = Nil, body: Body = NoBody) {
+ res.setStatus(status)
+ headers.foreach(x => res.setHeader(x.name, x.value))
+ body match {
+ case StringBody(body) => res.getWriter().write(body)
+ case StreamBody(body) =>
+ val os = res.getOutputStream()
+ Iterator.continually(body.read).takeWhile(-1 !=).foreach(os.write)
+ case XmlBody(body) =>
+ val w = res.getWriter()
+ body.foreach(n => w.write(n.toString()))
+ case NoBody =>
+ }
+ }
+
+ def redirect(loc: String) {
+ res.setStatus(303)
+ res.setHeader("Location", loc)
+ }
+ }
+}
+
@@ -0,0 +1,37 @@
+package com.earldouglas.rubble.tests
+
+import org.eclipse.jetty.server.Server
+import org.eclipse.jetty.server.nio.SelectChannelConnector
+import org.eclipse.jetty.util.thread.QueuedThreadPool
+import org.eclipse.jetty.webapp.WebAppContext
+
+import com.earldouglas.rubble.Rubble._
+
+import javax.servlet.http._
+
+object RubbleTests extends App {
+ val server = new Server()
+
+ val connector = new SelectChannelConnector()
+ connector.setPort(9000)
+ connector.setThreadPool(new QueuedThreadPool(24))
+
+ server.addConnector(connector)
+
+ val webapp = new WebAppContext()
+ webapp.setContextPath("/")
+ webapp.setWar("src/main/webapp")
+ server.setHandler(webapp)
+
+ server.start()
+ server.join()
+}
+
+class ExampleServlet extends HttpServlet {
+ override def doGet(req: HttpServletRequest, res: HttpServletResponse) =
+ req.uri match {
+ case "/fast" => res.respond(body = "<h1>slept for { duration } ms</h1>")
+ case _ => res.respond(status = 404)
+ }
+}
+
@@ -0,0 +1,21 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<web-app xmlns="http://java.sun.com/xml/ns/javaee"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
+ version="3.0"
+ >
+
+ <servlet>
+ <servlet-name>example</servlet-name>
+ <servlet-class>com.earldouglas.rubble.tests.ExampleServlet</servlet-class>
+ <init-param>
+ <param-name>url-pattern</param-name>
+ <param-value>/*</param-value>
+ </init-param>
+ </servlet>
+ <servlet-mapping>
+ <servlet-name>example</servlet-name>
+ <url-pattern>/*</url-pattern>
+ </servlet-mapping>
+
+</web-app>

0 comments on commit d259616

Please sign in to comment.