vendredi 16 décembre 2011

How to use Maven behing a proxy with authentication


Maven doesn't work with NTLM authentication. It means that you can try to use the settings.xml file as long as you want, you'll not go threw your proxy.

There is a simple fix to use Maven behind a NTLM authentication proxy : install a relay proxy that will take care of authentication on your machine.

First, download NTLMAPS from Sourceforge.
You need Python to run it.

Configure NTLMAPS startup batch :

@echo off
"PATH_TO_PYTHON/python.exe" "PATH_TO_NTLMAPS/main.py"


In NTLMAPS configuration file server.cfg, set values as below :

Dans la section [GENERAL] :
LISTEN_PORT:5865 #leave default port or set an authorised port
PARENT_PROXY:ADRESS_PROXY => #proxy adress
PARENT_PROXY_PORT:PORT_PROXY => #proxy port

Dans la section [NTLM_AUTH] :

NT_DOMAIN:DOMAIN_NT #your domain
USER:USER_NT  #your user
PASSWORD:PASSWORD_NT #your password

Start NTLMAPS with runserver.bat.
Finally configure Maven to use the relay proxy. In your settings.xml :

<?xml version="1.0" encoding="UTF-8"?>

<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
http://maven.apache.org/xsd/settings-1.0.0.xsd">


...

<proxies>
<proxy>
<active>true</active>
<protocol>http</protocol> => your protocol
<host>localhost</host>
<port>5865</port> => port in server.cfg
</proxy>
</proxies>


...
</settings>

And enjoy.

Documentation update for Sitebricks

If you don't know Sitebricks for you Java web application, you should visit sitebricks.org and give it a try. I've released a bunch of basic documentation to the website. My small contribution to this great project.

lundi 12 décembre 2011

Online Java Regex tester

As I was lazy to build a small program to test a regular expression, I've found this website. More than useful !

RegEx: online regular expression testing

vendredi 9 décembre 2011

How to use Apache HttpClient with JVM proxy settings ?

I usualy try to stay close to sitebricks core functionnality. When I faced the issue of accessing a distant website threw a post request, I tried to use sitebricks webclient as described on official website :


The current version is not ready to use as it doesn't support proxy (or at least I didn't find how to make in work) and also HTTPS request.
After a few search on google, I started to use Apache HttpClient, part of HttpComponents.
I notice that this implementation is not taking care of proxy settings in the JVM. I did that function to use them anyway.
Please notice that my final goal was to use a HTTPS distant website.

private DefaultHttpClient initHttpClient() {
  
  DefaultHttpClient client = new DefaultHttpClient();
  
  String proxyHost = System.getProperty("https.proxyHost");
  if(proxyHost == null) {
   proxyHost = System.getProperty("http.proxyHost");
  }
  
  if(proxyHost != null) {
   
   String proxyUser = System.getProperty("https.proxyUser");
   if(proxyUser == null) {
    proxyUser = System.getProperty("http.proxyUser");
   }
   
   String proxyPassword = System.getProperty("https.proxyPassword");
   if(proxyPassword == null) {
    proxyPassword = System.getProperty("http.proxyPassword");
   }
   
   if(proxyUser != null && proxyPassword != null) {
    client.getCredentialsProvider().setCredentials(
                     new AuthScope("localhost", 8080), 
                     new UsernamePasswordCredentials(proxyUser, proxyPassword));
   }
   
   String proxyPort = System.getProperty("https.proxyPort");
   if(proxyPort == null) {
    proxyPort = System.getProperty("http.proxyPort");
   }
   if(proxyPort == null) {
    proxyPort = "80";
   }
   
   HttpHost proxy = new HttpHost(proxyHost, Integer.parseInt(proxyPort));
            
            client.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
  }
  
  return client;

Finaly really easy to do.

lundi 28 novembre 2011

Do you know GraphViz support from Google Chart API ?

Google Chart API as a beta support for GraphViz that is not well known.

More or less all GraphViz options are supported. The only limitation I've seen is special fonts (only the default one is supported) and number of nodes/edges (200/400).

Google provide a web UI to generate an image map to turn your static graph into a fully linked nodes graph. In fact the algorithm is really simple. Here is an example with jQuery :


var fullUrl = 'URL_TO_BUILD_GRAPHVIZ_CHART';

// get it in Json format
$.get(fullUrl+'&chof=json', function(data) {
    // create a Map element
    var mapElement = $('<map>').attr('name','graph_map');
    var chart = data.chartshape;
    for (var i = 0; i < chart.length; i++) {
        // do nothing with edges
        if(chart[i].name.indexOf('unlabel') == -1) {
        var areaElement = $('<area>').attr('name',chart[i].name)
            .attr('shape',chart[i].type)
            .attr('coords',chart[i].coords.join(","))
            .attr('href','YOUR_LINK')
            .attr('title',chart[i].name);
            mapElement.append(areaElement);
        }
    }
    $('#google-chart').after(mapElement);
    $('#google-chart').attr('usemap','#graph_map');
}, 'json');