[Guava] Preconditions

Preconditions should be ideal for situations, where there is no specific business exceptions for checking arguments and performance is not an issue.




    * Returns the positive square root of the given value.
    * @throws IllegalArgumentException if the value is negative
   public static double sqrt(double value) {
     Preconditions.checkArgument(value >= 0.0, "negative value: %s", value);
     // calculate the square root


Here is more about checking conditions:


Spring security handle AJAX XHR request for logout users and response 401 instead of 302

I found this article with filter implementation:

Spring Security part VI : Session Timeout handling for Ajax calls

However in comment someone point that we can simply extend “LoginUrlAuthenticationEntryPoint”.

My solution sets only status 401 and doesn’t write anything to response body:

	static class AjaxAwareLoginUrlAuthenticationEntryPoint
			extends LoginUrlAuthenticationEntryPoint {

		public AjaxAwareLoginUrlAuthenticationEntryPoint(String loginFormUrl) {

		public void commence(HttpServletRequest request, HttpServletResponse response,
				AuthenticationException authException) throws IOException, ServletException {

			if (isAjaxRequest(request) && authException != null) {
			super.commence(request, response, authException);

		private static boolean isAjaxRequest(HttpServletRequest request) {
			return "XMLHttpRequest".equalsIgnoreCase(request.getHeader("X-Requested-With"));

Workaround for “X-Forwarded” headers in Spring UriComponentsBuilder

When our application server user proxy, that doesn’t set “X-Forwarded” headers with original request port, then we can use some hack in our application. Just ignore port when it is equal 80 or 443, cause it is default. For example instead of "http://localhost:80/app", we will get "http://localhost/app". And "https://localhost:443/app" will output "https://localhost/app".

UriComponentsBuilder currentUrl = ServletUriComponentsBuilder
int port = currentUrl.build().getPort();
currentUrl.port(port != 80 && port != 443 ? port : -1);
String currentUrlString = currentUrl.toUriString();

Inspired by Mat Banik strackoverflow answer – http://stackoverflow.com/a/5212336/5770135.

How to deal with properties in Spring context tests?

public class MyTestConfig {

static {
System.setProperty("my.company.hard.coded.props.example", "true"); 

/** properties loading */
public static PropertySourcesPlaceholderConfigurer properties() {
final PropertySourcesPlaceholderConfigurer pspc = new PropertySourcesPlaceholderConfigurer();
return pspc;


@SpringApplicationConfiguration(classes = MyTestConfig.class)
public class MySpringTest {

Java console program + Maven

Use “Assembler Plugin”:


How it looks:

$ mvn package appassembler:assemble
$ sh target/appassembler/bin/app
Hello World!

DynamicReports – tool for generating Java reports pdf/docx/xlsx

Undoubtedly the most popular library for Java reporting is Jasper Reports. However in case of new projects is also nice to look for better approaches.

If you look at alternatives to Jasper Reports, you will find tones of similar products -> http://alternativeto.net/software/jasperreports/.

I had a few picks, but decided to test only DynamicReports. Here is why:

After creating one pretty simple layout I find out some questions:

  • how to effectively test changes in layout design? I’ve used “JasperReportBuilder.show();”, but I’m not sure it’s the best option.
  • is any way to share “JasperReportBuilder” across many threads and pass only some parameters when compiling reports? So far in header and summary section I was creating components initialized with data, which I think is not good for performance.
  • what is the right design pattern for class implementing reports with “JasperReportBuilder”? Example of class creating invoice report, called “InvoiceDesign.java”, has one huge method “build()” packed with styles and setup of “JasperReportBuilder”. I know it’s only demo, but also I was expecting some production-ready examples.


I like DynamicReports mainly because it’s pure Java. Support for JasperReports is also encouraging. If I develop answers for above questions, I’m sure I will use it in the future.