package org.safehaus.subutai.common.util;


import java.util.Map;

import javax.ws.rs.core.Response;

import org.safehaus.subutai.common.exception.HTTPException;

import org.apache.cxf.jaxrs.client.WebClient;
import org.apache.cxf.jaxrs.ext.form.Form;


public class RestUtil
{
    public static final int RESPONSE_OK = 200;


    public static String get( String url, Map<String, String> params ) throws HTTPException
    {
        WebClient client = null;
        Response response = null;
        try
        {
            client = WebClient.create( url );
            if ( params != null )
            {
                for ( Map.Entry<String, String> entry : params.entrySet() )
                {
                    client.query( entry.getKey(), entry.getValue() );
                }
            }
            response = client.get();
            if ( response.getStatus() != RESPONSE_OK )
            {
                throw new HTTPException( String.format( "Http status code: %d", response.getStatus() ) );
            }
            else
            {
                return response.readEntity( String.class );
            }
        }
        finally
        {
            if ( response != null )
            {
                try
                {
                    response.close();
                }
                catch ( Exception ignore )
                {
                }
            }
            if ( client != null )
            {
                try
                {
                    client.close();
                }
                catch ( Exception ignore )
                {
                }
            }
        }
    }


    public static String post( String url, Map<String, String> params ) throws HTTPException
    {
        WebClient client = null;
        Response response = null;
        try
        {
            client = WebClient.create( url );
            Form form = new Form();
            if ( params != null )
            {
                for ( Map.Entry<String, String> entry : params.entrySet() )
                {
                    form.set( entry.getKey(), entry.getValue() );
                }
            }
            response = client.form( form );
            if ( response.getStatus() != RESPONSE_OK )
            {
                throw new HTTPException( String.format( "Http status code: %d", response.getStatus() ) );
            }
            else
            {
                return response.readEntity( String.class );
            }
        }
        finally
        {
            if ( response != null )
            {
                try
                {
                    response.close();
                }
                catch ( Exception ignore )
                {
                }
            }
            if ( client != null )
            {
                try
                {
                    client.close();
                }
                catch ( Exception ignore )
                {
                }
            }
        }
    }
}

@@ -895,32 +895,239 @@
 )%0A            %7B%0A
+                if ( response.hasEntity() )%0A                %7B%0A                    throw new HTTPException( response.readEntity( String.class ) );%0A                %7D%0A                else%0A                %7B%0A    
                 
@@ -1228,34 +1228,80 @@
         
-%7D%0A            else
+    %7D%0A            %7D%0A            else if ( response.hasEntity() )
 %0A       
@@ -1892,24 +1892,46 @@
  %7D%0A        %7D
+%0A%0A        return null;
 %0A    %7D%0A%0A%0A   
@@ -2542,32 +2542,239 @@
 )%0A            %7B%0A
+                if ( response.hasEntity() )%0A                %7B%0A                    throw new HTTPException( response.readEntity( String.class ) );%0A                %7D%0A                else%0A                %7B%0A    
                 
@@ -2875,34 +2875,80 @@
         
-%7D%0A            else
+    %7D%0A            %7D%0A            else if ( response.hasEntity() )
 %0A       
@@ -3539,21 +3539,43 @@
  %7D%0A        %7D
+%0A%0A        return null;
 %0A    %7D%0A%7D%0A

Fix Karaf