axisservice2wsdl11.java

来自「开源的axis2框架的源码。用于开发WEBSERVER」· Java 代码 · 共 1,070 行 · 第 1/4 页

JAVA
1,070
字号
    private void generateHeaderMessages(AxisMessage axismessage, OMFactory fac,
                                        OMElement defintions) {
        ArrayList extList = axismessage.getSoapHeaders();
        for (int i = 0; i < extList.size(); i++) {
            SOAPHeaderMessage header = (SOAPHeaderMessage) extList.get(i);
            OMElement messageElement = fac.createOMElement(MESSAGE_LOCAL_NAME,
                                                           wsdl);
            messageElement.addAttribute(ATTRIBUTE_NAME, header.getMessage()
                    .getLocalPart(), null);
            defintions.addChild(messageElement);
            OMElement messagePart = fac.createOMElement(PART_ATTRIBUTE_NAME,
                                                        wsdl);
            messageElement.addChild(messagePart);
            messagePart.addAttribute(ATTRIBUTE_NAME, header.part(), null);
            if (header.getElement() == null) {
                throw new RuntimeException(ELEMENT_ATTRIBUTE_NAME
                        + " is null for " + header.getMessage());
            }
            messagePart.addAttribute(ELEMENT_ATTRIBUTE_NAME, WSDLSerializationUtil.getPrefix(header
                    .getElement().getNamespaceURI(), axisService.getNamespaceMap())
                    + ":" + header.getElement().getLocalPart(), null);
        }
    }

    private void writeMessage(AxisMessage axismessage, OMFactory fac,
                              OMElement defintions) {
        if (messagesMap.get(axismessage.getName()) == null) {
            messagesMap.put(axismessage.getName(), axismessage);
            QName schemaElementName = axismessage.getElementQName();
            OMElement messageElement = fac.createOMElement(MESSAGE_LOCAL_NAME,
                                                           wsdl);
            messageElement.addAttribute(ATTRIBUTE_NAME, axismessage.getName(),
                                        null);
            defintions.addChild(messageElement);
            if (schemaElementName != null) {
                OMElement messagePart = fac.createOMElement(
                        PART_ATTRIBUTE_NAME, wsdl);
                messageElement.addChild(messagePart);
                if(axismessage.getMessagePartName() != null) {
                	messagePart.addAttribute(ATTRIBUTE_NAME, axismessage.getMessagePartName(), null);
            	} else {
            		messagePart.addAttribute(ATTRIBUTE_NAME, axismessage.getPartName(), null);
            	}
                messagePart.addAttribute(ELEMENT_ATTRIBUTE_NAME, WSDLSerializationUtil.getPrefix(
                        schemaElementName.getNamespaceURI(), axisService.getNamespaceMap()) + ":" +
                        schemaElementName.getLocalPart(), null);
            }
        }

    }

    /**
     * Builds the &lt;portType&gt; element in the passed WSDL definition.  When this returns
     * successfully, there will be a new child element under definitons for the portType.
     *
     * @param fac the active OMFactory
     * @param defintions the WSDL &lt;definitions&gt; element
     * @throws Exception if there's a problem
     */
    private void generatePortType(OMFactory fac, OMElement defintions)
            throws Exception {
        OMElement portType = fac.createOMElement(PORT_TYPE_LOCAL_NAME, wsdl);
        defintions.addChild(portType);

        portType.addAttribute(ATTRIBUTE_NAME, axisService.getName()
                + PORT_TYPE_SUFFIX, null);

        addPolicyAsExtAttribute(PolicyInclude.PORT_TYPE_POLICY, axisService
                .getPolicyInclude(), portType, fac);
        for (Iterator operations = axisService.getOperations(); operations.hasNext();) {
            AxisOperation axisOperation = (AxisOperation) operations.next();
            if (axisOperation.isControlOperation() || axisOperation.getName() == null) {
                continue;
            }
            String operationName = axisOperation.getName().getLocalPart();
            OMElement operation = fac.createOMElement(OPERATION_LOCAL_NAME,
                                                      wsdl);
            WSDLSerializationUtil.addWSDLDocumentationElement(axisOperation, operation, fac, wsdl);
            portType.addChild(operation);
            operation.addAttribute(ATTRIBUTE_NAME, operationName, null);
            addPolicyAsExtElement(PolicyInclude.OPERATION_POLICY, axisOperation
                    .getPolicyInclude(), operation);

            String MEP = axisOperation.getMessageExchangePattern();
            if (WSDL2Constants.MEP_URI_IN_ONLY.equals(MEP)
                    || WSDL2Constants.MEP_URI_IN_OPTIONAL_OUT
                    .equals(MEP)
                    || WSDL2Constants.MEP_URI_OUT_OPTIONAL_IN
                    .equals(MEP)
                    || WSDL2Constants.MEP_URI_ROBUST_OUT_ONLY
                    .equals(MEP)
                    || WSDL2Constants.MEP_URI_ROBUST_IN_ONLY
                    .equals(MEP)
                    || WSDL2Constants.MEP_URI_IN_OUT
                    .equals(MEP)) {
                AxisMessage inaxisMessage = axisOperation
                        .getMessage(WSDLConstants.MESSAGE_LABEL_IN_VALUE);
                if (inaxisMessage != null) {
                    OMElement input = fac.createOMElement(IN_PUT_LOCAL_NAME,
                                                          wsdl);
                    WSDLSerializationUtil.addWSDLDocumentationElement(inaxisMessage, input, fac, wsdl);
                    input.addAttribute(MESSAGE_LOCAL_NAME, tns.getPrefix()
                            + ":" + inaxisMessage.getName(), null);
                    addPolicyAsExtElement(PolicyInclude.INPUT_POLICY,
                                          inaxisMessage.getPolicyInclude(), input);
                    WSDLSerializationUtil.addWSAWActionAttribute(input, axisOperation
                            .getInputAction(),wsaw);
                    operation.addChild(input);
                }
            }

            if (WSDL2Constants.MEP_URI_OUT_ONLY.equals(MEP)
                    || WSDL2Constants.MEP_URI_OUT_OPTIONAL_IN
                    .equals(MEP)
                    || WSDL2Constants.MEP_URI_IN_OPTIONAL_OUT
                    .equals(MEP)
                    || WSDL2Constants.MEP_URI_ROBUST_OUT_ONLY
                    .equals(MEP)
                    || WSDL2Constants.MEP_URI_ROBUST_IN_ONLY
                    .equals(MEP)
                    || WSDL2Constants.MEP_URI_IN_OUT
                    .equals(MEP)) {
                AxisMessage outAxisMessage = axisOperation
                        .getMessage(WSDLConstants.MESSAGE_LABEL_OUT_VALUE);
                if (outAxisMessage != null) {
                    OMElement output = fac.createOMElement(OUT_PUT_LOCAL_NAME,
                                                           wsdl);
                    WSDLSerializationUtil.addWSDLDocumentationElement(outAxisMessage, output, fac, wsdl);
                    output.addAttribute(MESSAGE_LOCAL_NAME, tns.getPrefix()
                            + ":" + outAxisMessage.getName(), null);
                    addPolicyAsExtElement(PolicyInclude.OUTPUT_POLICY,
                                          outAxisMessage.getPolicyInclude(), output);
                    WSDLSerializationUtil.addWSAWActionAttribute(output, axisOperation
                            .getOutputAction(),wsaw);
                    operation.addChild(output);
                }
            }

            // generate fault Messages
            ArrayList faultMessages = axisOperation.getFaultMessages();
            if (faultMessages != null) {
                for (int i = 0; i < faultMessages.size(); i++) {
                    AxisMessage faultyMessage = (AxisMessage) faultMessages
                            .get(i);
                    OMElement fault = fac.createOMElement(FAULT_LOCAL_NAME, wsdl);
                    WSDLSerializationUtil.addWSDLDocumentationElement(faultyMessage,
                                                                      fault, fac, wsdl);
                    fault.addAttribute(MESSAGE_LOCAL_NAME, tns.getPrefix()
                            + ":" + faultyMessage.getName(), null);
                    fault.addAttribute(ATTRIBUTE_NAME, faultyMessage.getName(), null);
                    WSDLSerializationUtil.addWSAWActionAttribute(fault, axisOperation
                            .getFaultAction(faultyMessage.getName()),wsaw);
                    // TODO add policies for fault messages
                    operation.addChild(fault);
                }
            }

        }
    }

    /**
     * Generate the WSDL &lt;service&gt; element
     *
     * @param fac the active OMFactory
     * @param defintions the WSDL &lt;definitions&gt; element under which to put the service
     * @param disableREST if false, generate REST binding, if true, don't
     * @throws Exception if there's a problem
     */
    public void generateService(OMFactory fac, OMElement defintions, boolean disableREST)
            throws Exception {
        OMElement service = fac.createOMElement(SERVICE_LOCAL_NAME, wsdl);
        defintions.addChild(service);
        service.addAttribute(ATTRIBUTE_NAME, axisService.getName(), null);
        generateSOAP11Ports(fac, service);
        generateSOAP12Ports(fac, service);

        addPolicyAsExtElement(PolicyInclude.SERVICE_POLICY, axisService
                .getPolicyInclude(), service);
        if (!disableREST) {
            generateHTTPPorts(fac, service);
        }
    }

    private void generateSOAP11Ports(OMFactory fac, OMElement service)
            throws Exception {
        for (int i = 0; i < serviceEndpointURLs.length; i++) {
            String urlString = serviceEndpointURLs[i];
            if (urlString != null) {
                String protocol = new URI(urlString).getScheme();
                OMElement port = fac.createOMElement(PORT, wsdl);
                service.addChild(port);
                String name = axisService.getName() + SOAP11PORT
                        + ((protocol == null) ? "" : "_" + protocol);
                if (i > 0) {
                    name += i;
                }
                port.addAttribute(ATTRIBUTE_NAME, name, null);
                port.addAttribute(BINDING_LOCAL_NAME, tns.getPrefix() + ":"
                        + axisService.getName() + BINDING_NAME_SUFFIX, null);
                WSDLSerializationUtil.addExtensionElement(fac, port, SOAP_ADDRESS, LOCATION, urlString,
                                    soap);

                addPolicyAsExtElement(PolicyInclude.PORT_POLICY, axisService
                        .getPolicyInclude(), port);
            }
        }

    }

    private void generateHTTPPorts(OMFactory fac, OMElement service)
            throws Exception {
        for (int i = 0; i < serviceEndpointURLs.length; i++) {
            String urlString = serviceEndpointURLs[i];
            if (urlString != null && urlString.startsWith("http")) {
                OMElement port = fac.createOMElement(PORT, wsdl);
                service.addChild(port);
                String name = axisService.getName() + HTTP_PORT;
                if (i > 0) {
                    name += i;
                }
                port.addAttribute(ATTRIBUTE_NAME, name, null);
                port.addAttribute(BINDING_LOCAL_NAME, tns.getPrefix() + ":"
                        + axisService.getName() + HTTP_BINDING, null);
                OMElement extElement = fac.createOMElement("address", http);
                port.addChild(extElement);
//                urlString = urlString.replaceAll(servicePath, "rest");
                extElement.addAttribute("location", urlString, null);
            }
        }
    }

    private void generateSOAP12Ports(OMFactory fac, OMElement service)
            throws Exception {
        for (int i = 0; i < serviceEndpointURLs.length; i++) {
            String urlString = serviceEndpointURLs[i];
            if (urlString != null) {
                String protocol = new URI(urlString).getScheme();
                OMElement port = fac.createOMElement(PORT, wsdl);
                service.addChild(port);
                String name = axisService.getName() + SOAP12PORT
                        + ((protocol == null) ? "" : "_" + protocol);
                if (i > 0) {
                    name += i;
                }
                port.addAttribute(ATTRIBUTE_NAME, name, null);
                port.addAttribute(BINDING_LOCAL_NAME, tns.getPrefix() + ":"
                        + axisService.getName() + SOAP12BINDING_NAME_SUFFIX, null);
                WSDLSerializationUtil.addExtensionElement(fac, port, SOAP_ADDRESS, LOCATION, urlString,
                                    soap12);

                addPolicyAsExtElement(PolicyInclude.PORT_POLICY, axisService
                        .getPolicyInclude(), port);
            }
        }
    }

    /**
     * Generate the &lt;binding&gt; for SOAP 1.1 underneath the passed definitions
     *
     * @param fac the active OMFactory
     * @param defintions the WSDL &lt;definitions&gt; element under which to put the binding
     * @throws Exception if there's a problem
     */
    private void generateSOAP11Binding(OMFactory fac, OMElement defintions)
            throws Exception {
        OMElement binding = fac.createOMElement(BINDING_LOCAL_NAME, wsdl);
        defintions.addChild(binding);
        binding.addAttribute(ATTRIBUTE_NAME, axisService.getName()

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?