/**
 *     Copyright (C) 2010 Julien SMADJA <julien dot smadja at gmail dot com> - Arnaud LEMAIRE <alemaire at norad dot fr>
 *
 *     Licensed under the Apache License, Version 2.0 (the "License");
 *     you may not use this file except in compliance with the License.
 *     You may obtain a copy of the License at
 *
 *             http://www.apache.org/licenses/LICENSE-2.0
 *
 *     Unless required by applicable law or agreed to in writing, software
 *     distributed under the License is distributed on an "AS IS" BASIS,
 *     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *     See the License for the specific language governing permissions and
 *     limitations under the License.
 */

package net.awired.clients.common;

import javax.ws.rs.core.MediaType;
import com.google.common.base.Preconditions;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientHandlerException;
import com.sun.jersey.api.client.UniformInterfaceException;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;
import com.sun.jersey.api.client.filter.HTTPBasicAuthFilter;

public class GenericSoftwareClient {

    private Client client;

    private static final ResourceCache CACHE = new ResourceCache();

    public GenericSoftwareClient() {
        ClientConfig clientConfig = new DefaultClientConfig();
        client = Client.create(clientConfig);
    }

    public GenericSoftwareClient(String login, String password) {
        Preconditions.checkNotNull(login, "login is mandatory");
        Preconditions.checkNotNull(password, "password is mandatory");
        ClientConfig clientConfig = new DefaultClientConfig();
        client = Client.create(clientConfig);
        client.addFilter(new HTTPBasicAuthFilter(login, password));
    }

    public <T> T resource(String url, Class<T> clazz) throws ResourceNotFoundException {
        checkUrl(url);
        checkClass(clazz);
        try {
            T object = CACHE.get(url, clazz);
            if (object == null) {
                WebResource resource = client.resource(url);
                object = resource.get(clazz);
                CACHE.put(object, url, clazz);
            }
            return object;
        } catch (Throwable t) {
            String errorMessage = "Can't get resource of type " + clazz.getName() + " at '" + url + "'";
            throw new ResourceNotFoundException(errorMessage, t);
        }
    }

    public <T> T resource(String url, Class<T> clazz, MediaType mediaType) throws ResourceNotFoundException {
        checkUrl(url);
        checkClass(clazz);
        checkMediaType(mediaType);
        try {
            T object = CACHE.get(url, clazz, mediaType);
            if (object == null) {
                WebResource resource = client.resource(url);
                object = resource.accept(mediaType).get(clazz);
                CACHE.put(object, url, clazz, mediaType);
            }
            return object;
        } catch (UniformInterfaceException e) {
            throw new ResourceNotFoundException(e);
        } catch (ClientHandlerException e) {
            throw new ResourceNotFoundException(e);
        }
    }

    private void checkMediaType(MediaType mediaType) {
        Preconditions.checkNotNull(mediaType, "mediaType is mandatory");
    }

    private <T> void checkClass(Class<T> clazz) {
        Preconditions.checkNotNull(clazz, "clazz is mandatory");
    }

    private void checkUrl(String url) {
        Preconditions.checkNotNull(url, "url is mandatory");
    }

}

@@ -757,16 +757,132 @@
 ommon;%0A%0A
+import static com.google.common.base.Preconditions.checkNotNull;%0A%0Aimport java.io.InputStream;%0Aimport java.net.URL;%0A%0A
 import j
@@ -908,16 +908,17 @@
 iaType;%0A
+%0A
 import c
@@ -934,33 +934,29 @@
 .common.
-base.Precondition
+io.ByteStream
 s;%0Aimpor
@@ -1689,38 +1689,24 @@
 ) %7B%0A        
-Preconditions.
 checkNotNull
@@ -1740,38 +1740,24 @@
 %22);%0A        
-Preconditions.
 checkNotNull
@@ -2617,32 +2617,255 @@
        %7D%0A    %7D%0A%0A
+    public %3CT%3E boolean exist(String url, Class%3CT%3E clazz) %7B%0A        try %7B%0A            resource(url, clazz);%0A            return true;%0A        %7D catch (ResourceNotFoundException e) %7B%0A            return false;%0A        %7D%0A    %7D%0A%0A
     public %3CT%3E T
@@ -3578,32 +3578,354 @@
        %7D%0A    %7D%0A%0A
+    public %3CT%3E T existingResource(String url, Class%3CT%3E clazz, MediaType mediaType) %7B%0A        try %7B%0A            return resource(url, clazz, mediaType);%0A        %7D catch (ResourceNotFoundException e) %7B%0A            throw new RuntimeException(%22You should check your url before calling existingResource()%22, e);%0A        %7D%0A    %7D%0A%0A
     private void
@@ -3967,38 +3967,24 @@
 ) %7B%0A        
-Preconditions.
 checkNotNull
@@ -4083,38 +4083,24 @@
 ) %7B%0A        
-Preconditions.
 checkNotNull
@@ -4185,30 +4185,16 @@
         
-Preconditions.
 checkNot
@@ -4231,10 +4231,359 @@
 %0A    %7D%0A%0A
+    public boolean contains(URL url, String word) %7B%0A        try %7B%0A            InputStream stream = url.openStream();%0A            byte%5B%5D byteArray = ByteStreams.toByteArray(stream);%0A            String string = new String(byteArray);%0A            return string.contains(word);%0A        %7D catch (Exception e) %7B%0A            return false;%0A        %7D%0A    %7D%0A%0A
 %7D%0A

fix bug #117