Browse files

make all execute methods work in BetamaxHttpClient

  • Loading branch information...
1 parent d46b252 commit 5326e30c0abdd03702f3b85072307de4c2179c26 Rob Fletcher committed Oct 1, 2012
View
111 src/main/groovy/co/freeside/betamax/httpclient/BetamaxHttpClient.groovy
@@ -1,104 +1,39 @@
package co.freeside.betamax.httpclient
import co.freeside.betamax.Recorder
-import co.freeside.betamax.message.httpclient.HttpRequestAdapter
-import co.freeside.betamax.handler.*
-import org.apache.http.*
+import org.apache.http.ConnectionReuseStrategy
import org.apache.http.client.*
-import org.apache.http.client.methods.HttpUriRequest
-import org.apache.http.conn.ClientConnectionManager
-import org.apache.http.entity.*
-import org.apache.http.impl.EnglishReasonPhraseCatalog
+import org.apache.http.conn.*
+import org.apache.http.conn.routing.HttpRoutePlanner
import org.apache.http.impl.client.DefaultHttpClient
-import org.apache.http.message.BasicHttpResponse
import org.apache.http.params.HttpParams
-import org.apache.http.protocol.HttpContext
-import static java.util.Locale.ENGLISH
-import static org.apache.http.HttpHeaders.VIA
-import static org.apache.http.HttpVersion.HTTP_1_1
+import org.apache.http.protocol.*
-class BetamaxHttpClient implements HttpClient {
+class BetamaxHttpClient extends DefaultHttpClient {
- private final HttpClient delegate
- private final HttpHandler handlerChain
+ private final Recorder recorder
BetamaxHttpClient(Recorder recorder) {
- this(new DefaultHttpClient(), recorder)
- }
-
- BetamaxHttpClient(HttpClient delegate, Recorder recorder) {
- this.delegate = delegate
- handlerChain = new TapeReader(recorder)
- handlerChain <<
- new TapeWriter(recorder) <<
- new HeaderFilter() <<
- new TargetConnector(delegate)
- }
-
- @Override
- HttpParams getParams() {
- delegate.params
- }
-
- @Override
- ClientConnectionManager getConnectionManager() {
- delegate.connectionManager
- }
-
- @Override
- HttpResponse execute(HttpUriRequest request) {
- def requestWrapper = new HttpRequestAdapter(request)
- def responseWrapper = handlerChain.handle(requestWrapper)
-
- def response = new BasicHttpResponse(
- HTTP_1_1,
- responseWrapper.status,
- EnglishReasonPhraseCatalog.INSTANCE.getReason(responseWrapper.status, ENGLISH)
- )
- responseWrapper.headers.each { name, value ->
- value.tokenize(',').each {
- response.addHeader(name, it.trim())
- }
- }
- response.addHeader(VIA, 'Betamax')
- if (responseWrapper.hasBody()) {
- response.entity = new ByteArrayEntity(responseWrapper.bodyAsBinary.bytes, ContentType.create(responseWrapper.contentType))
- }
- response
- }
-
- @Override
- HttpResponse execute(HttpUriRequest request, HttpContext context) {
- return null //To change body of implemented methods use File | Settings | File Templates.
+ this.recorder = recorder
}
@Override
- HttpResponse execute(HttpHost target, HttpRequest request) {
- return null //To change body of implemented methods use File | Settings | File Templates.
+ protected RequestDirector createClientRequestDirector(
+ HttpRequestExecutor requestExec,
+ ClientConnectionManager conman,
+ ConnectionReuseStrategy reustrat,
+ ConnectionKeepAliveStrategy kastrat,
+ HttpRoutePlanner rouplan,
+ HttpProcessor httpProcessor,
+ HttpRequestRetryHandler retryHandler,
+ RedirectStrategy redirectStrategy,
+ AuthenticationStrategy targetAuthStrategy,
+ AuthenticationStrategy proxyAuthStrategy,
+ UserTokenHandler userTokenHandler,
+ HttpParams params) {
+ def director = super.createClientRequestDirector(requestExec, conman, reustrat, kastrat, rouplan, httpProcessor, retryHandler, redirectStrategy, targetAuthStrategy, proxyAuthStrategy, userTokenHandler, params)
+ new BetamaxRequestDirector(director, recorder)
}
- @Override
- HttpResponse execute(HttpHost target, HttpRequest request, HttpContext context) {
- return null //To change body of implemented methods use File | Settings | File Templates.
- }
-
- @Override
- def <T> T execute(HttpUriRequest request, ResponseHandler<? extends T> responseHandler) {
- return null //To change body of implemented methods use File | Settings | File Templates.
- }
-
- @Override
- def <T> T execute(HttpUriRequest request, ResponseHandler<? extends T> responseHandler, HttpContext context) {
- return null //To change body of implemented methods use File | Settings | File Templates.
- }
-
- @Override
- def <T> T execute(HttpHost target, HttpRequest request, ResponseHandler<? extends T> responseHandler) {
- return null //To change body of implemented methods use File | Settings | File Templates.
- }
-
- @Override
- def <T> T execute(HttpHost target, HttpRequest request, ResponseHandler<? extends T> responseHandler, HttpContext context) {
- return null //To change body of implemented methods use File | Settings | File Templates.
- }
}
+
View
51 src/main/groovy/co/freeside/betamax/httpclient/BetamaxRequestDirector.groovy
@@ -0,0 +1,51 @@
+package co.freeside.betamax.httpclient
+
+import co.freeside.betamax.Recorder
+import co.freeside.betamax.handler.*
+import co.freeside.betamax.message.httpclient.HttpRequestAdapter
+import org.apache.http.*
+import org.apache.http.client.RequestDirector
+import org.apache.http.entity.*
+import org.apache.http.impl.EnglishReasonPhraseCatalog
+import org.apache.http.impl.client.DefaultHttpClient
+import org.apache.http.message.BasicHttpResponse
+import org.apache.http.protocol.HttpContext
+
+class BetamaxRequestDirector implements RequestDirector {
+
+ private final RequestDirector delegate
+ private final HttpHandler handlerChain
+
+ BetamaxRequestDirector(RequestDirector delegate, Recorder recorder) {
+ this.delegate = delegate
+
+ handlerChain = new TapeReader(recorder)
+ handlerChain <<
+ new TapeWriter(recorder) <<
+ new HeaderFilter() <<
+ new TargetConnector(new DefaultHttpClient())
+ }
+
+ @Override
+ HttpResponse execute(HttpHost target, HttpRequest request, HttpContext context) {
+ def requestWrapper = new HttpRequestAdapter(request)
+ def responseWrapper = handlerChain.handle(requestWrapper)
+
+ def response = new BasicHttpResponse(
+ HttpVersion.HTTP_1_1,
+ responseWrapper.status,
+ EnglishReasonPhraseCatalog.INSTANCE.getReason(responseWrapper.status, Locale.ENGLISH)
+ )
+ responseWrapper.headers.each { name, value ->
+ value.tokenize(',').each {
+ response.addHeader(name, it.trim())
+ }
+ }
+ response.addHeader(HttpHeaders.VIA, 'Betamax')
+ if (responseWrapper.hasBody()) {
+ response.entity = new ByteArrayEntity(responseWrapper.bodyAsBinary.bytes, ContentType.create(responseWrapper.contentType))
+ }
+ response
+ }
+
+}

0 comments on commit 5326e30

Please sign in to comment.