Showing posts from 2015

HSQLDB Query log - Output SQL statements to log file

It's sometimes useful (especially for debbuging) to be able to see all the SQL statements that the database runsLots of RDBMS have this very useful feature (MySQL for example calls it query log)The HSQLDB is one of those and it can be accomplished in several ways, here I will show you how to do it within the db url and using a SQL statement : 1.- In the DB url Just add the following parameter to your db url : hsqldb.sqllog=3 For example : jdbc:hsqldb:file:c:/dev/db/my_db;shutdown=true;hsqldb.sqllog=3 2.- Through a SQL statement SET DATABASE EVENT LOG SQL LEVEL 3 Once this is done you will find a log file in the same folder as your database with the naming convention like ${dbname}.sql.log like so : my_db.sql.log More info here : hsqldb monitoring documentation

Content disposition duplicate headers solution for google chrome

Recently I had a problem while serving dynamic content in a Spring MVC application; (more details here) when attempting to open some of the dynamic content with Chrome I was having a weird error message : Duplicate headers received from server The response from the server contained duplicate headers. This problem is generally the result of a misconfigured website or proxy. Only the website or proxy administrator can fix this issue. Error 349 (net::ERR_RESPONSE_HEADERS_MULTIPLE_CONTENT_DISPOSITION): Multiple distinct Content-Disposition headers received. This is disallowed to protect against HTTP response splitting attacks. I wasn't sure why I was having this problem, since some dynamic content was working properly and I was setting only once the Content-Disposition header on my codeSo after searching around a bit I stumbled upon the HTTP specs it turns out the Content-Disposition header should not contain a coma since it will be treated as a header separator Multiple message-…

Spring mvc send binary content from controller

Sometimes binary files such as images, documents are stored in the database; these binary files need then to be served dynamically Below I'll show a code snippet showing how to handle this easily with Spring MVCBefore we begin just in case this is the list of libraries I used when coding this example : Spring 3+Spring Data JPA Eclipselink 2.4+Apache TikaSlugify @Entity public class Resouce{ @Id @Column(name = "ID") @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; @Column(name = "FILE_NAME", length = 200) private String fileName; @Column(name = "FILE") @Lob private byte[] file; //SETTERS - GETTERS OMMITED } @ResponseStatus(value = HttpStatus.NOT_FOUND) public class ResourceNotFoundException extends RuntimeException { } /** * @author ulf */ @Controller @RequestMapping("/resource") public class ResourceController{ // a basic Spring data repository @Autowired pri…

Jasper Reports PDF image quality (blurry and poor) solution

Recently I had to create a PDF Jasper Report that contained a lot of small images (I needed to represent checkboxes); however upon generating the PDF the image quality was really awful since for some reason they were been scaled (even though I had configured the report to show them in real size and the image was the same dimensions as its placeholder) I spent quite some time searching for a solutions, one popular one was to add the following parameter either to the report as a property or in the in the CLASSPATH to be picked up by the engine but to no avail.In the end after searching and reading different threads I found 2 solutions that work :1. Convert your PNG to SVGYou can convert your PNGs to SVGs either by using something like Adobe Ilustrator or something similar or you can also head up to this website : PNG to SVG converter and follow the instructionsYou can then place your SVGs in the appropriate folder in your application where you can pass the pat…

Creating custom font icons

FontIcons have become really popular in the past few years and they have a lot of advantages such as : They are vector based so no loss in quality when re-sizingThe ability to easily change their size/color/shade ; without involving an image editor (they are just fonts!) The ability to combine font-icons or add text-decorations, gradients, textures (depending on browser support) When creating FontIcons you have a few options, here I will discuss how to create them from PNG files 1. Converting your png to svg Sorry I lied you cannot directly create a FontIcon from a PNG; you need to convert it first to a vectorial format SVGOnce again you have various options when it comes to converting a PNG; if you are lucky you might have an Adobe Illustrator licence where you can perform the conversion (or maybe another program I'm not aware of), otherwise you will have to use another approach, in my case I use PNG to SVG converter.The site is pretty straightforward, just upload your PNG store …

POI Excel : Making a read only final file prevent edit/select

Excel allows to make the sheet of a workbook to be read only, with the possibility to fine tune the locking options like for example :Locking cell editionLocking cell selectionLocking cell formatLocking cell insertionYou can get the list of locking options from the Excel security configuration dialog as you can see in the screenshot provided below : You will then have to provide a password for the locking featureYou can also achieve this easily with POI in a few easy steps as shown in the code below : private static void lockAll(Sheet s, String password){ // cast the sheet to the appropriate type XSSFSheet sheet = ((XSSFSheet)s); //protect the sheet with a password sheet.protectSheet(password); //enable the locking features sheet.enableLocking(); // fine tune the locking options (in this example we are blocking all operations on the cells: select, edit, etc.) sheet.lockSelectLockedCells(true); sheet.lockSelectUnlockedCells(true); …

Better documentation with ascciidoc and asciidoctor

This article is just a link to a presentation I published on slideshare, you can check it out here : Better documentation with ascciidoc and asciidoctor

Introspected tunnels to localhost

Have you ever found yourself in a situation when you are debbugging an application that runs on your localhost or using a remote API and where you need your application to be accessible from the outside, for example for a callback? If like me you have; you soon realize that it's a pain!Now there are a few ways you can deal with this but recently I found out about a great service Ngrok and I must say it works like a charm; just create an account (or not actually) then download the executable an run it!Regarding the licence model, Ngrok is a pay-what-you-want service so depending on your needs you might have to pay for some features but not necessary.The picture below (taken from ngrok website gives you an idea of how it works) : One last cool thing about Ngrok is that you not only get the tunneling features but also a monitoring console and the ability to replay requests! So go ahead and give it a try I must admit it's a great service

Extract information from a java process that is running

Sometimes you need to obtain information regarding a JAVA process and the different options used to run this process such as the encoding, the timezone, the classpath, etc.Luckily there a tool bundled with the JDK that allows you to do so, you just need to provide it the PID of the concerned process; this can be done in different ways, personally I prefer to use another tool bundled with the JDK JPS however this might not work (in windows) for wrapped java applications such as tomcat service, so you can do it with the task manager or another toolOnce you have the PID is pretty straight-forward in its most basic form just provide the PID to the command line tool like so : jinfo 6544 You should get some output similar to this : Attaching to process ID 6544, please wait... Debugger attached successfully. Server compiler detected. JVM version is 25.20-b23 Java System Properties: java.vendor = Oracle Corporation catalina.base = C:\dev\servers\tomcat8 = HotSpot 6…

Edit tomcat service start-up options

There is an easy way to change a Tomcat configured as a service start-up options and configurations such as : Memory options (xmx, xms, etc.)Logging configurationMiscellaneous Java configurationThis can be done through the tomcat binary.1.-Head up to your tomcat's binary folder cd C:\dev\servers\tomcat8\bin> 2.- Launch the "Apache tomcat properties" window tomcat8w.exe //ES//MyServiceName Where MyServiceName is the name of the service you configured when installing tomcat as a service. This should open the following popup window : 3.- Edit your required properties Edit the properties; save the configuration and start/restart the service and you are done

Scala : easily load an parse configuration files

There are a lot of ways to handle configuration files in a Scala program; my favorite one is to use TypeSafe's Config projectThis library scans the CLASSPATH for a predefined set of configuration files in different formatsUsing it it's pretty straightforward so let's get to it : 1.- Add your dependency to sbt libraryDependencies += "com.typesafe" % "config" % "1.2.1" 2.- Loading the configuration file //load the configuration file from the classpath val conf = ConfigFactory.load The convenience method ConfigFactory.load() loads the following (first-listed are higher priority):system propertiesapplication.conf (all resources on classpath with this name)application.json (all resources on classpath with this name) (all resources on classpath with this name)reference.conf (all resources on classpath with this name)The idea is that libraries and frameworks should ship with a reference.conf in their jar. Applications should p…