Clover coverage report - Apache Addressing - 1.0
Coverage timestamp: Tue Mar 22 2005 07:59:24 EST
file stats: LOC: 198   Methods: 11
NCLOC: 133   Classes: 1
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover
 
 Source file Conditionals Statements Methods TOTAL
ClientSideAddressingHandler.java 34.4% 72.7% 100% 63.3%
coverage coverage
 1   
 /*
 2   
  * Copyright  2004 The Apache Software Foundation.
 3   
  *
 4   
  *  Licensed under the Apache License, Version 2.0 (the "License");
 5   
  *  you may not use this file except in compliance with the License.
 6   
  *  You may obtain a copy of the License at
 7   
  *
 8   
  *      http://www.apache.org/licenses/LICENSE-2.0
 9   
  *
 10   
  *  Unless required by applicable law or agreed to in writing, software
 11   
  *  distributed under the License is distributed on an "AS IS" BASIS,
 12   
  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13   
  *  See the License for the specific language governing permissions and
 14   
  *  limitations under the License.
 15   
  *
 16   
  */
 17   
 package org.apache.ws.addressing.handler;
 18   
 
 19   
 import org.apache.axis.message.addressing.Action;
 20   
 import org.apache.axis.message.addressing.AddressingHeaders;
 21   
 import org.apache.axis.message.addressing.Constants;
 22   
 import org.apache.axis.message.addressing.EndpointReference;
 23   
 import org.apache.axis.message.addressing.To;
 24   
 import org.apache.axis.types.URI;
 25   
 import org.apache.commons.logging.Log;
 26   
 import org.apache.commons.logging.LogFactory;
 27   
 
 28   
 import javax.xml.rpc.JAXRPCException;
 29   
 import javax.xml.rpc.handler.MessageContext;
 30   
 import javax.xml.rpc.handler.soap.SOAPMessageContext;
 31   
 import javax.xml.soap.SOAPMessage;
 32   
 
 33   
 /**
 34   
  * A client-side JAX-RPC {@link javax.xml.rpc.handler.Handler} that inserts WS-Addressing headers into outgoing SOAP requests
 35   
  * and extracts them from incoming SOAP responses.
 36   
  *
 37   
  * @author Davanum Srinivas (dims@yahoo.com)
 38   
  * @author Ian P. Springer <ian_springer@hp.com>
 39   
  */
 40   
 public class ClientSideAddressingHandler extends AbstractAddressingHandler {
 41   
 
 42   
     private static final Log LOG =
 43   
             LogFactory.getLog(ClientSideAddressingHandler.class.getName());
 44   
 
 45   
     private static final String MSG_CONTEXT_PROP__SOAP_ACTION = javax.xml.rpc.Call.SOAPACTION_URI_PROPERTY;
 46   
 
 47  2
     public boolean handleRequest(MessageContext msgContext) {
 48  2
         SOAPMessageContext soapMsgContext = (SOAPMessageContext) msgContext;
 49  2
         try {
 50  2
             AddressingHeaders headers =
 51   
                     (AddressingHeaders) msgContext.getProperty(Constants.ENV_ADDRESSING_REQUEST_HEADERS);
 52  2
             if (headers == null) {
 53  2
                 headers = new AddressingHeaders();
 54   
             }
 55  2
             headers.setSetMustUnderstand(isMustUnderstandEnabled(msgContext));
 56  2
             addMessageIdHeader(headers);
 57  2
             addToHeader(headers, msgContext);
 58  2
             addActionHeader(headers, msgContext);
 59  2
             addFromHeader(headers, msgContext);
 60  2
             addReplyToHeader(headers, msgContext);
 61  2
             if (headers.getFaultTo() == null && msgContext
 62   
                     .containsProperty(Constants.ENV_ADDRESSING_FAULTTO_URI)) {
 63  0
                 headers.setFaultTo(new EndpointReference((String) msgContext
 64   
                         .getProperty(Constants.ENV_ADDRESSING_FAULTTO_URI)));
 65   
             }
 66   
 
 67  2
             SOAPMessage msg = soapMsgContext.getMessage();
 68  2
             headers.toEnvelope(msg.getSOAPPart().getEnvelope(), getActor());
 69  2
             msgContext.setProperty(Constants.ENV_ADDRESSING_REQUEST_HEADERS, headers);
 70   
         } catch (Exception e) {
 71  0
             if (LOG.isDebugEnabled()) {
 72  0
                 e.printStackTrace();
 73   
             }
 74  0
             throw new JAXRPCException("unexpected error in handleRequest(): " + e, e);
 75   
         }
 76  2
         return CONTINUE_HANDLER_CHAIN_PROCESSING;
 77   
     }
 78   
 
 79  1
     public boolean handleResponse(MessageContext msgContext) {
 80  1
         SOAPMessageContext soapMsgContext = (SOAPMessageContext) msgContext;
 81  1
         try {
 82  1
             SOAPMessage msg = soapMsgContext.getMessage();
 83  1
             if (msg == null) {
 84  0
                 return CONTINUE_HANDLER_CHAIN_PROCESSING;
 85   
             }
 86   
 
 87  1
             AddressingHeaders headers =
 88   
                     new AddressingHeaders(msg.getSOAPPart().getEnvelope(),
 89   
                             getActor(),
 90   
                             true,
 91   
                             isRemoveHeadersEnabled(),
 92   
                             false,
 93   
                             getReferencePropertyQNames());
 94  1
             msgContext.setProperty(Constants.ENV_ADDRESSING_RESPONSE_HEADERS,
 95   
                     headers);
 96   
         } catch (Exception e) {
 97  0
             if (LOG.isDebugEnabled()) {
 98  0
                 e.printStackTrace();
 99   
             }
 100  0
             throw new JAXRPCException("unexpected error in handleResponse(): " + e, e);
 101   
         }
 102  1
         return CONTINUE_HANDLER_CHAIN_PROCESSING;
 103   
     }
 104   
 
 105  1
     public boolean handleFault(MessageContext messageContext) {
 106  1
         return CONTINUE_HANDLER_CHAIN_PROCESSING;
 107   
     }
 108   
 
 109   
     /**
 110   
      * The JAX-RPC APIs don't provide a way to retrieve the value of the SOAPAction HTTP header,
 111   
      * so platform-specific subclasses should implement this method if feasible.
 112   
      *
 113   
      * @param msgContext JAX-RPC message context
 114   
      * @return the value of the SOAPAction HTTP header
 115   
      */
 116  2
     protected String getSOAPAction(MessageContext msgContext) {
 117  2
         String soapAction = (String) msgContext.getProperty(MSG_CONTEXT_PROP__SOAP_ACTION);
 118  2
         return soapAction != null ? soapAction : "";
 119   
     }
 120   
 
 121   
     /**
 122   
      * The JAX-RPC APIs don't provide a way to set the value of the SOAPAction HTTP header,
 123   
      * so platform-specific subclasses should implement this method if feasible.
 124   
      *
 125   
      * @param msgContext JAX-RPC message context
 126   
      * @param actionURI  the SOAPAction URI to be set
 127   
      */
 128  3
     protected void setSOAPAction(MessageContext msgContext, String actionURI) {
 129  3
         msgContext.setProperty(MSG_CONTEXT_PROP__SOAP_ACTION, actionURI);
 130   
     }
 131   
 
 132   
     /**
 133   
      * Returns the endpoint URL for the specified message context. Since there's no
 134   
      * generic JAX-RPC way to obtain the request's endpoint URL, we return null here.
 135   
      * Platform-specific handlers can override if the platform provides a means to
 136   
      * obtain the endpoint URL.
 137   
      *
 138   
      * @return the endpoint URL for the specified message context
 139   
      */
 140  2
     protected String getEndpointURL(MessageContext msgContext) {
 141  2
         return null;
 142   
     }
 143   
 
 144  2
     private void addReplyToHeader(AddressingHeaders addrHeaders, MessageContext msgContext)
 145   
             throws URI.MalformedURIException {
 146  2
         if (isPropertyTrue(msgContext, Constants.ENV_ADDRESSING_SEND_REPLYTO)) {
 147  0
             if (addrHeaders.getReplyTo() == null) {
 148  0
                 if (msgContext
 149   
                         .containsProperty(Constants.ENV_ADDRESSING_REPLYTO_URI)) {
 150  0
                     addrHeaders.setReplyTo(new EndpointReference((String) msgContext
 151   
                             .getProperty(Constants.ENV_ADDRESSING_REPLYTO_URI)));
 152   
                 } else {
 153  0
                     addrHeaders.setReplyTo(addrHeaders.getFrom());
 154   
                 }
 155   
             }
 156   
         }
 157   
     }
 158   
 
 159  2
     private void addFromHeader(AddressingHeaders addrHeaders, MessageContext msgContext)
 160   
             throws URI.MalformedURIException {
 161  2
         if (addrHeaders.getFrom() == null) {
 162  2
             if (msgContext
 163   
                     .containsProperty(Constants.ENV_ADDRESSING_FROM_URI)) {
 164  0
                 addrHeaders.setFrom(new EndpointReference((String) msgContext
 165   
                         .getProperty(Constants.ENV_ADDRESSING_FROM_URI)));
 166   
             } else {
 167  2
                 addrHeaders.setFrom(new EndpointReference(Constants.NS_URI_ANONYMOUS));
 168   
             }
 169   
         }
 170   
     }
 171   
 
 172  2
     private void addMessageIdHeader(AddressingHeaders addrHeaders)
 173   
             throws URI.MalformedURIException {
 174  2
         if (addrHeaders.getMessageID() == null) {
 175  2
             addrHeaders.setMessageID(createMessageID());
 176   
         }
 177   
     }
 178   
 
 179  2
     private void addToHeader(AddressingHeaders addrHeaders, MessageContext msgContext)
 180   
             throws URI.MalformedURIException {
 181  2
         if (addrHeaders.getTo() == null) {
 182  2
             String endpointURL = getEndpointURL(msgContext);
 183  2
             addrHeaders.setTo(endpointURL != null ? new To(endpointURL) : null);
 184   
         }
 185   
     }
 186   
 
 187  2
     private void addActionHeader(AddressingHeaders addrHeaders, MessageContext msgContext)
 188   
             throws URI.MalformedURIException {
 189  2
         String actionURI = getSOAPAction(msgContext);
 190  2
         if (actionURI != null) {
 191  2
             addrHeaders.setAction(new Action(new URI(actionURI)));
 192  0
         } else if (addrHeaders.getAction() != null) {
 193  0
             setSOAPAction(msgContext, addrHeaders.getAction().toString());
 194   
         }
 195   
     }
 196   
 
 197   
 }
 198