package org.jboss.net.axis.security.handler;
import java.security.KeyStore;
import java.util.Map;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import org.apache.axis.AxisFault;
import org.apache.axis.MessageContext;
import org.apache.log4j.Logger;
import org.apache.ws.axis.security.WSDoAllConstants;
import org.apache.ws.axis.security.WSDoAllSender;
import org.apache.ws.security.components.crypto.Crypto;
import org.jboss.net.axis.security.JBossCryptoFactory;
import org.jboss.net.axis.security.SecurityConstants;
import org.jboss.security.SecurityDomain;
public class WSSResponseHandler extends WSDoAllSender
{
protected Logger log = Logger.getLogger(this.getClass());
SecurityDomain domain = null;
public void invoke(MessageContext mc) throws AxisFault
{
if (!WSDoAllConstants.USE_REQ_SIG_CERT.equals(getOption(WSDoAllConstants.ENCRYPTION_USER)))
{
String actor = (String) getOption(WSDoAllConstants.ACTOR);
String alias = null;
Map signers = (Map) mc.getProperty(SecurityConstants.MC_REQ_SIGNERS);
if (signers != null)
{
alias = (String) signers.get(actor);
}
mc.setProperty(WSDoAllConstants.ENCRYPTION_USER, alias);
}
super.invoke(mc);
}
protected Crypto loadSignatureCrypto() throws AxisFault
{
if (log.isDebugEnabled())
log.debug("Loading the Signature Crypto Class");
if (domain == null)
getSecurityDomain();
KeyStore truststore = domain.getTrustStore();
if (truststore == null)
throw new AxisFault("WSSReceiverHandler: No truststore available.");
String cryptoClass;
if ((cryptoClass = (String) getOption(SecurityConstants.HANDLER_CRYPTO_CLASS)) == null)
throw new AxisFault("WSSReceiverHandler: No Crypto implementation was defined.");
return JBossCryptoFactory.getInstance(cryptoClass, truststore);
}
protected Crypto loadEncryptionCrypto() throws AxisFault
{
if (log.isDebugEnabled())
log.debug("Loading the Decryption Crypto Class");
if (domain == null)
getSecurityDomain();
KeyStore keystore = domain.getKeyStore();
if (keystore == null)
throw new AxisFault("WSSReceiverHandler: No keystore available.");
String cryptoClass;
if ((cryptoClass = (String) getOption(SecurityConstants.HANDLER_CRYPTO_CLASS)) == null)
throw new AxisFault("WSSReceiverHandler: No Crypto implementation was defined.");
return JBossCryptoFactory.getInstance(cryptoClass, keystore);
}
private void getSecurityDomain() throws AxisFault
{
String sd;
if ((sd = (String) getOption(SecurityConstants.HANDLER_SEC_DOMAIN)) == null)
sd = "java:/jaas/other"; if (log.isDebugEnabled())
log.debug("WSSReceiveHandler, securityDomain=" + sd);
try
{
Object tempDomain = new InitialContext().lookup(sd);
if (tempDomain != null && tempDomain instanceof SecurityDomain)
domain = (SecurityDomain) tempDomain;
else
{
log.fatal("The SecurityManager named " + sd + " is not a SecurityDomain");
throw new AxisFault("WSSReceiverHandler: No security domain is available.");
}
}
catch (NamingException e)
{
throw new AxisFault("Unable to find the securityDomain named: " + sd, e);
}
}
}