blob: dc932d7dbf7911b75bccb160228f7f0cd3b6ffa0 [file] [log] [blame]
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" lang="en"><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/><link rel="stylesheet" href="../.resources/report.css" type="text/css"/><link rel="shortcut icon" href="../.resources/report.gif" type="image/gif"/><title>PostDispatchVerificationHandler.java</title><link rel="stylesheet" href="../.resources/prettify.css" type="text/css"/><script type="text/javascript" src="../.resources/prettify.js"></script></head><body onload="window['PR_TAB_WIDTH']=4;prettyPrint()"><div class="breadcrumb" id="breadcrumb"><span class="right"><a href="../.sessions.html" class="el_session">Sessions</a></span><a href="../index.html" class="el_report">Coverage Report</a> &gt; <a href="index.html" class="el_package">org.apache.rampart.handler</a> &gt; <span class="el_source">PostDispatchVerificationHandler.java</span></div><h1>PostDispatchVerificationHandler.java</h1><pre class="source lang-java linenums">/*
* Copyright 2004,2005 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an &quot;AS IS&quot; BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.rampart.handler;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMException;
import org.apache.axiom.soap.SOAPHeader;
import org.apache.axiom.soap.SOAPHeaderBlock;
import org.apache.axis2.AxisFault;
import org.apache.axis2.context.MessageContext;
import org.apache.axis2.description.HandlerDescription;
import org.apache.axis2.description.Parameter;
import org.apache.axis2.engine.Handler;
import org.apache.neethi.Assertion;
import org.apache.neethi.Policy;
import org.apache.neethi.PolicyEngine;
import org.apache.rampart.RampartMessageData;
import org.apache.rampart.policy.RampartPolicyData;
import org.apache.rampart.util.RampartUtil;
import org.apache.ws.secpolicy.model.Binding;
import org.apache.ws.secpolicy.model.SupportingToken;
import org.apache.ws.security.WSConstants;
import org.apache.ws.security.handler.WSHandlerConstants;
import org.apache.ws.security.handler.WSHandlerResult;
import java.util.Iterator;
import java.util.List;
/**
* Handler to verify the message security after dispatch
*
*/
<span class="fc" id="L47">public class PostDispatchVerificationHandler implements Handler {</span>
private HandlerDescription handlerDesc;
/**
* @see org.apache.axis2.engine.Handler#cleanup()
*/
public void cleanup() {
<span class="nc" id="L55"> }</span>
/**
* @see org.apache.axis2.engine.Handler#flowComplete(org.apache.axis2.context.MessageContext)
*/
public void flowComplete(MessageContext msgContext) {
<span class="fc" id="L61"> }</span>
/**
* @see org.apache.axis2.engine.Handler#getHandlerDesc()
*/
public HandlerDescription getHandlerDesc() {
<span class="fc" id="L67"> return this.handlerDesc;</span>
}
/**
* @see org.apache.axis2.engine.Handler#getName()
*/
public String getName() {
<span class="nc" id="L74"> return &quot;Post dispatch security verification handler&quot;;</span>
}
/**
* @see org.apache.axis2.engine.Handler#getParameter(java.lang.String)
*/
public Parameter getParameter(String name) {
<span class="nc" id="L81"> return this.handlerDesc.getParameter(name);</span>
}
/**
* @see org.apache.axis2.engine.Handler#init(org.apache.axis2.description.HandlerDescription)
*/
public void init(HandlerDescription handlerDesc) {
<span class="fc" id="L88"> this.handlerDesc = handlerDesc;</span>
<span class="fc" id="L89"> }</span>
/**
* @see org.apache.axis2.engine.Handler#invoke(org.apache.axis2.context.MessageContext)
*/
public InvocationResponse invoke(MessageContext msgContext)
throws AxisFault {
<span class="fc bfc" id="L97" title="All 2 branches covered."> if (!msgContext.isEngaged(WSSHandlerConstants.SECURITY_MODULE_NAME)) {</span>
<span class="fc" id="L98"> return InvocationResponse.CONTINUE;</span>
}
<span class="fc" id="L101"> Policy policy = msgContext.getEffectivePolicy();</span>
<span class="fc bfc" id="L103" title="All 2 branches covered."> if(msgContext.getProperty(RampartMessageData.KEY_RAMPART_POLICY) != null) {</span>
<span class="fc" id="L104"> policy = (Policy)msgContext.getProperty(RampartMessageData.KEY_RAMPART_POLICY);</span>
}
<span class="pc bpc" id="L108" title="1 of 2 branches missed."> if(policy == null) {</span>
<span class="nc" id="L109"> policy = msgContext.getEffectivePolicy();</span>
}
<span class="pc bpc" id="L112" title="1 of 2 branches missed."> if(policy == null) {</span>
<span class="nc" id="L113"> Parameter param = msgContext.getParameter(RampartMessageData.KEY_RAMPART_POLICY);</span>
<span class="nc bnc" id="L114" title="All 2 branches missed."> if(param != null) {</span>
<span class="nc" id="L115"> OMElement policyElem = param.getParameterElement().getFirstElement();</span>
<span class="nc" id="L116"> policy = PolicyEngine.getPolicy(policyElem);</span>
}
}
<span class="pc bpc" id="L120" title="1 of 2 branches missed."> if(policy == null) {</span>
<span class="nc" id="L121"> return InvocationResponse.CONTINUE;</span>
}
<span class="fc" id="L124"> Iterator&lt;List&lt;Assertion&gt;&gt; alternatives = policy.getAlternatives();</span>
<span class="fc" id="L126"> boolean securityPolicyPresent = false;</span>
<span class="pc bpc" id="L127" title="1 of 2 branches missed."> if(alternatives.hasNext()) {</span>
<span class="fc" id="L128"> List&lt;Assertion&gt; assertions = alternatives.next();</span>
<span class="pc bpc" id="L129" title="1 of 2 branches missed."> for (Iterator&lt;Assertion&gt; iterator = assertions.iterator(); iterator.hasNext();) {</span>
<span class="fc" id="L130"> Assertion assertion = iterator.next();</span>
//Check for any *Binding assertion
<span class="fc bfc" id="L132" title="All 2 branches covered."> if (assertion instanceof Binding) {</span>
<span class="fc" id="L133"> securityPolicyPresent = true;</span>
<span class="fc" id="L134"> break;</span>
// There can be security policies containing only supporting tokens
<span class="pc bpc" id="L136" title="1 of 2 branches missed."> } else if (assertion instanceof SupportingToken) {</span>
<span class="nc" id="L137"> securityPolicyPresent = true; </span>
<span class="nc" id="L138"> break;</span>
}
<span class="fc" id="L140"> }</span>
}
<span class="pc bpc" id="L145" title="1 of 2 branches missed."> if (securityPolicyPresent) {</span>
<span class="fc" id="L146"> RampartPolicyData rpd = (RampartPolicyData)msgContext.</span>
getProperty(RampartMessageData.RAMPART_POLICY_DATA);
// Security policy data has not been populated at the time of verification
<span class="pc bpc" id="L149" title="1 of 2 branches missed."> if (rpd == null ) {</span>
<span class="nc" id="L150"> throw new AxisFault(&quot;InvalidSecurity&quot;);</span>
}
<span class="fc" id="L153"> boolean isInitiator = false;</span>
<span class="fc" id="L154"> Parameter clientSideParam = msgContext.getAxisService().</span>
getParameter(RampartMessageData.PARAM_CLIENT_SIDE);
<span class="fc bfc" id="L156" title="All 2 branches covered."> if(clientSideParam != null) {</span>
<span class="fc" id="L157"> isInitiator = true;</span>
}
//Now check for security processing results if security policy is available
<span class="pc bpc" id="L161" title="2 of 4 branches missed."> if(RampartUtil.isSecHeaderRequired(rpd,isInitiator,true) &amp;&amp; </span>
msgContext.getProperty(WSHandlerConstants.RECV_RESULTS) == null) {
<span class="nc" id="L163"> throw new AxisFault(&quot;InvalidSecurity&quot;);</span>
}
}
//Check for an empty security processing results when parameter based
//configuration is used
<span class="pc bpc" id="L170" title="2 of 4 branches missed."> if(msgContext.getParameter(WSSHandlerConstants.INFLOW_SECURITY) != null ||</span>
msgContext.getProperty(WSSHandlerConstants.INFLOW_SECURITY) != null) {
<span class="nc bnc" id="L172" title="All 2 branches missed."> if(msgContext.getProperty(WSHandlerConstants.RECV_RESULTS) == null) {</span>
<span class="nc" id="L173"> throw new AxisFault(&quot;InvalidSecurity&quot;);</span>
} else {
<span class="nc bnc" id="L175" title="All 2 branches missed."> if(((List&lt;WSHandlerResult&gt;)msgContext.getProperty(WSHandlerConstants.RECV_RESULTS)).size() == 0) {</span>
<span class="nc" id="L176"> throw new AxisFault(&quot;InvalidSecurity&quot;);</span>
}
}
}
// If a security header is there and Rampart is engaged, it has to be processed.
// If it is not processed, there must have been a problem in picking the policy
<span class="fc" id="L184"> SOAPHeaderBlock secHeader = getSecurityHeader(msgContext);</span>
<span class="pc bpc" id="L186" title="2 of 4 branches missed."> if (secHeader != null &amp;&amp; (secHeader.isProcessed() == false)) {</span>
<span class="nc" id="L187"> throw new AxisFault(&quot;InvalidSecurity - Security policy not found&quot;);</span>
}
<span class="fc" id="L190"> return InvocationResponse.CONTINUE;</span>
}
private SOAPHeaderBlock getSecurityHeader(MessageContext msgContext) throws AxisFault {
<span class="fc" id="L196"> SOAPHeader header = null;</span>
try {
<span class="fc" id="L198"> header = msgContext.getEnvelope().getHeader();</span>
<span class="nc" id="L199"> } catch (OMException ex) {</span>
<span class="nc" id="L200"> throw new AxisFault(</span>
&quot;PostDispatchVerificationHandler: cannot get SOAP header after security processing&quot;,
ex);
<span class="fc" id="L203"> }</span>
<span class="pc bpc" id="L205" title="1 of 2 branches missed."> if(header == null) {</span>
<span class="nc" id="L206"> return null;</span>
}
<span class="fc" id="L209"> Iterator headers = header.getChildElements();</span>
<span class="fc" id="L211"> SOAPHeaderBlock headerBlock = null;</span>
<span class="pc bpc" id="L213" title="1 of 2 branches missed."> while (headers.hasNext()) { </span>
// Find the wsse header
<span class="fc" id="L215"> SOAPHeaderBlock hb = (SOAPHeaderBlock) headers.next();</span>
<span class="pc bpc" id="L216" title="2 of 4 branches missed."> if (hb.getLocalName().equals(WSConstants.WSSE_LN)</span>
&amp;&amp; hb.getNamespace().getNamespaceURI().equals(WSConstants.WSSE_NS)) {
<span class="fc" id="L218"> headerBlock = hb;</span>
<span class="fc" id="L219"> break;</span>
}
<span class="nc" id="L221"> }</span>
<span class="fc" id="L223"> return headerBlock;</span>
}
}
</pre><div class="footer"><span class="right">Created with <a href="http://www.eclemma.org/jacoco">JaCoCo</a> 0.6.1.201212231917</span></div></body></html>