How to display HTTPBuilder logs in groovyConsole

Viewed 5182

The Problem

I'm getting groovyx.net.http.HttpResponseException: Not Found and want to see the logs from HTTPBuilder. I'm using Groovy 2.1.9 with groovyConsole.

What I tried

So I checked this blog post that says about adding log4j.xml to groovy.home/conf/. I did that, here's my file:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">

<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" debug="false">
    <appender name="console" class="org.apache.log4j.ConsoleAppender">
        <param name="Target" value="System.out" />
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%d{ISO8601} %-5p  %c{1} - %m%n" />
        </layout>
    </appender>

    <category name="groovyx.net.http">
        <priority value="DEBUG" />
    </category>

    <!-- Use DEBUG to see basic request/response info;  
         Use TRACE to see headers for HttpURLClient. -->
    <category name="groovyx.net.http.HttpURLClient">
        <priority value="INFO" />
    </category>

    <category name="org.apache.http">
        <priority value="DEBUG" />
    </category>
    <category name="org.apache.http.headers">
        <priority value="DEBUG" />
    </category>
    <category name="org.apache.http.wire">
        <priority value="DEBUG" />
    </category>

    <root>
        <priority value="INFO" />
        <appender-ref ref="console" />
    </root>
</log4j:configuration>

And here's my script that I'm running in the console:

import groovyx.net.http.*
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.X509Certificate;

import javax.net.ssl.X509TrustManager
import javax.net.ssl.SSLContext
import java.security.cert.X509Certificate
import javax.net.ssl.TrustManager
import java.security.SecureRandom
import org.apache.http.conn.ssl.SSLSocketFactory
import org.apache.http.conn.scheme.Scheme
import org.apache.http.conn.scheme.SchemeRegistry

import groovy.util.logging.*

@Grab(group='org.codehaus.groovy.modules.http-builder', module='http-builder', version='0.6')
@Grab( 'log4j:log4j:1.2.16' )
@Log4j
class WebserviceTest {
  static void main (String[] args) {

    def http = new HTTPBuilder('https://localhost:8443/project/service/')
    log.debug "http is instanceof ${http.getClass()}" //this is printed

    //=== SSL UNSECURE CERTIFICATE ===
   def sslContext = SSLContext.getInstance("SSL")              
   sslContext.init(null, [ new X509TrustManager() {
     public X509Certificate[] getAcceptedIssuers() {null}
     public void checkClientTrusted(X509Certificate[] certs, String authType) { }
     public void checkServerTrusted(X509Certificate[] certs, String authType) { }
   } ] as TrustManager[], new SecureRandom())
   def sf = new SSLSocketFactory(sslContext, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER)
   def httpsScheme = new Scheme("https", sf, 4483)
   http.client.connectionManager.schemeRegistry.register( httpsScheme )
   //================================

    //the next line throws the exeption...
    http.post(path: '/method', contentType: ContentType.JSON, query: [param: '1000']) { resp, reader ->
        println "resp is instanceof ${resp.getClass()}"
        println "and reader is instanceof ${reder.getClass()}"

        println "response status: ${resp.statusLine}"
        println 'Headers: -----------'
        resp.headers.each { h ->
          println " ${h.name} : ${h.value}"
        }
        println 'Response data: -----'
        System.out << reader
        println '\n--------------------'

    }
  } 
}

EDIT: my HttpResponseException is solved, I had a / unnecessary in the request, but still no builder logs appearing.

2 Answers
Related