http-builder RESTClient dependencies

Hi Jaimie,

I'm hoping you can help pointing me in the right direction, I'm receiving this class dependency in my logs :

2013-03-27 11:24:40,153 http-bio-8081-exec-1 ERROR [webwork.util.ValueStack] METHOD: "promotedLinks", exception:

java.lang.NoClassDefFoundError: groovy/lang/GroovyObject

while using:

-----------------------

import groovyx.net.http.RESTClient

import groovy.util.slurpersupport.GPathResult;

import static groovyx.net.http.ContentType.URLENC

def myRC = new RESTClient("http://localhost/")

------------------------

I'm using the http-builder-0-1.7-20130216.204411-3.jar to access the RESTClient class.

I noticed that this class it's in the jar:

../META-INF/lib/groovy-all-1.8.5-scriptrunner.jar

Should I install groovy-all-1.0-jsr-06.jar to resolve this dependency? Or should this be picked up by your groovyrunner jar?

Leo

1 answer

1 accepted

I decided to try apache's implementation instead, this script is called from another script and should really pass the rest_data parameter (needs to be cleaned up).

-rw-r--r-- 1 root root 115669 Mar 27 14:08 httpclient-cache-4.2.3.jar

-rw-r--r-- 1 root root 223571 Mar 27 13:53 httpcore-4.2.2.jar

-rw-r--r-- 1 root root 433070 Mar 27 13:51 httpclient-4.2.3.jar

Here is the script that eventually worked.

import groovy.sql.Sql;
import com.atlassian.jira.util.ImportUtils
import com.atlassian.jira.ComponentManager;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.IssueManager;
import groovy.json.*;
import java.util.ArrayList as ArrayList;
import java.sql.Connection;
import org.apache.log4j.Category;
import org.ofbiz.core.entity.ConnectionFactory;
import org.ofbiz.core.entity.DelegatorInterface;
import java.security.MessageDigest
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

log.error("sugarREST.groovy script " );
def username = "USERNAME";
def password = "PASSWORD";
def url = "http://localhost/service/v4/rest.php";
def restdata = new BasicNameValuePair("rest_data", '{"user_auth":{"user_name":"' + username + '","password":"' + MD5(password) + '","version":"1"},"application_name":"RestTest","name_value_list":[]}');
//println("restdata: " + restdata);

def result = call("login", restdata, url);
def session_id = result.id;

//println("SESSION ID: " + result.id);

//get list of records --------------------------------
def get_entry_list_parameters = new BasicNameValuePair("rest_data", '{"session" : "' + session_id + '", "module_name" : "Opportunities", "query" : "", "order_by" : "", "offset" : "0", \
     "fields" : ["name","description","start_date", "last_name", "end_date", "status", "nrc"], \
     "link_name_to_fields_array":[], "max_results":"2", "deleted":"0", "Favorites":false }');

//println("get_entry_list_parameters: " + get_entry_list_parameters);

def get_entry_list_result = call('get_entry_list', get_entry_list_parameters, url);

println("RESULTS: " + get_entry_list_result);

//--------------------------------------------
// sugar call
//--------------------------------------------
def call (method, rest_data, restURL) {
     def response = null;
     def returnVal = "";

     try {
      DefaultHttpClient client = new DefaultHttpClient();
      def post = new HttpPost(restURL);

      List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
      nameValuePairs.add(new BasicNameValuePair("method", "${method}"));
      nameValuePairs.add(new BasicNameValuePair("input_type", "JSON"));
      nameValuePairs.add(new BasicNameValuePair("response_type", "JSON"));
      nameValuePairs.add(rest_data);

      post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
      response = client.execute(post);
      BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
      String line = "";
      while ((line = rd.readLine()) != null) {
          returnVal = returnVal + line;
          //println ("LINE : " + line);
      }

    } catch (e) {
      //e.printStackTrace();
      log.error("sugarREST.groovy failed, turn on stack trace to see the problem");
   }

   if(returnVal && returnVal != null && returnVal != "null" ) {
      println ("VALUE: [" + returnVal + "]");
      def jsonObj = new JsonSlurper().parseText(returnVal);
      return jsonObj;
   } else {
      return null;
   }
}

//-----
//MD5
//-----
 def MD5(String s) {
    MessageDigest digest = MessageDigest.getInstance("MD5")
    digest.update(s.bytes);
    new BigInteger(1, digest.digest()).toString(16).padLeft(32, '0')
 }


It's a bit of a pain using httpbuilder, because, although it is excellent and easy to use, the maven dependencies include a load of xml stuff and httpcommons that conflicts with other libraries in use.

I spent a couple of days trying to wade through those dependencies which eventually led to conflicting calls. After that, it just wasn't worth the effort.

Suggest an answer

Log in or Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published May 30, 2018 in Marketplace Apps

Three tips for boosting your board's efficiency with Story Maps

Trello is one of the most effective tools for driving your sprints. It's customizable for every Agile team and product owners and Scrum masters (SM) love it. However, Agile teams often struggle with:...

818 views 2 9
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you