ICE09 . playing with java, scala, groovy and spring .

Playing with Spring

Archive for October, 2012

Proxying your Web Service Requests with Gradle and Groovy

Posted by ice09 on October 13, 2012

Intention

Sometimes you have to mock a Web Service request, eg. to ensure that a client works well with a new interface.
Usually, this can be done easily with soapUI in mocking Web Servide mode:

However, in this special case several Web Services had to be called, but only some of them should be intercepted, others should just be just forwarded to the actually running Web Services.

Groovy Solution

As so often, Groovy is the perfect solution for this. There are just three files involved, with these three files, together with the Gradle Quickstart plugin of the Springsource Tool Suite Grale plugin.

So, just three steps to your Groovy Proxy Server solution:

  1. Download Springsource Too Suite and the Gradle Plugin, create a Gradle Quckstart Plugin Gradle project
  2. Create the ProxyServer Groovy script and the sample response file in src/main/resources
  3. Add the application plugin as well as the groovy plugin to the Gradle build … that’s it. I’d like to say: Gradle rocks!

The main ProxyServer Groovy script

package de.ice09.blog

import static groovyx.net.http.Method.*
import com.sun.net.httpserver.HttpExchange
import com.sun.net.httpserver.HttpHandler
import com.sun.net.httpserver.HttpServer
import org.apache.http.client.ResponseHandler
import org.apache.http.client.methods.HttpPost
import org.apache.http.entity.StringEntity
import org.apache.http.impl.client.BasicResponseHandler
import org.apache.http.impl.client.DefaultHttpClient

class ProxyHttpHandler implements HttpHandler {
        def answers = []

        ProxyHttpHandler(def answers) {
                this.answers = answers
				println "prepared answers: $answers"
        }

        void handle(HttpExchange exchange) throws IOException {
                def reqText = exchange.requestBody.getText()
                def fileName = answers.find{ reqText.contains(it) }
                def response
                if (fileName != null) {
					println "...sending prepared answer: $fileName"
                    response = new File("${fileName}.xml").getText()
                } else {
                    println ">>> redirect >>>"

                    HttpPost builder = new HttpPost( "http://localhost:8088/mockGetCodeDecodeSoap12" )
                    StringEntity strent= new StringEntity(reqText);
                    strent.setContentType("application/soap+xml; charset=utf-8");
                    builder.setEntity(strent);

                    DefaultHttpClient client = new DefaultHttpClient();
                    ResponseHandler<String> responseh = new BasicResponseHandler();
                    String responseBody = client.execute(builder, responseh);
                    println(responseBody);
                    response = responseBody
                }
                exchange.sendResponseHeaders(200, response.length());
                def outout = exchange.responseBody
                outout.write(response.bytes);
                outout.close();
        }

}
def server = HttpServer.create(new InetSocketAddress(8080), 0);
def answers = []
def files = new File(".").eachFile {
	if (it.name.endsWith("xml")) {
		answers << it.name - ".xml"
	}
}
def context = server.createContext("/", new ProxyHttpHandler(answers));
server.executor = null
server.start();
println "######################\nstarted proxyserver...\n######################"

This script scans the local directory for all XML files and stores the filenames. If a requestbody is retrieved with the Java Web Server, which has somehow the filename in it’s text (this is very fuzzy, however, it worked for our problem), the prepared text file is sent back.
If no corresponding file is found, the request is forwarded to the real service and the response is “tunneled”.

So, only the build.gradle is missing, it is the original Quickstart gradle build file, extended just with the application plugin, the groovy plugin and their configurations.

So, the application can be build with gradle installApp, which creates the complete application, which should be started in the root directory with bin/gradleapp.bat. Voila!

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'application'
apply plugin: 'groovy'

mainClassName = "de.ice09.blog.ProxyServer"

sourceCompatibility = 1.5
version = '1.0'
jar {
    manifest {
        attributes 'Implementation-Title': 'Gradle Quickstart', 'Implementation-Version': version
    }
}

repositories {
    mavenCentral()
}

installApp {
	from 'src/main/resources'
}

dependencies {
	groovy group: 'org.codehaus.groovy.modules.http-builder', name: 'http-builder', version: '0.5.2'
    testCompile group: 'junit', name: 'junit', version: '4.+'
}

test {
    systemProperties 'property': 'value'
}

Posted in Groovy | Leave a Comment »