Friday, January 3, 2014

JSONProvider builds wrong String

Looking at my backlog I found a very interesting ticket:

As a consumer of JSON web services, I would like the services to render fields with consistent types regardless of the content. When postal codes begin with zero, they are rendered as strings. When they begin with a non-zero character, they are rendered as numbers. This makes handling the JSON responses very cumbersome and causes hard to track down bugs in our code. 

It would be better if the conversion checked the type in the xsd (in the case of postal codes, string) and rendered it accordingly. 


Weird, isn't it? Then I realize, I wasn't the only one with this issue, a found a few other clueless developers with the same problem (look here and here), so that, I wasn't alone.

But so far no one had an answer, until I checked the code of my JSONProvider, this one: org.apache.cxf.jaxrs.provider.JSONProvider

My conclusion was simple, depending on the value of the string passed to the Converter, it will be casted to Boolean, String or Number.

There are 2 choices: Create my own custom JSONProvider extending from the original (and try to fix the world) or implement another which really worked as expected.

In a few words, I chose JacksonJsonProvider, however I had to customize it a little bit, because I needed to fix the issue and not create a new one (i needed the same output), and so far this is the result:

This is the original output

This is my implementation
And my new output I still have something to fix-customize because that "ArrayList" looks ugly though, but code and areCode now make sense, because they match my XSD and Pojo as well.

Wednesday, December 11, 2013

How to edit the hosts file in Mac OS

The hosts file is a text file that maps hostnames to IP addresses. Upon typing a url address on the browser, the system is checking if there is a relevant entry on the hosts file and gets the corresponding IP address, else it resolves the IP via the active connection’s DNS servers.

The hosts file can be edited to block certain hostnames (like ad-serving/malicious hosts), or used for web development purposes, i.e. to redirect domains to local addresses.

Editing the hosts file
Editing the hosts file in Mac OS, is a pretty easy task, especially if you are familiar with the terminal. Just follow these steps:
  1. Open the Terminal
  2. Open the hosts file by typing on the Terminal that you have just opened:
    $ sudo nano /private/etc/hosts
    

    Type your user password when prompted.
  3. Edit the hosts file:
    The hosts file contains some comments (lines starting with the # symbol), as well as some default hostname mappings (e.g. 127.0.0.1 – localhost). Simply append your new mappings underneath the default ones. Or edit one of the default values if you know what you are doing! You can navigate the file using the arrow keys.
  4. Save the hosts file
    When done editing the hosts file, press control-o to save the file. Press enter on the filename prompt, and control-x to exit the editor.
  5. Flush the DNS cache (Optional)
    On Leopard you can issue a simple Terminal command to flush the DNS cache, and have your host file changes to take immediate effect:
    $ dscacheutil -flushcache
    
You can now test your new mapping on the browser!