Scalatra in Action
Dave Hrycyszyn, Stefan Ollinger, and Ross A. Baker
  • May 2016
  • ISBN 9781617291296
  • 280 pages
  • printed in black & white

If you’re interested in Scala and RESTful APIs, just stop—you’ve found your bible!

Alberto Quario, UniCredit, UniCredit

Scalatra in Action introduces the Scalatra framework and the Sinatra model. It covers the framework in its entirety, starting with concepts like request routing, input handling, actions, and HTTP responses, then proceeds to more advanced topics, such as data access, handling heavy load, asynchronicity, securing applications, designing and documenting RESTful APIs, and real-time web programming.

Table of Contents detailed table of contents

Part 1: Introduction to Scalatra

1. Introduction

1.1. What’s Scalatra good at?

1.2. Hello, world!

1.3. Micro-frameworks versus full-stack frameworks

1.4. How does Scalatra compare with other Scala web frameworks?

1.4.1. Scalatra is a lean MVC framework

1.4.2. Scalatra runs on Servlets

1.5. Installing Scalatra

1.5.1. Generating a new project

1.5.2. Downloading dependencies and building the app

1.5.3. Starting the Hello, World application

1.5.4. Making changes and seeing them in your browser

1.6. Summary

2. A taste of Scalatra

2.1. Your first Scalatra application

2.2. Designing the UI

2.3. What’s in a Scalatra project?

2.4. Building the data model

2.5. Retrieving pages

2.5.1. A page retrieval route

2.5.2. A page retrieval action

2.6. Rendering the page

2.6.1. A quick introduction to Scalate

2.6.2. Adding a layout

2.7. Writing tests

2.7.1. Writing your first test

2.7.2. Running your tests

2.7.3. Adding another test

2.8. Getting ready for deployment

2.9. Summary

3. Routing

3.1. Anatomy of a route

3.2. Choosing the right method

3.2.1. The CRUD methods

3.2.2. The lesser-known methods

3.2.3. Overriding the method

3.3. Route matchers

3.3.1. Path expressions

3.3.2. Regular expressions

3.3.3. Boolean expressions

3.4. Advanced route matching

3.4.1. Conflict resolution

3.4.2. Rails-style path expressions

3.5. Summary

4. Working with user input

4.1. The life of a request

4.2. Routes and actions

4.3. HTTP parameter handling in Scalatra

4.3.1. Query string parameters

4.3.2. Path parameters

4.3.3. Form params

4.3.4. Params vs Multiparams

4.3.5. Dealing with unexpected input

4.3.6. Typed parameters

4.4. Filters

4.4.1. Selectively running filters

4.4.2. Filter conditions

4.5. Other kinds of user input

4.5.1. Request headers

4.5.2. COokies

4.6. Request helpers

4.6.1. Halting

4.6.2. Redirecting

4.6.3. ActionResult

4.7. Summary

Part 2: Common development tasks

5. Handling JSON

5.1. Introducing JsonSupport

5.1.1. Adding JSON support to an application

5.1.2. Introducing the JValue type

5.2. Producing and consuming JSON

5.2.1. Producing JSON

5.2.2. Consuming JSON

5.3. Customizing JSON support & handling mismatches

5.3.1. Customizing the JSON support & using field serializers

5.3.2. Handling polymorphism with type hints

5.3.3. Handling heterogeneity with custom serializers

5.4. JSONP

5.5. Conclusion

6. Handling Files

6.1. Serving files

6.1.1. Serving files through a route

6.1.2. Serving static resources

6.1.3. Applying GZIP compression to responses

6.2. Receiving files

6.2.1. SUpporting file uploads

6.2.2. Configuring the upload support

6.2.3. Handling upload errors

6.3. Summary

7. Server-side templating

7.1. Deciding whether server-side templating is right for you

7.1.1. Web sites

7.1.2. Web APIs

7.2. Introducing Scalate

7.2.1. Installing Scalate in a Scalatra app

7.2.2. Scalate directory structure

7.3. Serving content with the Scalate template system

7.3.1. Your first Scaml template

7.3.2. Layouts

7.3.3. Invoking your template

7.3.4. A comparison of dialects

7.4. Serving content with Twirl

7.4.1. Configuring your project

7.4.2. Using Twirl

7.5. Summary

8. Testing

8.1. Integration testing with Specs2

8.1.1. Getting started with Specs2

8.1.2. Asserting over the entire response

8.1.3. Testing as JValues

8.1.4. Running your tests

8.2. Unit testing with Specs2

8.2.1. Testing with stubbed dependencies

8.2.2. Injecting dependencies into a servlet

8.3. Testing with ScalaTest

8.3.1. Setting up Scalatra's ScalaTest

8.3.2. Your first ScalaTest

8.3.3. Running ScalaTest tests

8.4. Summary

9. Configuration, build, and deployment

9.1. Configuring a Scalatra application

9.1.1. Working with application environments

9.1.2. Using a type-safe application configuration

9.2. Building a Scalatra application

9.2.1. A Scalatra application as an sbt project

9.2.2. Working with the xsbt-web-plugin

9.2.3. Using sbt-web to simplify working with web assets

9.2.4. Precompiling Scalate templates with the scalate-generator plugin

9.3. Deploying as a web archive

9.4. Deploying as a standalone distribution

9.4.1. Embedding a servlet container in an application

9.4.2. Building a distribution package

9.5. Running Scalatra as a Docker container

9.6. Summary

10. Working with a database

10.1. Working with a relational database and example scenario

10.2. Integration of Slick and working with a DBIO action

10.3. Defining table models and working with a TableQuery

10.4. Using the query language

10.4.1. Defining queries

10.4.2. Defining joins

10.4.3. Update and delete statements

10.4.4. Organizing queries as extension methods

10.5. Summary

Part 3: Advanced topics

11. Authentication

11.1. A new, improved Hacker Tracker

11.2. An introduction to Scentry

11.2.1. Session handling in Scalatra

11.2.2. Scentry setup

11.3. Protecting routes with HTTP Basic Authentication

11.3.1. The simplest possible Scentry strategy

11.3.2. A basic auth strategy

11.3.3. A basic authentication support trait

11.3.4. Protecting the DatabaseSetupController

11.4. Using forms authentication

11.4.1. A simple login form

11.4.2. Building a UserPasswordStrategy

11.4.3. Creating an AuthenticationSupport trait

11.4.4. Protecting your controllers with AuthenticationSupport

11.4.5. Deciding whether to run a strategy

11.4.6. Logging out

11.5.1. Building the RememberMeStrategy

11.5.2. Scentry callbacks

11.6. Summary

12. Asynchronous programming

12.1. Exploring concurrency in Scalatra

12.2. Using Futures in Scalatra

12.3. Using Akka Actors from Scalatra

12.4. Using Scalatra for Big Data

12.5. Summary

13. Creating a RESTful JSON API with Swagger

13.1. An API for the Hacker Tracker

13.2. Adding an API controller

13.2.1. Returning Hacker JSON from the ApiController

13.2.2. Refactoring to remove duplication

13.3. Self-documenting APIs using Swagger

13.3.1. Swagger—what is it?

13.3.2. Swaggerize the application

13.3.3. Documenting the routes

13.3.4. Cross-origin API security and Swagger UI

13.4. Securing your API

13.4.1. HMAC—what is it?

13.4.2. An HMAC calculator in Scala

13.4.3. Protecting the API with a trait

13.5. Summary

Appendixes

Appendix A: Installationg and Development Setup

A.1. Installation

A.1.1. Install JDK 7

A.1.2. Installing Conscript and Giter8

A.1.3. Generating your first Scalatra project

A.1.4. Building your code with the Simple Build Tool (SBT)

A.1.5. The structure of a Scalatra project

A.1.6. Configuring your application with SBT

A.1.7. SBT tasks

A.1.8. Mounting servlets in ScalatraBootstap

A.2. Do you need an IDE?

A.2.1. Emacs

A.2.2. Eclipse

A.2.3. IntelliJ IDEA

A.3. Summary

About the Technology

Scalatra is a lightweight Scala web framework similar to the popular Ruby-based Sinatra. It’s perfect for running real-time applications on multicore servers, and is a fast way to spin up web apps and build HTTP APIs for mobile, Backbone.js, and AngularJS apps.

About the book

Scalatra in Action covers the Scalatra framework in its entirety, starting with concepts such as request routing, input handling, actions, and HTTP responses. For readers who don’t already know Scala, the book introduces the Scala language and sbt, the Simple Build Tool. You’ll learn how to use Scalatra’s powerful templating engine, Scalate. It also covers advanced topics such as data access, handling heavy load, asynchronicity, securing your application, designing RESTful APIs, and real-time web programming.

What's inside

  • Make clean templates using Scalate
  • Integrate with libraries that supplement Scalatra
  • Write tests using Specs2
  • Integrate Scalatra with databases

About the reader

Readers should be familiar with the basics of HTTP, REST, and web applications. No experience with Scalatra, Sinatra, or Scala is required.

About the authors

Dave Hrycyszyn is technical director for a London-based agency specializing in agile software design and development. Stefan Ollinger is an active Scalatra contributor. Ross A. Baker is a Senior Cloud Engineer, a Scalate commiter, and organizer of the Indy Scala meetup.


Buy
  • combo $44.99 pBook + eBook
  • eBook $35.99 pdf + ePub + kindle

FREE domestic shipping on three or more pBooks

There are many ways to learn Scalatra, but the best one is with Scalatra in Action.

Adam Słysz, Founder of Binary Horizon

An excellent way to become productive from day one.

Ramsés Morales, VMware

Clear and thorough coverage of the Scalatra framework.

Alain Couniot, STIB-MIVB