Book Image

Developing Middleware in Java EE 8

Book Image

Developing Middleware in Java EE 8

Overview of this book

Middleware is the infrastructure in software based applications that enables businesses to solve problems, operate more efficiently, and make money. As the use of middleware extends beyond a single application, the importance of having it written by experts increases substantially. This book will help you become an expert in developing middleware for a variety of applications. The book starts off by exploring the latest Java EE 8 APIs with newer features and managing dependencies with CDI 2.0. You will learn to implement object-to-relational mapping using JPA 2.1 and validate data using bean validation. You will also work with different types of EJB to develop business logic, and with design RESTful APIs by utilizing different HTTP methods and activating JAX-RS features in enterprise applications. You will learn to secure your middleware with Java Security 1.0 and implement various authentication techniques, such as OAuth authentication. In the concluding chapters, you will use various test technologies, such as JUnit and Mockito, to test applications, and Docker to deploy your enterprise applications. By the end of the book, you will be proficient in developing robust, effective, and distributed middleware for your business.
Table of Contents (18 chapters)
Title Page
Copyright and Credits
Dedication
Packt Upsell
Contributors
Preface
Index

Chapter roadmap


The learning journey we'll be moving on alongside the next chapter is planned to be as follows:

  • We'll start by introducing the concept of dependency injection and how to use it to simplify management processes, and the different dependencies and resources your application maintains.
  • We'll move on to data management topics by learning how to map your data into relational databases using the Java Persistence API, and how to validate your business objects against your business rules using the Java Validation API.
  • Next, we'll learn how to encapsulate business logic and expose it to other layers by learning how to build business components using the Enterprise JavaBeans API, and how to expose your business functionalities into RESTful services using the Java API for RESTful services, JAX-RS. We'll also cover a newly introduced API, JSON-B 1.0, and learn how to use it to perform complex JSON processing operations.
  • After that, we'll learn about another system-to-system communication model, which is messaging. We'll understand the concepts and architectural philosophy of messaging techniques, and learn how to apply our knowledge using JMS 2.0.
  • Then, we'll learn how to send notifications to our system users and send other information by sending them emails using the JavaMail API.
  • Then, we'll learn how to build interactive web applications with real-time communication using the WebSockets API.
  • Then, we'll learn how to secure our enterprise applications using the newly introduced Java Security API, and how to provide authentication and authorization features for any enterprise application, with this easy-to-use API.

Here's a full list of the main APIs we'll be covering in this book:

  • Contexts and Dependency Injection (CDI) 2.0
  • Java Persistence API 2.1
  • Bean Validation API 2.0
  • Enterprise JavaBeans 3.2
  • Java API for RESTful Services JAX-RS 2.1
  • JSON-Binding (JSON-B) API 1.0 (new)
  • Java Messaging System (JMS) 2.0
  • JavaMail 1.6.
  • Java Security 1.0 (new)
  • WebSockets 1.1

Contexts and dependency injection

When designing an enterprise solution, one of the primary tasks is to divide your application into separate components that interact with each other. To avoid all the hassle of managing our components, their dependencies, and their life cycles, the contexts and dependency API (CDI) has been developed to be the backbone of component and dependency management. By components, we mean objects that encapsulate your application's business logic. By dependencies, we mean commonly used application-shared resources such as a database connection, user sessions, web service endpoints, and such.

In Chapter 2, Dependency Injection Using CDI 2.0, we'll learn how to create and use CDI beans, how to use bean scopes, how to provide different implementations of the same bean, and how to inject beans into other beans. Moreover, we'll learn about some more advanced topics, such as producers, interceptors, and events.

Data persistence

The data access layer is the most fundamental part of any enterprise application. A common problem arises when dealing with a relational database from an object-oriented system—all runtime data are represented as objects, where the real data is stored as rows in tables. The Java Persistence API (JPA) provides Java developers with all the required operations, mappings, and techniques for mapping objects to the relational database.

In Chapter 3Accessing the Database with JPA 2.1, we'll learn how to create and use JPA entities and map them to tables and columns. Moreover, we'll learn how to perform the four CRUD operations: mapping entity relationships, using the JPA Query Language and the Criteria API, and mapping inheritance relationships.

Data validation

Validating your application's data is a required step before any of your business operations. Jakarta EE provides the Java Validation API, which integrates well with other APIs, like JPA and JAX-RS. With this API, you can declare all your validation rules to be processed automatically for you, whenever you receive new data from the user. It's as easy as annotating your domain objects with the appropriate annotation.

In Chapter 4Validating Data with Bean Validation 2.0, we'll learn how to use the Java Validation API to perform programmatic and automatic bean validation, how to validate graphs of objects in a bean, and the different validation constraints available. Moreover, we'll learn how to validate bean method parameters and return values, and how to define custom validation constraints to handle more complex and recurrent validation scenarios.

Enterprise JavaBeans

The core composing components of an enterprise application in Jakarta EE are Enterprise JavaBeans. They are plain Java objects with embedded business logic, supported by different services from the business tier, such as remoting, transaction management, user session management, timer services, and more.

In this book, we'll learn what enterprise Java beans are, what are they used for, and what services they provide for your embedded business code. We'll study and practice the three available types of sessions bean: stateless, stateful, and singleton.

RESTful services

RESTful services are a key technology for making computer systems talk to each other and, more specifically, making application frontends talk to their backends, and sometimes to other third-party integrations. They are functions that are deployed on a server and can be called remotely from any other system. RESTful is not just another protocol for remoting with HTTP; it's a full architectural style used to build your enterprise applications and make them extensible by other modules or separate systems.

In Chapter 5,  Exposing Web Services with JAX-RS 2.1, we will learn what RESTful services are and how to create your own RESTful services using JAX-RS. Moreover, we'll learn how to use Postman to test your RESTful services. JAX-RS topics such as accepting and processing user parameters, producing JSON responses, and uploading files are all covered in this chapter. In the final section, we'll learn about the newly introduced support for server-sent events in JAX-RS and look at examples of how to use it to provide your clients with on-demand real-time notifications about different business events.

JSON processing

As JSON is a very common format for exchanging data in REST operations, the need for a native API in Java to perform advanced processing over JSON has arisen. Finally, Jakarta EE was introduced?

In this book, we'll get an introduction to the newly introduced API, the JSON Binding API, and see how we can use it to directly manipulate JSON in advanced contexts with practical examples.

Messaging

Software-to-software messaging is another model of how software communicates with each other. In this model, a messaging middleware stands in the middle of a sender and a receiver to enable the reliable exchange of messages even if one of the two parties is out of service. Moreover, this model enables the application of scaling techniques such as load balancing and message broadcasting to subscribed components.

In this book, we'll learn the basic principles of messaging and how the JMS API provides a comprehensive framework to build a full-featured messaging system to realize the described messaging model. We'll also learn the difference between the two messaging models—Point-to-Point (P2P) and the Publish-Subscribe model—and how to implement both of them, with examples, using the JMS API.

Mailing

Software-to-user communication is also essential in any enterprise application. Software needs to mail its users about notifications, updates, changes, password change confirmations, and so on. Although in a large-scale development, a separate third-party cloud mailing solution would be a good idea, it's still very common to directly use the mailing APIs to communicate with our users, especially in small-scale developments.

In Chapter 8, Sending Mails with JavaMail 1.6, we'll get a brief introduction to the main mailing protocols. After that, we'll learn and practice how to programmatically send plain text and HTML emails, in addition to optionally attaching files to your emails.

WebSockets

WebSockets is one of the major overall advancements in HTTP communication. It extends HTTP to allow it to handle one or more full-duplex communication channels over a single HTTP connection, enabling all kinds of applications with real-time communication requirements to appear on the web market, such as chatting, multi-player gaming, collaborative document editing, and many more.

In Chapter 8Sending Mails with JavaMail 1.6, we'll learn what WebSockets are and when to use them. Moreover, we'll learn and practice how to create WebSockets endpoints in Java and how to create a client for them in JavaScript. In addition, we'll learn how to maintain and encode user state in our server. At the end of the chapter, we'll look at a complete example of using WebSockets to implement a cinema tickets booking interface for our book's project.

Security

Authentication and authorization are the most important aspects of any middleware solution. Any enterprise application should provide a way to authenticate users before letting them in and should also check their authorization before availing any functionality to them.

Although JACC and JASPIC have existed since the early days of Java EE, they have gotten more complicated as a result of their continuous evolution. The need to restructure the Security API was a priority request by Java EE developers over the years and, therefore, the Java Security API 1.0 was introduced in Jakarta EE 8.

In Chapter 9, Securing an Application with Java Security 1.0, we'll learn the concepts and terminology related to this new API and how to get started with it by creating a simple login example. Moreover, we'll take a more in-depth look at basic concepts, such as identity stores, authentication context objects, and authentication mechanisms.