Java Ninja Chronicles By Norris Shelton

Things I learned in the pursuit of code

This is an example of how to make a global exception handler. Our company had a need to work with an external vendor. They needed to get error states (person not found, account disabled, insufficient access, etc). These were mapped to various HTTP status codes and they wanted a JSON body with specific information in it. Springframework provides the ability to specify an exception handler that can process exceptions for multiple controllers.

The business logic was handled by creating Exception objects for each type of exception that was needed. Each custom exception had an empty constructor and a constructor that accepts the message. This is an example:

package com.javaninja.exception;

/**
 * Missing, invalid or expired token.
 */
public class AuthorizationException extends Exception {

    /**
     * Constructs a new exception with {@code null} as its detail message. The cause is not initialized, and may
     * subsequently be initialized by a call to {@link #initCause}.
     */
    public AuthorizationException() {
    }

    public AuthorizationException(String message) {
        super(message);
    }
}

Once you have the exceptions, you are ready to create something that can handle them. Springframework used to only support exception handlers that were within the individual controllers.

As of Spring MVC 3.2, they now support an exception handler that can handle exceptions for multiple controllers via @ControllerAdvice. This takes an optional property basePackages. The basepackages property allows you to set which packages this exception handler handles exceptions for. You can not specify it for the exception handler to apply to all controllers or you can list a package or multiple packages that are comma-separated.

An example of an exception handler that handles all packages is:

@ControllerAdvice
public class MyExceptionHandler {
    //...
}

An example of an exception handler that handles a single package is:

@ControllerAdvice(basePackages = "com.javaninja.services")
public class MyExceptionHandler {
    //...
}

An example of an exception handler that handles multiple packages is:

@ControllerAdvice(basePackages = "com.javaninja.services,com.javaninja.other")
public class MyExceptionHandler {
    //...
}

Now that you have a class that can handle exceptions, it’s time to get to work. To handle an exception, you annotate a method with @ExceptionHandler. There is an optional value property. You can specify one or more exceptions to handle. If you don’t list any exceptions, the exception handler will handle all exceptions.

An example from the Springframework documentation of an exception handler method that handles a single exception is:

@ExceptionHandler(IOException.class)
public ResponseEntity<String> handleIOException(IOException ex) {
// prepare responseEntity
return responseEntity;
}

An example that handles several exceptions is:

@ExceptionHandler(value = {IOException.class, SqlException.class})
public ResponseEntity<String> handleIOException(IOException ex) {
// prepare responseEntity
return responseEntity;
}

An example that handles all exceptions is:

@ExceptionHandler
public ResponseEntity<String> handleIOException(IOException ex) {
// prepare responseEntity
return responseEntity;
}

These methods returned a ResponseEntity. This offers you a tremendous amount of flexibility. Much more than I needed in my case.

If you are working on JSON services, you can use normal things like @ResponseBody to translate an object into it’s JSON representation.

    @ExceptionHandler(IOException.class)
    @ResponseBody
    public MyExceptionResponse handleThisException(Exception e) {
        return createResponse(e);
    }

Returning a certain HTTP status code after encountering an exception isn’t an uncommon thing. To do this, you use @ResponseStatus. There is a required value of HttpStatus type. An example is:

    @ExceptionHandler(IOException.class)
    @ResponseBody
    @ResponseStatus(value = HttpStatus.BAD_REQUEST)
    public MyExceptionResponse handleThisException(Exception e) {
        return createResponse(e);
    }

Now, to bring this all together. This is an full example:

package com.javaninja.exception;

import org.joda.time.DateTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

import javax.validation.ConstraintViolationException;

/**
 * Handles exceptions for the Java Ninja.
 * @author norris.shelton
 */
@ControllerAdvice
public class ThisExceptionHandler {

    private Logger logger = LoggerFactory.getLogger(getClass());

    /**
     * Handle invalid request exceptions.
     * @param e exception object to be handled
     * @return exception response as a JSON body
     */
    @ExceptionHandler(ClassNotFoundException.class)
    @ResponseStatus(value = HttpStatus.NOT_FOUND)
    @ResponseBody
    public MyExceptionResponse handleBadRequestException(Exception e) {
        return createResponse(e);
    }

    /**
     * Handle authorization exceptions.
     * @param e exception object to be handled
     * @return exception response as a JSON body
     */
    @ExceptionHandler(value = {CGIServerException.class, AbandonedObjectException.class})
    @ResponseStatus(value = HttpStatus.UNAUTHORIZED)
    @ResponseBody
    public MyExceptionResponse handleAuthorizationException(Exception e) {
        return createResponse(e);
    }


    /*
     * Create the response body
     */

    /**
     * Create the response object.
     * @param e exception object to be handled
     * @return exception response object
     */
    private MyExceptionResponse createResponse(Exception e) {
        logger.info("{}  message: {}", e.getClass().getSimpleName(), e.getMessage());
        MyExceptionResponse exceptionResponse = new MyExceptionResponse(e.getClass().getSimpleName(),
                                                                        e.getMessage(),
                                                                        DateTime.now());
        logger.debug("exception response: {}", exceptionResponse);
        return exceptionResponse;
    }
}

When this exception handler encounters an exception, it returns a specific error code and the JSON payload appears similar to:

{
    "code": "TxnNotFoundException",
    "msg": "Unable to find the requested transaction",
    "timestamp": "2015-07-30 12:51:54"
}

July 30th, 2015

Posted In: Java, java ninja, Javaninja, Spring

Tags: , , ,

Leave a Comment

There are several times when you need to specify a location in a Springframework configuration. An example is

<util:properties id="gsiProperties" location="classpath:gaming-adapter-default.properties"/>

A location is represented as a Resource. There are several ways to specify where to find the resource.

  • classpath: This represents a resource that should be retrieved from the classpath. It works the way a normal classloader does. The first match wins. Package names can also be represented as file directories (e.g. /this/that/my.properties
  • classpath:* This means that ALL resources that match the given name must be obtained, then merged to form a single resource. Meaning multiple property files could be mapped to. The sum of them would be used to supply the properties.
  • file: This is used to access a file system resource. This can be an absolute path or relative path.
  • http: This is used to access resources via HTTP protocol with a URL.
  • ftp: this is used to access a resource via FTP protocol.
  • blank This relies upon the implementation of the underlying context. For web applications, this utilizes the Servlet context.

July 28th, 2015

Posted In: java ninja, Javaninja, Spring

Tags: , , , , ,

Leave a Comment

Intellij IDEA doesn’t show hidden files in the file chooser dialog on Mac OS/X. This can be frustrating when you are trying to find files that are always in a hidden directory, such as Maven files. There are two ways around this.

This will enable it for the current window

ctrl + shift + .

There is another solution, but it involves editing the registry. Hit the following keys to access the Find Action menu.

cmd + shift + a

Once there, scroll down to ide.mac.file.chooser.show.hidden.files. Check the checkbox and select Close and away you go.

July 28th, 2015

Posted In: IntelliJ, java ninja, Javaninja, Mac, OS/X

Tags: , , ,

One Comment

A common problem is to retrieve a property from a property files and to use that value within your code. The Springframework has several tools that make that very easy to do.

The first thing to do is to create your properties file. Mine contains the following:

aggregatorId=gsi
igpUrl=http://localhost:8080/igp-services
defaultSupplierId=voi

Next, you need to make Spring aware of the properties file. To do this, we will use the util schema. Line 4 shows that the properties has an id value. This allows us to later use that idid.key, instead of key.

xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
...
<util:properties id="gsiProperties" location="gaming-adapter-default.properties"/>

Now that you have the properties available to Spring, it’s time to use them. Notice that we are using Spring Expression Language (SpEl) to inject the value(…) of the key. Normally, I would use @Autowired or @Resource(name=”…”) to inject the properties object as a map, then call get(…) on the map. SpEl gives you an additional notational choice to inject the data. @value(…) can be placed on fields, methods, method parameters and constructor parameters. Inside the @value, you can use #{…}. You do not need # for predefined variables (e.g. @value({systemProperties.property})). If the property cannot be listed directly because it contains a period, etc, you can place it within square brackets with a single tick. An example is @Value(#{systemProperties[‘user.region’]}).

    @Value("#{gsiProperties.igpUrl}")
    private String igpUrl;

    @Value("#{gsiProperties.aggregatorId}")
    private String aggregatorId;

    @Value("#{gsiProperties.defaultSupplierId}")
    private String defaultSupplierId;
...
    model.setIdAggregator(aggregatorId);
    model.setIdGameSupplier(defaultSupplierId);

July 23rd, 2015

Posted In: Java, java ninja, Javaninja, SpEl, Spring

Tags: , , , ,

Leave a Comment

HTML5 is making geolocation much, much easier to do. It is now a standard API and it’s pretty easy to do.

    The Geolocation API is supported by the following browser versions and up:

  • Chrome 5.0
  • Firefox 3.5
  • Android 2.0
  • Iphone 3.0
  • IE 9.0+
  • Safari 5.0
  • Opera 10.6

The basic functionality to get the user’s current geolocation is accessed by:

<script>
     navigator.geolocation.getCurrentPosition(success, error, positionOptions);
</script>

The error handler and the options are optional. My signature is as follows:

<script>
     navigator.geolocation.getCurrentPosition(
          handleGeolocationSuccess,
          handleGeolocationError,
          positionOptions
);
</script>

It is also possible watch a user’s position. If you watch a user’s position, then you don’t have to poll it. The browser will notify you when the user’s location changes.

This is how the watch functionality is accessed:

<script>
     var id = navigator.geolocation.watchPosition(success, error, positionOptions);
</script>

The id is needed to stop the watch functionality.

<script>
     navigator.geolocation.clearWatch(id);
</script>

The positionOptions object is used to specify options to the geolocation API. The definition of the positionOptions object is as follows:

  • enableHighAccuracy – boolean. Require high accuracy (e.g. GPS).
  • timeout – milliseconds. How long should the code wait till it determines that it can’t get a location
  • maximumAge – the maximum age of a previous location that can be used. Use the last position that the browser has as long as that position is less than N milliseconds old. Set to 0 to require the current position immediately.

An example of the positions object is:

{enableHighAccuracy: true, timeout: 5000, maximumAge: 60000}

The full example is below:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Java Ninja Geolocation prototype</title>
</head>
<body>
    <button onclick="getCurrentGeolocation()">Locate Me!!!</button>
    <button onclick="watchGeolocation()">Watch Me!!!</button>
    <button id="stopWatchingGeolocation" style="display: none" onclick="stopWatchingGeolocation()">
        Stop watching Me!!!
    </button>

    <script>
        /** Variable to make seconds more readable. */
        var seconds = 1000;  // 1000 milliseconds = 1 second

        /** Positions Object to contain all of the settings that we want for the geolocation.getCurrentPosition call. */
        var positionOptions = {
            /** require high accuracy (e.g. GPS). */
            enableHighAccuracy: true,
            timeout: 5 * seconds,
            /**
             * Use a position that is up to a minute old.
             * Set to 0 to ask for the current position right now.  Makes it faster and less expensive for the user if
             * they are getting repeated requests.
             */
            maximumAge: 60 * seconds
        };

        /** The geolocation watch id.  This is used to stop the watch. */
        var watchGeolocationId = 0;

        /**
         * Get the user's current geolocation.
         */
        function getCurrentGeolocation() {
            navigator.geolocation.getCurrentPosition(
                    handleGeolocationSuccess,
                    handleGeolocationError,
                    positionOptions
            );
        }

        /**
         * Watch the user's geolocation.
         */
        function watchGeolocation() {
            watchGeolocationId = navigator.geolocation.watchPosition(
                    handleGeolocationSuccess,
                    handleGeolocationError,
                    {enableHighAccuracy: true, timeout: Infinity, maximumAge: 0}
            );
            // Used old school so the example wouldn't have to pull in Jquery
            document.getElementById("stopWatchingGeolocation").style.display = "block";
        }

        /**
         * Stop watching the user's geolocation.
         */
        function stopWatchingGeolocation() {
            navigator.geolocation.clearWatch(watchGeolocationId);
            // Used old school so the example wouldn't have to pull in Jquery
            document.getElementById("stopWatchingGeolocation").style.display = "none";
        }

        /*
         * Geolocation handler functions
         */


        /**
         * Success handler for geolocation.getCurrentPosition.
         * @param position
         */
        function handleGeolocationSuccess(position) {
            var latitude = position.coords.latitude;
            var longitude = position.coords.longitude;
            var accuracy = position.coords.accuracy;

            alert('latitude=' + latitude + "\nlongitude=" + longitude + "\naccuracy=" + accuracy + " meters");
        }

        /**
         * Error handler for geolocation.getCurrentPosition.
         * @param err
         */
        function handleGeolocationError(err) {
            if (err.code == 1) {
                alert('PERMISSION_DENIED - User declined to provide geolocation data');
            } else if (err.code == 2) {
                alert('POSITION_UNAVAILABLE - Network is down or GPS satellites are unavailable')
            } else if (err.code == 3) {
                alert('TIMEOUT - Unable to determine location within the allotted time')
            }
        }
    </script>
</body>
</html>

July 9th, 2015

Posted In: Geolocation, HTML, HTML5

Tags: , ,

Leave a Comment

LinkedIn Auto Publish Powered By : XYZScripts.com