1/* 2 * Copyright (c) 1997, 2013, 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 26package com.sun.xml.internal.ws.addressing.policy; 27 28import com.sun.xml.internal.ws.addressing.W3CAddressingMetadataConstants; 29import com.sun.xml.internal.ws.api.WSBinding; 30import com.sun.xml.internal.ws.api.model.SEIModel; 31import com.sun.xml.internal.ws.policy.AssertionSet; 32import com.sun.xml.internal.ws.policy.Policy; 33import com.sun.xml.internal.ws.policy.PolicyAssertion; 34import com.sun.xml.internal.ws.policy.PolicyException; 35import com.sun.xml.internal.ws.policy.PolicyMap; 36import com.sun.xml.internal.ws.policy.PolicySubject; 37import com.sun.xml.internal.ws.policy.jaxws.spi.PolicyMapConfigurator; 38import com.sun.xml.internal.ws.policy.privateutil.PolicyLogger; 39import com.sun.xml.internal.ws.policy.sourcemodel.AssertionData; 40import com.sun.xml.internal.ws.policy.subject.WsdlBindingSubject; 41 42import java.util.ArrayList; 43import java.util.Collection; 44import java.util.Collections; 45import java.util.logging.Level; 46import javax.xml.namespace.QName; 47import javax.xml.ws.soap.AddressingFeature; 48 49/** 50 * Generate an addressing policy and updates the PolicyMap if AddressingFeature is enabled. 51 * 52 * @author Fabian Ritzmann 53 * @author Rama Pulavarthi 54 */ 55public class AddressingPolicyMapConfigurator implements PolicyMapConfigurator { 56 57 private static final PolicyLogger LOGGER = PolicyLogger.getLogger(AddressingPolicyMapConfigurator.class); 58 59 private static final class AddressingAssertion extends PolicyAssertion { 60 /** 61 * Creates an assertion with nested alternatives. 62 * 63 * @param assertionData 64 * @param nestedAlternative 65 */ 66 AddressingAssertion(AssertionData assertionData, final AssertionSet nestedAlternative) { 67 super(assertionData, null, nestedAlternative); 68 } 69 70 /** 71 * Creates an assertion with no nested alternatives. 72 * 73 * @param assertionData 74 */ 75 AddressingAssertion(AssertionData assertionData) { 76 super(assertionData, null, null); 77 } 78 } 79 80 81 /** 82 * Puts an addressing policy into the PolicyMap if the addressing feature was set. 83 */ 84 public Collection<PolicySubject> update(final PolicyMap policyMap, final SEIModel model, final WSBinding wsBinding) 85 throws PolicyException { 86 LOGGER.entering(policyMap, model, wsBinding); 87 88 Collection<PolicySubject> subjects = new ArrayList<PolicySubject>(); 89 if (policyMap != null) { 90 final AddressingFeature addressingFeature = wsBinding.getFeature(AddressingFeature.class); 91 if (LOGGER.isLoggable(Level.FINEST)) { 92 LOGGER.finest("addressingFeature = " + addressingFeature); 93 } 94 if ((addressingFeature != null) && addressingFeature.isEnabled()) { 95 //add wsam:Addrressing assertion if not exists. 96 addWsamAddressing(subjects, policyMap, model, addressingFeature); 97 } 98 } // endif policy map not null 99 LOGGER.exiting(subjects); 100 return subjects; 101 } 102 103 private void addWsamAddressing(Collection<PolicySubject> subjects, PolicyMap policyMap, SEIModel model, AddressingFeature addressingFeature) 104 throws PolicyException { 105 final QName bindingName = model.getBoundPortTypeName(); 106 final WsdlBindingSubject wsdlSubject = WsdlBindingSubject.createBindingSubject(bindingName); 107 final Policy addressingPolicy = createWsamAddressingPolicy(bindingName, addressingFeature); 108 final PolicySubject addressingPolicySubject = new PolicySubject(wsdlSubject, addressingPolicy); 109 subjects.add(addressingPolicySubject); 110 if (LOGGER.isLoggable(Level.FINE)) { 111 LOGGER.fine("Added addressing policy with ID \"" + addressingPolicy.getIdOrName() + "\" to binding element \"" + bindingName + "\""); 112 } 113 } 114 115 /** 116 * Create a policy with an WSAM Addressing assertion. 117 */ 118 private Policy createWsamAddressingPolicy(final QName bindingName, AddressingFeature af) { 119 final ArrayList<AssertionSet> assertionSets = new ArrayList<AssertionSet>(1); 120 final ArrayList<PolicyAssertion> assertions = new ArrayList<PolicyAssertion>(1); 121 final AssertionData addressingData = 122 AssertionData.createAssertionData(W3CAddressingMetadataConstants.WSAM_ADDRESSING_ASSERTION); 123 if (!af.isRequired()) { 124 addressingData.setOptionalAttribute(true); 125 } 126 try { 127 AddressingFeature.Responses responses = af.getResponses(); 128 if (responses == AddressingFeature.Responses.ANONYMOUS) { 129 AssertionData nestedAsserData = AssertionData.createAssertionData(W3CAddressingMetadataConstants.WSAM_ANONYMOUS_NESTED_ASSERTION); 130 PolicyAssertion nestedAsser = new AddressingAssertion(nestedAsserData, null); 131 assertions.add(new AddressingAssertion(addressingData, AssertionSet.createAssertionSet(Collections.singleton(nestedAsser)))); 132 } else if (responses == AddressingFeature.Responses.NON_ANONYMOUS) { 133 final AssertionData nestedAsserData = AssertionData.createAssertionData(W3CAddressingMetadataConstants.WSAM_NONANONYMOUS_NESTED_ASSERTION); 134 PolicyAssertion nestedAsser = new AddressingAssertion(nestedAsserData, null); 135 assertions.add(new AddressingAssertion(addressingData, AssertionSet.createAssertionSet(Collections.singleton(nestedAsser)))); 136 } else { 137 assertions.add(new AddressingAssertion(addressingData, AssertionSet.createAssertionSet(null))); 138 } 139 } catch (NoSuchMethodError e) { 140 //If JAX-WS 2.2 API is really required, it would been reported in @Addressing or wsam:Addressing processing 141 //Don't add any nested assertion to mimic the 2.1 behavior 142 assertions.add(new AddressingAssertion(addressingData, AssertionSet.createAssertionSet(null))); 143 } 144 assertionSets.add(AssertionSet.createAssertionSet(assertions)); 145 return Policy.createPolicy(null, bindingName.getLocalPart() + "_WSAM_Addressing_Policy", assertionSets); 146 } 147} 148