<commit_before>package com.kryptnostic.v2.crypto;

import java.io.IOException;
import java.security.InvalidAlgorithmParameterException;
import java.security.NoSuchAlgorithmException;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import retrofit.RetrofitError;

import com.google.common.base.Optional;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.google.common.collect.Maps;
import com.kryptnostic.api.v1.KryptnosticConnection;
import com.kryptnostic.kodex.v1.crypto.ciphers.AesCryptoService;
import com.kryptnostic.kodex.v1.crypto.ciphers.CryptoService;
import com.kryptnostic.kodex.v1.crypto.ciphers.Cypher;
import com.kryptnostic.kodex.v1.exceptions.types.SecurityConfigurationException;
import com.kryptnostic.v2.storage.api.KeyStorageApi;
import com.kryptnostic.v2.storage.api.ObjectStorageApi;
import com.kryptnostic.v2.storage.models.VersionedObjectKey;
import com.kryptnostic.v2.storage.models.VersionedObjectKeySet;

/**
 * Provider class for {@link CryptoService} objects used for encrypting and decrypting objects.
 *
 * @author <NAME> &lt;<EMAIL>&gt;
 *
 */
public class KryptnosticCryptoServiceLoader implements CryptoServiceLoader {
    private static final Logger                                   logger = LoggerFactory
                                                                                 .getLogger( KryptnosticCryptoServiceLoader.class );

    private final LoadingCache<VersionedObjectKey, CryptoService> keyCache;
    private KeyStorageApi                                         keyStorageApi;
    private ObjectStorageApi                                      objectStorageApi;
    private KryptnosticConnection                                 connection;
    private Cypher                                                cypher;

    public KryptnosticCryptoServiceLoader(
            final KryptnosticConnection connection,
            final KeyStorageApi keyApi,
            ObjectStorageApi objectStorageApi,
            Cypher cypher ) {
        this.keyStorageApi = keyApi;
        this.objectStorageApi = objectStorageApi;
        this.connection = connection;
        this.cypher = cypher;
        keyCache = CacheBuilder.newBuilder().maximumSize( 1000 ).expireAfterWrite( 10, TimeUnit.MINUTES )
                // TODO: make this a separate class
                .build( new CacheLoader<VersionedObjectKey, CryptoService>() {
                    @Override
                    public Map<VersionedObjectKey, CryptoService> loadAll( Iterable<? extends VersionedObjectKey> keys )
                            throws IOException,
                            SecurityConfigurationException {
                        VersionedObjectKeySet ids = new VersionedObjectKeySet();

                        for ( VersionedObjectKey key : keys ) {
                            ids.add( key );
                        }

                        Map<VersionedObjectKey, byte[]> data = keyApi.getObjectCryptoServices( ids );
                        if ( data.size() != ids.size() ) {
                            throw new InvalidCacheLoadException( "Unable to retrieve all keys." );
                        }
                        Map<VersionedObjectKey, CryptoService> processedData = Maps.newHashMap();

                        for ( Map.Entry<VersionedObjectKey, byte[]> entry : data.entrySet() ) {
                            byte[] crypto = entry.getValue();
                            if ( crypto != null ) {
                                CryptoService service = connection.newCryptoManager().getRsaCryptoService().decrypt(
                                        crypto,
                                        AesCryptoService.class );
                                processedData.put( entry.getKey(), service );
                            }
                        }
                        return processedData;
                    }

                    @Override
                    public CryptoService load( VersionedObjectKey key ) throws IOException,
                            SecurityConfigurationException {
                        byte[] crypto = null;
                        try {
                            crypto = keyApi.getObjectCryptoService( key.getObjectId(), key.getVersion() );
                        } catch ( RetrofitError e ) {
                            logger.error( "Failed to load crypto service from backend for id {} ", key, e );
                            throw new IOException( e );
                        }
                        if ( ( crypto == null ) ) {
                            try {<commit_msg>Handling getobjectcryptoservice as a potentially null returning call<commit_after>package com.kryptnostic.v2.crypto;

import java.io.IOException;
import java.security.InvalidAlgorithmParameterException;
import java.security.NoSuchAlgorithmException;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.base.Optional;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.google.common.collect.Maps;
import com.kryptnostic.api.v1.KryptnosticConnection;
import com.kryptnostic.kodex.v1.crypto.ciphers.AesCryptoService;
import com.kryptnostic.kodex.v1.crypto.ciphers.CryptoService;
import com.kryptnostic.kodex.v1.crypto.ciphers.Cypher;
import com.kryptnostic.kodex.v1.exceptions.types.SecurityConfigurationException;
import com.kryptnostic.v2.storage.api.KeyStorageApi;
import com.kryptnostic.v2.storage.api.ObjectStorageApi;
import com.kryptnostic.v2.storage.models.VersionedObjectKey;
import com.kryptnostic.v2.storage.models.VersionedObjectKeySet;

/**
 * Provider class for {@link CryptoService} objects used for encrypting and decrypting objects.
 *
 * @author <NAME> &lt;<EMAIL>&gt;
 *
 */
public class KryptnosticCryptoServiceLoader implements CryptoServiceLoader {
    private static final Logger                                   logger = LoggerFactory
                                                                                 .getLogger( KryptnosticCryptoServiceLoader.class );

    private final LoadingCache<VersionedObjectKey, CryptoService> keyCache;
    private KeyStorageApi                                         keyStorageApi;
    private ObjectStorageApi                                      objectStorageApi;
    private KryptnosticConnection                                 connection;
    private Cypher                                                cypher;

    public KryptnosticCryptoServiceLoader(
            final KryptnosticConnection connection,
            final KeyStorageApi keyApi,
            ObjectStorageApi objectStorageApi,
            Cypher cypher ) {
        this.keyStorageApi = keyApi;
        this.objectStorageApi = objectStorageApi;
        this.connection = connection;
        this.cypher = cypher;
        keyCache = CacheBuilder.newBuilder().maximumSize( 1000 ).expireAfterWrite( 10, TimeUnit.MINUTES )
                // TODO: make this a separate class
                .build( new CacheLoader<VersionedObjectKey, CryptoService>() {
                    @Override
                    public Map<VersionedObjectKey, CryptoService> loadAll( Iterable<? extends VersionedObjectKey> keys )
                            throws IOException,
                            SecurityConfigurationException {
                        VersionedObjectKeySet ids = new VersionedObjectKeySet();

                        for ( VersionedObjectKey key : keys ) {
                            ids.add( key );
                        }

                        Map<VersionedObjectKey, byte[]> data = keyApi.getObjectCryptoServices( ids );
                        if ( data.size() != ids.size() ) {
                            throw new InvalidCacheLoadException( "Unable to retrieve all keys." );
                        }
                        Map<VersionedObjectKey, CryptoService> processedData = Maps.newHashMap();

                        for ( Map.Entry<VersionedObjectKey, byte[]> entry : data.entrySet() ) {
                            byte[] crypto = entry.getValue();
                            if ( crypto != null ) {
                                CryptoService service = connection.newCryptoManager().getRsaCryptoService().decrypt(
                                        crypto,
                                        AesCryptoService.class );
                                processedData.put( entry.getKey(), service );
                            }
                        }
                        return processedData;
                    }

                    @Override
                    public CryptoService load( VersionedObjectKey key ) throws IOException,
                            SecurityConfigurationException {
                        byte[] crypto = keyApi.getObjectCryptoService( key.getObjectId(), key.getVersion() );
                        if ( ( crypto == null ) ) {
                            try {
{'fraction_non_alphanumeric': 0.07394403659963826, 'fraction_numerical': 0.003830194701563996, 'mean_word_length': 1.7325581395348837, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 24, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3405368', 'n_tokens_mistral': 2274, 'n_tokens_neox': 2177, 'n_words': 455}