001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common Development
008: * and Distribution License("CDDL") (collectively, the "License"). You
009: * may not use this file except in compliance with the License. You can obtain
010: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
011: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
012: * language governing permissions and limitations under the License.
013: *
014: * When distributing the software, include this License Header Notice in each
015: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
016: * Sun designates this particular file as subject to the "Classpath" exception
017: * as provided by Sun in the GPL Version 2 section of the License file that
018: * accompanied this code. If applicable, add the following below the License
019: * Header, with the fields enclosed by brackets [] replaced by your own
020: * identifying information: "Portions Copyrighted [year]
021: * [name of copyright owner]"
022: *
023: * Contributor(s):
024: *
025: * If you wish your version of this file to be governed by only the CDDL or
026: * only the GPL Version 2, indicate your decision by adding "[Contributor]
027: * elects to include this software in this distribution under the [CDDL or GPL
028: * Version 2] license." If you don't indicate a single choice of license, a
029: * recipient has the option to distribute your version of this file under
030: * either the CDDL, the GPL Version 2 or to extend the choice of license to
031: * its licensees as provided above. However, if you add GPL Version 2 code
032: * and therefore, elected the GPL Version 2 license, then the option applies
033: * only if the new code is made subject to such option by the copyright
034: * holder.
035: */
036:
037: package com.sun.xml.ws.addressing.policy;
038:
039: import com.sun.xml.ws.api.WSBinding;
040: import com.sun.xml.ws.api.addressing.AddressingVersion;
041: import com.sun.xml.ws.api.model.SEIModel;
042: import com.sun.xml.ws.policy.AssertionSet;
043: import com.sun.xml.ws.policy.Policy;
044: import com.sun.xml.ws.policy.PolicyAssertion;
045: import com.sun.xml.ws.policy.PolicyException;
046: import com.sun.xml.ws.policy.PolicyMap;
047: import com.sun.xml.ws.policy.PolicyMapExtender;
048: import com.sun.xml.ws.policy.PolicyMapKey;
049: import com.sun.xml.ws.policy.PolicySubject;
050: import com.sun.xml.ws.policy.jaxws.spi.PolicyMapUpdateProvider;
051: import com.sun.xml.ws.policy.privateutil.PolicyLogger;
052: import com.sun.xml.ws.policy.sourcemodel.AssertionData;
053: import java.util.ArrayList;
054: import java.util.logging.Level;
055: import javax.xml.namespace.QName;
056: import javax.xml.ws.soap.AddressingFeature;
057:
058: /**
059: * Generate an addressing policy.
060: *
061: * @author Fabian Ritzmann
062: */
063: public class AddressingMapUpdateProvider implements
064: PolicyMapUpdateProvider {
065:
066: private static final PolicyLogger LOGGER = PolicyLogger
067: .getLogger(AddressingMapUpdateProvider.class);
068:
069: static final class AddressingAssertion extends PolicyAssertion {
070:
071: AddressingAssertion(AssertionData data) {
072: super (data, null);
073: }
074: }
075:
076: /**
077: * Puts an addressing policy into the PolicyMap if the addressing feature was set.
078: */
079: public void update(final PolicyMapExtender policyMapMutator,
080: final PolicyMap policyMap, final SEIModel model,
081: final WSBinding wsBinding) throws PolicyException {
082: LOGGER.entering(policyMapMutator, policyMap, model, wsBinding);
083:
084: if (policyMap != null) {
085: final AddressingFeature addressingFeature = wsBinding
086: .getFeature(AddressingFeature.class);
087: if (LOGGER.isLoggable(Level.FINEST)) {
088: LOGGER.finest("addressingFeature = "
089: + addressingFeature);
090: }
091: if ((addressingFeature != null)
092: && addressingFeature.isEnabled()) {
093: final AddressingVersion addressingVersion = AddressingVersion
094: .fromFeature(addressingFeature);
095: final QName usingAddressing = new QName(
096: addressingVersion.policyNsUri,
097: "UsingAddressing");
098: final PolicyMapKey endpointKey = PolicyMap
099: .createWsdlEndpointScopeKey(model
100: .getServiceQName(), model.getPortName());
101: final Policy existingPolicy = policyMap
102: .getEndpointEffectivePolicy(endpointKey);
103: if ((existingPolicy == null)
104: || !existingPolicy.contains(usingAddressing)) {
105: final QName bindingName = model
106: .getBoundPortTypeName();
107: final Policy addressingPolicy = createAddressingPolicy(
108: bindingName, usingAddressing,
109: addressingFeature.isRequired());
110: final PolicySubject addressingPolicySubject = new PolicySubject(
111: bindingName, addressingPolicy);
112: final PolicyMapKey aKey = PolicyMap
113: .createWsdlEndpointScopeKey(model
114: .getServiceQName(), model
115: .getPortName());
116: policyMapMutator.putEndpointSubject(aKey,
117: addressingPolicySubject);
118: if (LOGGER.isLoggable(Level.FINE)) {
119: LOGGER
120: .fine("Added addressing policy with ID \""
121: + addressingPolicy
122: .getIdOrName()
123: + "\" to binding element \""
124: + bindingName + "\"");
125: }
126: } else {
127: if (LOGGER.isLoggable(Level.FINE)) {
128: LOGGER
129: .fine("Addressing policy exists already, doing nothing");
130: }
131: }
132: }
133: } // endif policy map not null
134: LOGGER.exiting();
135: }
136:
137: /**
138: * Create a policy with an MTOM assertion.
139: *
140: * @param bindingName The wsdl:binding element name. Used to generate a (locally) unique ID for the policy.
141: * @param assertionName The fully qualified name of the addressing policy assertion.
142: * @param isRequired True, if the addressing feature was set to required, false otherwise.
143: * @return A policy that contains one policy assertion that corresponds to the given assertion name.
144: */
145: private Policy createAddressingPolicy(final QName bindingName,
146: final QName assertionName, final boolean isRequired) {
147: final ArrayList<AssertionSet> assertionSets = new ArrayList<AssertionSet>(
148: 1);
149: final ArrayList<PolicyAssertion> assertions = new ArrayList<PolicyAssertion>(
150: 1);
151: final AssertionData addressingData = AssertionData
152: .createAssertionData(assertionName);
153: if (!isRequired) {
154: addressingData.setOptionalAttribute(true);
155: }
156: assertions.add(new AddressingAssertion(addressingData));
157: assertionSets.add(AssertionSet.createAssertionSet(assertions));
158: return Policy.createPolicy(null, bindingName.getLocalPart()
159: + "_Addressing_Policy", assertionSets);
160: }
161: }
|