Πέμπτη, 13 Φεβρουαρίου 2014

Simple TCP listener for logging HTTP requests

In case you are developing an http client and you want to examine the exact contents of an http request there is a fast way to do so, by using the always handy netcat tool. nc comes preinstalled on Mac. So to setup a listener on e.g. port 8080 you may use the following command:

sudo nc -l 8080

But the client will need a reply too so, better use:

echo -e 'HTTP/1.1 200 OK\r\n' | sudo nc -l 8080;

Now direct your http request to localhost:8080. nc will print the entire http request on standard output and will reply with a status 200 OK.

Now, if you want the listener to keep listening for the next request, you may use the following bash script. The script also allows terminating the program using Control-c. Otherwise, the while loop would go on forever.

#!/bin/bash

[ "$#" -eq 1 ] || { echo "Usage $0 "; exit 1; }

PORT=$1

killed=0
trap '{ echo "sigint"; killed=1; }' SIGINT

echo "Listening for connections on port $PORT"

while (( !killed )); do
echo '======================='
echo -e 'HTTP/1.1 200 OK\r\n' | sudo nc -l $PORT;
echo ''
done

Πέμπτη, 16 Μαΐου 2013

How to find the serial version UID of a java class

If tools such as serialver and javap are hard to use, you can simply use the following code:


public static void main(String[] args) throws IOException, ClassNotFoundException {
     ObjectStreamClass descriptor = 
           ObjectStreamClass.lookup(YourClassHere.class);
     System.out.println("serialVersionUID:" + descriptor.getSerialVersionUID());
}

Πέμπτη, 2 Μαΐου 2013

Search and replace with regular expressions in Eclipse

Have you ever wanted to do some refactoring of code which involves replacing the same kind of code in many places? Let say for example you need to add an attribute to all xml elements of certain type but the value of the attribute depends on an already existing value. Ok... you can either prepare yourself for hours of boring typing or just use the magic of regular expressions inside Eclipse.

Let's say you want to convert this xml:


 id="12435" title="Eclipse rules" />
 id="54645" title="Regular expressions cookbook" />
 id="46576" title="Java is great" />

to this:

 id="12435" title="Eclipse rules" isbn="0000-12435-0000" />
 id="54645" title="Regular expressions are magic" isbn="0000-54645-0000" />
 id="46576" title="Java is great" isbn="0000-46576-0000" />

And, yeah, assume you have a few hundreds of these lines :)

To save your hands and your time, you can just do the following:

Search for:  
id="(\d+)" title="(.*)"
(with regular expression option checked)

And replace with:
id="$1" title="$2" isbn="0000-$1-0000"

This works both within a file and across many files. Voila, you're done!

Τετάρτη, 13 Μαρτίου 2013

Check open ports on Mac OSX

Netstat works differently in Mac and Unix. So in Mac it's better to use lsof (list open files).

lsof -i -P | grep "LISTEN"

How to clear a project from its eclipse metadata

Say you have a Maven project that you have imported into Eclipse and you would like to clean re-import it. Or maybe you have created a new project and you want to import it into version control without its Eclipse metadata. Simple! Delete the project from eclipse, then open Terminal, go to the root directory of the project and run:

find . \( -name .project -o -name .settings -o -name .classpath \) -exec rm -rf '{}' \;

The above command deletes files and directories without warning. Use it with caution!

You may get "No such file or directory" messages. That means the script already deleted that file or directory so no problem.

In addition, if you want to delete IntelliJ metadata you may do the following:

find . \( -name .idea -o -name *.iml \) -exec rm -rf '{}' \;

Specify or check active maven profiles at command line

To force a specific Maven profile to active and force another to non active (which would otherwise be active) you can use the -P parameter. In bash shell you will need to escape the ! character with a \

So for example to run the validate phase with prof-one active and prof-two non active run the following:

mvn validate help:active-profiles -P prof-one,\!prof-two

The help:active-profiles goal prints debug info about the active profiles.

Κυριακή, 26 Απριλίου 2009

How to save JSF state across web flow view states

Spring-faces provides great integration between Spring Web Flow 2 and JSF. Each view-state in web flow renders a JSF view, a stateful tree of JSF components. When exiting a view-state (e.g. when transitioning from one view-state to another) the state of the JSF component tree is naturally lost. There are times though when you want to transition back to that view-state and see the view exactly the way it was last time. For example you might want to transition to a view-state where you search for and select a value and then you want to go back to the originating view-state and see it exactly the way you left it.

A nice way to achieve this with spring-faces is to store the JSF component tree state in the flow scope when leaving the view-state. Then, when entering it again, you may restore it and clean the flow scope. The best place to put this logic is in a FlowExecutionListener. Saving the JSF view state must happen before the transition and restoring must happen on entry of the view-state.

The fundamental questions are:
1) where can you find the JSF state to save?
2) where should you put the JSF state when you restore?

Well, the answer to both questions is the same. When you restore the JSF state you need to put it at the same place that you find it when saving it. That is in the view scope under the key "flowSerializedViewState". So right before the transition you need to copy the contents of viewState.flowSerializedViewState to flowScope.myKey and on entry of the view-state you need to copy the contents of flowScope.myKey to viewState.flowSerializedViewState. Web flow's JSF StateManager will actually restore the JSF component tree using the state you injected into the view scope.

That's it. Remember that this is actually a hack of the Spring-faces state manager, so it comes with no guarantees of later versions compatibility. But it's a neat way to save and restore the JSF view state accross Spring web flow view-states. This works with the current latest version of SWF (2.0.7).