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.encoding.policy;
27 
28 import static com.sun.xml.internal.ws.encoding.policy.EncodingConstants.OPTIMIZED_MIME_SERIALIZATION_ASSERTION;
29 import com.sun.xml.internal.ws.api.WSBinding;
30 import com.sun.xml.internal.ws.api.model.SEIModel;
31 import com.sun.xml.internal.ws.policy.AssertionSet;
32 import com.sun.xml.internal.ws.policy.Policy;
33 import com.sun.xml.internal.ws.policy.PolicyAssertion;
34 import com.sun.xml.internal.ws.policy.PolicyException;
35 import com.sun.xml.internal.ws.policy.PolicyMap;
36 import com.sun.xml.internal.ws.policy.PolicySubject;
37 import com.sun.xml.internal.ws.policy.jaxws.spi.PolicyMapConfigurator;
38 import com.sun.xml.internal.ws.policy.privateutil.PolicyLogger;
39 import com.sun.xml.internal.ws.policy.sourcemodel.AssertionData;
40 import com.sun.xml.internal.ws.policy.subject.WsdlBindingSubject;
41 
42 import java.util.ArrayList;
43 import java.util.Collection;
44 import java.util.logging.Level;
45 import javax.xml.namespace.QName;
46 import javax.xml.ws.soap.MTOMFeature;
47 
48 /**
49  * Generate an MTOM policy if MTOM was enabled.
50  *
51  * @author Jakub Podlesak (japod at sun.com)
52  * @author Fabian Ritzmann
53  */
54 public class MtomPolicyMapConfigurator implements PolicyMapConfigurator {
55 
56     private static final PolicyLogger LOGGER = PolicyLogger.getLogger(MtomPolicyMapConfigurator.class);
57 
58     static class MtomAssertion extends PolicyAssertion {
59 
60         private static final AssertionData mtomData;
61         static {
62             mtomData= AssertionData.createAssertionData(OPTIMIZED_MIME_SERIALIZATION_ASSERTION);
63             //JAX-WS MTOMFeature does n't currently capture if MTOM is required/optional.
64             //JAX-WS accepts both normal messages and XOP encoded messages. Using wsp:Optional=true represents that behavior.
65             //Moreover, this allows interoperability with non-MTOM aware clients.
66             //See https://wsit.dev.java.net/issues/show_bug.cgi?id=1062
67             mtomData.setOptionalAttribute(true);
68         }
69 
MtomAssertion()70         MtomAssertion() {
71             super(mtomData, null, null);
72 
73         }
74     }
75 
76     /**
77      * Generates an MTOM policy if MTOM is enabled.
78      *
79      * <ol>
80      * <li>If MTOM is enabled
81      * <ol>
82      * <li>If MTOM policy does not already exist, generate
83      * <li>Otherwise do nothing
84      * </ol>
85      * <li>Otherwise, do nothing (that implies that we do not remove any MTOM policies if MTOM is disabled)
86      * </ol>
87      *
88      */
update(PolicyMap policyMap, SEIModel model, WSBinding wsBinding)89     public Collection<PolicySubject> update(PolicyMap policyMap, SEIModel model, WSBinding wsBinding) throws PolicyException {
90         LOGGER.entering(policyMap, model, wsBinding);
91 
92         Collection<PolicySubject> subjects = new ArrayList<PolicySubject>();
93         if (policyMap != null) {
94             final MTOMFeature mtomFeature = wsBinding.getFeature(MTOMFeature.class);
95             if (LOGGER.isLoggable(Level.FINEST)) {
96                 LOGGER.finest("mtomFeature = " + mtomFeature);
97             }
98             if ((mtomFeature != null) && mtomFeature.isEnabled()) {
99                 final QName bindingName = model.getBoundPortTypeName();
100                 final WsdlBindingSubject wsdlSubject = WsdlBindingSubject.createBindingSubject(bindingName);
101                 final Policy mtomPolicy = createMtomPolicy(bindingName);
102                 final PolicySubject mtomPolicySubject = new PolicySubject(wsdlSubject, mtomPolicy);
103                 subjects.add(mtomPolicySubject);
104                 if (LOGGER.isLoggable(Level.FINEST)) {
105                     LOGGER.fine("Added MTOM policy with ID \"" + mtomPolicy.getIdOrName() + "\" to binding element \"" + bindingName + "\"");
106                 }
107             }
108         } // endif policy map not null
109 
110         LOGGER.exiting(subjects);
111         return subjects;
112     }
113 
114 
115     /**
116      * Create a policy with an MTOM assertion.
117      *
118      * @param model The binding element name. Used to generate a (locally) unique ID for the policy.
119      * @return The policy.
120      */
createMtomPolicy(final QName bindingName)121     private Policy createMtomPolicy(final QName bindingName) {
122         ArrayList<AssertionSet> assertionSets = new ArrayList<AssertionSet>(1);
123         ArrayList<PolicyAssertion> assertions = new ArrayList<PolicyAssertion>(1);
124         assertions.add(new MtomAssertion());
125         assertionSets.add(AssertionSet.createAssertionSet(assertions));
126         return Policy.createPolicy(null, bindingName.getLocalPart() + "_MTOM_Policy", assertionSets);
127     }
128 
129 }
130