Friday, November 28, 2014

Book Review: RESTful Java Patterns and Best Practices

I had the privilege to review the book RESTful Java Patterns and Best Practices, authored by Bhakti Mehta for Packt Publishing.  In short, this book hits the sweet spot when it comes to learning utilization of RESTful web services.  As the book provides a solid foundation for the creation, testing, and utilization of RESTful web services with JAX-RS 2.0, it also provides information on essential topics, such as authentication, security, and performance.  There is even coverage of advanced topics, such as rate-limiting, polling, and micro services.  




The book begins by providing a general understanding RESTful web services, design principals, and the differences between the various HTTP verbs.  This discussion is provided at a high level, such that it is generalized enough to cover various RESTful web service implementations.  The first chapter then goes into the Java API for RESTful Web Services.  This section of the first chapter is brief, and to the point.  It clearly explains how to create services and then test/access them via the Client API, cURL, and other utilities.  As mentioned at the top of this post, Chapter 1 provides a solid foundation...and it is a quick read also, which enables developers to get up and running quickly with the JAX-RS API.

Chapter 2 covers fundamentals, such as returning various content types (JSON, XML, etc.), and designing meaningful URL patterns to obtain the desired media type.  This chapter delves into streaming and chunked content, parsing JSON, and versioning support.  Chapter 2 builds upon Chapter 1 very nicely, further explaining the fundamental concepts of RESTful services.

The third chapter covers security, validation, and logging of RESTful web services.  This is a chapter for intermediate users, as it goes beyond the basics and covers the use of filters for logging, and constraint annotations for validation.  It then provides a brief example of building an exception class.  Lastly, this chapter goes into authentication practices using SAML (Security Assertion Markup Language), OAuth, and tokens.  While this chapter sets a good stage for discussing the various types of authentication, it does not go into detail on these topics.

Chapter 4 covers performance via caching, asynchronous and long-running jobs, and partial updates.  The chapter clearly explains how to utilize the Cache-Control header and ETags.  It then explains how to design asynchronous web services, and some best practices.  The last topic in the chapter is partial updates...a topic that is briefly explained, but leaves room for further exploration.

The next chapter goes into advanced topics for RESTful web services...including rate-limiting, response pagination, internationalization and localization, and pluggability and extensibility.  I really liked the way that this chapter covered rate-limiting in detail, and explained why this is an important topic.  It then provides a detailed explanation for response pagination, along with a good example.  The internationalization and localization section is brief, but provides a good enough explanation for getting started with internationalization.  Lastly, the chapter provides a brief overview HATEOAS (Hypermedia as the Engine of Application State), testing, and extensibility.

The final chapter does a good job of wrapping up the many topics of RESTful web services, discussing the future of REST and emerging standards.  It is a chapter that explains polling and the importance of providing real-time APIs for avoiding inefficiencies.  Along the way, it touches upon various topics, such as WebHooks, SSEs (Server Sent Events), and WebSockets, and how they relate to RESTful services.

The book contains a helpful Appendix which covers various APIs from GitHub, Facebook, and Twitter.  It is a nice closeout to the book, providing an overview of how to glean information from these important social media networks.

Overall, this book is a great read for getting started with RESTful web service creation and utilization with JAX-RS.  I enjoyed the fact that the book was authored by Bhakti, who is very experienced with the topic, as she is a founding member of the GlassFish Application Server.  I recommend this read for anyone who wants to get up and running quickly with REST, and also learn about various other RESTful concepts along the way.  While this book does not delve into fine-grained details of topics such as authentication and partial updates, it provides a good foundation for learning more on such topics.  Readers will go away from this book with a solid understanding of how to develop RESTful web services, and they will be able to start developing services after reading through the book.  Those who wish to delve into more advanced topics will be provided with a good overview after reading this book, and they'll be pointed into the right direction for learning more on such topics.

Sunday, November 23, 2014

NetBeans Tip: Working with JPQL

Have you heard of the NetBeans JPQL tool?  This is an often overlooked feature of NetBeans IDE that can save developers a lot of time.

If you are a Java EE developer, then you've likely encountered moments when you would like to see the results of a Java Persistence Query Language (JPQL) query before you've written your session bean method.  Being that I come from a database administrator background, I am oftentimes finding myself at the SQL command-line (or NetBeans SQL Editor) writing queries, and then translating the SQL into JPQL.  NetBeans IDE contains a JPQL tool (since NetBeans 7.3), which makes it easy to write JPQL queries and retrieve results immediately.  Let's see how:

The JPQL tool can be invoked within the context of a NetBeans project.  The project must have a valid Persistence Context, meaning that it is wired up for database access.  Once this is in-place, simply right click on the persistence.xml file, and click on the "Run JPQL Query" to open the tool.





















The tool provides a nice editor that provides a top pane for entering a query, and a bottom pane for showing the SQL or the results of the query execution.  If you have more than one Persistence Context within your persistence.xml, then you will have the option to select which context you would like to utilize for the query.












To execute the JPQL, simply click on the button next to the Persistence Context pull-down, or right click in the query editor and choose "Run JPQL".

The tool comes in super handy when you are executing queries that involve more than one entity.













Another handy reference for the tool, Geertjan's blog post:
https://blogs.oracle.com/geertjan/entry/test_jpql_with_netbeans_ide

Note:  Be sure to "Build" your project successfully before trying to use the JPQL tool.  If you do not have a successful build, you will likely encounter an error stating that the abstract schema type cannot be found:

Tuesday, November 04, 2014

Building and Testing JSF.next

The JSF 2.3 Expert Group is hard at work determining which features will be part of the upcoming release.  The JSF Team been working hard improving CDI alignment, among other things.  There are already a number of new features in the JSF 2.3 codebase that you can begin to test.  I will attempt to keep this post updated with the latest features that have been added.  For the most up-to-date reference, please see Manfred's blog.  To reference the information from Manfred's blog, take a look here.

JSF 2.3 Features

**Updated 04/2015:

 - PostRenderViewEvent added, which is published immediately after a view is rendered

    <f:event type="postRenderView" listener="#{managedBean.doPostProcessing}" />

 - Support java.lang.Iterable in UIData and UIRepeat
 - Support for java.util.Map in UIData and UIRepeat
 - Facelets hot reload will be off by default.
   In JSF 2.2 and prior, Facelets were set to refresh the cache periodically by default.
 - Injection into the following artifacts:
  •     javax.faces.convert.Converter
  •     javax.faces.validator.Validator
  •     javax.faces.component.behavior.Behavior
Arjan Tijms explains the enhancements for CDI injection and resolving of EL artifacts very well in his post on JSF 2.3.  
Below, I list the default producers that are part of JSF 2.3.  
(As per Arjan Tijms post:  What's New in JSF 2.3?)

In order to facilitate CDI injection, there will be default producers for the following:
  •  ViewRoot
  •  ViewMap (@ViewMap)
  •  RequestCookieMap (@RequestCookieMap)
  •  SessionMap (@SessionMap)
  •  ApplicationMap (@ApplicationMap)
  •  FacesContext
  •  ExternalContext
- CDI managed validator:
  @FacesValidator(value = "myValidator", managed = true)
  public class MyValidator implements Validator {
     @Inject
     MyModel model;
    ..
  }
- CDI managed converter:
  @FacesConverter(value = "myConverter", managed = true)
  public class MyConverter implements Converter {
    ..
  }
The features that have been added as of 11/4/2014 are as follows: - Inject @ViewMap
@ViewMap
@Inject
Map viewMap;
- #1333 - Support @Inject for UIViewRoot

@Inject
UIViewRoot viewRoot;
- #1332 - Let CDI handle #{view}
- #1331 - Let CDI handle #{application}
- #1254 - contracts attribute too restrictive.
- #1328 - Let CDI handle #{session} EL resolving
- #1325 - Let CDI handle #{applicationScope}
- #1311 - Let CDI handle #{facesContext} EL resolving
- #1323 - Support @Inject for the applicationMap

@ApplicationMap
@Inject
Map applicationMap;
- #1322 - Simplify #{externalContext} to use ExternalContextProducer
- #1309 - Support @Inject for ExternalContext

@Inject ExternalContext externalContext;
- #527 - Support @Inject for FacesContext

 @Inject FacesContext facesContext;
Please reference the JavaServer Faces JIRA for more information.

  * Be sure to test against one of the milestone releases before placing JIRA issues.

  Taking JSF 2.3 for a Test Spin

 If you would like to start testing out these new features today, the easiest way to get started is to simply download the 2.3 SNAPSHOT and then replace the javax.faces.jar file within your GlassFish/glassfish/modules directory with the snapshot. You will need to be sure to reference version 2.3 in your faces-config.xml. as follows:
<faces-config  version="2.3"
              xmlns="http://xmlns.jcp.org/xml/ns/javaee"
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_3.xsd">

</faces-config>
If you are adventurous and you would like to build JSF from source, that is also fairly simple.  To do so, follow these steps:
 1)  Clone the trunk using Git: git clone https://github.com/javaserverfaces/mojarra.git
(Old Repository Path: Check out the trunk using SVN: svn checkout --username yourname --password x https://svn.java.net/svn/mojarra~svn/trunk)
 2)  Copy the build.properties.glassfish file to build.properties
 3)  Edit the build.properties file and set jsf.build.home to your source home.
 4)  From the source home, run ant clean main The jsf-api.jar will be in SOURCE_HOME/jsf-api/build/lib and jsf-impl.jar will be in SOURCE_HOME/jsf-ri/build/lib

For more information on building the sources (and even contributing), please see the following reference: https://wikis.oracle.com/display/GlassFish/JavaServerFacesRI#JavaServerFacesRI-HowcanIcontribute%3F