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

package org.bouncycastle.mime.smime;

import org.bouncycastle.cms.RecipientInformationStore;
import org.bouncycastle.cms.OriginatorInformation;
import org.bouncycastle.cms.SignerInformationStore;
import java.io.IOException;
import org.bouncycastle.cms.CMSException;
import org.bouncycastle.mime.MimeIOException;
import org.bouncycastle.cms.CMSEnvelopedDataParser;
import org.bouncycastle.util.Store;
import java.util.Map;
import org.bouncycastle.cms.CMSSignedData;
import org.bouncycastle.util.io.Streams;
import java.util.HashMap;
import java.io.InputStream;
import org.bouncycastle.mime.ConstantMimeContext;
import org.bouncycastle.mime.MimeContext;
import org.bouncycastle.mime.Headers;
import org.bouncycastle.mime.MimeParserContext;
import org.bouncycastle.operator.DigestCalculator;
import org.bouncycastle.mime.MimeParserListener;

public abstract class SMimeParserListener implements MimeParserListener
{
    private DigestCalculator[] digestCalculators;
    private SMimeMultipartContext parent;
    
    @Override
    public MimeContext createContext(final MimeParserContext mimeParserContext, final Headers headers) {
        if (headers.isMultipart()) {
            this.parent = new SMimeMultipartContext(mimeParserContext, headers);
            this.digestCalculators = this.parent.getDigestCalculators();
            return this.parent;
        }
        return new ConstantMimeContext();
    }
    
    @Override
    public void object(final MimeParserContext mimeParserContext, final Headers headers, final InputStream inputStream) throws IOException {
        try {
            if (headers.getContentType().equals("application/pkcs7-signature") || headers.getContentType().equals("application/x-pkcs7-signature")) {
                final HashMap hashMap = new HashMap();
                for (int i = 0; i != this.digestCalculators.length; ++i) {
                    this.digestCalculators[i].getOutputStream().close();
                    hashMap.put(this.digestCalculators[i].getAlgorithmIdentifier().getAlgorithm(), this.digestCalculators[i].getDigest());
                }
                final CMSSignedData cmsSignedData = new CMSSignedData(hashMap, Streams.readAll(inputStream));
                this.signedData(mimeParserContext, headers, cmsSignedData.getCertificates(), cmsSignedData.getCRLs(), cmsSignedData.getAttributeCertificates(), cmsSignedData.getSignerInfos());
            }
            else if (headers.getContentType().equals("application/pkcs7-mime") || headers.getContentType().equals("application/x-pkcs7-mime")) {
                final CMSEnvelopedDataParser cmsEnvelopedDataParser = new CMSEnvelopedDataParser(inputStream);
                this.envelopedData(mimeParserContext, headers, cmsEnvelopedDataParser.getOriginatorInfo(), cmsEnvelopedDataParser.getRecipientInfos());
                cmsEnvelopedDataParser.close();
            }
            else {
                this.content(mimeParserContext, headers, inputStream);
            }
        }
        catch (final CMSException ex) {
            throw new MimeIOException("CMS failure: " + ex.getMessage(), ex);
        }
    }
    
    public void content(final MimeParserContext mimeParserContext, final Headers headers, final InputStream inputStream) throws IOException {
        throw new IllegalStateException("content handling not implemented");
    }
    
    public void signedData(final MimeParserContext mimeParserContext, final Headers headers, final Store store, final Store store2, final Store store3, final SignerInformationStore signerInformationStore) throws IOException, CMSException {
        throw new IllegalStateException("signedData handling not implemented");
    }
    
    public void envelopedData(final MimeParserContext mimeParserContext, final Headers headers, final OriginatorInformation originatorInformation, final RecipientInformationStore recipientInformationStore) throws IOException, CMSException {
        throw new IllegalStateException("envelopedData handling not implemented");
    }
}
