Skip to content

Instantly share code, notes, and snippets.

import org.scalatest.matchers.ShouldMatchers
import org.scalatest.FunSuite
import javax.servlet.http.{HttpServletResponse, HttpServletRequest, HttpServlet}
import java.net.URLEncoder
import org.mortbay.jetty.testing.{HttpTester, ServletTester}
class CharsetServlet extends HttpServlet {
override def doGet(req: HttpServletRequest, res: HttpServletResponse) = {
val foo = req.getParameter("foo")
res.setContentType("text/plain; charset=utf-8")
// I think this is possible, if we just ignore the names.
//
// year = :year, month = :month, day = :day
get("/date/:year/:month/:day") { (year, month, day) =>
<ul>
<li>Year: { year }</li>
<li>Month: { month }</li>
<li>Day: { day }</li>
</ul>
}
@rossabaker
rossabaker / gist:441675
Created June 17, 2010 04:20
sbt scopes more like Maven
override def ivyXML =
<configurations defaultconfmapping="*->default(compile)">
<conf name="compile" />
<conf name="runtime" extends="compile" />
<conf name="test" extends="runtime" />
<conf name="provided" />
</configurations>
override def defaultConfigurationExtensions = false
@rossabaker
rossabaker / gist:443103
Created June 18, 2010 02:00
Scala equality pitfall
scala> null == 42
res0: Boolean = false
scala> (null:java.lang.Integer) == 42
java.lang.NullPointerException
at scala.runtime.BoxesRunTime.equalsNumNum(Unknown Source)
at scala.runtime.BoxesRunTime.equalsNumObject(Unknown Source)
at .<init>(<console>:6)
at .<clinit>(<console>)
at RequestResult$.<init>(<console>:9)
final class RichAnyRef[T <:AnyRef](val value: T) extends Proxy {
def self: Any = value
def ? : Option[T] = Option(value)
def ?[R] ( func: (T=>R) ) : Option[R] = Option(value) map func
}
implicit def RichAnyRefWrapper[T <:AnyRef](x: T) = new RichAnyRef(x)
final class RichOption[T](val value: Option[T]) extends Proxy {
def self: Any = value
def | (other:T): T = value.getOrElse(other)
@rossabaker
rossabaker / gist:654058
Created October 29, 2010 18:19
Ensure that SSGI response bodies are renderable
scala> import org.scalatra.ssgi._
import org.scalatra.ssgi._
scala> val res = Response(body = "foo")
res: org.scalatra.ssgi.Response[java.lang.String] = Response(200,Map(),foo)
scala> res map { _.toUpperCase }
res10: org.scalatra.ssgi.Response[java.lang.String] = Response(200,Map(),FOO)
scala> res map { x => <h1>{x}</h1> }
trait AuthenticationSupport
        extends ScentrySupport[ApiUser]
                with FlashMapSupport
                with CookieSupport
                with Logging { self: ScalatraKernel =>
  before { scentry.authenticate('RememberMe) }
  override protected def registerAuthStrategies = {
    scentry.registerStrategy('UserPassword, app => new UserPasswordStrategy(app))
trait ScalatraTestDependencies extends BasicManagedProject {
def testProjectDependencies = List(scalatest, specs)
abstract override def dependencies = super.dependencies.toList ++ testProjectDependencies
override def deliverProjectDependencies: Iterable[ModuleID] = {
val testDependencyIds = testProjectDependencies.map(_.projectID).toList
super.deliverProjectDependencies.toList -- testDependencyIds ++ (testDependencyIds map { _ % "test" })
}
implicit def writesToWriterToCharRenderable(writesToWriter: { def writeTo(writer: Writer): Unit }) =
new CharRenderable {
def writeTo(out: OutputStream, cs: Charset) { writeTo(new OutputStreamWriter(out, cs)) }
def writeTo(writer: Writer) { writesToWriter.writeTo(writer) }
}
package com.mojolly.websocket
import akka.actor.{Scheduler, Actor}
import java.util.concurrent.TimeUnit
import Actor._
import org.jredis.ri.alphazero.JRedisClient
/*
* Gets executed by the listener defined in web.xml
*/