SlideShare a Scribd company logo
1 of 23
Download to read offline
Scalatra	
  2.2	
  

NOVEMBER	
  2012	
  
2	
  



Short	
  introduc,on	
  
And	
  overview	
  of	
  	
  
New	
  features	
  
What?	
  
●  Rou5ng	
  
●  Params	
  
●  Json4s	
  
●  Akka	
  
●  Atmosphere	
  
●  Commands	
  
Hello,	
  Scalatra	
  
class	
  HelloWorld	
  extends	
  ScalatraServlet	
  {	
  	
  	
  
	
  	
  get("/hello/:name")	
  {	
  
	
  	
  	
  	
  <h1>Hello,	
  {params("name")}</h1>	
  
	
  	
  }	
  
}	
  
Hello,	
  Scalatra:	
  Routes	
  
class	
  HelloWorld	
  extends	
  ScalatraServlet	
  {	
  	
  	
  
	
  	
  get("/hello/:name")	
  {	
  
	
  	
  	
  	
  <h1>Hello,	
  {params("name")}</h1>	
  
	
  	
  }	
  
}	
  

●  Matches	
  GET	
  requests	
  to	
  /hello/*	
  

●  Captures	
  named	
  parameters	
  from	
  the	
  route	
  (here,	
  :name).	
  

●  Matching	
  requests	
  are	
  dispatched	
  to	
  the	
  ac5on.	
  
Hello,	
  Scalatra:	
  Ac,ons	
  
class	
  HelloWorld	
  extends	
  ScalatraServlet	
  {	
  	
  	
  
	
  	
  get("/hello/:name")	
  {	
  
	
  	
  	
  	
  <h1>Hello,	
  {params("name")}</h1>	
  
	
  	
  }	
  
}	
  
●  When	
  a	
  request	
  matches	
  a	
  route,	
  the	
  ac5on	
  is	
  run.	
  
●  A	
  response	
  is	
  generated	
  from	
  the	
  ac5on's	
  result.	
  
   Supports	
  XML	
  literals,	
  Byte	
  arrays,	
  Strings,	
  Files,	
  
   InputStreams.	
  
●  Rendering	
  of	
  ac5on	
  results	
  is	
  customizable.	
  
Hello	
  Scalatra:	
  ScalatraServlet	
  

class	
  HelloWorld	
  extends	
  ScalatraServlet	
  {	
  
	
  	
  get("/hello/:name")	
  {	
  
	
  	
  	
  	
  <h1>Hello,	
  {params("name")}</h1>	
  
	
  	
  }	
  
}	
  
●  ScalatraServlet	
  provides	
  the	
  HTTP	
  DSL.	
  
●  Scalatra	
  2.x	
  is	
  built	
  on	
  Java	
  Servlets,	
  the	
  
   standard	
  web	
  server	
  API	
  for	
  the	
  JVM.	
  
Why	
  Scalatra?	
  
●  Based	
  on	
  Sinatra,	
                 •  Java	
  
   which	
  I	
  like	
  to	
  call	
        •  F#	
  
   "the	
  universal	
  web	
                •  Erlang	
  
   DSL".	
  	
  Clones	
  exist	
  in:	
  
                                             •  Haskell	
  
      •  Ruby	
  
                                             •  Clojure	
  
      •  PHP	
  
                                             •  Bash	
  
      •  Python	
  
                                             •  Many	
  others	
  
      •  JavaScript	
  
Scalatra	
  Philosophy	
  
●  Stateless	
  by	
  default,	
       •  Akka	
  
   sessions	
  if	
  you	
  need	
     •  Atmosphere	
  
   them.	
                             •  Embrace	
  standards	
  
●  Lean	
  by	
  default,	
            •  HTTP	
  
   integra5ons	
  if	
  they're	
      •  JVM	
  
   popular.	
  
                                       •  Scala	
  
     •  Scalate	
  
                                       •  Beginner	
  friendly.	
  
     •  JSON4s	
  
                                       •  Scale	
  with	
  your	
  app	
  
                                          and	
  your	
  exper5se.	
  
Rou,ng	
  in	
  depth:	
  Methods	
  
get("/hello/:name")	
  {	
  "Hi,	
  "+params("name")	
  }	
  
●  Routes	
  start	
  with	
  an	
  HTTP	
  method.	
  
     •  Get	
  /	
  Head	
  /	
  Op5ons	
  /	
  Connect	
  
     •  Post	
  /	
  Put	
  /	
  Patch	
  /	
  Delete	
  /	
  Trace	
  

●  Advanced	
  usage:	
  can	
  call	
  at	
  any5me	
  to	
  register	
  
   routes	
  on	
  the	
  fly.	
  
Rou,ng	
  in	
  depth:	
  Params	
  
get("/hello/:name")	
  {	
  "Hi,	
  "+params("name")	
  }	
  

●  Matches	
  the	
  servlet	
  path.	
  
     •  Pro5p:	
  compose	
  large	
  apps	
  with	
  mul5ple	
  servlets	
  

●  Declare	
  route	
  params	
  with	
  a	
  leading	
  ':’	
  
     •  Matches	
  everything	
  up	
  to	
  next	
  '/',	
  '?',	
  or	
  '#'	
  
Rou,ng	
  in	
  depth:	
  Params	
  
Rou,ng	
  in	
  depth:	
  Splats	
  
get("/hello/*")	
  {	
  multiParams("splat").mkString(",")	
  }	
  

●  Splats get stored in the captures param.

●  If you expect multiple values, you can access it through
   multiParams.
    •  params is just a convenient view of multiParams
Rou,ng	
  in	
  depth:	
  regex	
  
get("""/hello/(w+)""".r)	
  {	
  "Hello,	
  "+params("captures")	
  }	
  

	
  


●      Use if you have a real hairy route that standard
       syntax can't handle.

●      No named groups yet. Java didn't add named
       regexes until version 7!
Rou,ng	
  in	
  depth:	
  booleans	
  
def	
  userAgent	
  =	
  request.getHeader("User-­‐Agent”)	
  

get("/hello/:name",	
  !userAgent.contains("Lynx"))	
  {	
  	
  
   status	
  =	
  403	
  
   "You	
  and	
  your	
  fancy,	
  schmancy	
  browser.	
  Get	
  out.”	
  }	
  


●  Both paths and boolean expressions are
   converted to route matchers. Implicitly
●  A route matches if and only if all route
   matchers match.
Ac,ons	
  in	
  depth:	
  return	
  value	
  

●  The return value of an action becomes the
   body of the response.
	
  
•  It's just a
                                        •  Override
Ac,ons	
  in	
  depth:	
  render	
  pipeline	
  
                                                                      	


protected	
  def	
  renderPipeline:	
  RenderPipeline	
  =	
  {	
  
	
  	
  case	
  bytes:	
  Array[Byte]	
  =>	
  
	
  	
  	
  	
  response.outputStream.write(bytes)	
  
	
  	
  case	
  is:	
  java.io.InputStream	
  =>	
  
	
  	
  	
  	
  using(is)	
  {	
  util.io.copy(_,	
  response.outputStream)	
  }	
  
}	


●  It's just a partial function.
●  Override it. Use orElse to delegate to
   the default.
Ac,ons	
  in	
  depth:	
  mutable	
  
Ac,ons	
  in	
  depth:	
  mutable	
  
get("/hello/:name")	
  {	
  
  status	
  =	
  HttpServletResponse.SC_GONE	
  	
  
  contentType	
  =	
  "text/html”	
  
  <h1>Goodbye,	
  cruel	
  world</h1>	
  }	
  

●  Status and headers are typically set in an
   imperative fashion.	
  
●  Response is thread local. Relax.
Commands	
  
●  Capture	
  input	
  
●  Validate	
  input	
  
Integra,ons:	
  JSON4S	
  
●  Serialize	
  case	
  classes	
  to	
  json	
  
●  Handles	
  xml	
  as	
  well	
  as	
  json	
  
Integra,ons:	
  Akka	
  
●  Takes	
  advantage	
  of	
  servlet	
  3.0	
  async	
  support	
  
Integra,ons:	
  Atmosphere	
  
●  WebSocket	
  	
  
●  Server	
  side	
  events	
  
●  Like	
  socket.io	
  but	
  not	
  socket.io	
  

More Related Content

What's hot

Event Driven Architecture with Apache Camel
Event Driven Architecture with Apache CamelEvent Driven Architecture with Apache Camel
Event Driven Architecture with Apache Camelprajods
 
Oak, the architecture of Apache Jackrabbit 3
Oak, the architecture of Apache Jackrabbit 3Oak, the architecture of Apache Jackrabbit 3
Oak, the architecture of Apache Jackrabbit 3Jukka Zitting
 
Apache Camel Introduction & What's in the box
Apache Camel Introduction & What's in the boxApache Camel Introduction & What's in the box
Apache Camel Introduction & What's in the boxClaus Ibsen
 
Using Apache Camel as AKKA
Using Apache Camel as AKKAUsing Apache Camel as AKKA
Using Apache Camel as AKKAJohan Edstrom
 
Introduction to Apache Camel
Introduction to Apache CamelIntroduction to Apache Camel
Introduction to Apache CamelClaus Ibsen
 
The Parenscript Common Lisp to JavaScript compiler
The Parenscript Common Lisp to JavaScript compilerThe Parenscript Common Lisp to JavaScript compiler
The Parenscript Common Lisp to JavaScript compilerVladimir Sedach
 
Performance Optimization of Rails Applications
Performance Optimization of Rails ApplicationsPerformance Optimization of Rails Applications
Performance Optimization of Rails ApplicationsSerge Smetana
 
Taking Apache Camel For A Ride
Taking Apache Camel For A RideTaking Apache Camel For A Ride
Taking Apache Camel For A RideBruce Snyder
 
Introduction to Apache Camel
Introduction to Apache CamelIntroduction to Apache Camel
Introduction to Apache CamelFuseSource.com
 
Rails Application Optimization Techniques & Tools
Rails Application Optimization Techniques & ToolsRails Application Optimization Techniques & Tools
Rails Application Optimization Techniques & Toolsguest05c09d
 
Building Asynchronous Applications
Building Asynchronous ApplicationsBuilding Asynchronous Applications
Building Asynchronous ApplicationsJohan Edstrom
 
ApacheCon EU 2016 - Apache Camel the integration library
ApacheCon EU 2016 - Apache Camel the integration libraryApacheCon EU 2016 - Apache Camel the integration library
ApacheCon EU 2016 - Apache Camel the integration libraryClaus Ibsen
 
Ruby/rails performance and profiling
Ruby/rails performance and profilingRuby/rails performance and profiling
Ruby/rails performance and profilingDanny Guinther
 
How to distribute Ruby to the world
How to distribute Ruby to the worldHow to distribute Ruby to the world
How to distribute Ruby to the worldHiroshi SHIBATA
 
TorqueBox - Ruby Hoedown 2011
TorqueBox - Ruby Hoedown 2011TorqueBox - Ruby Hoedown 2011
TorqueBox - Ruby Hoedown 2011Lance Ball
 

What's hot (20)

Event Driven Architecture with Apache Camel
Event Driven Architecture with Apache CamelEvent Driven Architecture with Apache Camel
Event Driven Architecture with Apache Camel
 
Oak, the architecture of Apache Jackrabbit 3
Oak, the architecture of Apache Jackrabbit 3Oak, the architecture of Apache Jackrabbit 3
Oak, the architecture of Apache Jackrabbit 3
 
Apache Camel Introduction & What's in the box
Apache Camel Introduction & What's in the boxApache Camel Introduction & What's in the box
Apache Camel Introduction & What's in the box
 
Using Apache Camel as AKKA
Using Apache Camel as AKKAUsing Apache Camel as AKKA
Using Apache Camel as AKKA
 
Introduction to Apache Camel
Introduction to Apache CamelIntroduction to Apache Camel
Introduction to Apache Camel
 
2008-12 OJUG JCR Demo
2008-12 OJUG JCR Demo2008-12 OJUG JCR Demo
2008-12 OJUG JCR Demo
 
20140925 rails pacific
20140925 rails pacific20140925 rails pacific
20140925 rails pacific
 
The Parenscript Common Lisp to JavaScript compiler
The Parenscript Common Lisp to JavaScript compilerThe Parenscript Common Lisp to JavaScript compiler
The Parenscript Common Lisp to JavaScript compiler
 
Ruby on the JVM
Ruby on the JVMRuby on the JVM
Ruby on the JVM
 
RubyGems 3 & 4
RubyGems 3 & 4RubyGems 3 & 4
RubyGems 3 & 4
 
Performance Optimization of Rails Applications
Performance Optimization of Rails ApplicationsPerformance Optimization of Rails Applications
Performance Optimization of Rails Applications
 
Taking Apache Camel For A Ride
Taking Apache Camel For A RideTaking Apache Camel For A Ride
Taking Apache Camel For A Ride
 
Introduction to Apache Camel
Introduction to Apache CamelIntroduction to Apache Camel
Introduction to Apache Camel
 
Rails Application Optimization Techniques & Tools
Rails Application Optimization Techniques & ToolsRails Application Optimization Techniques & Tools
Rails Application Optimization Techniques & Tools
 
Building Asynchronous Applications
Building Asynchronous ApplicationsBuilding Asynchronous Applications
Building Asynchronous Applications
 
ApacheCon EU 2016 - Apache Camel the integration library
ApacheCon EU 2016 - Apache Camel the integration libraryApacheCon EU 2016 - Apache Camel the integration library
ApacheCon EU 2016 - Apache Camel the integration library
 
Ruby/rails performance and profiling
Ruby/rails performance and profilingRuby/rails performance and profiling
Ruby/rails performance and profiling
 
Gems on Ruby
Gems on RubyGems on Ruby
Gems on Ruby
 
How to distribute Ruby to the world
How to distribute Ruby to the worldHow to distribute Ruby to the world
How to distribute Ruby to the world
 
TorqueBox - Ruby Hoedown 2011
TorqueBox - Ruby Hoedown 2011TorqueBox - Ruby Hoedown 2011
TorqueBox - Ruby Hoedown 2011
 

Similar to Scalatra 2.2 overview: routing, params, JSON4s, Akka, and more

Ratpack Web Framework
Ratpack Web FrameworkRatpack Web Framework
Ratpack Web FrameworkDaniel Woods
 
Real time Analytics with Apache Kafka and Apache Spark
Real time Analytics with Apache Kafka and Apache SparkReal time Analytics with Apache Kafka and Apache Spark
Real time Analytics with Apache Kafka and Apache SparkRahul Jain
 
Rapid API Development ArangoDB Foxx
Rapid API Development ArangoDB FoxxRapid API Development ArangoDB Foxx
Rapid API Development ArangoDB FoxxMichael Hackstein
 
Java colombo-deep-dive-into-jax-rs
Java colombo-deep-dive-into-jax-rsJava colombo-deep-dive-into-jax-rs
Java colombo-deep-dive-into-jax-rsSagara Gunathunga
 
Develop realtime web with Scala and Xitrum
Develop realtime web with Scala and XitrumDevelop realtime web with Scala and Xitrum
Develop realtime web with Scala and XitrumNgoc Dao
 
plackdo, plack-like web interface on perl6
plackdo, plack-like web interface on perl6plackdo, plack-like web interface on perl6
plackdo, plack-like web interface on perl6Nobuo Danjou
 
apachecamelk-april2019-190409093034.pdf
apachecamelk-april2019-190409093034.pdfapachecamelk-april2019-190409093034.pdf
apachecamelk-april2019-190409093034.pdfssuserbb9f511
 
Introducing Kafka-on-Pulsar: bring native Kafka protocol support to Apache Pu...
Introducing Kafka-on-Pulsar: bring native Kafka protocol support to Apache Pu...Introducing Kafka-on-Pulsar: bring native Kafka protocol support to Apache Pu...
Introducing Kafka-on-Pulsar: bring native Kafka protocol support to Apache Pu...StreamNative
 
Design for Scalability in ADAM
Design for Scalability in ADAMDesign for Scalability in ADAM
Design for Scalability in ADAMfnothaft
 
Introduction to ZooKeeper - TriHUG May 22, 2012
Introduction to ZooKeeper - TriHUG May 22, 2012Introduction to ZooKeeper - TriHUG May 22, 2012
Introduction to ZooKeeper - TriHUG May 22, 2012mumrah
 
1.6 米嘉 gobuildweb
1.6 米嘉 gobuildweb1.6 米嘉 gobuildweb
1.6 米嘉 gobuildwebLeo Zhou
 
PHP, the GraphQL ecosystem and GraphQLite
PHP, the GraphQL ecosystem and GraphQLitePHP, the GraphQL ecosystem and GraphQLite
PHP, the GraphQL ecosystem and GraphQLiteJEAN-GUILLAUME DUJARDIN
 
Apache Camel K - Copenhagen v2
Apache Camel K - Copenhagen v2Apache Camel K - Copenhagen v2
Apache Camel K - Copenhagen v2Claus Ibsen
 

Similar to Scalatra 2.2 overview: routing, params, JSON4s, Akka, and more (20)

Ratpack Web Framework
Ratpack Web FrameworkRatpack Web Framework
Ratpack Web Framework
 
Polyglot Grails
Polyglot GrailsPolyglot Grails
Polyglot Grails
 
Real time Analytics with Apache Kafka and Apache Spark
Real time Analytics with Apache Kafka and Apache SparkReal time Analytics with Apache Kafka and Apache Spark
Real time Analytics with Apache Kafka and Apache Spark
 
Logstash
LogstashLogstash
Logstash
 
Follow the White Rabbit - Message Queues with PHP
Follow the White Rabbit - Message Queues with PHPFollow the White Rabbit - Message Queues with PHP
Follow the White Rabbit - Message Queues with PHP
 
06 pig-01-intro
06 pig-01-intro06 pig-01-intro
06 pig-01-intro
 
Giraph+Gora in ApacheCon14
Giraph+Gora in ApacheCon14Giraph+Gora in ApacheCon14
Giraph+Gora in ApacheCon14
 
Rapid API Development ArangoDB Foxx
Rapid API Development ArangoDB FoxxRapid API Development ArangoDB Foxx
Rapid API Development ArangoDB Foxx
 
Java colombo-deep-dive-into-jax-rs
Java colombo-deep-dive-into-jax-rsJava colombo-deep-dive-into-jax-rs
Java colombo-deep-dive-into-jax-rs
 
Develop realtime web with Scala and Xitrum
Develop realtime web with Scala and XitrumDevelop realtime web with Scala and Xitrum
Develop realtime web with Scala and Xitrum
 
plackdo, plack-like web interface on perl6
plackdo, plack-like web interface on perl6plackdo, plack-like web interface on perl6
plackdo, plack-like web interface on perl6
 
apachecamelk-april2019-190409093034.pdf
apachecamelk-april2019-190409093034.pdfapachecamelk-april2019-190409093034.pdf
apachecamelk-april2019-190409093034.pdf
 
Introducing Kafka-on-Pulsar: bring native Kafka protocol support to Apache Pu...
Introducing Kafka-on-Pulsar: bring native Kafka protocol support to Apache Pu...Introducing Kafka-on-Pulsar: bring native Kafka protocol support to Apache Pu...
Introducing Kafka-on-Pulsar: bring native Kafka protocol support to Apache Pu...
 
scalaphx-akka-http
scalaphx-akka-httpscalaphx-akka-http
scalaphx-akka-http
 
Rango
RangoRango
Rango
 
Design for Scalability in ADAM
Design for Scalability in ADAMDesign for Scalability in ADAM
Design for Scalability in ADAM
 
Introduction to ZooKeeper - TriHUG May 22, 2012
Introduction to ZooKeeper - TriHUG May 22, 2012Introduction to ZooKeeper - TriHUG May 22, 2012
Introduction to ZooKeeper - TriHUG May 22, 2012
 
1.6 米嘉 gobuildweb
1.6 米嘉 gobuildweb1.6 米嘉 gobuildweb
1.6 米嘉 gobuildweb
 
PHP, the GraphQL ecosystem and GraphQLite
PHP, the GraphQL ecosystem and GraphQLitePHP, the GraphQL ecosystem and GraphQLite
PHP, the GraphQL ecosystem and GraphQLite
 
Apache Camel K - Copenhagen v2
Apache Camel K - Copenhagen v2Apache Camel K - Copenhagen v2
Apache Camel K - Copenhagen v2
 

Recently uploaded

Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024The Digital Insurer
 
Science&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdfScience&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdfjimielynbastida
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024BookNet Canada
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
APIForce Zurich 5 April Automation LPDG
APIForce Zurich 5 April  Automation LPDGAPIForce Zurich 5 April  Automation LPDG
APIForce Zurich 5 April Automation LPDGMarianaLemus7
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 

Recently uploaded (20)

Vulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptxVulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024
 
Science&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdfScience&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdf
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
Hot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort Service
Hot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort ServiceHot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort Service
Hot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort Service
 
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
APIForce Zurich 5 April Automation LPDG
APIForce Zurich 5 April  Automation LPDGAPIForce Zurich 5 April  Automation LPDG
APIForce Zurich 5 April Automation LPDG
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 

Scalatra 2.2 overview: routing, params, JSON4s, Akka, and more

  • 2. 2   Short  introduc,on   And  overview  of     New  features  
  • 3. What?   ●  Rou5ng   ●  Params   ●  Json4s   ●  Akka   ●  Atmosphere   ●  Commands  
  • 4. Hello,  Scalatra   class  HelloWorld  extends  ScalatraServlet  {          get("/hello/:name")  {          <h1>Hello,  {params("name")}</h1>      }   }  
  • 5. Hello,  Scalatra:  Routes   class  HelloWorld  extends  ScalatraServlet  {          get("/hello/:name")  {          <h1>Hello,  {params("name")}</h1>      }   }   ●  Matches  GET  requests  to  /hello/*   ●  Captures  named  parameters  from  the  route  (here,  :name).   ●  Matching  requests  are  dispatched  to  the  ac5on.  
  • 6. Hello,  Scalatra:  Ac,ons   class  HelloWorld  extends  ScalatraServlet  {          get("/hello/:name")  {          <h1>Hello,  {params("name")}</h1>      }   }   ●  When  a  request  matches  a  route,  the  ac5on  is  run.   ●  A  response  is  generated  from  the  ac5on's  result.   Supports  XML  literals,  Byte  arrays,  Strings,  Files,   InputStreams.   ●  Rendering  of  ac5on  results  is  customizable.  
  • 7. Hello  Scalatra:  ScalatraServlet   class  HelloWorld  extends  ScalatraServlet  {      get("/hello/:name")  {          <h1>Hello,  {params("name")}</h1>      }   }   ●  ScalatraServlet  provides  the  HTTP  DSL.   ●  Scalatra  2.x  is  built  on  Java  Servlets,  the   standard  web  server  API  for  the  JVM.  
  • 8. Why  Scalatra?   ●  Based  on  Sinatra,   •  Java   which  I  like  to  call   •  F#   "the  universal  web   •  Erlang   DSL".    Clones  exist  in:   •  Haskell   •  Ruby   •  Clojure   •  PHP   •  Bash   •  Python   •  Many  others   •  JavaScript  
  • 9. Scalatra  Philosophy   ●  Stateless  by  default,   •  Akka   sessions  if  you  need   •  Atmosphere   them.   •  Embrace  standards   ●  Lean  by  default,   •  HTTP   integra5ons  if  they're   •  JVM   popular.   •  Scala   •  Scalate   •  Beginner  friendly.   •  JSON4s   •  Scale  with  your  app   and  your  exper5se.  
  • 10. Rou,ng  in  depth:  Methods   get("/hello/:name")  {  "Hi,  "+params("name")  }   ●  Routes  start  with  an  HTTP  method.   •  Get  /  Head  /  Op5ons  /  Connect   •  Post  /  Put  /  Patch  /  Delete  /  Trace   ●  Advanced  usage:  can  call  at  any5me  to  register   routes  on  the  fly.  
  • 11. Rou,ng  in  depth:  Params   get("/hello/:name")  {  "Hi,  "+params("name")  }   ●  Matches  the  servlet  path.   •  Pro5p:  compose  large  apps  with  mul5ple  servlets   ●  Declare  route  params  with  a  leading  ':’   •  Matches  everything  up  to  next  '/',  '?',  or  '#'  
  • 12. Rou,ng  in  depth:  Params  
  • 13. Rou,ng  in  depth:  Splats   get("/hello/*")  {  multiParams("splat").mkString(",")  }   ●  Splats get stored in the captures param. ●  If you expect multiple values, you can access it through multiParams. •  params is just a convenient view of multiParams
  • 14. Rou,ng  in  depth:  regex   get("""/hello/(w+)""".r)  {  "Hello,  "+params("captures")  }     ●  Use if you have a real hairy route that standard syntax can't handle. ●  No named groups yet. Java didn't add named regexes until version 7!
  • 15. Rou,ng  in  depth:  booleans   def  userAgent  =  request.getHeader("User-­‐Agent”)   get("/hello/:name",  !userAgent.contains("Lynx"))  {     status  =  403   "You  and  your  fancy,  schmancy  browser.  Get  out.”  }   ●  Both paths and boolean expressions are converted to route matchers. Implicitly ●  A route matches if and only if all route matchers match.
  • 16. Ac,ons  in  depth:  return  value   ●  The return value of an action becomes the body of the response.  
  • 17. •  It's just a •  Override Ac,ons  in  depth:  render  pipeline   protected  def  renderPipeline:  RenderPipeline  =  {      case  bytes:  Array[Byte]  =>          response.outputStream.write(bytes)      case  is:  java.io.InputStream  =>          using(is)  {  util.io.copy(_,  response.outputStream)  }   } ●  It's just a partial function. ●  Override it. Use orElse to delegate to the default.
  • 18. Ac,ons  in  depth:  mutable  
  • 19. Ac,ons  in  depth:  mutable   get("/hello/:name")  {   status  =  HttpServletResponse.SC_GONE     contentType  =  "text/html”   <h1>Goodbye,  cruel  world</h1>  }   ●  Status and headers are typically set in an imperative fashion.   ●  Response is thread local. Relax.
  • 20. Commands   ●  Capture  input   ●  Validate  input  
  • 21. Integra,ons:  JSON4S   ●  Serialize  case  classes  to  json   ●  Handles  xml  as  well  as  json  
  • 22. Integra,ons:  Akka   ●  Takes  advantage  of  servlet  3.0  async  support  
  • 23. Integra,ons:  Atmosphere   ●  WebSocket     ●  Server  side  events   ●  Like  socket.io  but  not  socket.io