// Copyright 2013 The Apache Software Foundation
//
// 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 org.apache.tapestry5.internal.services.assets;

import org.apache.commons.codec.binary.Hex;
import org.apache.tapestry5.ioc.Resource;
import org.apache.tapestry5.ioc.internal.util.CollectionFactory;
import org.apache.tapestry5.ioc.internal.util.InternalUtils;
import org.apache.tapestry5.services.assets.AssetChecksumGenerator;
import org.apache.tapestry5.services.assets.StreamableResource;
import org.apache.tapestry5.services.assets.StreamableResourceProcessing;
import org.apache.tapestry5.services.assets.StreamableResourceSource;

import java.io.IOException;
import java.io.InputStream;
import java.security.MessageDigest;
import java.util.Map;

public class AssetChecksumGeneratorImpl implements AssetChecksumGenerator
{
    private final static int BUFFER_SIZE = 4096;

    private final StreamableResourceSource streamableResourceSource;

    private final ResourceChangeTracker tracker;

    private final Map<StreamableResource, String> cache = CollectionFactory.newConcurrentMap();

    public AssetChecksumGeneratorImpl(StreamableResourceSource streamableResourceSource, ResourceChangeTracker tracker)
    {
        this.streamableResourceSource = streamableResourceSource;
        this.tracker = tracker;

        tracker.clearOnInvalidation(cache);
    }

    public String generateChecksum(Resource resource) throws IOException
    {
        StreamableResource streamable = streamableResourceSource.getStreamableResource(resource, StreamableResourceProcessing.COMPRESSION_DISABLED,
                tracker);

        return generateChecksum(streamable);
    }

    @Override
    public String generateChecksum(StreamableResource resource) throws IOException
    {
        String result = cache.get(resource);

        if (result == null)
        {
            result = toChecksum(resource.openStream());

            cache.put(resource, result);
        }

        return result;
    }

    private String toChecksum(InputStream is) throws IOException
    {
        try
        {
            MessageDigest digest = MessageDigest.getInstance("MD5");

            digestStream(digest, is);

            is.close();

            byte[] bytes = digest.digest();
            char[] encoded = Hex.encodeHex(bytes);

            return new String(encoded);
        } catch (Exception ex)
        {
            throw new RuntimeException(ex);
        } finally
        {
            InternalUtils.close(is);
        }
    }

    private void digestStream(MessageDigest digest, InputStream stream) throws IOException
    {
        byte[] buffer = new byte[BUFFER_SIZE];

        while (true)
        {
            int length = stream.read(buffer);

            if (length < 0)
            {
                return;
            }

            digest.update(buffer, 0, length);
        }
    }
}

@@ -697,16 +697,68 @@
 ry.Hex;%0A
+import org.apache.commons.codec.digest.DigestUtils;%0A
 import o
@@ -860,69 +860,8 @@
 ry;%0A
-import org.apache.tapestry5.ioc.internal.util.InternalUtils;%0A
 impo
@@ -1193,44 +1193,8 @@
 am;%0A
-import java.security.MessageDigest;%0A
 impo
@@ -1291,57 +1291,8 @@
 or%0A%7B
-%0A    private final static int BUFFER_SIZE = 4096;
 %0A%0A  
@@ -2497,262 +2497,54 @@
     
-try%0A        %7B%0A            MessageDigest digest = MessageDigest.getInstance(%22MD5%22);%0A%0A            digestStream(digest, is);%0A%0A            is.close();%0A%0A            byte%5B%5D bytes = digest.digest();%0A            char%5B%5D encoded = Hex.encodeHex(bytes);%0A%0A  
+byte%5B%5D digest = DigestUtils.md5(is);%0A%0A
         
   re
@@ -2543,569 +2543,49 @@
     
-  
 return 
-new String(encoded);%0A        %7D catch (Exception ex)%0A        %7B%0A            throw new RuntimeException(ex);%0A        %7D finally%0A        %7B%0A            InternalUtils.close(is);%0A        %7D%0A    %7D%0A%0A    private void digestStream(MessageDigest digest, InputStream stream) throws IOException%0A    %7B%0A        byte%5B%5D buffer = new byte%5BBUFFER_SIZE%5D;%0A%0A        while (true)%0A        %7B%0A            int length = stream.read(buffer);%0A%0A            if (length %3C 0)%0A            %7B%0A                return;%0A            %7D%0A%0A            digest.update(buffer, 0, length);%0A    
+Hex.encodeHexString(digest);%0A
     %7D%0A
-    %7D
 %0A%7D%0A

Use utilities from commons-codec