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.encoding.policy;
27
28import com.sun.xml.internal.ws.api.client.SelectOptimalEncodingFeature;
29import com.sun.xml.internal.ws.policy.AssertionSet;
30import com.sun.xml.internal.ws.policy.Policy;
31import com.sun.xml.internal.ws.policy.PolicyAssertion;
32import com.sun.xml.internal.ws.policy.PolicyException;
33import com.sun.xml.internal.ws.policy.PolicyMap;
34import com.sun.xml.internal.ws.policy.PolicyMapKey;
35import com.sun.xml.internal.ws.policy.jaxws.spi.PolicyFeatureConfigurator;
36
37import java.util.Collection;
38import java.util.Iterator;
39import java.util.LinkedList;
40import javax.xml.namespace.QName;
41
42import static com.sun.xml.internal.ws.encoding.policy.EncodingConstants.SELECT_OPTIMAL_ENCODING_ASSERTION;
43import javax.xml.ws.WebServiceFeature;
44
45/**
46 * A configurator provider for FastInfoset policy assertions.
47 *
48 * @author Paul.Sandoz@Sun.Com
49 * @author Fabian Ritzmann
50 */
51public class SelectOptimalEncodingFeatureConfigurator implements PolicyFeatureConfigurator {
52    public static final QName enabled = new QName("enabled");
53
54    /**
55     * Process SelectOptimalEncoding policy assertions.
56     *
57     * @param key Key that identifies the endpoint scope.
58     * @param policyMap The policy map.
59     * @throws PolicyException If retrieving the policy triggered an exception.
60     */
61    public Collection<WebServiceFeature> getFeatures(PolicyMapKey key, PolicyMap policyMap) throws PolicyException {
62        final Collection<WebServiceFeature> features = new LinkedList<WebServiceFeature>();
63        if ((key != null) && (policyMap != null)) {
64            Policy policy = policyMap.getEndpointEffectivePolicy(key);
65            if (null!=policy && policy.contains(SELECT_OPTIMAL_ENCODING_ASSERTION)) {
66                Iterator <AssertionSet> assertions = policy.iterator();
67                while(assertions.hasNext()){
68                    AssertionSet assertionSet = assertions.next();
69                    Iterator<PolicyAssertion> policyAssertion = assertionSet.iterator();
70                    while(policyAssertion.hasNext()){
71                        PolicyAssertion assertion = policyAssertion.next();
72                        if(SELECT_OPTIMAL_ENCODING_ASSERTION.equals(assertion.getName())){
73                            String value = assertion.getAttributeValue(enabled);
74                            boolean isSelectOptimalEncodingEnabled = value == null || Boolean.valueOf(value.trim());
75                            features.add(new SelectOptimalEncodingFeature(isSelectOptimalEncodingEnabled));
76                        }
77                    }
78                }
79            }
80        }
81        return features;
82    }
83}
84