1 /*
   2  * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.xml.internal.ws.addressing.policy;
  27 
  28 import com.sun.xml.internal.ws.api.addressing.AddressingVersion;
  29 import com.sun.xml.internal.ws.policy.AssertionSet;
  30 import com.sun.xml.internal.ws.policy.NestedPolicy;
  31 import com.sun.xml.internal.ws.policy.Policy;
  32 import com.sun.xml.internal.ws.policy.PolicyAssertion;
  33 import com.sun.xml.internal.ws.policy.PolicyException;
  34 import com.sun.xml.internal.ws.policy.PolicyMap;
  35 import com.sun.xml.internal.ws.policy.PolicyMapKey;
  36 import com.sun.xml.internal.ws.policy.jaxws.spi.PolicyFeatureConfigurator;
  37 import com.sun.xml.internal.ws.policy.privateutil.PolicyLogger;
  38 import com.sun.xml.internal.ws.addressing.W3CAddressingMetadataConstants;
  39 import com.sun.xml.internal.ws.resources.ModelerMessages;
  40 import com.sun.xml.internal.bind.util.Which;
  41 
  42 import java.util.Collection;
  43 import java.util.Iterator;
  44 import java.util.LinkedList;
  45 import java.util.logging.Level;
  46 import javax.xml.namespace.QName;
  47 import javax.xml.ws.WebServiceFeature;
  48 import javax.xml.ws.WebServiceException;
  49 import javax.xml.ws.soap.AddressingFeature;
  50 
  51 /**
  52  * This Policy extension configures the WSDLModel with AddressingFeature when Addressing assertions are present in the
  53  * PolicyMap.
  54  *
  55  * @author japod
  56  * @author Rama Pulavarthi
  57  */
  58 public class AddressingFeatureConfigurator implements PolicyFeatureConfigurator {
  59 
  60     private static final PolicyLogger LOGGER = PolicyLogger.getLogger(AddressingFeatureConfigurator.class);
  61 
  62     private static final QName[] ADDRESSING_ASSERTIONS = {
  63         new QName(AddressingVersion.MEMBER.policyNsUri, "UsingAddressing")};
  64 
  65     /**
  66      * Creates a new instance of AddressingFeatureConfigurator
  67      */
  68     public AddressingFeatureConfigurator() {
  69     }
  70 
  71     public Collection<WebServiceFeature> getFeatures(final PolicyMapKey key, final PolicyMap policyMap) throws PolicyException {
  72         LOGGER.entering(key, policyMap);
  73         final Collection<WebServiceFeature> features = new LinkedList<WebServiceFeature>();
  74         if ((key != null) && (policyMap != null)) {
  75             final Policy policy = policyMap.getEndpointEffectivePolicy(key);
  76             for (QName addressingAssertionQName : ADDRESSING_ASSERTIONS) {
  77                 if ((policy != null) && policy.contains(addressingAssertionQName)) {
  78                     final Iterator <AssertionSet> assertions = policy.iterator();
  79                     while(assertions.hasNext()){
  80                         final AssertionSet assertionSet = assertions.next();
  81                         final Iterator<PolicyAssertion> policyAssertion = assertionSet.iterator();
  82                         while(policyAssertion.hasNext()){
  83                             final PolicyAssertion assertion = policyAssertion.next();
  84                             if(assertion.getName().equals(addressingAssertionQName)){
  85                                 final WebServiceFeature feature = AddressingVersion.getFeature(addressingAssertionQName.getNamespaceURI(), true, !assertion.isOptional());
  86                                 if (LOGGER.isLoggable(Level.FINE)) {
  87                                     LOGGER.fine("Added addressing feature \"" + feature + "\" for element \"" + key + "\"");
  88                                 }
  89                                 features.add(feature);
  90                             } // end-if non optional wsa assertion found
  91                         } // next assertion
  92                     } // next alternative
  93                 } // end-if policy contains wsa assertion
  94             } //end foreach addr assertion
  95 
  96             // Deal with WS-Addressing 1.0 Metadata assertions
  97             if (policy != null && policy.contains(W3CAddressingMetadataConstants.WSAM_ADDRESSING_ASSERTION)) {
  98                 for (AssertionSet assertions : policy) {
  99                     for (PolicyAssertion assertion : assertions) {
 100                         if (assertion.getName().equals(W3CAddressingMetadataConstants.WSAM_ADDRESSING_ASSERTION)) {
 101                             NestedPolicy nestedPolicy = assertion.getNestedPolicy();
 102                             boolean requiresAnonymousResponses = false;
 103                             boolean requiresNonAnonymousResponses = false;
 104                             if (nestedPolicy != null) {
 105                                 requiresAnonymousResponses = nestedPolicy.contains(W3CAddressingMetadataConstants.WSAM_ANONYMOUS_NESTED_ASSERTION);
 106                                 requiresNonAnonymousResponses = nestedPolicy.contains(W3CAddressingMetadataConstants.WSAM_NONANONYMOUS_NESTED_ASSERTION);
 107                             }
 108                             if(requiresAnonymousResponses && requiresNonAnonymousResponses) {
 109                                 throw new WebServiceException("Only one among AnonymousResponses and NonAnonymousResponses can be nested in an Addressing assertion");
 110                             }
 111 
 112                             final WebServiceFeature feature;
 113                             try {
 114                                 if (requiresAnonymousResponses) {
 115                                     feature = new AddressingFeature(true, !assertion.isOptional(), AddressingFeature.Responses.ANONYMOUS);
 116                                 } else if (requiresNonAnonymousResponses) {
 117                                     feature = new AddressingFeature(true, !assertion.isOptional(), AddressingFeature.Responses.NON_ANONYMOUS);
 118                                 } else {
 119                                     feature = new AddressingFeature(true, !assertion.isOptional());
 120                                 }
 121                             } catch (NoSuchMethodError e) {
 122                                 throw LOGGER.logSevereException(new PolicyException(ModelerMessages.RUNTIME_MODELER_ADDRESSING_RESPONSES_NOSUCHMETHOD(toJar(Which.which(AddressingFeature.class))), e));
 123                             }
 124                             if (LOGGER.isLoggable(Level.FINE)) {
 125                                 LOGGER.fine("Added addressing feature \"" + feature + "\" for element \"" + key + "\"");
 126                             }
 127                             features.add(feature);
 128                         }
 129                     }
 130                 }
 131             }
 132         }
 133         LOGGER.exiting(features);
 134         return features;
 135     }
 136 
 137     /**
 138      * Given the URL String inside jar, returns the URL to the jar itself.
 139      */
 140     private static String toJar(String url) {
 141         if(!url.startsWith("jar:"))
 142             return url;
 143         url = url.substring(4); // cut off jar:
 144         return url.substring(0,url.lastIndexOf('!'));    // cut off everything after '!'
 145     }
 146 }