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.policy.jaxws;
27 
28 import com.sun.xml.internal.ws.api.policy.AlternativeSelector;
29 import com.sun.xml.internal.ws.api.policy.PolicyResolver;
30 import com.sun.xml.internal.ws.api.policy.ValidationProcessor;
31 import com.sun.xml.internal.ws.policy.AssertionSet;
32 import com.sun.xml.internal.ws.policy.EffectivePolicyModifier;
33 import com.sun.xml.internal.ws.policy.Policy;
34 import com.sun.xml.internal.ws.policy.PolicyAssertion;
35 import com.sun.xml.internal.ws.policy.PolicyException;
36 import com.sun.xml.internal.ws.policy.PolicyMap;
37 import com.sun.xml.internal.ws.policy.spi.PolicyAssertionValidator.Fitness;
38 import com.sun.xml.internal.ws.resources.PolicyMessages;
39 
40 import javax.xml.ws.WebServiceException;
41 
42 /**
43  * This default implementation runs the policy validators on the server side and
44  * selects a policy alternative on the client side.
45  *
46  * @author Rama Pulavarthi
47  * @author Fabian Ritzmann
48  */
49 public class DefaultPolicyResolver implements PolicyResolver {
50 
resolve(ServerContext context)51     public PolicyMap resolve(ServerContext context) {
52         PolicyMap map = context.getPolicyMap();
53         if(map != null)
54             validateServerPolicyMap(map);
55         return map;
56     }
57 
resolve(ClientContext context)58     public PolicyMap resolve(ClientContext context) {
59         PolicyMap map = context.getPolicyMap();
60         if(map != null)
61             map =  doAlternativeSelection(map);
62         return map;
63     }
64 
65     /**
66      * Checks if the PolicyMap has only single alternative in the scope.
67      *
68      * @param policyMap
69      *      PolicyMap that needs to be validated.
70      */
validateServerPolicyMap(PolicyMap policyMap)71     private void validateServerPolicyMap(PolicyMap policyMap) {
72         try {
73             final ValidationProcessor validationProcessor = ValidationProcessor.getInstance();
74 
75             for (Policy policy : policyMap) {
76 
77                 // TODO:  here is a good place to check if the actual policy has only one alternative...
78 
79                 for (AssertionSet assertionSet : policy) {
80                     for (PolicyAssertion assertion : assertionSet) {
81                         Fitness validationResult = validationProcessor.validateServerSide(assertion);
82                         if (validationResult != Fitness.SUPPORTED) {
83                             throw new PolicyException(PolicyMessages.WSP_1015_SERVER_SIDE_ASSERTION_VALIDATION_FAILED(
84                                     assertion.getName(),
85                                     validationResult));
86                         }
87                     }
88                 }
89             }
90         } catch (PolicyException e) {
91             throw new WebServiceException(e);
92         }
93     }
94 
95     /**
96      * Selects a best alternative if there are multiple policy alternatives.
97      *
98      * @param policyMap
99      * @return
100      */
doAlternativeSelection(PolicyMap policyMap)101     private PolicyMap doAlternativeSelection(PolicyMap policyMap) {
102         final EffectivePolicyModifier modifier = EffectivePolicyModifier.createEffectivePolicyModifier();
103         modifier.connect(policyMap);
104         try {
105             AlternativeSelector.doSelection(modifier);
106         } catch (PolicyException e) {
107             throw new WebServiceException(e);
108         }
109         return policyMap;
110     }
111 }
112