Hints

Debugging Tips for NetBeans/Glassfish (that might be useful for the assignments)

It is unlikely that you will need to refer to the following information during the weekly practical activities.

They might come in handy when you start working on your own.

Attached is the simplified workflow handed out during the Week 1 in-lab exercise.

The first page contains instructions for setting up a new repository (and at the bottom of the page, how to look at older version). The second page shows a development workflow to use once your repository has been set up.

The dark black circles and arrows indicate the steps you follow when you're working alone. When you're working in a team, you would follow both the dark black and light gray arrows.

Payara server is a drop-in replacement for GlassFish. It should be fully compatible with your current GlassFish installation but resolves a number of bugs.

Here’s how to install it on your personal computer / laptop:

  1. Exit NetBeans
  2. Download the latest Payara Server from http://www.payara.fish/downloads
  3. Locate the GlassFish Server installation folder on your computer. On Linux it will typically be /usr/usr/local/glassfish-4.1.1, on Windows it will be in C:\Program Files\glassfish-4.1.1 and on Mac it will be in /Applications/NetBeans/glassfish-4.1.1.
  4. Inside the GlassFish installation folder there will be several directories (bin, glassfish, javadb and so on): check that those directories are there and then delete everything so the GlassFish installation folder is empty.
  5. Inside the Paraya Server zip file that you downloaded, there is a folder called payara41 containing similar directories (bin, glassfish, javadb and so on). Unzip these directories into your GlassFish installation folder.
  6. Restart NetBeans - now when you see GlassFish 4.1.1, it will be using Payara Server.

Sometimes the Application Server might not be deploying your code properly.

This happens most often in four scenarios:

  1. If you are changing annotations on your code (e.g., making @SessionScoped into @RequestScoped) but letting NetBeans automatically redeploy whenever you save.
  2. You have an Enterprise Application consisting of an EJB project and a Web project, but there was a compile problem.
  3. You have an Enterprise Application but you've accidentally deployed the Web project separately to the Enterprise Application project.
  4. You've made lots of changes to your code, deploying often, but things suddenly stop working in ways that don't make sense.

Here are some steps to follow that usually solve most problems.:

  1. Deploy the application again.

    Right click on the project and click "Clean". Right click again and "Run" or "Deploy". Does it work now?

  2. Undeploy all applications. Deploy again.

    In the Services tab, locate Servers > GlassFish Server > Applications. Right click on Applications to refresh the list. Select all the Applications with your mouse (hold down the shift key). Then right click and select "Undeploy".

    Deploy your project again. Does it work now?

  3. Clear your browser history.

    You don't need to clear everything. You only need to clear the cookies associated with GlassFish. Another way to do this is to start a private/incognito browsing window.

  4. Undeploy all applications. Restart GlassFish. Deploy again.

    If you are seeing error messages about NetBeans being unable to delete JAR files, then you should undeploy all applications and restart GlassFish. To restart GlassFish, in the Services tab locate Servers > GlassFish Server. Then, right click on GlassFish Server and select Restart (or Stop and then Start).

If JavaServer Faces isn't working for you, the most common problems are:

  1. Your backing bean is annotated with @Named but you've forgotten to make the first letter lowercase in expression language.

    e.g., MyBackingBean should be referred to in expression language as #{myBackingBean} (the first letter is different).

  2. You have imported the wrong package for @RequestScoped or @SessionScoped.

    Use javax.enterprise.context.*. Do NOT use javax.faces.bean.*.

    Yes, this is counterintuitive. The reason is that you are using @Named which works with the more modern CDI annotations that are in javax.enterprise.context.

  3. Your code doesn't compile

    In NetBeans, check that your Java files don't have little red exclamation marks on their file icons in the Projects tab.

Intercepting Connections

Attached is a simple utility that can be helpful for debugging JAX-RS clients. When you see the actual data being sent via the network, you can figure out why things are going wrong.

It accepts HTTP connections, forwards it to a HTTPS connection and does a search-and-replace in the stream to ensure any "Host" headers are correct. It outputs both sides of the connection to a window.

To use it, change your target from https://test-api.pin.net.au/blahblahblah to http://localhost:8081/blahblahblah.

Save it as a Java file in a project and then right click on the Java file and select "Run".

Using Basic Authentication

Adam Bien has an easy guide to using Basic Authentication in a JAX-RS client.

For example, the Pin Payments API documentation states that you should use HTTP basic authentication with your secret API key (find it when you log in) as the username and a blank string as the password.

Handling Error Responses

Some APIs can return different JSON structures based on the response code.

Here are two possibilities for handling error messages in the JAX-RS client:

  1. Use javax.ws.rs.core.Response as the response type, check for an error code than read the actual JSON data using response.readEntity(...).

    There's an example in accepted answer for this StackOverflow question: https://stackoverflow.com/questions/22561527/handling-custom-error-response-in-jax-rs-2-0-client-library

  2. You could have a single bean that combines attributes for successful and unsuccessful responses. The error attributes would be ignored in success, and the success attributes would be ignored during an error.

    In the second approach, you would want to be careful about not using the combined Error/Success class as a business object. However, it might make reasonable design sense if you only treat it as a web service response "message".