1 /*
   2  * Copyright (c) 1997, 2010, 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.addressing.policy;
  27 
  28 import com.sun.xml.internal.ws.api.addressing.AddressingVersion;
  29 import com.sun.xml.internal.ws.policy.PolicyAssertion;
  30 import com.sun.xml.internal.ws.policy.NestedPolicy;
  31 import com.sun.xml.internal.ws.policy.privateutil.PolicyLogger;
  32 import com.sun.xml.internal.ws.policy.spi.PolicyAssertionValidator;
  33 import com.sun.xml.internal.ws.addressing.W3CAddressingMetadataConstants;
  34 
  35 import java.util.ArrayList;
  36 import javax.xml.namespace.QName;
  37 
  38 
  39 /**
  40  * This class validates the Addressing assertions.
  41  * If the assertion is wsam:Addressing, it makes sure that only valid assertions are nested.
  42  *
  43  * @author japod
  44  * @author Rama Pulavarthi
  45  */
  46 public class AddressingPolicyValidator implements PolicyAssertionValidator {
  47 
  48     private static final ArrayList<QName> supportedAssertions = new ArrayList<QName>();
  49 
  50     static {
  51         supportedAssertions.add(new QName(AddressingVersion.MEMBER.policyNsUri, "UsingAddressing"));
  52         supportedAssertions.add(W3CAddressingMetadataConstants.WSAM_ADDRESSING_ASSERTION);
  53         supportedAssertions.add(W3CAddressingMetadataConstants.WSAM_ANONYMOUS_NESTED_ASSERTION);
  54         supportedAssertions.add(W3CAddressingMetadataConstants.WSAM_NONANONYMOUS_NESTED_ASSERTION);
  55     }
  56 
  57     /**
  58      * Creates a new instance of AddressingPolicyValidator
  59      */
  60     public AddressingPolicyValidator() {
  61     }
  62 
  63     public Fitness validateClientSide(PolicyAssertion assertion) {
  64         return supportedAssertions.contains(assertion.getName()) ? Fitness.SUPPORTED : Fitness.UNKNOWN;
  65     }
  66 
  67     public Fitness validateServerSide(PolicyAssertion assertion) {
  68         if (!supportedAssertions.contains(assertion.getName()))
  69             return Fitness.UNKNOWN;
  70 
  71         //Make sure wsam:Addressing contains only one of the allowed nested assertions.
  72         if (assertion.getName().equals(W3CAddressingMetadataConstants.WSAM_ADDRESSING_ASSERTION)) {
  73             NestedPolicy nestedPolicy = assertion.getNestedPolicy();
  74             if (nestedPolicy != null) {
  75                 boolean requiresAnonymousResponses = false;
  76                 boolean requiresNonAnonymousResponses = false;
  77                 for (PolicyAssertion nestedAsser : nestedPolicy.getAssertionSet()) {
  78                     if (nestedAsser.getName().equals(W3CAddressingMetadataConstants.WSAM_ANONYMOUS_NESTED_ASSERTION)) {
  79                         requiresAnonymousResponses = true;
  80                     } else if (nestedAsser.getName().equals(W3CAddressingMetadataConstants.WSAM_NONANONYMOUS_NESTED_ASSERTION)) {
  81                         requiresNonAnonymousResponses = true;
  82                     } else {
  83                         LOGGER.warning("Found unsupported assertion:\n" + nestedAsser + "\nnested into assertion:\n" + assertion);
  84                         return Fitness.UNSUPPORTED;
  85                     }
  86                 }
  87 
  88                 if (requiresAnonymousResponses && requiresNonAnonymousResponses) {
  89                     LOGGER.warning("Only one among AnonymousResponses and NonAnonymousResponses can be nested in an Addressing assertion");
  90                     return Fitness.INVALID;
  91                 }
  92             }
  93         }
  94 
  95         return Fitness.SUPPORTED;
  96     }
  97 
  98     public String[] declareSupportedDomains() {
  99         return new String[]{AddressingVersion.MEMBER.policyNsUri, AddressingVersion.W3C.policyNsUri, W3CAddressingMetadataConstants.WSAM_NAMESPACE_NAME};
 100     }
 101 
 102     private static final PolicyLogger LOGGER = PolicyLogger.getLogger(AddressingPolicyValidator.class);
 103 }