package org.fabric3.samples.hibernate;

import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.UriBuilder;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;
import org.codehaus.jackson.jaxrs.JacksonJaxbJsonProvider;

/**
 * @version $Rev$ $Date$
 */
public class HibernateClient {
    private static final String BASE_URI = "http://localhost:8181/messages";

    public static void main(String[] args) {
        Message message = new Message();
        message.setText("Test message " + System.currentTimeMillis());

        ClientConfig cc = new DefaultClientConfig();
        cc.getClasses().add(JacksonJaxbJsonProvider.class);
        Client client = Client.create(cc);
        UriBuilder uri = UriBuilder.fromUri(BASE_URI);
        WebResource resource = client.resource(uri.path("message").build());

        System.out.println("Creating message");
        ClientResponse r = resource.type(MediaType.APPLICATION_JSON).post(ClientResponse.class, message);
        String location = r.getHeaders().get("location").get(0);
        resource = client.resource(location);

        Message response = resource.type(MediaType.APPLICATION_JSON).get(Message.class);

        WebResource messagesResource = client.resource(BASE_URI);
        MessageList messages = messagesResource.accept(MediaType.APPLICATION_JSON).type(MediaType.APPLICATION_JSON).get(MessageList.class);
        for (Message entry : messages.getMessages()) {
            System.out.println("The message text for id " + entry.getId() + " is: " + entry.getText());
        }


        // resource.delete();
    }
}

@@ -352,24 +352,85 @@
 ientConfig;%0A
+import com.sun.jersey.api.client.filter.HTTPBasicAuthFilter;%0A
 import org.c
@@ -826,24 +826,25 @@
 ntConfig();%0A
+%0A
         cc.g
@@ -933,16 +933,160 @@
 ate(cc);
+%0A%0A        // set basic auth filter%0A        HTTPBasicAuthFilter filter = new HTTPBasicAuthFilter(%22foo%22, %22bar%22);%0A        client.addFilter(filter);
 %0A       

add basic auth to the Jeresy client