Lombok Maven settings

dependency

<dependency>
 <groupId>org.projectlombok</groupId>
 <artifactId>lombok</artifactId>
 <version>1.16.16</version>
 <optional>true</optional>
</dependency>

plugin

<plugin>
 <groupId>org.projectlombok</groupId>
 <artifactId>lombok-maven-plugin</artifactId>
 <version>1.16.18.0</version>
 <executions>
  <execution>
   <phase>generate-sources</phase>
   <goals>
    <goal>delombok</goal>
   </goals>
   <configuration>
    <outputDirectory>${project.basedir}/src/main/java/</outputDirectory>
   </configuration>
  </execution>
 </executions>
</plugin>

Then, all domain classes goes to src/main/lombok(manually add source folder)
(optional)Eclipse build path ignore pattern for src/main/lombok: **/*.java
If you don’t want to ignore java files in the above folder, Eclipse will complain something like type already exists. It’s ok, because maven won’t pick up the lombok folder unless it’s explicitly set to be included.

This blog may not be up to date. Please see the original post from Blogger http://ift.tt/2tUTzp6
via IFTTT.

Quick links of Groovy/Grails related plugins

Following URLs are available for Eclipse new software install tool. Change the version number can get same plugins for different Eclipse versions.

Groovy compilers:
org.codehaus.groovy.eclipse.site – http://ift.tt/2tdZbbf (Neon)
org.codehaus.groovy.eclipse.site – http://ift.tt/2sujCUe (Oxygen)

Spring/Grails IDEs:
SpringSource Update Site for Eclipse 4.6 – http://ift.tt/2tdJiBv (Neon)
SpringSource Update Site for Eclipse 4.7 – http://ift.tt/2suGBi0 (Oxygen)

This blog may not be up to date. Please see the original post from Blogger http://ift.tt/2teecKa
via IFTTT.

Quick links of Groovy/Grails related plugins

Following URLs are available for Eclipse new software install tool. Change the version number can get same plugins for different Eclipse versions.

Groovy compilers:
org.codehaus.groovy.eclipse.site – http://ift.tt/2tdZbbf (Neon)
org.codehaus.groovy.eclipse.site – http://ift.tt/2sujCUe (Oxygen)

Spring/Grails IDEs:
SpringSource Update Site for Eclipse 4.6 – http://ift.tt/2tdJiBv (Neon)
SpringSource Update Site for Eclipse 4.7 – http://ift.tt/2suGBi0 (Oxygen)

This blog may not be up to date. Please see the original post from Blogger http://ift.tt/2teecKa
via IFTTT.

Debian VM setup

This article is for setting up Debian on VirtualBox.

Download Debian

Go to the official site of Debian;
In Getting Debian page, download the small installation image (32-bit/64-bit).

Install Debian

Create a Debian VM in VirtualBox;
Insert the Debian ISO into the empty slot in Setting/Storage/Controller: IDE;
Start the VM and follow the steps to install Debian.

Install Guest Additions Driver

Start user session (login) in command line (Ctrl + Alt + F1/F7 to switch between GUI and command line);
Execute sudo apt-get install build-essential linux-headers-$(uname -r);
In the VM window, select Insert Guest Addition CD Image... under Device menu;
Execute sudo bash /media/cdrom/autorun.sh

Setup Shared Folders

Open Devices/Shared Folders/Shared Folders Settings
Under Shared Folders, click adds new shared folder;
Choose Folder Path, and check Auto-mount and Make Permanent, and click OK
Option 1: sudo nano /etc/group and edit the line starts with vboxsf to vboxsf:x:[number (don't change)]:[username1],[username2],...
Option 2: sudo adduser [username] vboxsf

Setup Host-only Network

Go to File/Preference/Network;
Click Adds new host-only network, and take a note of the ip address (referred as noted ip/netmask);
ifconfig is deprecated, use ip addr show or abbreviated as ip a to check network settings;
Still can use /sbin/ifconfig after sudo apt-get install net-tools;
Option 1 – peremptory: sudo /sbin/ifconfig eth1 [noted ip] netmask [noted netmask]
Option 2 – permanent: sudo nano /etc/network/interfaces

auto eth1
iface eth1 inet static
# the noted ip address
address 192.168.55.1
# the noted netmask
netmask 255.255.255.0
# the network of the above ip address you need to calculate
network 192.168.55.0
# the broadcast of the above ip address you need to calculate
broadcast 192.168.55.255

This blog may not be up to date. Please see the original post from Blogger http://ift.tt/2t8Ssir
via IFTTT.

Quick Start with Nashorn engine

As a replacement, Nashorn succeeded Rhino engine in JDK8 to provide performant JavaScript reflection. There are many real world products built in Java which have scripting portals for users to customize the system, e.g. ServiceNow (although it still uses Rhino by the time this article is written).
Before jumping into Java code, I just want make a very short intro to command line execution using jjs. Simply start bin/jjs.exe by typing jjs in command line after you got system path parameter set up.

$ jjs
jjs> exit()

Start JavaScript engine in Java

To evaluate JavaScript in Java, javax.script.ScriptEngine is used:

ScriptEngineManager factory = new ScriptEngineManager();
ScriptEngine engine = factory.getEngineByName("JavaScript"); // Using default JS engine, i.e. Nashorn
engine.eval("print('hello world!');"); // JS code is essentially a String in Java

Invoke JavaScript Functions from Java

Each ScriptEngine has its own namespace. (In JavaScript point of view, each ScriptEngine hosts one global scope.) Functions within it can run by normal JavaScript calls, as well as Java invocation:

engine.eval("function fun (name) {return 'hello ' + name;}; print(fun('Jianzhao'));");
Invocable invocable = (Invocable) engine;
Object result = invocable.invokeFunction("fun", "William");
System.out.println(result + " : " + result.getClass());

Invoke Java Method from JavaScript

To successfully invoke any Java method, you need a full class name including package path, and should not violating Java security constraint:

package ca.ljz;

import javax.script.*;

public class Cls {
    public static void main(String[] args) throws Exception {
        ScriptEngineManager factory = new ScriptEngineManager();
        ScriptEngine engine = factory.getEngineByName("JavaScript");
        engine.eval("var Cls = Java.type('ca.ljz.Cls'); print(new Cls().meth('Will'));");
    }
    public String meth(String name) {
        return "hello "+ name;
    }
}

This blog may not be up to date. Please see the original post from Blogger http://ift.tt/2pKWy0p
via IFTTT.

Introduce to the Advanced Features of HAL

In Get Start with HAL, I went through the basic steps to setup a Spring Boot project and get HAL resource to work. This article is for you to check out some relatively advanced features used in a real word project called STARS. The features to be introduced include typical structure of an HAL project, handling composite primary keys, applying security constraints and so on.

Prerequisite

To be able to understand this article, you need to know the basis of HAL, Database, and Aspect Oriented Programming. You can find more information by following the link on each topic. In addition, since this article references a lot of code in project STARS, you may also need a runtime to download and run the project.

Example Project Synopsis

Sport Telemetry and Report System (STARS), a project adopting HAL, provides a web portal for users to store, manage, review and analyze the movement trajectory and body positioning data of their sport sessions. The version to be referencing in this article is v1.0.0-Alpha.

Structure of an HAL project

As a standard of RESTful Web Services, HAL inherits characteristics of REST, i.e. a clear separation between frontend and backend makes an HAL project to be standalone. It doesn’t care what type of clients to cooperate with.
As a Java implementation of HAL, Spring Framework features HAL through Spring Data REST. To better provide HAL standardized web API, Spring Data REST adopts aspect oriented programing (AOP) paradigm. Unlike traditional layered architecture, AOP allows developers to focus on special operations. Spring HAL automatically provides default CRUD operations. They are interceptable on different aspects to enable customized security constraints, content manipulations, associated operations, and so on.

Handling Composite Key

Composite Primary Key is a very common practice in database. To get it to work with HAL, there are 3 things you need to take care of: embedded primary key, JSON Representation, and conversion.

Embedded Primary Key

In JPA, composite primary key is also called embedded id, which is a special object containing all the columns of the primary key. In the example project, Record is a domain class representing the table stars_record, which has owner and title columns as a composite primary key. To represent this composite primary key, we need a special object called RecordPK as an embedded id.

JSON Representation

Once you have the embedded id, the project still cannot work as expected because it doesn’t know how to represent id in the self-reflect URL since the id contains more than one part. To specify the format of the id, you need to override toString method in the embedded id object. Check toString() in RecordPK, you will find that each part of composite key is delimited by & sign.

PK Conversion

So far, we have the way to represent composite primary key properly. The last thing is to convert the self-reflect URL back to the embedded id object. To do that, you need to implement a BackendIdConverter as a service. You should list all the domains that contain embedded id, and provide convertors for them. In addition, link toString method of embedded id objects to this service to make self-reflect URL to be properly displayed.

Apply Security Check

In Spring Data REST, the most common practice of securing the application would be just using Spring Security. This article is not about Spring Security, but the idea of how to apply it in an HAL project. So, if you are interested in how it is configured in the example project, you can still trace from WebSecurityConfig.java and read the comments.
To apply security on Record for example, you need to know what methods of the repository are provided by Spring. As we declared the interface RecordRepository, Spring will proxy this interface and its methods, and provides HAL REST APIs. You will find all the methods in RecordRepository.java.
From previous section, we talked about AOP. In RecordRepository, I used annotations as the way to adopt AOP to interfere the invocation of each method. @PreAuthorize is the only annotation I used for security check, although, there are many other annotations can be used in different scenario.
Instead of explaining every single annotation and its value, I would like to just pick a typical one:

@Override
@PreAuthorize("hasRole('ADMIN')")
Iterable<Record> findAll();

This method provides a web interface to return all the records in a JSON format when an HTTP GET request being sent to http://localhost:8080/rest/records. If there is no @PreAuthorize annotation, anyone can get all records in the system, which should not be the case. We want only admin users to be able to perform this query. So, by saying ” hasRole(‘ADMIN’)”, users without admin privilege will not be allowed to invoke this method. In this way, the project can be secured safely and efficiently.

Conclusion

In this article, we went through some advanced, but also commonly used features of Spring Data REST, which is a Java implementation of HAL. Since HAL and Spring Data REST are really big topics, I’m sure you will find more when doing the actual development. However, this article talks about the most important concepts you have to know, and I hope you enjoy working with HAL.

This blog may not be up to date. Please see the original post from Blogger http://ift.tt/2od7HEZ
via IFTTT.