package org.jscep.client.inspect;

import java.io.IOException;
import java.security.cert.CertStore;
import java.security.cert.X509CertSelector;
import java.util.Arrays;
import java.util.Collection;

/**
 * Default implementation of the <code>CertStoreInspector</code>
 */
final class DefaultCertStoreInspector extends AbstractCertStoreInspector {
	/**
	 * @param store
	 *            the store to inspect
	 */
	DefaultCertStoreInspector(final CertStore store) {
		super(store);
	}

	/**
     	 * {@inheritDoc}
     	 */
	protected Collection<X509CertSelector> getIssuerSelectors(byte[] subjectDN) {
		X509CertSelector caSelector = new X509CertSelector();
		caSelector.setBasicConstraints(0);
		try {
            		caSelector.setSubject(subjectDN);
		} catch (IOException e) {
            		// Do nothing
        	}
		return Arrays.asList(caSelector);
	}

	/**
     	 * {@inheritDoc}
     	 */
	protected Collection<X509CertSelector> getSignerSelectors() {
		X509CertSelector digSigSelector = new X509CertSelector();
		digSigSelector.setBasicConstraints(-2);
		digSigSelector.setKeyUsage(new boolean[] { true });

		X509CertSelector caSelector = new X509CertSelector();
		caSelector.setBasicConstraints(0);

		return Arrays.asList(digSigSelector, caSelector);
	}

	/**
     	 * {@inheritDoc}
     	 */
	protected Collection<X509CertSelector> getRecipientSelectors() {
		X509CertSelector keyEncSelector = new X509CertSelector();
		keyEncSelector.setBasicConstraints(-2);
		keyEncSelector.setKeyUsage(new boolean[] { false, false, true });

		X509CertSelector dataEncSelector = new X509CertSelector();
		dataEncSelector.setBasicConstraints(-2);
		dataEncSelector
				.setKeyUsage(new boolean[] { false, false, false, true });

		X509CertSelector caSelector = new X509CertSelector();
		caSelector.setBasicConstraints(0);

		return Arrays.asList(keyEncSelector, dataEncSelector, caSelector);
	}
}


@@ -1352,32 +1352,174 @@
 ntSelectors() %7B%0A
+%09%09boolean digialSignature = false;%0A%09%09boolean nonRepudiation = false;%0A%09%09boolean keyEncipherment = true;%0A%09%09boolean dataEncipherment = false;%0A%09%09%0A
 %09%09X509CertSelect
@@ -1648,38 +1648,158 @@
 lean%5B%5D %7B
- false, false,
+%0A%09%09%09digitalSignature, %0A%09%09%09nonRepudiation, %0A%09%09%09keyEncipherment,%0A%09%09%09dataEncipherment%0A%09%09%7D);%0A%0A%09%09keyEncipherment = false;%0A%09%09dataEncipherment =
  true
- %7D)
 ;%0A%0A%09%09X50
@@ -1918,13 +1918,8 @@
 ctor
-%0A%09%09%09%09
 .set
@@ -1946,35 +1946,94 @@
 %5B%5D %7B
- false, false, false, true 
+%0A%09%09%09digitalSignature, %0A%09%09%09nonRepudiation, %0A%09%09%09keyEncipherment, %0A%09%09%09dataEncipherment%0A%09%09
 %7D);%0A

Improve readability of DefaultCertStoreInspector