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.tools.internal.ws.wsdl.parser;
  27 
  28 import com.sun.tools.internal.ws.api.wsdl.TWSDLExtensible;
  29 import com.sun.tools.internal.ws.api.wsdl.TWSDLParserContext;
  30 import com.sun.tools.internal.ws.resources.ModelerMessages;
  31 import com.sun.tools.internal.ws.resources.WsdlMessages;
  32 import com.sun.tools.internal.ws.util.xml.XmlUtil;
  33 import com.sun.tools.internal.ws.wscompile.ErrorReceiver;
  34 import com.sun.tools.internal.ws.wsdl.document.Fault;
  35 import com.sun.tools.internal.ws.wsdl.document.Input;
  36 import com.sun.tools.internal.ws.wsdl.document.Output;
  37 import com.sun.xml.internal.ws.addressing.W3CAddressingMetadataConstants;
  38 import com.sun.xml.internal.ws.api.addressing.AddressingVersion;
  39 import org.w3c.dom.Element;
  40 import org.xml.sax.Locator;
  41 
  42 import javax.xml.namespace.QName;
  43 import java.util.Map;
  44 
  45 import static com.sun.xml.internal.ws.addressing.v200408.MemberSubmissionAddressingConstants.WSA_ACTION_QNAME;
  46 
  47 /**
  48  * @author Arun Gupta
  49  */
  50 public class MemberSubmissionAddressingExtensionHandler extends W3CAddressingExtensionHandler {
  51 
  52     private ErrorReceiver errReceiver;
  53     private boolean extensionModeOn;
  54 
  55     public MemberSubmissionAddressingExtensionHandler(Map<String, AbstractExtensionHandler> extensionHandlerMap, ErrorReceiver env, boolean extensionModeOn) {
  56         super(extensionHandlerMap, env);
  57         this.errReceiver = env;
  58         this.extensionModeOn = extensionModeOn;
  59     }
  60 
  61     @Override
  62     public String getNamespaceURI() {
  63         return AddressingVersion.MEMBER.wsdlNsUri;
  64     }
  65 
  66     protected QName getWSDLExtensionQName() {
  67         return AddressingVersion.MEMBER.wsdlExtensionTag;
  68     }
  69 
  70     @Override
  71     public boolean handlePortExtension(TWSDLParserContext context, TWSDLExtensible parent, Element e) {
  72         // ignore any extension elements
  73         return false;
  74     }
  75 
  76     @Override
  77     public boolean handleInputExtension(TWSDLParserContext context, TWSDLExtensible parent, Element e) {
  78         if (extensionModeOn) {
  79             warn(context.getLocation(e));
  80             String actionValue = XmlUtil.getAttributeNSOrNull(e, WSA_ACTION_QNAME);
  81             if (actionValue == null || actionValue.equals("")) {
  82                 return warnEmptyAction(parent, context.getLocation(e));
  83             }
  84             ((Input) parent).setAction(actionValue);
  85             return true;
  86         } else {
  87             return fail(context.getLocation(e));
  88         }
  89     }
  90 
  91     private boolean fail(Locator location) {
  92         errReceiver.warning(location,
  93                 ModelerMessages.WSDLMODELER_INVALID_IGNORING_MEMBER_SUBMISSION_ADDRESSING(
  94                         AddressingVersion.MEMBER.nsUri, W3CAddressingMetadataConstants.WSAM_NAMESPACE_NAME));
  95         return false;
  96     }
  97 
  98     private void warn(Locator location) {
  99         errReceiver.warning(location,
 100                 ModelerMessages.WSDLMODELER_WARNING_MEMBER_SUBMISSION_ADDRESSING_USED(
 101                         AddressingVersion.MEMBER.nsUri, W3CAddressingMetadataConstants.WSAM_NAMESPACE_NAME));
 102     }
 103 
 104     @Override
 105     public boolean handleOutputExtension(TWSDLParserContext context, TWSDLExtensible parent, Element e) {
 106         if (extensionModeOn) {
 107             warn(context.getLocation(e));
 108             String actionValue = XmlUtil.getAttributeNSOrNull(e, WSA_ACTION_QNAME);
 109             if (actionValue == null || actionValue.equals("")) {
 110                 return warnEmptyAction(parent, context.getLocation(e));
 111             }
 112             ((Output) parent).setAction(actionValue);
 113             return true;
 114         } else {
 115             return fail(context.getLocation(e));
 116         }
 117     }
 118 
 119     @Override
 120     public boolean handleFaultExtension(TWSDLParserContext context, TWSDLExtensible parent, Element e) {
 121         if (extensionModeOn) {
 122             warn(context.getLocation(e));
 123             String actionValue = XmlUtil.getAttributeNSOrNull(e, WSA_ACTION_QNAME);
 124             if (actionValue == null || actionValue.equals("")) {
 125                 errReceiver.warning(context.getLocation(e), WsdlMessages.WARNING_FAULT_EMPTY_ACTION(parent.getNameValue(), parent.getWSDLElementName().getLocalPart(), parent.getParent().getNameValue()));
 126                 return false; // keep compiler happy
 127             }
 128             ((Fault) parent).setAction(actionValue);
 129             return true;
 130         } else {
 131             return fail(context.getLocation(e));
 132         }
 133     }
 134 
 135     private boolean warnEmptyAction(TWSDLExtensible parent, Locator pos) {
 136         errReceiver.warning(pos, WsdlMessages.WARNING_INPUT_OUTPUT_EMPTY_ACTION(parent.getWSDLElementName().getLocalPart(), parent.getParent().getNameValue()));
 137         return false;
 138     }
 139 }