Save This Page
Home » axis2-1.5-src » org.apache » axis2 » description » [javadoc | source]
    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one
    3    * or more contributor license agreements. See the NOTICE file
    4    * distributed with this work for additional information
    5    * regarding copyright ownership. The ASF licenses this file
    6    * to you under the Apache License, Version 2.0 (the
    7    * "License"); you may not use this file except in compliance
    8    * with the License. You may obtain a copy of the License at
    9    *
   10    * http://www.apache.org/licenses/LICENSE-2.0
   11    *
   12    * Unless required by applicable law or agreed to in writing,
   13    * software distributed under the License is distributed on an
   14    * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
   15    * KIND, either express or implied. See the License for the
   16    * specific language governing permissions and limitations
   17    * under the License.
   18    */
   19   
   20   package org.apache.axis2.description;
   21   
   22   import org.apache.axiom.om;
   23   import org.apache.axiom.soap.SOAP11Constants;
   24   import org.apache.axiom.soap.SOAP12Constants;
   25   import org.apache.axis2.AxisFault;
   26   import org.apache.axis2.addressing.AddressingConstants;
   27   import org.apache.axis2.addressing.AddressingHelper;
   28   import org.apache.axis2.description.java2wsdl.Java2WSDLConstants;
   29   import org.apache.axis2.engine.AxisConfiguration;
   30   import org.apache.axis2.namespace.Constants;
   31   import org.apache.axis2.util;
   32   import org.apache.axis2.util.XMLUtils;
   33   import org.apache.axis2.wsdl.SOAPHeaderMessage;
   34   import org.apache.axis2.wsdl.WSDLConstants;
   35   import org.apache.neethi.Policy;
   36   import org.apache.neethi.PolicyComponent;
   37   import org.apache.neethi.PolicyReference;
   38   import org.apache.neethi.PolicyRegistry;
   39   import org.apache.ws.commons.schema.XmlSchema;
   40   
   41   import javax.xml.namespace.QName;
   42   import java.io.StringReader;
   43   import java.io.StringWriter;
   44   import java.util;
   45   
   46   public class AxisService2WSDL11 implements Java2WSDLConstants {
   47   
   48   	private AxisService axisService;
   49   
   50   	private String serviceName;
   51   
   52       private String targetNamespace;
   53   
   54   	private OMElement definition;
   55   
   56   	private OMNamespace soap;
   57   
   58   	private OMNamespace soap12;
   59   
   60   	private OMNamespace http;
   61   
   62   	private OMNamespace mime;
   63   
   64   	private OMNamespace tns;
   65   
   66   	private OMNamespace wsdl;
   67   
   68   	private OMNamespace wsaw;
   69   
   70   	private String style = DOCUMENT;
   71   
   72   	private String use = LITERAL;
   73   
   74   	private HashMap policiesInDefinitions;
   75   
   76   	private ExternalPolicySerializer serializer;
   77   
   78   	private HashMap messagesMap;
   79   
   80       public AxisService2WSDL11(AxisService service) throws Exception {
   81           this.axisService = service;
   82           this.serviceName = service.getName();
   83           init();
   84   	}
   85   
   86   	private void init() throws AxisFault {
   87   /*
   88   		// the EPR list of AxisService contains REST EPRs as well. Those REST
   89   		// EPRs will be used to generated HTTPBinding
   90   		// and rest of the EPRs will be used to generate SOAP 1.1 and 1.2
   91   		// bindings. Let's first initialize those set of
   92   		// EPRs now to be used later, especially when we generate the WSDL.
   93           String[] serviceEndpointURLs = axisService.getEPRs();
   94   		if (serviceEndpointURLs == null) {
   95   			Map endpointMap = axisService.getEndpoints();
   96   			if (endpointMap.size() > 0) {
   97   				Iterator endpointItr = endpointMap.values().iterator();
   98   				if (endpointItr.hasNext()) {
   99   					AxisEndpoint endpoint = (AxisEndpoint) endpointItr.next();
  100   					serviceEndpointURLs = new String[] { endpoint
  101   							.getEndpointURL() };
  102   				}
  103   
  104   			} else {
  105   				serviceEndpointURLs = new String[] { axisService
  106   						.getEndpointName() };
  107   			}
  108   		}
  109   */
  110   		this.targetNamespace = axisService.getTargetNamespace();
  111   
  112   		serializer = new ExternalPolicySerializer();
  113   		// CHECKME check whether service.getAxisConfiguration() return null ???
  114   
  115   		AxisConfiguration configuration = axisService.getAxisConfiguration();
  116   		if (configuration != null) {
  117   			serializer.setAssertionsToFilter(configuration
  118   					.getLocalPolicyAssertions());
  119   		}
  120   	}
  121   
  122   	public AxisService2WSDL11(AxisService service, String serviceName)
  123   			throws Exception {
  124   		this.axisService = service;
  125   		this.serviceName = serviceName;
  126   		init();
  127   	}
  128   
  129       /**
  130        * Build the OM structure of the WSDL document
  131        *
  132        * @return an OMElement containing a WSDL document
  133        * @throws Exception
  134        */
  135       public OMElement generateOM() throws Exception {
  136   
  137   		OMFactory fac = OMAbstractFactory.getOMFactory();
  138   		wsdl = fac.createOMNamespace(WSDL_NAMESPACE, DEFAULT_WSDL_NAMESPACE_PREFIX);
  139   		OMElement ele = fac.createOMElement("definitions", wsdl);
  140   		setDefinitionElement(ele);
  141   
  142   		policiesInDefinitions = new HashMap();
  143   
  144   		Map namespaceMap = axisService.getNamespaceMap();
  145   		if (namespaceMap == null)
  146   			namespaceMap = new HashMap();
  147   
  148   		WSDLSerializationUtil.populateNamespaces(ele, namespaceMap);
  149   		soap = ele.declareNamespace(URI_WSDL11_SOAP, SOAP11_PREFIX);
  150   		soap12 = ele.declareNamespace(URI_WSDL12_SOAP, SOAP12_PREFIX);
  151   		http = ele.declareNamespace(HTTP_NAMESPACE, HTTP_PREFIX);
  152   		mime = ele.declareNamespace(MIME_NAMESPACE, MIME_PREFIX);
  153   		wsaw = ele.declareNamespace(AddressingConstants.Final.WSAW_NAMESPACE, "wsaw");
  154   		String prefix = WSDLSerializationUtil.getPrefix(axisService.getTargetNamespace(),
  155                                                           namespaceMap);
  156   		if (prefix == null || "".equals(prefix)) {
  157   			prefix = DEFAULT_TARGET_NAMESPACE_PREFIX;
  158   		}
  159   
  160   		namespaceMap.put(prefix, axisService.getTargetNamespace());
  161   		tns = ele.declareNamespace(axisService.getTargetNamespace(), prefix);
  162   
  163   		// axis2.xml indicated no HTTP binding?
  164   		boolean disableREST = false;
  165   		Parameter disableRESTParameter = axisService.getParameter(
  166                   org.apache.axis2.Constants.Configuration.DISABLE_REST);
  167   		if (disableRESTParameter != null
  168   				&& JavaUtils.isTrueExplicitly(disableRESTParameter.getValue())) {
  169   			disableREST = true;
  170   		}
  171   
  172           boolean disableSOAP11 = false;
  173   		Parameter disableSOAP11Parameter = axisService
  174   				.getParameter(org.apache.axis2.Constants.Configuration.DISABLE_SOAP11);
  175   		if (disableSOAP11Parameter != null
  176   				&& JavaUtils.isTrueExplicitly(disableSOAP11Parameter.getValue())) {
  177   			disableSOAP11 = true;
  178   		}
  179   
  180           // axis2.xml indicated no SOAP 1.2 binding?
  181   		boolean disableSOAP12 = false;
  182   		Parameter disableSOAP12Parameter = axisService
  183   				.getParameter(org.apache.axis2.Constants.Configuration.DISABLE_SOAP12);
  184   		if (disableSOAP12Parameter != null
  185   				&& JavaUtils.isTrueExplicitly(disableSOAP12Parameter.getValue())) {
  186   			disableSOAP12 = true;
  187   		}
  188   
  189   		// adding documentation element
  190   		// <documentation>&lt;b&gt;NEW!&lt;/b&gt; This method accepts an ISBN
  191   		// string and returns &lt;b&gt;Amazon.co.uk&lt;/b&gt; Sales Rank for
  192   		// that book.</documentation>
  193   		WSDLSerializationUtil.addWSDLDocumentationElement(axisService, ele,	fac, wsdl);
  194   
  195   		ele.addAttribute("targetNamespace", axisService.getTargetNamespace(), null);
  196   		OMElement wsdlTypes = fac.createOMElement("types", wsdl);
  197   		ele.addChild(wsdlTypes);
  198   
  199   		// populate the schema mappings
  200   		axisService.populateSchemaMappings();
  201   
  202   		ArrayList schemas = axisService.getSchema();
  203   		for (int i = 0; i < schemas.size(); i++) {
  204   			StringWriter writer = new StringWriter();
  205   
  206   			// XmlSchema schema = (XmlSchema) schemas.get(i);
  207   			XmlSchema schema = axisService.getSchema(i);
  208   
  209   			String targetNamespace = schema.getTargetNamespace();
  210   			if (!Constants.NS_URI_XML.equals(targetNamespace)) {
  211   				schema.write(writer);
  212   				String schemaString = writer.toString();
  213   				if (!"".equals(schemaString)) {
  214   					wsdlTypes.addChild(XMLUtils.toOM(new StringReader(schemaString)));
  215   				}
  216   			}
  217   		}
  218   		generateMessages(fac, ele);
  219   		generatePortType(fac, ele);
  220   
  221   		// generateSOAP11Binding(fac, ele);
  222   		// if (!disableSOAP12) {
  223   		// generateSOAP12Binding(fac, ele);
  224   		// }
  225   		// if (!disableREST) {
  226   		// generateHTTPBinding(fac, ele);
  227   		// }
  228   
  229   		generateService(fac, ele, disableREST, disableSOAP12 , disableSOAP11);
  230   		addPoliciesToDefinitionElement(policiesInDefinitions.values().iterator(), definition);
  231   
  232   		return ele;
  233   	}
  234   
  235   	private void generateMessages(OMFactory fac, OMElement defintions) {
  236   		HashSet faultMessageNames = new HashSet();
  237   		messagesMap = new HashMap();
  238   
  239   		Iterator operations = axisService.getOperations();
  240   		while (operations.hasNext()) {
  241   			AxisOperation axisOperation = (AxisOperation) operations.next();
  242   			if (axisOperation.isControlOperation()) {
  243   				continue;
  244   			}
  245   			String MEP = axisOperation.getMessageExchangePattern();
  246   			if (WSDL2Constants.MEP_URI_IN_ONLY.equals(MEP)
  247   					|| WSDL2Constants.MEP_URI_IN_OPTIONAL_OUT.equals(MEP)
  248   					|| WSDL2Constants.MEP_URI_OUT_OPTIONAL_IN.equals(MEP)
  249   					|| WSDL2Constants.MEP_URI_ROBUST_OUT_ONLY.equals(MEP)
  250   					|| WSDL2Constants.MEP_URI_ROBUST_IN_ONLY.equals(MEP)
  251   					|| WSDL2Constants.MEP_URI_IN_OUT.equals(MEP)) {
  252                   AxisMessage inaxisMessage =
  253                           axisOperation.getMessage(WSDLConstants.MESSAGE_LABEL_IN_VALUE);
  254   				if (inaxisMessage != null) {
  255   					writeMessage(inaxisMessage, fac, defintions);
  256   					generateHeaderMessages(inaxisMessage, fac, defintions);
  257   				}
  258   			}
  259   
  260   			if (WSDL2Constants.MEP_URI_OUT_ONLY.equals(MEP)
  261   					|| WSDL2Constants.MEP_URI_OUT_OPTIONAL_IN.equals(MEP)
  262   					|| WSDL2Constants.MEP_URI_IN_OPTIONAL_OUT.equals(MEP)
  263   					|| WSDL2Constants.MEP_URI_ROBUST_OUT_ONLY.equals(MEP)
  264   					|| WSDL2Constants.MEP_URI_IN_OUT.equals(MEP)) {
  265   				AxisMessage outAxisMessage = axisOperation
  266   						.getMessage(WSDLConstants.MESSAGE_LABEL_OUT_VALUE);
  267   				if (outAxisMessage != null) {
  268   					writeMessage(outAxisMessage, fac, defintions);
  269   					generateHeaderMessages(outAxisMessage, fac, defintions);
  270   				}
  271   			}
  272   
  273   			// generate fault Messages
  274   			ArrayList faultyMessages = axisOperation.getFaultMessages();
  275   			if (faultyMessages != null) {
  276                   for (Object faultyMessage : faultyMessages) {
  277                       AxisMessage axisMessage = (AxisMessage)faultyMessage;
  278                       String name = axisMessage.getName();
  279                       if (faultMessageNames.add(name)) {
  280                           writeMessage(axisMessage, fac, defintions);
  281                           generateHeaderMessages(axisMessage, fac, defintions);
  282                       }
  283                   }
  284   			}
  285   		}
  286   	}
  287   
  288   	private void generateHeaderMessages(AxisMessage axismessage, OMFactory fac,
  289   			OMElement defintions) {
  290   		ArrayList extList = axismessage.getSoapHeaders();
  291           for (Object anExtList : extList) {
  292               SOAPHeaderMessage header = (SOAPHeaderMessage)anExtList;
  293               OMElement messageElement = fac.createOMElement(MESSAGE_LOCAL_NAME, wsdl);
  294               messageElement.addAttribute(ATTRIBUTE_NAME, header.getMessage().getLocalPart(), null);
  295               defintions.addChild(messageElement);
  296               OMElement messagePart = fac.createOMElement(PART_ATTRIBUTE_NAME, wsdl);
  297               messageElement.addChild(messagePart);
  298               messagePart.addAttribute(ATTRIBUTE_NAME, header.part(), null);
  299               if (header.getElement() == null) {
  300                   throw new RuntimeException(ELEMENT_ATTRIBUTE_NAME
  301                                              + " is null for " + header.getMessage());
  302               }
  303               messagePart.addAttribute(ELEMENT_ATTRIBUTE_NAME,
  304                                        WSDLSerializationUtil.getPrefix(header.getElement()
  305                                                .getNamespaceURI(), axisService.getNamespaceMap())
  306                                        + ":" + header.getElement().getLocalPart(), null);
  307           }
  308   	}
  309   
  310   	private void writeMessage(AxisMessage axismessage, OMFactory fac, OMElement defintions) {
  311   		if (messagesMap.get(axismessage.getName()) == null) {
  312   			messagesMap.put(axismessage.getName(), axismessage);
  313   			QName schemaElementName = axismessage.getElementQName();
  314   			OMElement messageElement = fac.createOMElement(MESSAGE_LOCAL_NAME, wsdl);
  315   			messageElement.addAttribute(ATTRIBUTE_NAME, axismessage.getName(), null);
  316   			defintions.addChild(messageElement);
  317   			if (schemaElementName != null) {
  318   				OMElement messagePart = fac.createOMElement(PART_ATTRIBUTE_NAME, wsdl);
  319   				messageElement.addChild(messagePart);
  320   				if (axismessage.getMessagePartName() != null) {
  321   					messagePart.addAttribute(ATTRIBUTE_NAME,
  322                                                axismessage.getMessagePartName(),
  323                                                null);
  324   				} else {
  325   					messagePart.addAttribute(ATTRIBUTE_NAME, axismessage.getPartName(), null);
  326   				}
  327   				messagePart.addAttribute(ELEMENT_ATTRIBUTE_NAME,
  328   						WSDLSerializationUtil.getPrefix(schemaElementName.getNamespaceURI(),
  329                                                           axisService.getNamespaceMap())
  330   								+ ":" + schemaElementName.getLocalPart(), null);
  331   			}
  332   		}
  333   
  334   	}
  335   
  336   	/**
  337   	 * Builds the &lt;portType&gt; element in the passed WSDL definition. When
  338   	 * this returns successfully, there will be a new child element under
  339   	 * definitons for the portType.
  340   	 * 
  341   	 * @param fac
  342   	 *            the active OMFactory
  343   	 * @param defintions
  344   	 *            the WSDL &lt;definitions&gt; element
  345   	 * @throws Exception
  346   	 *             if there's a problem
  347   	 */
  348   	private void generatePortType(OMFactory fac, OMElement defintions)
  349   			throws Exception {
  350   		OMElement portType = fac.createOMElement(PORT_TYPE_LOCAL_NAME, wsdl);
  351   		defintions.addChild(portType);
  352   
  353   		portType.addAttribute(ATTRIBUTE_NAME, serviceName + PORT_TYPE_SUFFIX,
  354   				null);
  355   
  356   		addPolicyAsExtAttribute(axisService, portType, fac);
  357   
  358   		for (Iterator operations = axisService.getOperations(); operations
  359   				.hasNext();) {
  360   			AxisOperation axisOperation = (AxisOperation) operations.next();
  361   			if (axisOperation.isControlOperation()
  362   					|| axisOperation.getName() == null) {
  363   				continue;
  364   			}
  365   			String operationName = axisOperation.getName().getLocalPart();
  366   			OMElement operation = fac.createOMElement(OPERATION_LOCAL_NAME,
  367   					wsdl);
  368   			WSDLSerializationUtil.addWSDLDocumentationElement(axisOperation,
  369   					operation, fac, wsdl);
  370   			portType.addChild(operation);
  371   			operation.addAttribute(ATTRIBUTE_NAME, operationName, null);
  372   			addPolicyAsExtAttribute(axisOperation, operation,fac);
  373   
  374   			String MEP = axisOperation.getMessageExchangePattern();
  375   			if (WSDL2Constants.MEP_URI_IN_ONLY.equals(MEP)
  376   					|| WSDL2Constants.MEP_URI_IN_OPTIONAL_OUT.equals(MEP)
  377   					|| WSDL2Constants.MEP_URI_OUT_OPTIONAL_IN.equals(MEP)
  378   					|| WSDL2Constants.MEP_URI_ROBUST_OUT_ONLY.equals(MEP)
  379   					|| WSDL2Constants.MEP_URI_ROBUST_IN_ONLY.equals(MEP)
  380   					|| WSDL2Constants.MEP_URI_IN_OUT.equals(MEP)) {
  381   				AxisMessage inaxisMessage = axisOperation
  382   						.getMessage(WSDLConstants.MESSAGE_LABEL_IN_VALUE);
  383   				if (inaxisMessage != null) {
  384   					OMElement input = fac.createOMElement(IN_PUT_LOCAL_NAME,
  385   							wsdl);
  386   					WSDLSerializationUtil.addWSDLDocumentationElement(
  387   							inaxisMessage, input, fac, wsdl);
  388   					input.addAttribute(MESSAGE_LOCAL_NAME, tns.getPrefix()
  389   							+ ":" + inaxisMessage.getName(), null);
  390   					addPolicyAsExtAttribute(inaxisMessage, input,fac);
  391   
  392   					WSDLSerializationUtil.addWSAWActionAttribute(input,
  393   							axisOperation.getInputAction(), wsaw);
  394   					operation.addChild(input);
  395   				}
  396   			}
  397   
  398   			if (WSDL2Constants.MEP_URI_OUT_ONLY.equals(MEP)
  399   					|| WSDL2Constants.MEP_URI_OUT_OPTIONAL_IN.equals(MEP)
  400   					|| WSDL2Constants.MEP_URI_IN_OPTIONAL_OUT.equals(MEP)
  401   					|| WSDL2Constants.MEP_URI_ROBUST_OUT_ONLY.equals(MEP)
  402   					|| WSDL2Constants.MEP_URI_IN_OUT.equals(MEP)) {
  403   				AxisMessage outAxisMessage = axisOperation
  404   						.getMessage(WSDLConstants.MESSAGE_LABEL_OUT_VALUE);
  405   				if (outAxisMessage != null) {
  406   					OMElement output = fac.createOMElement(OUT_PUT_LOCAL_NAME,
  407   							wsdl);
  408   					WSDLSerializationUtil.addWSDLDocumentationElement(
  409   							outAxisMessage, output, fac, wsdl);
  410   					output.addAttribute(MESSAGE_LOCAL_NAME, tns.getPrefix()
  411   							+ ":" + outAxisMessage.getName(), null);
  412   					addPolicyAsExtAttribute(outAxisMessage, output, fac);
  413   					WSDLSerializationUtil.addWSAWActionAttribute(output,
  414   							axisOperation.getOutputAction(), wsaw);
  415   					operation.addChild(output);
  416   				}
  417   			}
  418   
  419   			// generate fault Messages
  420   			ArrayList faultMessages = axisOperation.getFaultMessages();
  421   			if (faultMessages != null) {
  422                   for (Object faultMessage : faultMessages) {
  423                       AxisMessage faultyMessage = (AxisMessage)faultMessage;
  424                       OMElement fault = fac.createOMElement(FAULT_LOCAL_NAME, wsdl);
  425                       WSDLSerializationUtil.addWSDLDocumentationElement(faultyMessage,
  426                                                                         fault,
  427                                                                         fac,
  428                                                                         wsdl);
  429                       fault.addAttribute(MESSAGE_LOCAL_NAME, tns.getPrefix()
  430                                                              + ":" + faultyMessage.getName(), null);
  431                       fault.addAttribute(ATTRIBUTE_NAME, faultyMessage.getName(), null);
  432                       WSDLSerializationUtil.addWSAWActionAttribute(fault,
  433                                                                    axisOperation.getFaultAction(
  434                                                                            faultyMessage.getName()),
  435                                                                    wsaw);
  436                       // TODO add policies for fault messages
  437                       operation.addChild(fault);
  438                   }
  439   			}
  440   
  441   		}
  442   	}
  443   
  444   	/**
  445   	 * Generate the WSDL &lt;service&gt; element
  446   	 * 
  447   	 * @param fac
  448   	 *            the active OMFactory
  449   	 * @param defintions
  450   	 *            the WSDL &lt;definitions&gt; element under which to put the
  451   	 *            service
  452   	 * @param disableREST
  453   	 *            if false, generate REST binding, if true, don't
  454   	 * @param disableSOAP12
  455   	 *            if false, generate SOAP 1.2 binding, if true, don't
  456   	 * @throws Exception
  457   	 *             if there's a problem
  458   	 */
  459   	public void generateService(OMFactory fac, OMElement defintions, boolean disableREST,
  460                                   boolean disableSOAP12, boolean disableSOAP11)
  461               throws Exception {
  462   		OMElement service = fac.createOMElement(SERVICE_LOCAL_NAME, wsdl);
  463   		defintions.addChild(service);
  464   		service.addAttribute(ATTRIBUTE_NAME, serviceName, null);
  465   
  466           if (!disableSOAP11) {
  467   			generateSoap11Port(fac, defintions, service);
  468   		}
  469   
  470           if (!disableSOAP12) {
  471   			// generateSOAP12Ports(fac, service);
  472   			generateSoap12Port(fac, defintions, service);
  473   		}
  474   
  475   
  476           addPolicyAsExtElement(PolicyInclude.SERVICE_POLICY, axisService.getPolicyInclude(),
  477                                 service);
  478   		// addPolicyAsExtElement(PolicyInclude.AXIS_SERVICE_POLICY, axisService.
  479   		// getPolicyInclude(), service);
  480   
  481   		if (!disableREST) {
  482   			// generateHTTPPorts(fac, service);
  483   			generateHttpPort(fac, definition, service);
  484   		}
  485   	}
  486   
  487   	private void writeSoapHeaders(AxisMessage inaxisMessage, OMFactory fac,
  488   			OMElement input, OMNamespace soapNameSpace) throws Exception {
  489   		ArrayList extElementList;
  490   		extElementList = inaxisMessage.getSoapHeaders();
  491   		if (extElementList != null) {
  492   			Iterator elements = extElementList.iterator();
  493   			while (elements.hasNext()) {
  494   				SOAPHeaderMessage soapheader = (SOAPHeaderMessage) elements
  495   						.next();
  496   				addSOAPHeader(fac, input, soapheader, soapNameSpace);
  497   			}
  498   		}
  499   	}
  500   
  501   	private void addExtensionElement(OMFactory fac, OMElement element,
  502   			String name, String att1Name, String att1Value, String att2Name,
  503   			String att2Value, OMNamespace soapNameSpace) {
  504   		OMElement soapbinding = fac.createOMElement(name, soapNameSpace);
  505   		element.addChild(soapbinding);
  506   		soapbinding.addAttribute(att1Name, att1Value, null);
  507   		if (att2Name != null) {
  508   			soapbinding.addAttribute(att2Name, att2Value, null);
  509   		}
  510   	}
  511   
  512   	private void setDefinitionElement(OMElement defintion) {
  513   		this.definition = defintion;
  514   	}
  515   
  516   	private void addSOAPHeader(OMFactory fac, OMElement element,
  517   			SOAPHeaderMessage header, OMNamespace soapNameSpace) {
  518   		OMElement extElement = fac.createOMElement("header", soapNameSpace);
  519   		element.addChild(extElement);
  520   		String use = header.getUse();
  521   		if (use != null) {
  522   			extElement.addAttribute("use", use, null);
  523   		}
  524   		if (header.part() != null) {
  525   			extElement.addAttribute("part", header.part(), null);
  526   		}
  527   		if (header.getMessage() != null) {
  528   			extElement.addAttribute("message", WSDLSerializationUtil.getPrefix(
  529   					targetNamespace, axisService.getNamespaceMap())
  530   					+ ":" + header.getMessage().getLocalPart(), null);
  531   		}
  532   	}
  533   
  534   	private void addPolicyAsExtElement(int type, PolicyInclude policyInclude,
  535   			OMElement parentElement) throws Exception {
  536   		ArrayList elementList = policyInclude.getPolicyElements(type);
  537   
  538           for (Object policyElement : elementList) {
  539               if (policyElement instanceof Policy) {
  540                   OMElement child = PolicyUtil.getPolicyComponentAsOMElement(
  541                           (PolicyComponent)policyElement, serializer);
  542   
  543   //                OMNode firstChildElem = parentElement.getFirstElement();
  544   
  545                   // if (firstChildElem == null) {
  546                   // parentElement.addChild(child);
  547                   // } else {
  548                   // firstChildElem.insertSiblingBefore(child);
  549                   // }
  550                   // there is a problem with the OM insertSiblingBefore element
  551                   // with
  552                   // drops the already exists elements.
  553                   // since there is no any techical problem of adding policy
  554                   // elements after other
  555                   // children temporaliy fix this as it is.
  556                   // one OM fix this issue we can revert this change.
  557                   parentElement.addChild(child);
  558   
  559               } else if (policyElement instanceof PolicyReference) {
  560                   OMElement child = PolicyUtil
  561                           .getPolicyComponentAsOMElement((PolicyComponent)policyElement);
  562                   OMElement firstChildElem = parentElement.getFirstElement();
  563   
  564                   if (firstChildElem == null) {
  565                       parentElement.addChild(child);
  566                   } else {
  567                       firstChildElem.insertSiblingBefore(child);
  568                   }
  569   
  570                   PolicyRegistry reg = policyInclude.getPolicyRegistry();
  571                   String key = ((PolicyReference)policyElement).getURI();
  572   
  573                   if (key.startsWith("#")) {
  574                       key = key.substring(key.indexOf("#") + 1);
  575                   }
  576   
  577                   Policy p = reg.lookup(key);
  578   
  579                   if (p == null) {
  580                       throw new Exception("Policy not found for uri : " + key);
  581                   }
  582   
  583                   addPolicyToDefinitionElement(key, p);
  584               }
  585           }
  586   	}
  587   
  588   	private void addPoliciesToDefinitionElement(Iterator iterator,
  589   			OMElement definitionElement) throws Exception {
  590   		Policy policy;
  591   		OMElement policyElement;
  592   		OMNode firstChild;
  593   
  594   		for (; iterator.hasNext();) {
  595   			policy = (Policy) iterator.next();
  596   			policyElement = PolicyUtil.getPolicyComponentAsOMElement(policy,
  597   					serializer);
  598   			firstChild = definition.getFirstOMChild();
  599   			if (firstChild != null) {
  600   				firstChild.insertSiblingBefore(policyElement);
  601   			} else {
  602   				definitionElement.addChild(policyElement);
  603   			}
  604   		}
  605   	}
  606   
  607   	private void addPolicyToDefinitionElement(String key, Policy policy) {
  608   		policiesInDefinitions.put(key, policy);
  609   	}
  610   
  611   	public String getStyle() {
  612   		return style;
  613   	}
  614   
  615   	public void setStyle(String style) {
  616   		this.style = style;
  617   	}
  618   
  619   	public String getUse() {
  620   		return use;
  621   	}
  622   
  623   	public void setUse(String use) {
  624   		this.use = use;
  625   	}
  626   
  627   	private void generateSoap11Port(OMFactory fac, OMElement definition,
  628   			OMElement service) throws Exception {
  629   		Iterator iterator = axisService.getEndpoints().values().iterator();
  630   		AxisEndpoint axisEndpoint;
  631   		AxisBinding axisBinding;
  632   		for (; iterator.hasNext();) {
  633   			axisEndpoint = (AxisEndpoint) iterator.next();
  634   			/*
  635   			 * Some transports might not be active at runtime.
  636   			 */
  637   			if (!axisEndpoint.isActive()) {
  638   				continue;
  639   			}
  640   			axisBinding = axisEndpoint.getBinding();
  641   			String type = axisBinding.getType();
  642   			if (Java2WSDLConstants.TRANSPORT_URI.equals(type)
  643   					|| WSDL2Constants.URI_WSDL2_SOAP.equals(type)) {
  644   				String version = (String) axisBinding
  645   						.getProperty(WSDL2Constants.ATTR_WSOAP_VERSION);
  646   				if (SOAP11Constants.SOAP_ENVELOPE_NAMESPACE_URI.equals(version)) {
  647   					OMElement port = fac.createOMElement(PORT, wsdl);
  648   					service.addChild(port);
  649   					port.addAttribute(ATTRIBUTE_NAME, axisEndpoint.getName(),
  650   							null);
  651   					QName qname = axisBinding.getName();
  652   					port.addAttribute(BINDING_LOCAL_NAME, tns.getPrefix() + ":"
  653   							+ qname.getLocalPart(), null);
  654   					String endpointURL = getEndpointURL(axisEndpoint);
  655   					WSDLSerializationUtil.addExtensionElement(fac, port,
  656   							SOAP_ADDRESS, LOCATION, (endpointURL == null) ? ""
  657   									: endpointURL, soap);
  658   					generateEPRElement(fac, port, endpointURL);
  659   					addPolicyAsExtElement(axisEndpoint, port);
  660   					generateSoap11Binding(fac, definition, axisEndpoint
  661   							.getBinding());
  662   				}
  663   			}
  664   		}
  665   	}
  666   
  667   	private void generateSoap12Port(OMFactory fac, OMElement definition,
  668   			OMElement service) throws Exception {
  669   
  670   		// /////////////////// FIXME //////////////////////////////////////////
  671   		Iterator iterator = axisService.getEndpoints().values().iterator();
  672   		AxisEndpoint axisEndpoint;
  673   		AxisBinding axisBinding;
  674   		for (; iterator.hasNext();) {
  675   			axisEndpoint = (AxisEndpoint) iterator.next();
  676   			/*
  677   			 * 
  678   			 */
  679   			if (!axisEndpoint.isActive()) {
  680   				continue;
  681   			}
  682   			axisBinding = axisEndpoint.getBinding();
  683   			String type = axisBinding.getType();
  684   			if (Java2WSDLConstants.TRANSPORT_URI.equals(type)
  685   					|| WSDL2Constants.URI_WSDL2_SOAP.equals(type)) {
  686   				String version = (String) axisBinding
  687   						.getProperty(WSDL2Constants.ATTR_WSOAP_VERSION);
  688   				if (SOAP12Constants.SOAP_ENVELOPE_NAMESPACE_URI.equals(version)) {
  689   
  690   					OMElement port = fac.createOMElement(PORT, wsdl);
  691   					service.addChild(port);
  692   					port.addAttribute(ATTRIBUTE_NAME, axisEndpoint.getName(),
  693   							null);
  694   					QName qname = axisBinding.getName();
  695   					port.addAttribute(BINDING_LOCAL_NAME, tns.getPrefix() + ":"
  696   							+ qname.getLocalPart(), null);
  697   					String endpointURL = getEndpointURL(axisEndpoint);
  698   					WSDLSerializationUtil.addExtensionElement(fac, port,
  699   							SOAP_ADDRESS, LOCATION, (endpointURL == null) ? ""
  700   									: endpointURL, soap12);
  701   					generateEPRElement(fac, port, endpointURL);
  702   					addPolicyAsExtElement(axisEndpoint, port);
  703   					generateSoap12Binding(fac, definition, axisEndpoint
  704   							.getBinding());
  705   				}
  706   			}
  707   		}
  708   	}
  709   
  710   	private void generateHttpPort(OMFactory fac, OMElement definition,
  711   			OMElement service) throws Exception {
  712   
  713   		// /////////////////// FIXME //////////////////////////////////////////
  714   		Iterator iterator = axisService.getEndpoints().values().iterator();
  715   		AxisEndpoint axisEndpoint;
  716   		AxisBinding axisBinding;
  717   		for (; iterator.hasNext();) {
  718   			axisEndpoint = (AxisEndpoint) iterator.next();
  719   			/*
  720   			 * 
  721   			 */
  722   			if (!axisEndpoint.isActive()) {
  723   				continue;
  724   			}
  725   			axisBinding = axisEndpoint.getBinding();
  726   			String type = axisBinding.getType();
  727   			if (WSDL2Constants.URI_WSDL2_HTTP.equals(type)) {
  728   				OMElement port = fac.createOMElement(PORT, wsdl);
  729   				service.addChild(port);
  730   				port.addAttribute(ATTRIBUTE_NAME, axisEndpoint.getName(), null);
  731   				QName qname = axisBinding.getName();
  732   				port.addAttribute(BINDING_LOCAL_NAME, tns.getPrefix() + ":"
  733   						+ qname.getLocalPart(), null);
  734   				OMElement extElement = fac.createOMElement("address", http);
  735   				String endpointURL = getEndpointURL(axisEndpoint);
  736   				extElement.addAttribute("location", (endpointURL == null) ? ""
  737   						: endpointURL, null);
  738   				port.addChild(extElement);
  739   
  740   				addPolicyAsExtElement(axisEndpoint, port);
  741   				generateHttpBinding(fac, definition, axisEndpoint.getBinding());
  742   			}
  743   		}
  744   	}
  745   
  746   	private void generateSoap11Binding(OMFactory fac, OMElement defintions,
  747   			AxisBinding axisBinding) throws Exception {
  748   		if (isAlreadyAdded(axisBinding, defintions)) {
  749   			return;
  750   		}
  751   		OMElement binding = fac.createOMElement(BINDING_LOCAL_NAME, wsdl);
  752   		OMElement serviceElement = defintions.getFirstChildWithName(new QName(
  753   				wsdl.getNamespaceURI(), SERVICE_LOCAL_NAME));
  754   		serviceElement.insertSiblingBefore(binding);
  755   
  756   		QName qname = axisBinding.getName();
  757   		binding.addAttribute(ATTRIBUTE_NAME, qname.getLocalPart(), null);
  758   		binding.addAttribute("type", tns.getPrefix() + ":" + serviceName
  759   				+ PORT_TYPE_SUFFIX, null);
  760   
  761   		// Adding ext elements
  762   		addPolicyAsExtElement(axisBinding, binding);
  763   		addExtensionElement(fac, binding, BINDING_LOCAL_NAME, TRANSPORT,
  764   				TRANSPORT_URI, STYLE, style, soap);
  765   
  766   		// /////////////////////////////////////////////////////////////////////
  767   		// Add WS-Addressing UsingAddressing element if appropriate
  768   		// SHOULD be on the binding element per the specification
  769   		if (AddressingHelper
  770   				.getAddressingRequirementParemeterValue(axisService).equals(
  771   						AddressingConstants.ADDRESSING_OPTIONAL)) {
  772   			WSDLSerializationUtil.addExtensionElement(fac, binding,
  773   					AddressingConstants.USING_ADDRESSING,
  774   					DEFAULT_WSDL_NAMESPACE_PREFIX + ":required", "true", wsaw);
  775   		} else if (AddressingHelper.getAddressingRequirementParemeterValue(
  776   				axisService).equals(AddressingConstants.ADDRESSING_REQUIRED)) {
  777   			WSDLSerializationUtil.addExtensionElement(fac, binding,
  778   					AddressingConstants.USING_ADDRESSING,
  779   					DEFAULT_WSDL_NAMESPACE_PREFIX + ":required", "true", wsaw);
  780   		}
  781   		// //////////////////////////////////////////////////////////////////////
  782   
  783   		for (Iterator axisBindingOperations = axisBinding.getChildren(); axisBindingOperations
  784   				.hasNext();) {
  785   			AxisBindingOperation axisBindingOperation = (AxisBindingOperation) axisBindingOperations
  786   					.next();
  787   			AxisOperation axisOperation = axisBindingOperation
  788   					.getAxisOperation();
  789   			if (axisOperation.isControlOperation()
  790   					|| axisOperation.getName() == null) {
  791   				continue;
  792   			}
  793   			String opeartionName = axisOperation.getName().getLocalPart();
  794   			OMElement operation = fac.createOMElement(OPERATION_LOCAL_NAME,
  795   					wsdl);
  796   			binding.addChild(operation);
  797   			String soapAction = axisOperation.getSoapAction();
  798   			if (soapAction == null) {
  799   				soapAction = "";
  800   			}
  801   			addPolicyAsExtElement(axisBindingOperation, operation);
  802   			addExtensionElement(fac, operation, OPERATION_LOCAL_NAME,
  803   					SOAP_ACTION, soapAction, STYLE, style, soap);
  804   
  805   			// addPolicyAsExtElement(PolicyInclude.BINDING_OPERATION_POLICY,
  806   			// axisOperation.getPolicyInclude(), operation);
  807   
  808   			String MEP = axisOperation.getMessageExchangePattern();
  809   
  810   			if (WSDL2Constants.MEP_URI_IN_ONLY.equals(MEP)
  811   					|| WSDL2Constants.MEP_URI_IN_OPTIONAL_OUT.equals(MEP)
  812   					|| WSDL2Constants.MEP_URI_OUT_OPTIONAL_IN.equals(MEP)
  813   					|| WSDL2Constants.MEP_URI_ROBUST_OUT_ONLY.equals(MEP)
  814   					|| WSDL2Constants.MEP_URI_ROBUST_IN_ONLY.equals(MEP)
  815   					|| WSDL2Constants.MEP_URI_IN_OUT.equals(MEP)) {
  816   				AxisBindingMessage axisBindingInMessage = (AxisBindingMessage) axisBindingOperation
  817   						.getChild(WSDLConstants.MESSAGE_LABEL_IN_VALUE);
  818   				if (axisBindingInMessage != null) {
  819   					AxisMessage inaxisMessage = axisBindingInMessage
  820   							.getAxisMessage();
  821   
  822   					if (inaxisMessage != null) {
  823   						operation.addAttribute(ATTRIBUTE_NAME, opeartionName,
  824   								null);
  825   						OMElement input = fac.createOMElement(
  826   								IN_PUT_LOCAL_NAME, wsdl);
  827   						addPolicyAsExtElement(axisBindingInMessage, input);
  828   						addExtensionElement(fac, input, SOAP_BODY, SOAP_USE,
  829   								use, null, targetNamespace, soap);
  830   						// addPolicyAsExtElement(PolicyInclude.BINDING_INPUT_POLICY,
  831   						// inaxisMessage.getPolicyInclude(), input);
  832   						operation.addChild(input);
  833   						writeSoapHeaders(inaxisMessage, fac, input, soap12);
  834   					}
  835   				}
  836   			}
  837   
  838   			if (WSDL2Constants.MEP_URI_OUT_ONLY.equals(MEP)
  839   					|| WSDL2Constants.MEP_URI_OUT_OPTIONAL_IN.equals(MEP)
  840   					|| WSDL2Constants.MEP_URI_IN_OPTIONAL_OUT.equals(MEP)
  841   					|| WSDL2Constants.MEP_URI_ROBUST_OUT_ONLY.equals(MEP)
  842   					|| WSDL2Constants.MEP_URI_IN_OUT.equals(MEP)) {
  843   
  844   				AxisBindingMessage axisBindingOutMessage = (AxisBindingMessage) axisBindingOperation
  845   						.getChild(WSDLConstants.MESSAGE_LABEL_OUT_VALUE);
  846   				if (axisBindingOutMessage != null) {
  847   					AxisMessage outAxisMessage = axisBindingOutMessage
  848   							.getAxisMessage();
  849   					if (outAxisMessage != null) {
  850   						OMElement output = fac.createOMElement(
  851   								OUT_PUT_LOCAL_NAME, wsdl);
  852   						addPolicyAsExtElement(axisBindingOutMessage, output);
  853   						addExtensionElement(fac, output, SOAP_BODY, SOAP_USE,
  854   								use, null, targetNamespace, soap);
  855   						// addPolicyAsExtElement(PolicyInclude.BINDING_OUTPUT_POLICY,
  856   						// outAxisMessage.getPolicyInclude(), output);
  857   						operation.addChild(output);
  858   						writeSoapHeaders(outAxisMessage, fac, output, soap12);
  859   					}
  860   				}
  861   			}
  862   
  863   			// generate fault Messages
  864   			ArrayList faultyMessages = axisBindingOperation.getFaults();
  865   			if (faultyMessages != null) {
  866                   for (Object faultyMessage1 : faultyMessages) {
  867                       AxisBindingMessage bindingFaultyMessage = (AxisBindingMessage)faultyMessage1;
  868                       if (bindingFaultyMessage != null) {
  869                           AxisMessage faultyMessage = bindingFaultyMessage.getAxisMessage();
  870   
  871                           OMElement fault = fac.createOMElement(FAULT_LOCAL_NAME, wsdl);
  872   
  873                           addPolicyAsExtElement(bindingFaultyMessage, fault);
  874                           addExtensionElement(fac, fault, FAULT_LOCAL_NAME,
  875                                               SOAP_USE, use, ATTRIBUTE_NAME,
  876                                               faultyMessage.getName(), soap);
  877                           fault.addAttribute(ATTRIBUTE_NAME, faultyMessage.getName(), null);
  878                           // add policies for fault messages
  879                           operation.addChild(fault);
  880                           writeSoapHeaders(faultyMessage, fac, fault, soap);
  881                       }
  882                   }
  883   			}
  884   		}
  885   	}
  886   
  887   	private void generateSoap12Binding(OMFactory fac, OMElement definitions,
  888   			AxisBinding axisBinding) throws Exception {
  889   		if (isAlreadyAdded(axisBinding, definitions)) {
  890   			return;
  891   		}
  892   		OMElement binding = fac.createOMElement(BINDING_LOCAL_NAME, wsdl);
  893   		OMElement serviceElement = definitions.getFirstChildWithName(new QName(
  894   				wsdl.getNamespaceURI(), SERVICE_LOCAL_NAME));
  895   		serviceElement.insertSiblingBefore(binding);
  896   
  897   		QName qname = axisBinding.getName();
  898   		binding.addAttribute(ATTRIBUTE_NAME, qname.getLocalPart(), null);
  899   		binding.addAttribute("type", tns.getPrefix() + ":" + serviceName
  900   				+ PORT_TYPE_SUFFIX, null);
  901   
  902   		// Adding ext elements
  903   		addPolicyAsExtElement(axisBinding, binding);
  904   		addExtensionElement(fac, binding, BINDING_LOCAL_NAME, TRANSPORT,
  905   				TRANSPORT_URI, STYLE, style, soap12);
  906   
  907   		// /////////////////////////////////////////////////////////////////////
  908   		// Add WS-Addressing UsingAddressing element if appropriate
  909   		// SHOULD be on the binding element per the specification
  910   		if (AddressingHelper
  911   				.getAddressingRequirementParemeterValue(axisService).equals(
  912   						AddressingConstants.ADDRESSING_OPTIONAL)) {
  913   			WSDLSerializationUtil.addExtensionElement(fac, binding,
  914   					AddressingConstants.USING_ADDRESSING,
  915   					DEFAULT_WSDL_NAMESPACE_PREFIX + ":required", "true", wsaw);
  916   		} else if (AddressingHelper.getAddressingRequirementParemeterValue(
  917   				axisService).equals(AddressingConstants.ADDRESSING_REQUIRED)) {
  918   			WSDLSerializationUtil.addExtensionElement(fac, binding,
  919   					AddressingConstants.USING_ADDRESSING,
  920   					DEFAULT_WSDL_NAMESPACE_PREFIX + ":required", "true", wsaw);
  921   		}
  922   		// //////////////////////////////////////////////////////////////////////
  923   
  924   		for (Iterator axisBindingOperations = axisBinding.getChildren(); axisBindingOperations
  925   				.hasNext();) {
  926   			AxisBindingOperation axisBindingOperation = (AxisBindingOperation) axisBindingOperations
  927   					.next();
  928   			AxisOperation axisOperation = axisBindingOperation
  929   					.getAxisOperation();
  930   			if (axisOperation.isControlOperation()
  931   					|| axisOperation.getName() == null) {
  932   				continue;
  933   			}
  934   			String opeartionName = axisOperation.getName().getLocalPart();
  935   			OMElement operation = fac.createOMElement(OPERATION_LOCAL_NAME,
  936   					wsdl);
  937   			binding.addChild(operation);
  938   			String soapAction = axisOperation.getSoapAction();
  939   			if (soapAction == null) {
  940   				soapAction = "";
  941   			}
  942   			addPolicyAsExtElement(axisBindingOperation, operation);
  943   			addExtensionElement(fac, operation, OPERATION_LOCAL_NAME,
  944   					SOAP_ACTION, soapAction, STYLE, style, soap12);
  945   
  946   			// addPolicyAsExtElement(PolicyInclude.BINDING_OPERATION_POLICY,
  947   			// axisOperation.getPolicyInclude(), operation);
  948   
  949   			String MEP = axisOperation.getMessageExchangePattern();
  950   
  951   			if (WSDL2Constants.MEP_URI_IN_ONLY.equals(MEP)
  952   					|| WSDL2Constants.MEP_URI_IN_OPTIONAL_OUT.equals(MEP)
  953   					|| WSDL2Constants.MEP_URI_OUT_OPTIONAL_IN.equals(MEP)
  954   					|| WSDL2Constants.MEP_URI_ROBUST_OUT_ONLY.equals(MEP)
  955   					|| WSDL2Constants.MEP_URI_ROBUST_IN_ONLY.equals(MEP)
  956   					|| WSDL2Constants.MEP_URI_IN_OUT.equals(MEP)) {
  957   				AxisBindingMessage axisBindingInMessage = (AxisBindingMessage) axisBindingOperation
  958   						.getChild(WSDLConstants.MESSAGE_LABEL_IN_VALUE);
  959   				if (axisBindingInMessage != null) {
  960   					AxisMessage inaxisMessage = axisBindingInMessage
  961   							.getAxisMessage();
  962   
  963   					if (inaxisMessage != null) {
  964   						operation.addAttribute(ATTRIBUTE_NAME, opeartionName,
  965   								null);
  966   						OMElement input = fac.createOMElement(
  967   								IN_PUT_LOCAL_NAME, wsdl);
  968   						addPolicyAsExtElement(axisBindingInMessage, input);
  969   						addExtensionElement(fac, input, SOAP_BODY, SOAP_USE,
  970   								use, null, targetNamespace, soap12);
  971   						operation.addChild(input);
  972   						writeSoapHeaders(inaxisMessage, fac, input, soap12);
  973   					}
  974   				}
  975   			}
  976   
  977   			if (WSDL2Constants.MEP_URI_OUT_ONLY.equals(MEP)
  978   					|| WSDL2Constants.MEP_URI_OUT_OPTIONAL_IN.equals(MEP)
  979   					|| WSDL2Constants.MEP_URI_IN_OPTIONAL_OUT.equals(MEP)
  980   					|| WSDL2Constants.MEP_URI_ROBUST_OUT_ONLY.equals(MEP)
  981   					|| WSDL2Constants.MEP_URI_IN_OUT.equals(MEP)) {
  982   
  983   				AxisBindingMessage axisBindingOutMessage = (AxisBindingMessage) axisBindingOperation
  984   						.getChild(WSDLConstants.MESSAGE_LABEL_OUT_VALUE);
  985   				if (axisBindingOutMessage != null) {
  986   					AxisMessage outAxisMessage = axisBindingOutMessage
  987   							.getAxisMessage();
  988   					if (outAxisMessage != null) {
  989   						OMElement output = fac.createOMElement(
  990   								OUT_PUT_LOCAL_NAME, wsdl);
  991   						addPolicyAsExtElement(axisBindingOutMessage, output);
  992   						addExtensionElement(fac, output, SOAP_BODY, SOAP_USE,
  993   								use, null, targetNamespace, soap12);
  994   						// addPolicyAsExtElement(PolicyInclude.BINDING_OUTPUT_POLICY,
  995   						// outAxisMessage.getPolicyInclude(), output);
  996   						operation.addChild(output);
  997   						writeSoapHeaders(outAxisMessage, fac, output, soap12);
  998   					}
  999   				}
 1000   			}
 1001   
 1002   			// generate fault Messages
 1003   			ArrayList faultyMessages = axisBindingOperation.getFaults();
 1004   			if (faultyMessages != null) {
 1005                   for (Object faultyMessage1 : faultyMessages) {
 1006                       AxisBindingMessage bindingFaultyMessage = (AxisBindingMessage)faultyMessage1;
 1007                       if (bindingFaultyMessage != null) {
 1008                           AxisMessage faultyMessage = bindingFaultyMessage
 1009                                   .getAxisMessage();
 1010                           OMElement fault = fac.createOMElement(FAULT_LOCAL_NAME,
 1011                                                                 wsdl);
 1012                           addPolicyAsExtElement(bindingFaultyMessage, fault);
 1013                           addExtensionElement(fac, fault, FAULT_LOCAL_NAME,
 1014                                               SOAP_USE, use, ATTRIBUTE_NAME, faultyMessage
 1015                                   .getName(), soap12);
 1016                           fault.addAttribute(ATTRIBUTE_NAME, faultyMessage
 1017                                   .getName(), null);
 1018                           // add policies for fault messages
 1019                           operation.addChild(fault);
 1020                           writeSoapHeaders(faultyMessage, fac, fault, soap12);
 1021                       }
 1022                   }
 1023   			}
 1024   		}
 1025   	}
 1026   
 1027   	private void generateHttpBinding(OMFactory fac, OMElement definitions,
 1028   			AxisBinding axisBinding) throws Exception {
 1029   		if (isAlreadyAdded(axisBinding, definitions)) {
 1030   			return;
 1031   		}
 1032   		OMElement binding = fac.createOMElement(BINDING_LOCAL_NAME, wsdl);
 1033   		OMElement serviceElement = definitions.getFirstChildWithName(new QName(
 1034   				wsdl.getNamespaceURI(), SERVICE_LOCAL_NAME));
 1035   		serviceElement.insertSiblingBefore(binding);
 1036   
 1037   		QName qname = axisBinding.getName();
 1038   		binding.addAttribute(ATTRIBUTE_NAME, qname.getLocalPart(), null);
 1039   		binding.addAttribute("type", tns.getPrefix() + ":" + serviceName
 1040   				+ PORT_TYPE_SUFFIX, null);
 1041   
 1042   		OMElement httpBinding = fac.createOMElement("binding", http);
 1043   		binding.addChild(httpBinding);
 1044   		httpBinding.addAttribute("verb", "POST", null);
 1045   
 1046   		for (Iterator axisBindingOperations = axisBinding.getChildren(); axisBindingOperations
 1047   				.hasNext();) {
 1048   			AxisBindingOperation axisBindingOperation = (AxisBindingOperation) axisBindingOperations
 1049   					.next();
 1050   			AxisOperation axisOperation = axisBindingOperation
 1051   					.getAxisOperation();
 1052   			if (axisOperation.isControlOperation()
 1053   					|| axisOperation.getName() == null) {
 1054   				continue;
 1055   			}
 1056   			String opeartionName = axisOperation.getName().getLocalPart();
 1057   			OMElement operation = fac.createOMElement(OPERATION_LOCAL_NAME,
 1058   					wsdl);
 1059   			binding.addChild(operation);
 1060   
 1061   			OMElement httpOperation = fac.createOMElement("operation", http);
 1062   			operation.addChild(httpOperation);
 1063   			httpOperation.addAttribute("location", serviceName + "/"
 1064   					+ axisOperation.getName().getLocalPart(), null);
 1065   
 1066   			String MEP = axisOperation.getMessageExchangePattern();
 1067   
 1068   			if (WSDL2Constants.MEP_URI_IN_ONLY.equals(MEP)
 1069   					|| WSDL2Constants.MEP_URI_IN_OPTIONAL_OUT.equals(MEP)
 1070   					|| WSDL2Constants.MEP_URI_OUT_OPTIONAL_IN.equals(MEP)
 1071   					|| WSDL2Constants.MEP_URI_ROBUST_OUT_ONLY.equals(MEP)
 1072   					|| WSDL2Constants.MEP_URI_ROBUST_IN_ONLY.equals(MEP)
 1073   					|| WSDL2Constants.MEP_URI_IN_OUT.equals(MEP)) {
 1074   				AxisBindingMessage axisBindingInMessage = (AxisBindingMessage) axisBindingOperation
 1075   						.getChild(WSDLConstants.MESSAGE_LABEL_IN_VALUE);
 1076   				if (axisBindingInMessage != null) {
 1077   					AxisMessage inaxisMessage = axisBindingInMessage
 1078   							.getAxisMessage();
 1079   
 1080   					if (inaxisMessage != null) {
 1081   						operation.addAttribute(ATTRIBUTE_NAME, opeartionName,
 1082   								null);
 1083   						OMElement input = fac.createOMElement(
 1084   								IN_PUT_LOCAL_NAME, wsdl);
 1085   						OMElement inputelement = fac.createOMElement("content",
 1086   								mime);
 1087   						input.addChild(inputelement);
 1088   						inputelement.addAttribute("type", "text/xml", null);
 1089   						inputelement.addAttribute("part", axisOperation
 1090   								.getName().getLocalPart(), null);
 1091   						operation.addChild(input);
 1092   					}
 1093   				}
 1094   			}
 1095   
 1096   			if (WSDL2Constants.MEP_URI_OUT_ONLY.equals(MEP)
 1097   					|| WSDL2Constants.MEP_URI_OUT_OPTIONAL_IN.equals(MEP)
 1098   					|| WSDL2Constants.MEP_URI_IN_OPTIONAL_OUT.equals(MEP)
 1099   					|| WSDL2Constants.MEP_URI_ROBUST_OUT_ONLY.equals(MEP)
 1100   					|| WSDL2Constants.MEP_URI_IN_OUT.equals(MEP)) {
 1101   
 1102   				AxisBindingMessage axisBindingOutMessage = (AxisBindingMessage) axisBindingOperation
 1103   						.getChild(WSDLConstants.MESSAGE_LABEL_OUT_VALUE);
 1104   				if (axisBindingOutMessage != null) {
 1105   					AxisMessage outAxisMessage = axisBindingOutMessage
 1106   							.getAxisMessage();
 1107   					if (outAxisMessage != null) {
 1108   						OMElement output = fac.createOMElement(
 1109   								OUT_PUT_LOCAL_NAME, wsdl);
 1110   						OMElement outElement = fac.createOMElement("content",
 1111   								mime);
 1112   						outElement.addChild(outElement);
 1113   						outElement.addAttribute("type", "text/xml", null);
 1114   						outElement.addAttribute("part", axisOperation.getName()
 1115   								.getLocalPart(), null);
 1116   						output.addChild(outElement);
 1117   						operation.addChild(output);
 1118   					}
 1119   				}
 1120   			}
 1121   		}
 1122   	}
 1123   
 1124   	private void addPolicyAsExtElement(AxisDescription axisDescription,
 1125   			OMElement wsdlElement) throws Exception {
 1126   		PolicySubject policySubject = axisDescription.getPolicySubject();
 1127   		Collection attachPolicyComponents = policySubject
 1128   				.getAttachedPolicyComponents();
 1129   
 1130           for (Object policyElement : attachPolicyComponents) {
 1131               if (policyElement instanceof Policy) {
 1132                   PolicyReference policyReference =
 1133                           PolicyUtil.createPolicyReference((Policy)policyElement);
 1134                   OMElement policyRefElement =
 1135                           PolicyUtil.getPolicyComponentAsOMElement(policyReference, serializer);
 1136   
 1137                   OMNode firstChildElem = wsdlElement.getFirstElement();
 1138                   if (firstChildElem == null) {
 1139                       wsdlElement.addChild(policyRefElement);
 1140                   } else {
 1141                       firstChildElem.insertSiblingBefore(policyRefElement);
 1142                   }
 1143                   String key = policyReference.getURI();
 1144                   if (key.startsWith("#")) {
 1145                       key = key.substring(key.indexOf("#") + 1);
 1146                   }
 1147                   addPolicyToDefinitionElement(key, (Policy)policyElement);
 1148   
 1149               } else if (policyElement instanceof PolicyReference) {
 1150                   OMElement child =
 1151                           PolicyUtil.getPolicyComponentAsOMElement((PolicyComponent)policyElement,
 1152                                                                    serializer);
 1153                   OMElement firstChildElem = wsdlElement.getFirstElement();
 1154   
 1155                   if (firstChildElem == null) {
 1156                       wsdlElement.addChild(child);
 1157                   } else {
 1158                       firstChildElem.insertSiblingBefore(child);
 1159                   }
 1160   
 1161                   String key = ((PolicyReference)policyElement).getURI();
 1162                   if (key.startsWith("#")) {
 1163                       key = key.substring(key.indexOf("#") + 1);
 1164                   }
 1165   
 1166                   PolicyLocator locator = new PolicyLocator(axisService);
 1167                   Policy p = locator.lookup(key);
 1168   
 1169                   if (p == null) {
 1170                       throw new Exception("Policy not found for uri : " + key);
 1171                   }
 1172                   addPolicyToDefinitionElement(key, p);
 1173               }
 1174           }
 1175   	}
 1176   
 1177   	private void addPolicyAsExtAttribute(AxisDescription axisDescription,
 1178   			OMElement element, OMFactory factory) throws Exception {
 1179   
 1180   		PolicySubject policySubject = axisDescription.getPolicySubject();
 1181   		ArrayList policyURIs = new ArrayList();
 1182   
 1183           for (Object policyElement : policySubject.getAttachedPolicyComponents()) {
 1184               String key;
 1185   
 1186               if (policyElement instanceof Policy) {
 1187                   Policy p = (Policy)policyElement;
 1188   
 1189                   if (p.getId() != null) {
 1190                       key = "#" + p.getId();
 1191                   } else if (p.getName() != null) {
 1192                       key = p.getName();
 1193                   } else {
 1194                       throw new RuntimeException(
 1195                               "Can't add the Policy as an extensibility attribute since it doesn't have a id or a name attribute");
 1196                   }
 1197   
 1198                   policyURIs.add(key);
 1199                   addPolicyToDefinitionElement(key, p);
 1200               } else {
 1201                   String uri = ((PolicyReference)policyElement).getURI();
 1202                   PolicyLocator locator = new PolicyLocator(axisService);
 1203                   if (uri.startsWith("#")) {
 1204                       key = uri.substring(uri.indexOf('#') + 1);
 1205                   } else {
 1206                       key = uri;
 1207                   }
 1208   
 1209                   Policy p = locator.lookup(key);
 1210   
 1211                   if (p == null) {
 1212                       throw new RuntimeException("Cannot resolve " + uri
 1213                                                  + " to a Policy");
 1214                   }
 1215                   policyURIs.add(uri);
 1216                   addPolicyToDefinitionElement(key, p);
 1217               }
 1218           }
 1219   
 1220   		if (!policyURIs.isEmpty()) {
 1221   			String value = null;
 1222   
 1223   			/*
 1224   			 * We need to create a String that is like 'uri1 uri2 .." to set as
 1225   			 * the value of the wsp:PolicyURIs attribute.
 1226   			 */
 1227               for (Object policyURI : policyURIs) {
 1228                   String uri = (String)policyURI;
 1229                   value = (value == null) ? uri : value + " " + uri;
 1230               }
 1231   
 1232   			OMNamespace ns = factory.createOMNamespace(
 1233   					org.apache.neethi.Constants.URI_POLICY_NS,
 1234   					org.apache.neethi.Constants.ATTR_WSP);
 1235   			OMAttribute URIs = factory.createOMAttribute("PolicyURIs", ns,
 1236   					value);
 1237   			element.addAttribute(URIs);
 1238   		}
 1239   	}
 1240   
 1241   	private boolean isAlreadyAdded(AxisBinding axisBinding,
 1242   			OMElement definitionElement) {
 1243   		QName bindingName = axisBinding.getName();
 1244   		QName name = new QName("name");
 1245   		for (Iterator iterator = definitionElement
 1246   				.getChildrenWithName(new QName(wsdl.getNamespaceURI(),
 1247   						BINDING_LOCAL_NAME)); iterator.hasNext();) {
 1248   			OMElement element = (OMElement) iterator.next();
 1249   			String value = element.getAttributeValue(name);
 1250   			if (bindingName.getLocalPart().equals(value)) {
 1251   				return true;
 1252   			}
 1253   		}
 1254   		return false;
 1255   	}
 1256   	
 1257   	private String getEndpointURL(AxisEndpoint axisEndpoint) {
 1258   		Parameter modifyAddressParam = axisService.getParameter("modifyUserWSDLPortAddress");
 1259               String endpointURL = axisEndpoint.getEndpointURL();
 1260               if (modifyAddressParam != null &&
 1261                       !Boolean.parseBoolean((String)modifyAddressParam.getValue())) {
 1262                   return endpointURL;
 1263               }
 1264   
 1265               String hostIP;
 1266       
 1267               // First check the hostname parameter 
 1268               hostIP = Utils.getHostname(axisService.getAxisConfiguration());
 1269           
 1270               //If it is not set extract the hostIP from the URL
 1271               if (hostIP == null) {
 1272                   hostIP = WSDLSerializationUtil.extractHostIP(axisService.getEndpointURL());
 1273               }
 1274           
 1275               //TODO This is to prevent problems when JAVA2WSDL tool is used where there is no
 1276               //Axis server running. calculateEndpointURL fails in this scenario, refer to 
 1277               // SimpleHTTPServer#getEPRsForService()
 1278     
 1279               if (hostIP != null) {
 1280                   return axisEndpoint.calculateEndpointURL(hostIP);
 1281               } else {
 1282                   return endpointURL;
 1283               }
 1284   	}
 1285   	
 1286   	/**
 1287   	 * Generate the Identity element according to the WS-AddressingAndIdentity if the 
 1288   	 * AddressingConstants.IDENTITY_PARAMETER parameter is set. 
 1289   	 * http://schemas.xmlsoap.org/ws/2006/02/addressingidentity/ 
 1290   	 */
 1291   	
 1292   	private void generateIdentityElement(OMFactory fac,OMElement epr, Parameter wsaIdParam) {
 1293   	    
 1294   	    // Create the Identity element
 1295   	    OMElement identity = fac.createOMElement(AddressingConstants.QNAME_IDENTITY);
 1296   	    OMElement keyInfo = fac.createOMElement(AddressingConstants.QNAME_IDENTITY_KEY_INFO);
 1297   	    OMElement x509Data = fac.createOMElement(AddressingConstants.QNAME_IDENTITY_X509_DATA);
 1298   	    OMElement x509cert = fac.createOMElement(AddressingConstants.QNAME_IDENTITY_X509_CERT);
 1299   	    x509cert.setText((String)wsaIdParam.getValue());
 1300   	    
 1301   	    x509Data.addChild(x509cert);
 1302   	    keyInfo.addChild(x509Data);
 1303   	    identity.addChild(keyInfo);
 1304   	    
 1305   	    epr.addChild(identity);
 1306   	    
 1307   	}
 1308   	
 1309   	
 1310   	/*
 1311   	 * Generate the EndpointReference element
 1312   	 * <wsa:EndpointReference>
 1313            *    <wsa:Address>
 1314            *        http://some.service.epr/
 1315            *     </wsa:Address>
 1316            * </wsa:EndpointReference>
 1317   	 * 
 1318   	 */
 1319   	private void generateEPRElement(OMFactory fac, OMElement port, String endpointURL){
 1320   	    
 1321   	    Parameter parameter = axisService.getParameter(AddressingConstants.IDENTITY_PARAMETER);
 1322   	            
 1323   	    // If the parameter is not set, return
 1324   	    if (parameter == null || parameter.getValue() == null) {
 1325   	        return;
 1326   	    }
 1327   	    
 1328   	    OMElement wsaEpr = fac.createOMElement(AddressingConstants.Final.WSA_ENDPOINT_REFERENCE);
 1329   	    
 1330   	    OMElement address = fac.createOMElement(AddressingConstants.Final.WSA_ADDRESS);
 1331   	    address.setText((endpointURL == null) ? "": endpointURL);
 1332   	    
 1333   	    wsaEpr.addChild(address);
 1334   	    
 1335   	    // This will generate the identity element if the service parameter is set
 1336   	    generateIdentityElement(fac, wsaEpr, parameter);
 1337   	    
 1338   	    port.addChild(wsaEpr);   
 1339   	    
 1340   	}
 1341   }

Save This Page
Home » axis2-1.5-src » org.apache » axis2 » description » [javadoc | source]