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.encoding.policy;
038:
039: import com.sun.xml.ws.api.WSBinding;
040: import com.sun.xml.ws.api.model.SEIModel;
041: import com.sun.xml.ws.policy.AssertionSet;
042: import com.sun.xml.ws.policy.Policy;
043: import com.sun.xml.ws.policy.PolicyAssertion;
044: import com.sun.xml.ws.policy.PolicyException;
045: import com.sun.xml.ws.policy.PolicyMap;
046: import com.sun.xml.ws.policy.PolicyMapExtender;
047: import com.sun.xml.ws.policy.PolicyMapKey;
048: import com.sun.xml.ws.policy.PolicySubject;
049: import com.sun.xml.ws.policy.jaxws.spi.PolicyMapUpdateProvider;
050: import com.sun.xml.ws.policy.privateutil.PolicyLogger;
051: import com.sun.xml.ws.policy.sourcemodel.AssertionData;
052: import java.util.ArrayList;
053: import javax.xml.ws.soap.MTOMFeature;
054:
055: import static com.sun.xml.ws.encoding.policy.EncodingConstants.OPTIMIZED_MIME_SERIALIZATION_ASSERTION;
056: import java.util.logging.Level;
057: import javax.xml.namespace.QName;
058:
059: /**
060: * Generate an MTOM policy if MTOM was enabled.
061: *
062: * @author Jakub Podlesak (japod at sun.com)
063: */
064: public class MtomMapUpdateProvider implements PolicyMapUpdateProvider {
065:
066: private static final PolicyLogger logger = PolicyLogger
067: .getLogger(MtomMapUpdateProvider.class);
068:
069: static class MtomAssertion extends PolicyAssertion {
070:
071: private static final AssertionData mtomData = AssertionData
072: .createAssertionData(OPTIMIZED_MIME_SERIALIZATION_ASSERTION);
073:
074: MtomAssertion() {
075: super (mtomData, null, null);
076: }
077: }
078:
079: /**
080: * Generates an MTOM policy if MTOM is enabled.
081: *
082: * <ol>
083: * <li>If MTOM is enabled
084: * <ol>
085: * <li>If MTOM policy does not already exist, generate
086: * <li>Otherwise do nothing
087: * </ol>
088: * <li>Otherwise, do nothing (that implies that we do not remove any MTOM policies if MTOM is disabled)
089: * </ol>
090: *
091: */
092: public void update(PolicyMapExtender policyMapMutator,
093: PolicyMap policyMap, SEIModel model, WSBinding wsBinding)
094: throws PolicyException {
095: logger.entering(policyMapMutator, policyMap, model, wsBinding);
096:
097: if (policyMap != null) {
098: final MTOMFeature mtomFeature = wsBinding
099: .getFeature(MTOMFeature.class);
100: if (logger.isLoggable(Level.FINEST)) {
101: logger.finest("mtomFeature = " + mtomFeature);
102: }
103: if ((mtomFeature != null) && mtomFeature.isEnabled()) {
104: final PolicyMapKey endpointKey = PolicyMap
105: .createWsdlEndpointScopeKey(model
106: .getServiceQName(), model.getPortName());
107: final Policy existingPolicy = policyMap
108: .getEndpointEffectivePolicy(endpointKey);
109: if ((existingPolicy == null)
110: || !existingPolicy
111: .contains(OPTIMIZED_MIME_SERIALIZATION_ASSERTION)) {
112: final QName bindingName = model
113: .getBoundPortTypeName();
114: final Policy mtomPolicy = createMtomPolicy(bindingName);
115: PolicySubject mtomPolicySubject = new PolicySubject(
116: bindingName, mtomPolicy);
117: PolicyMapKey aKey = PolicyMap
118: .createWsdlEndpointScopeKey(model
119: .getServiceQName(), model
120: .getPortName());
121: policyMapMutator.putEndpointSubject(aKey,
122: mtomPolicySubject);
123: logger.fine("Added MTOM policy with ID \""
124: + mtomPolicy.getIdOrName()
125: + "\" to binding element \"" + bindingName
126: + "\"");
127: } else {
128: logger
129: .fine("MTOM policy exists already, doing nothing");
130: }
131: }
132: } // endif policy map not null
133:
134: logger.exiting();
135: }
136:
137: /**
138: * Create a policy with an MTOM assertion.
139: *
140: * @param model The binding element name. Used to generate a (locally) unique ID for the policy.
141: * @return The policy.
142: */
143: private Policy createMtomPolicy(final QName bindingName) {
144: ArrayList<AssertionSet> assertionSets = new ArrayList<AssertionSet>(
145: 1);
146: ArrayList<PolicyAssertion> assertions = new ArrayList<PolicyAssertion>(
147: 1);
148: assertions.add(new MtomAssertion());
149: assertionSets.add(AssertionSet.createAssertionSet(assertions));
150: return Policy.createPolicy(null, bindingName.getLocalPart()
151: + "_MTOM_Policy", assertionSets);
152: }
153:
154: }
|