1 Mart 2013 Cuma

Spring 3.2 Json-p Message Converter

In Spring framework, there is a message converter for json but there isn't a converter for json-p. I 've seen a few solutions on here.

One of the solution in here, was extending MappingJacksonJsonView class. But I didn't like the idea of checking every 'GET' request whether it has a 'callback' param or not.

And the other solution in here, was extending MappingJackson2HttpMessageConverter class. I preferred this approach. But, I didn't like the idea of implementing an interface (JsonObject) for every request. So, I decided to change it a bit.
I decided to get the callback parameter over the servlet request. So, I neither had to implement JsonObject interface nor had to get the callback parameter with the @RequestParam annotation. Here is my solution:
/**
 * Converter class that we are going to use to create json-p messages.
 *
 * @author sinan.yumak
 *
 */
public class MappingJackson2JsonpHttpMessageConverter extends MappingJackson2HttpMessageConverter {

    private static final String DEFAULT_CALLBACK_PARAMETER = "callback";

    private static final List SUPPORTED_MEDIA_TYPES = new ArrayList() {{
        add( new MediaType("application", "x-javascript") );
        add( new MediaType("application", "javascript") );
        add( new MediaType("text", "javascript") );
    }};


    public MappingJackson2JsonpHttpMessageConverter() {
        setSupportedMediaTypes(SUPPORTED_MEDIA_TYPES);
    }


    @Override
    protected void writeInternal( Object object, HttpOutputMessage outputMessage )
        throws IOException, HttpMessageNotWritableException {
        JsonGenerator jsonGenerator = getJsonGenerator(outputMessage);

        try {
            String callbackParam = getRequestParam( DEFAULT_CALLBACK_PARAMETER );

            if ( Strings.isNullOrEmpty(callbackParam) ) {
                //if the callback parameter doesn't exists, use the default one...
                callbackParam = DEFAULT_CALLBACK_PARAMETER;
            }

            jsonGenerator.writeRaw(callbackParam);
            jsonGenerator.writeRaw(" (");
            getObjectMapper().writeValue(jsonGenerator, object);
            jsonGenerator.writeRaw(");");
            jsonGenerator.flush();
        } catch (JsonProcessingException ex) {
            throw new HttpMessageNotWritableException("Could not write JSON:"  + ex.getMessage(), ex);
        }
    }


    private JsonGenerator getJsonGenerator( HttpOutputMessage outputMessage ) throws IOException {
        JsonEncoding encoding = getJsonEncoding(outputMessage.getHeaders().getContentType());
        return getObjectMapper().getFactory().createJsonGenerator(outputMessage.getBody(), encoding);
    }

    /**
     * Returns given parameter from servlet request.
     *
     * @param paramName
     *         Name of the param
     */
    private String getRequestParam( String paramName ) {
        return getServletRequest().getParameter( paramName );
    }

    /**
     * Returns current servlet request.
     */
    private HttpServletRequest getServletRequest() {
        return ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();
    }
}
And you can register your converter like that:
@Configuration
public class WebMvcConfig extends WebMvcConfigurationSupport {

    @Override
    public void configureMessageConverters( List> converters ) {
        converters.add( new MappingJackson2JsonpHttpMessageConverter() );

        addDefaultHttpMessageConverters( converters );
    }

}

24 Şubat 2012 Cuma

Changing Jtable Focus Keys

JTable uses ctrl+tab and ctrl+shift+tab key combinations to change focus
to other components by default.

You can change it to expected tab and shift+tab combination with the
following code snippet:
       
        JTable table = ...
        Set forwKeys = new HashSet();
        forwKeys.add(AWTKeyStroke.getAWTKeyStroke("TAB"));
        table.setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, forwKeys);

        Set backKeys = new HashSet();
        backKeys.add(AWTKeyStroke.getAWTKeyStroke("shift TAB"));
        table.setFocusTraversalKeys(KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS, backKeys);

8 Kasım 2010 Pazartesi

mysqldump problem

mysqldump is a powerful tool for exporting, importing databases but it has
really big problems with different mysql versions. Even one or two version changes makes tool to produce unstable results. When you execute a command like
mysqldump -u root any_database < dumped.sql
it just shows some comments on the console but any changes reflected to database. After losing about 3 or 4 hours i've found another way to import exported data. On the command line,
$ mysql -u root

mysql>use any_database;

mysql>\. /path/to/sql/file or
mysql>source /path/to/sql/file

After that mysql starts to execute the sql file and problem goes away...

5 Temmuz 2010 Pazartesi

Jboss Seam Mail Attachment Problem

jboss-seam-mail 2.2.0.GA library has a bug which causes mails to be rendered without attachments. An issue has been opened in Jboss's issue tracker and a patch has been released about it. (JBSEAM-4442) In spite of applying patch to the library the bug is still alive. If you are experiencing the same problem, you should switch to an older and stable version like 2.0.2 to overcome this bug.

13 Eylül 2009 Pazar

Install moonOs 3 (Makara) From Hard Disk

moonOS is a complete and fully functional operative system based on the LXDE, Enlightenment DR17 and powered by the popular Linux Distribution Ubuntu. moonOS, a project started and designed by the Cambodian artist Chanrithy Thim (12rithy), which is perfect for any Desktop, Laptop PC or even for a Virtual Machine.

If you want to install it without burning a cd like me you can try to install from hdd. Due to being an Ubuntu based Linux distro, there is a similar installation procedure with Ubuntu. I am going to tell you about installing it from a Linux distro.

Firstly, create a directory under your home or another place. I will assume you've created a directory in /home named moonos. Then copy the iso image to this directory. To boot from this image we have to show grub where kernel and ramdisk files are. We will extract them from the image.


Open a console:

sudo su mount -o loop /home/moonos/moonos-3-makara-desktop-i386.iso /media/cdrom0

cp /media/cdrom0/casper/initrd.gz /home/moonos

cp /media/cdrom0/casper/vmlinuz /home/moonos

Now we will add an item to /boot/grub/menu.lst.

title Install moonOs

root (hd0,6)
kernel /home/moonos/vmlinuz boot=casper iso-scan/filename=/home/moonos/moonos-3-makara-desktop-i386.iso


My root directory is under (hd0,6) here.

Save the file and restart pc. After reboot, you will see Install moonOs in the grub menu.

Remember that you can't modify the partition where the iso file is located. So you should deal with partitioning operations before starting installation.

eth0 switches to eth1

I have an nVidia Corporation MCP65 chipset ethernet card. I was having problems with Pardus's network manager.
On every boot, I had to select network profile manually. After some research, i've seen that eth0 switches to eth1 or eth1 switches to eth0 on every boot.

I decided to investigate what happens when udev initializes the ethernet card. When
dmesg | grep eth is executed on the console,

[ 10.480852] forcedeth: Reverse Engineered nForce ethernet driver. Version 0.64.
[ 10.481261] forcedeth 0000:00:06.0: PCI INT A -> Link[LMAC] -> GSI 20 (level, low) -> IRQ 20
[ 10.481267] forcedeth 0000:00:06.0: setting latency timer to 64
[ 10.481316] forcedeth 0000:00:06.0: Invalid Mac address detected: 00:00:00:00:00:00
[ 10.481319] forcedeth 0000:00:06.0: Please complain to your hardware vendor. Switching to a random MAC.
[ 10.994564] forcedeth 0000:00:06.0: ifname eth0, PHY OUI 0x732 @ 1, addr 00:00:6c:6f:b7:63
[ 10.994578] forcedeth 0000:00:06.0: highdma pwrctl mgmt gbit lnktim msi desc-v3


Hmm. After googling, i've seen that mycard has a problem with getting mac address. Here is my udev rule for the card:


# PCI device 0x10de:0x0450 (forcedeth)
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="00:1a:73:03:0f:c1", ATTR{type}=="1", KERNEL=="eth*", NAME="eth0"

This rule was using card's mac address. When i restart, udev switches to a random mac address and eth0 changes depending on this.
Solution to this problem is to use the id of the card instead of mac address.


# PCI device 0x10de:0x0450 (forcedeth)
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ID=="0000:00:06.0", ATTR{type}=="1", KERNEL=="eth*", NAME="eth0"


And when i restart the system, problem goes away and network manager automatically connects...

Related link,
nvidia-nforce-network-adapter-has-different-mac-adress-every-boot-569576

12 Ağustos 2009 Çarşamba

Eclipse Source Folder Error

You've imported a non Eclipse project into the Eclipse and haven't told Eclipse
which folders contains source files, you will get a warning like this:

'This compilation unit is not on the build path of a Java project.'

You can easily solve this problem by editing .classpath file under the root directory of your project. Open this file with a text editor and add
these lines under to classpath tag:

< classpathentry kind="src" path="src/path/to/sources" />

For example, if you have a source folder under src/main, you need to add

<classpathentry kind="src" path="src/main" />