package chesterfield.jetty;

import chesterfield.*;
import org.mortbay.jetty.client.HttpClient;
import org.mortbay.jetty.client.HttpExchange;
import org.mortbay.io.ByteArrayBuffer;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

/**
 * Implementation of {@link chesterfield.CouchClient} using the Jetty HTTP client
 */
public class JettyCouchClient implements CouchClient
{
    private final HttpClient httpClient = new HttpClient();

    public JettyCouchClient() throws Exception
    {
        httpClient.setConnectorType(HttpClient.CONNECTOR_SELECT_CHANNEL);
        httpClient.start();
    }

    public CouchRequest createRequest(final String url)
    {
        return new CouchRequest()
        {
            public CouchResult execute(HttpMethod method) throws WireException
            {
                return executeWithBody(method, null);
            }

            public CouchResult executeWithBody(HttpMethod method, String body) throws WireException
            {
                HttpExchange.ContentExchange contentExchange = new HttpExchange.ContentExchange();
                contentExchange.setMethod(method.name());
                contentExchange.setURL(url);
                
                if (body != null)
                {
                    contentExchange.setRequestContent(new ByteArrayBuffer(body));
                }

                try
                {
                    httpClient.send(contentExchange);
                }
                catch (IOException e)
                {
                    throw new WireException(e.getMessage(), e);
                }
                
                try
                {
                    contentExchange.waitForDone();
                }
                catch (InterruptedException e)
                {
                    throw new RuntimeException(e.getMessage(), e);
                }

                try
                {
                    return new CouchResult(contentExchange.getResponseStatus(), contentExchange.getResponseContent());
                }
                catch (UnsupportedEncodingException e)
                {
                    throw new RuntimeException(e.getMessage(), e);
                }
            }
        };
    }
}

@@ -135,16 +135,53 @@
 change;%0A
+import org.mortbay.jetty.HttpFields;%0A
 import o
@@ -284,16 +284,91 @@
 ception;
+%0Aimport java.util.Map;%0Aimport java.util.HashMap;%0Aimport java.util.Iterator;
 %0A%0A/**%0A *
@@ -2036,32 +2036,137 @@
               %7B%0A
+                    final Map%3CString, String%3E headers = getHeaders(contentExchange.getResponseFields());%0A
                 
@@ -2265,16 +2265,25 @@
 ontent()
+, headers
 );%0A     
@@ -2458,32 +2458,649 @@
  %7D%0A            %7D
+%0A%0A            private Map%3CString, String%3E getHeaders(HttpFields httpFields)%0A            %7B%0A                final Map%3CString, String%3E headers = new HashMap%3CString, String%3E();%0A                if (httpFields != null)%0A                %7B%0A                    final Iterator%3CHttpFields.Field%3E iterator = httpFields.getFields();%0A                    while (iterator.hasNext())%0A                    %7B%0A                        final HttpFields.Field field = iterator.next();%0A                        headers.put(field.getName(), field.getValue());%0A                    %7D%0A                %7D%0A                return headers;%0A            %7D
 %0A        %7D;%0A    

Fix build breakage (need to specify response headers to the CouchResult)