package fr.synchrotron.soleil.ica.ci.service.legacymavenproxy.pommetadata;

import org.vertx.java.core.Vertx;
import org.vertx.java.core.shareddata.ConcurrentSharedMap;
import org.vertx.java.core.shareddata.SharedData;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * @author Gregory Boissinot
 */
public class POMCache {

    private static final String KEY_CACHE_POM_CONTENT = "pomContent";
    private static final String KEY_CACHE_POM_SHA1 = "pomContentSha1";

    private final ConcurrentSharedMap<String, String> pomContentMap;
    private final ConcurrentSharedMap<String, String> pomSha1Map;

    public POMCache(Vertx vertx) {
        final SharedData sharedData = vertx.sharedData();
        pomContentMap = sharedData.getMap(KEY_CACHE_POM_CONTENT);
        pomSha1Map = sharedData.getMap(KEY_CACHE_POM_SHA1);
    }

    public String getSha1(String pomSha1Path) {
        return pomSha1Map.get(pomSha1Path);
    }

    public void putPomContent(String pomPath, String pomContent) {

        pomContentMap.put(pomPath, pomContent);

        final String sha1Path = pomPath + ".sha1";

        try {
            MessageDigest crypt = MessageDigest.getInstance("SHA-1");
            crypt.reset();
            crypt.update(pomContent.getBytes("UTF-8"));
            pomSha1Map.put(sha1Path, String.valueOf(crypt.digest()));

        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }

    }
}

@@ -217,53 +217,8 @@
 a;%0A%0A
-import java.io.UnsupportedEncodingException;%0A
 impo
@@ -637,16 +637,53 @@
 a1Map;%0A%0A
+    private MessageDigest digester;%0A%0A
     publ
@@ -892,16 +892,190 @@
 M_SHA1);
+%0A%0A        try %7B%0A            digester = MessageDigest.getInstance(%22SHA-1%22);%0A        %7D catch (NoSuchAlgorithmException e) %7B%0A            throw new RuntimeException(e);%0A        %7D
 %0A    %7D%0A%0A
@@ -1237,24 +1237,48 @@
 Content) %7B%0A%0A
+        //--POM CONTENT%0A
         pomC
@@ -1310,24 +1310,41 @@
 mContent);%0A%0A
+        //--SHA1%0A
         fina
@@ -1382,25 +1382,24 @@
 .sha1%22;%0A
-%0A
         
 try %7B%0A  
@@ -1394,437 +1394,857 @@
     
-try %7B%0A            MessageDigest crypt = MessageDigest.getInstance(%22SHA-1%22);%0A            crypt.reset();%0A            crypt.update(pomContent.getBytes(%22UTF-8%22));%0A            pomSha1Map.put(sha1Path, String.valueOf(crypt.digest()));%0A%0A        %7D catch (NoSuchAlgorithmException e) %7B%0A            throw new RuntimeException(e);%0A        %7D catch (UnsupportedEncodingException e) %7B%0A            throw new RuntimeException(e);%0A        %7D%0A%0A    %7D
+digester.reset();%0A        final byte%5B%5D bytes = pomContent.getBytes();%0A        digester.update(bytes, 0, bytes.length);%0A        final String sha1 = encode(digester.digest());%0A        pomSha1Map.put(sha1Path, sha1);%0A%0A    %7D%0A%0A%0A    private String encode(byte%5B%5D binaryData) %7B%0A        if (binaryData.length != 16 && binaryData.length != 20) %7B%0A            int bitLength = binaryData.length * 8;%0A            throw new IllegalArgumentException(%22Unrecognised length for binary data: %22 + bitLength + %22 bits%22);%0A        %7D%0A%0A        String retValue = %22%22;%0A%0A        for (int i = 0; i %3C binaryData.length; i++) %7B%0A            String t = Integer.toHexString(binaryData%5Bi%5D & 0xff);%0A%0A            if (t.length() == 1) %7B%0A                retValue += (%220%22 + t);%0A            %7D else %7B%0A                retValue += t;%0A            %7D%0A        %7D%0A%0A        return retValue.trim();%0A    %7D%0A
 %0A%7D%0A

Use SHA1 encryption mechanism from Maven 2.1.0