Play Framework Essentials
Table of Contents
Play Framework Essentials
Credits
About the Author
About the Reviewers
www.PacktPub.com
Support files, eBooks, discount offers, and more
Why subscribe?
Free access for Packt account holders
Preface
What this book covers
What you need for this book
Who this book is for
Conventions
Reader feedback
Customer support
Downloading the example code
Errata
Piracy
Questions
1. Building a Web Service
Play â a framework used to write web applications
Bootstrapping a Play application
Play applications' layout
URL routing
Route path parameters
Parameters type coercion
Parameters with fixed values
Query string parameters
Default values of query string parameters
Trying the routes
Building HTTP responses
Serializing application data in JSON
Reading JSON requests
Validating JSON data
Handling optional values and recursive types
Summary
2. Persisting Data and Testing
Testing your web service
Writing and running tests
Testing the HTTP layer
Using the reverse router to generate URLs
Running a fake Play application
Effectively writing HTTP tests
Persisting data
Using a relational database
Getting a reference to the currently running application
Using a database management library
Using Slick in Scala
Using JPA in Java
Integrating with other persistence technologies
Populating the database with fixtures
The application's Global object
Managing database schema evolutions
Using an in-memory database for tests
Summary
3. Turning a Web Service into a Web Application
Delta between a web service and a web application
Using the template engine to build web pages
Inserting dynamic values
Looping and branching
Reusing document fragments
Comments
Import statements
Generating HTML forms
Repeated and nested fields
Reading and validating HTML form data
Handling the HTML form submission
Validating the HTML form data
The Scala form validation API
The Java form validation API
Optional and mandatory fields
Sharing JSON validation and HTML form validation rules
Handling content negotiation
Putting things together
Writing web user interface tests
Summary
4. Integrating with Client-side Technologies
Philosophy of Play regarding client-side technologies
Serving static assets
Sprinkling some JavaScript and CSS
Using the JavaScript reverse router
Managing assets from the build system
Producing web assets
Pipelining web assets' transformations
Concatenating and minifying JavaScript files
Gzipping assets
Fingerprinting assets
Managing JavaScript dependencies
Running JavaScript tests
Summary
5. Reactively Handling Long-running Requests
Play application's execution model
Scaling up your server
Embracing non-blocking APIs
Managing execution contexts
Writing incremental computations using iteratees
Streaming results using enumerators
Manipulating data streams by combining iteratees, enumerators, and enumeratees
Unidirectional streaming with server-sent events
Preparing the ground
Transforming streams of data using enumeratees
Implementing a publish/subscribe system using Akka
Bidirectional streaming with WebSockets
Controlling the data flow
Summary
6. Leveraging the Play Stack â Security, Internationalization, Cache, and the HTTP Client
Handling security concerns
Authentication
Cross-site scripting
Cross-site request forgery
HTTP request filters
Using the CSRF filter
Enabling HTTPS
Saving computation time using cache
Serving content in several languages
Calling remote web services
Background â the OAuth 2.0 protocol
Integrating your application with your preferred social network
Implementing the OAuth client
Calling the HTTP API of your preferred social network
Summary
7. Scaling Your Codebase and Deploying Your Application
Making an action's logic reusable and composable with action builders
Capturing the logic of actions that use blocking APIs
Capturing the logic of actions that require authentication
Combining action builders
Modularizing your code
Applying the inversion of control principle
Using dynamic calls in route definitions
Setting up a dependency injection system
Making your code injectable
Mocking components
Splitting your code into several artifacts
Splitting your controller layer into several artifacts
Application deployment
Deploying to your dedicated infrastructure
Deploying to the cloud
Handling per environment configuration
Overriding configuration settings using Java system properties
Using different configuration files
Summary
Index
Play Framework Essentials
Copyright © 2014 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.
First published: September 2014
Production reference: 1190914
Published by Packt Publishing Ltd.
Livery Place
35 Livery Street
Birmingham B3 2PB, UK.
ISBN 978-1-78398-240-0
www.packtpub.com
Author
Julien Richard-Foy
Reviewers
Shannon âjj Behrens
CĂ©dric Chantepie
Commissioning Editor
Amarabha Banerjee
Acquisition Editor
Vinay Argekar
Content Development Editor
Akashdeep Kundu
Technical Editors
Indrajit A. Das
Taabish Khan
Humera Shaikh
Copy Editors
Deepa Nambiar
Laxmi Subramanian
Project Coordinator
Kartik Vedam
Proofreaders
Simran Bhogal
Maria Gould
Ameesha Green
Paul Hindle
Indexers
Monica Ajmera Mehta
Tejal Soni
Graphics
Abhinash Sahu
Production Coordinators
Aparna Bhagat
Manu Joseph
Cover Work
Aparna Bhagat
Julien Richard-Foy likes to design code that seamlessly expresses the ideas he has in mind. He likes finding the right level of abstraction, separating concerns, or whatever else that makes the code easy to reason about, to maintain and to grow.
He works at Zengularity, the company that created the Play framework, and actively contributes to the evolution of the framework.
He aims at working on technically challenging and innovative projects that have a positive envir...