Java

22 Aug: Implementing a role hierarchy in Spring Security (Java config style)

From http://static.springsource.org/spring-security/site/docs/3.1.x/reference/authz-arch.html: It is a common requirement that a particular role in an application should automatically “include” other roles. For example, in an application which has the concept of an “admin” and a “user” role, you may want an admin to be able to do everything a normal user can. To achieve this, you can either make sure that all admin users are also assigned the “user” role. Alternatively, you can modify every access constraint which requires…

19 Aug: How to map unknown JSON properties with Jackson?

For a current project, I need to map a JSON object that have some known fields, and some “dynamics” fields. I don’t want to ignore these fields, just to get them in a map inside my bean. After digging for 20 minutes, I finally found the right annotations to use: @JsonAnyGetter and @JsonAnySetter. @JsonAnySetter is a simple marker that can be used to define a two-argument method (first argument name of property, second value to set), to…

19 Aug: A simple Tcp Client/Server using Reactor

Reactor is a foundation for asynchronous applications on the JVM. It provides abstractions for Java, Groovy and other JVM languages to make building event and data-driven applications easier. It’s also really fast. On modest hardware, it’s possible to process around 15,000,000 events per second with the fastest non-blockingDispatcher. Other dispatchers are available to provide the developer with a range of choices from thread-pool style, long-running task execution to non-blocking, high-volume task dispatching. Since the first milestone of…

09 Apr: Transfert files from Hadoop to a remote server via ssh

When working with Hadoop, you produce files in the hdfs. In order to copy them in one of your remote servers, you have to first use the get or the copyToLocal command to copy the files in your local filesystem and then use a scp command. But this two steps process is not really efficient since you are double-copying the files. sshj is a pure Java implementation of SSHv2 allowing you to connect to an sshd server…

24 Feb: A best Spring RestTemplate!

The RestTemplate is the central class for client-side HTTP access. It simplifies communication with HTTP servers, and enforces RESTful principles. It handles HTTP connections, leaving application code to provide URLs (with possible template variables) and extract results. The template uses a ClientHttpRequestFactory for creating HTTP connections. The default ClientHttpRequestFactory is a SimpleClientHttpRequestFactory that uses standard J2SE classes like java.net.HttpURLConnection. And this is not really production ready… (why?) So, the solution is to use the org.springframework.http.client.HttpComponentsClientHttpRequestFactory, which is…

24 Mar: Automatic retry on failure

What the need? To make processing more robust and less prone to failure, sometimes it helps to automatically retry a failed operation in case it might succeed on a subsequent attempt. For example a call to a web service that fails because of a network glitch or a DeadLockLoserException in a database update may resolve themselves after a short wait. So, how to…? To automate the retry of such operations the Spring team developped a retry module…

15 Mar: Using the new Spring Cache Abstraction in Spring Security

What the need? Caching is handled by storing the UserDetails object being placed in the UserCache. This ensures that subsequent requests with the same username can be validated without needing to query the UserDetailsService. It should be noted that if a user appears to present an incorrect password, the UserDetailsService will be queried to confirm the most up-to-date password was used for comparison. Caching is only likely to be required for stateless applications. In a normal web…

26 Sep: String – Best Pratices

Never, never, never use the String constructor Whenever you want to instantiate a String object, never use its constructor but always instantiate it directly. For example: //don’t: String slow = new String(“Yet another string object”); //do String fast = “Yet another string object”; From the Javadoc: Unless an explicit copy of original is needed, use of this constructor is unnecessary since Strings are immutable. Pattern and regular expressions For performance reasons, instances of pattern (java.util.regex.Pattern) should be…

08 Sep: HttpClient – Best Pratices

URLConnection -> HttpClient Except to manage specific resources other than http / https (ftp, gopher, file …), use the HttpClient and not the HttpURLConnection, and avoid to use both at the same time. The HttpClient is more mature and has a richer feature set as multi-threaded management, cookies, pipelining, connection pools … Instantiation Instantiating a HttpClient is costly in resources, we must focus on reuse, even singletons, as described in the doc: « Generally it is recommended…