axisservice.java

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

JAVA
1,682
字号
    /**
     * Engages a module. It is required to use this method.
     *
     * @param axisModule
     * @param engager
     */
    public void onEngage(AxisModule axisModule, AxisDescription engager)
            throws AxisFault {
        // adding module operations
        addModuleOperations(axisModule);

        Iterator operations = getOperations();
        while (operations.hasNext()) {
            AxisOperation axisOperation = (AxisOperation) operations.next();
            axisOperation.engageModule(axisModule, engager);
        }
    }

    /**
     * Maps an action (a SOAPAction or WSA action) to the given operation. This is used by
     * dispatching (both SOAPAction- and WSAddressing- based dispatching) to figure out which
     * operation a given message is for.  Some notes on restrictions of "action"
     * - A null or empty action will be ignored
     * - An action that is a duplicate and references an idential operation is allowed
     * - An acton that is a duplicate and references a different operation is NOT allowed.  In this
     * case, the action for the original operation is removed from the alias table, thus removing
     * the ability to route based on this action.  This is necessary to prevent mis-directing
     * incoming message to the wrong operation based on SOAPAction.
     *
     * @param action        the action key
     * @param axisOperation the operation to map to
     */
    public void mapActionToOperation(String action, AxisOperation axisOperation) {
        if (action == null || "".equals(action)) {
            if (log.isDebugEnabled()) {
                log.debug(
                        "mapActionToOperation: A null or empty action cannot be used to map to an operation.");
            }
            return;
        }
        if (log.isDebugEnabled()) {
            log.debug("mapActionToOperation: Mapping Action to Operation: action: " + action +
                      "; operation: " + axisOperation);
        }

        // First check if this action has already been flagged as invalid because it is a duplicate.
        if (invalidOperationsAliases.contains(action)) {
            // This SOAPAction has already been determined to be invalid; log a message
            // and do not add it to the operation alias map.
            if (log.isDebugEnabled()) {
                log.debug("mapActionToOperation: The action: " + action +
                          " can not be used for operation: "
                          + axisOperation + " with operation name: " + axisOperation.getName()
                          + " because that SOAPAction is not unique for this service.");
            }
            return;
        }

        // Check if the action is currently mapping to an operation.
        AxisOperation currentlyMappedOperation = getOperationByAction(action);
        if (currentlyMappedOperation != null) {
            if (currentlyMappedOperation == axisOperation) {
                // This maps to the same operation, then it is already in the alias table, so
                // just silently ignore this mapping request.
                if (log.isDebugEnabled()) {
                    log.debug(
                            "mapActionToOperation: This operation is already mapped to this action: " +
                            action + "; AxisOperation: "
                            + currentlyMappedOperation + " named: " +
                            currentlyMappedOperation.getName());
                }
            } else {
                // This action is already mapped, but it is to a different operation.  Remove
                // the action mapping from the alias table and add it to the list of invalid mappings
                operationsAliasesMap.remove(action);
                invalidOperationsAliases.add(action);
                if (log.isDebugEnabled()) {
                    log.debug(
                            "mapActionToOperation: The action is already mapped to a different " +
                            "operation.  The mapping of the action to any operations will be " +
                            "removed.  Action: " + action + "; original operation: " +
                            currentlyMappedOperation + " named " +
                            currentlyMappedOperation.getName() +
                            "; new operation: " + axisOperation + " named " +
                            axisOperation.getName());
                }
            }
        } else {
            operationsAliasesMap.put(action, axisOperation);
            //Adding operation name to the mapping table
//            operationsAliasesMap.put(axisOperation.getName().getLocalPart(), axisOperation);
        }
    }

    /**
     * Maps an constant string in the whttp:location to the given operation. This is used by
     * RequestURIOperationDispatcher based dispatching to figure out which operation it is that a
     * given message is for.
     *
     * @param string        the constant drawn from whttp:location
     * @param axisOperation the operation to map to
     */
    public void addHttpLocationDispatcherString(String string, AxisOperation axisOperation) {
        httpLocationDispatcherMap.put(string, axisOperation);
    }

    public void printSchema(OutputStream out) throws AxisFault {
        for (int i = 0; i < schemaList.size(); i++) {
            XmlSchema schema = addNameSpaces(i);
            schema.write(out);
        }
    }

    public XmlSchema getSchema(int index) {
        return addNameSpaces(index);
    }


    /**
     * Release the list of schema objects.
     * <p/>
     * In some environments, this can provide significant relief
     * of memory consumption in the java heap, as long as the
     * need for the schema list has completed.
     */
    public void releaseSchemaList() {
        if (schemaList != null) {
            // release the schema list
            schemaList.clear();
        }

        if (log.isDebugEnabled()) {
            log.debug("releaseSchemaList: schema list has been released.");
        }
    }

    private XmlSchema addNameSpaces(int i) {
        XmlSchema schema = (XmlSchema) schemaList.get(i);
        NamespaceMap map = (NamespaceMap) namespaceMap.clone();
        NamespacePrefixList namespaceContext = schema.getNamespaceContext();
        String prefixes[] = namespaceContext.getDeclaredPrefixes();
        for (int j = 0; j < prefixes.length; j++) {
            String prefix = prefixes[j];
            map.add(prefix, namespaceContext.getNamespaceURI(prefix));
        }
        schema.setNamespaceContext(map);
        return schema;
    }

    public void setEPRs(String[] eprs) {
        this.eprs = eprs;
    }

    public String[] getEPRs() throws AxisFault {
        if (eprs != null && eprs.length != 0) {
            return eprs;
        }
        eprs = calculateEPRs();
        return eprs;
    }

    private String[] calculateEPRs(){
        try {
            String requestIP = HttpUtils.getIpAddress(getAxisConfiguration());
            return calculateEPRs(requestIP);
        } catch (SocketException e) {
            log.error("Cannot get local IP address", e);
        }
        return new String[0];
    }

    private String[] calculateEPRs(String requestIP) {
        AxisConfiguration axisConfig = getAxisConfiguration();
        if (axisConfig == null) {
            return null;
        }
        ArrayList eprList = new ArrayList();
        if (enableAllTransports) {
            for (Iterator transports = axisConfig.getTransportsIn().values().iterator();
                 transports.hasNext();) {
                TransportInDescription transportIn = (TransportInDescription) transports.next();
                TransportListener listener = transportIn.getReceiver();
                if (listener != null) {
                    try {
                        EndpointReference[] eprsForService =
                                listener.getEPRsForService(this.name, requestIP);
                        if (eprsForService != null) {
                            for (int i = 0; i < eprsForService.length; i++) {
                                EndpointReference endpointReference = eprsForService[i];
                                if (endpointReference != null) {
                                    String address = endpointReference.getAddress();
                                    if (address != null) {
                                        eprList.add(address);
                                    }
                                }
                            }
                        }
                    } catch (AxisFault axisFault) {
                        log.warn(axisFault.getMessage());
                    }
                }
            }
        } else {
            List trs = this.exposedTransports;
            for (int i = 0; i < trs.size(); i++) {
                String trsName = (String) trs.get(i);
                TransportInDescription transportIn = axisConfig.getTransportIn(trsName);
                if (transportIn != null) {
                    TransportListener listener = transportIn.getReceiver();
                    if (listener != null) {
                        try {
                            EndpointReference[] eprsForService =
                                    listener.getEPRsForService(this.name, requestIP);
                            if (eprsForService != null) {
                                for (int j = 0; j < eprsForService.length; j++) {
                                    EndpointReference endpointReference = eprsForService[j];
                                    if (endpointReference != null) {
                                        String address = endpointReference.getAddress();
                                        if (address != null) {
                                            eprList.add(address);
                                        }
                                    }
                                }
                            }
                        } catch (AxisFault axisFault) {
                            log.warn(axisFault.getMessage());
                        }
                    }
                }
            }
        }
        eprs = (String[]) eprList.toArray(new String[eprList.size()]);
        return eprs;
    }

    private void printDefinitionObject(Definition definition, OutputStream out)
            throws AxisFault, WSDLException {
        if (isModifyUserWSDLPortAddress()) {
            setPortAddress(definition);
        }
        if (!wsdlImportLocationAdjusted){
           changeImportAndIncludeLocations(definition);
           wsdlImportLocationAdjusted = true;
        }
        WSDLWriter writer = WSDLFactory.newInstance().newWSDLWriter();
        writer.writeWSDL(definition, out);
    }

    public void printUserWSDL(OutputStream out,
                              String wsdlName) throws AxisFault {
        Definition definition = null;
        // first find the correct wsdl definition
        Parameter wsdlParameter = getParameter(WSDLConstants.WSDL_4_J_DEFINITION);
        if (wsdlParameter != null) {
            definition = (Definition) wsdlParameter.getValue();
        }

        if (definition != null) {
            try {
                printDefinitionObject(getWSDLDefinition(definition, wsdlName), out);
            } catch (WSDLException e) {
                throw AxisFault.makeFault(e);
            }
        } else {
            printWSDLError(out);
        }

    }

    /**
     * find the defintion object for given name
     * @param parentDefinition
     * @param name
     * @return wsdl definition
     */
    private Definition getWSDLDefinition(Definition parentDefinition, String name) {

        if (name == null) return parentDefinition;

        Definition importedDefinition = null;
        Iterator iter = parentDefinition.getImports().values().iterator();
        Vector values = null;
        Import wsdlImport = null;
        for (; iter.hasNext();) {
            values = (Vector) iter.next();
            for (Iterator valuesIter = values.iterator(); valuesIter.hasNext();) {
                wsdlImport = (Import) valuesIter.next();
                if (wsdlImport.getLocationURI().endsWith(name)) {
                    importedDefinition = wsdlImport.getDefinition();
                    break;
                } else {
                    importedDefinition = getWSDLDefinition(wsdlImport.getDefinition(), name);
                }
                if (importedDefinition != null) {
                    break;
                }
            }
            if (importedDefinition != null) {
                break;
            }
        }
        return importedDefinition;
    }

    /**
     * this procesdue recursively adjust the wsdl imports locations
     * and the schmea import and include locations.
     * @param definition
     */
    private void changeImportAndIncludeLocations(Definition definition){

        //adjust the schema locations in types section
        Types types = definition.getTypes();
        if (types != null) {
            List extensibilityElements = types.getExtensibilityElements();
            Object extensibilityElement = null;
            Schema schema = null;
            for (Iterator iter = extensibilityElements.iterator(); iter.hasNext();) {
                extensibilityElement = iter.next();
                if (extensibilityElement instanceof Schema) {
                    schema = (Schema) extensibilityElement;
                    changeLocations(schema.getElement());
                }
            }
        }

        Iterator iter = definition.getImports().values().iterator();
        Vector values = null;
        Import wsdlImport = null;
        String originalImprotString = null;
        for (; iter.hasNext();) {
            values = (Vector) iter.next();
            for (Iterator valuesIter = values.iterator(); valuesIter.hasNext();) {
                wsdlImport = (Import) valuesIter.next();
                originalImprotString = wsdlImport.getLocationURI();
                wsdlImport.setLocationURI(this.name + "?wsdl=" + originalImprotString);
                changeImportAndIncludeLocations(wsdlImport.getDefinition());

⌨️ 快捷键说明

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