// 
// Decompiled by Procyon v0.6.0
// 

package com.google.crypto.tink.jwt;

import com.google.crypto.tink.internal.MonitoringUtil;
import com.google.crypto.tink.internal.MutableMonitoringRegistry;
import com.google.crypto.tink.internal.MonitoringClient;
import com.google.errorprone.annotations.Immutable;
import com.google.crypto.tink.internal.PrimitiveRegistry;
import com.google.crypto.tink.internal.MutablePrimitiveRegistry;
import java.security.GeneralSecurityException;
import com.google.crypto.tink.internal.MonitoringAnnotations;
import com.google.crypto.tink.internal.KeysetHandleInterface;
import com.google.crypto.tink.internal.PrimitiveWrapper;

class JwtPublicKeySignWrapper implements PrimitiveWrapper<JwtPublicKeySign, JwtPublicKeySign>
{
    private static final JwtPublicKeySignWrapper WRAPPER;
    
    @Override
    public JwtPublicKeySign wrap(final KeysetHandleInterface keysetHandle, final MonitoringAnnotations annotations, final PrimitiveFactory<JwtPublicKeySign> factory) throws GeneralSecurityException {
        return new WrappedJwtPublicKeySign(keysetHandle, annotations, factory);
    }
    
    @Override
    public Class<JwtPublicKeySign> getPrimitiveClass() {
        return JwtPublicKeySign.class;
    }
    
    @Override
    public Class<JwtPublicKeySign> getInputPrimitiveClass() {
        return JwtPublicKeySign.class;
    }
    
    public static void register() throws GeneralSecurityException {
        MutablePrimitiveRegistry.globalInstance().registerPrimitiveWrapper((PrimitiveWrapper<Object, Object>)JwtPublicKeySignWrapper.WRAPPER);
    }
    
    public static void registerToInternalPrimitiveRegistry(final PrimitiveRegistry.Builder primitiveRegistryBuilder) throws GeneralSecurityException {
        primitiveRegistryBuilder.registerPrimitiveWrapper((PrimitiveWrapper<Object, Object>)JwtPublicKeySignWrapper.WRAPPER);
    }
    
    static {
        WRAPPER = new JwtPublicKeySignWrapper();
    }
    
    @Immutable
    private static class WrappedJwtPublicKeySign implements JwtPublicKeySign
    {
        private final JwtPublicKeySign primary;
        private final int primaryKeyId;
        private final MonitoringClient.Logger logger;
        
        public WrappedJwtPublicKeySign(final KeysetHandleInterface keysetHandle, final MonitoringAnnotations annotations, final PrimitiveFactory<JwtPublicKeySign> factory) throws GeneralSecurityException {
            this.primary = factory.create(keysetHandle.getPrimary());
            this.primaryKeyId = keysetHandle.getPrimary().getId();
            if (!annotations.isEmpty()) {
                final MonitoringClient client = MutableMonitoringRegistry.globalInstance().getMonitoringClient();
                this.logger = client.createLogger(keysetHandle, annotations, "jwtsign", "sign");
            }
            else {
                this.logger = MonitoringUtil.DO_NOTHING_LOGGER;
            }
        }
        
        @Override
        public String signAndEncode(final RawJwt token) throws GeneralSecurityException {
            try {
                final String output = this.primary.signAndEncode(token);
                this.logger.log(this.primaryKeyId, 1L);
                return output;
            }
            catch (final GeneralSecurityException e) {
                this.logger.logFailure();
                throw e;
            }
        }
    }
}
