Permalink
Browse files

Inital commit with working Scalatra application

  • Loading branch information...
tdziurko committed Apr 26, 2013
1 parent 23d3009 commit 099c7ac30fe0f63319a890e848a94390c035e801
View
@@ -0,0 +1,6 @@
*.iml
*.ipr
*.iws
.idea/
target/
*.log
View
@@ -0,0 +1,59 @@
import sbt._
import Keys._
import scala._
object Resolvers {
val customResolvers = Seq(
"Sonatype releases" at "http://oss.sonatype.org/content/repositories/releases/",
"Sonatype snapshots" at "http://oss.sonatype.org/content/repositories/snapshots/"
)
}
object BuildSettings {
import Resolvers._
val buildSettings = Defaults.defaultSettings ++ Seq(
organization := "pl.tomaszdziurko",
version := "0.0.1-SNAPSHOT",
scalaVersion := "2.10.0",
resolvers := customResolvers,
scalacOptions += "-unchecked",
classpathTypes ~= (_ + "orbit")
)
}
object Dependencies {
val scalatraVersion = "2.2.0"
val scalatra = "org.scalatra" %% "scalatra" % scalatraVersion
val scalatraJson = "org.scalatra" %% "scalatra-json" % scalatraVersion
val json4s = "org.json4s" %% "json4s-jackson" % "3.1.0"
val jetty = "org.eclipse.jetty" % "jetty-webapp" % "8.1.7.v20120910" % "container"
val scalatraStack = Seq(scalatra, scalatraJson )
// If the scope is provided;test, as in scalatra examples then gen-idea generates the incorrect scope (test).
// As provided implies test, so is enough here.
val servletApiProvided = "org.eclipse.jetty.orbit" % "javax.servlet" % "3.0.0.v201112011016" % "provided" artifacts (Artifact("javax.servlet", "jar", "jar"))
}
object ExampleAppBuild extends Build {
import Dependencies._
import BuildSettings._
import com.github.siasia.WebPlugin.webSettings
lazy val rest: Project = Project(
"app",
file("."),
settings = buildSettings ++ webSettings ++
Seq(libraryDependencies ++= Seq(json4s, servletApiProvided)
++ scalatraStack
++ Seq(jetty, servletApiProvided))
)
}
View
@@ -0,0 +1 @@
sbt.version=0.12.2
@@ -0,0 +1,12 @@
import sbt._
import Keys._
object Plugins extends Build {
lazy val plugins = Project(
"plugins",
file("."),
settings = Defaults.defaultSettings ++ Seq(
libraryDependencies += ("com.github.siasia" %% "xsbt-web-plugin" % "0.12.0-0.2.11.1"),
addSbtPlugin("com.github.mpeltonen" % "sbt-idea" % "1.2.0")))
}
View
@@ -0,0 +1,3 @@
@setlocal
java -jar sbt-launch.jar container:start "~ compile"
View
3 run.sh
@@ -0,0 +1,3 @@
#!/bin/sh
java -Xms512m -Xmx512m -jar sbt-launch.jar container:start "~ compile"
View
Binary file not shown.
@@ -0,0 +1,15 @@
import pl.tomaszdziurko.escapejson.ExampleServlet
import org.scalatra._
import javax.servlet.ServletContext
class ScalatraBootstrap extends LifeCycle {
val Prefix = "/rest/"
override def init(context: ServletContext) {
context.mount(new ExampleServlet(), Prefix + "example")
context.put("example", this)
}
}
@@ -0,0 +1,39 @@
package pl.tomaszdziurko.escapejson
import org.scalatra.json.{JValueResult, JacksonJsonSupport}
import org.json4s.{DefaultFormats, Formats}
import java.util.Date
import org.scalatra._
class ExampleServlet() extends ScalatraServlet with JacksonJsonSupport with JValueResult {
protected implicit val jsonFormats: Formats = DefaultFormats
val messages = List(
Message(1, "<script><alert>Script hacker</alert></script>", "Johny Hacker", new Date()),
Message(2, "Decent message", "John Smith", new Date()),
Message(3, "Message with <b>bolded fragment</b>", "Kate Norton", new Date())
)
before() {
contentType = formats("json")
}
get("/") {
messages
}
get("/:id") {
val id = params("id").toInt;
val messageOptional = messages.find((m: Message) => m.id == id)
log("optional =" + messageOptional)
messageOptional match {
case Some(e) => e
case _ =>
}
}
}
case class Message(id: Int, text: String, author: String, created: Date)
@@ -0,0 +1,15 @@
<?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">
<display-name>Scalatra Json Escaping</display-name>
<description>Simple example how to escape JSON returned via rest api in Scalatra</description>
<listener>
<listener-class>org.scalatra.servlet.ScalatraListener</listener-class>
</listener>
</web-app>

0 comments on commit 099c7ac

Please sign in to comment.