Search in sources :

Example 6 with WSDLSOAPOperation

use of org.wso2.carbon.apimgt.impl.wsdl.model.WSDLSOAPOperation in project carbon-apimgt by wso2.

the class SOAPOperationBindingUtils method getGeneratedSwaggerFromWSDL.

/**
 * Generate the swagger from the WSDL info
 *
 * @param wsdlInfo WSDLInfo object which has parsed WSDL data
 * @return Generated the swagger from the WSDL info
 * @throws APIManagementException if an error occurs when generating swagger
 */
private static String getGeneratedSwaggerFromWSDL(WSDLInfo wsdlInfo) throws APIManagementException {
    Set<WSDLSOAPOperation> operations;
    Map<String, ModelImpl> paramModelMap;
    String swaggerStr = SOAPToRESTConstants.EMPTY_STRING;
    operations = wsdlInfo.getSoapBindingOperations();
    paramModelMap = wsdlInfo.getParameterModelMap();
    populateSoapOperationParameters(operations);
    Swagger swaggerDoc = new Swagger();
    for (WSDLSOAPOperation operation : operations) {
        Path path = new Path();
        Operation op = new Operation();
        List<ModelImpl> inputParameterModel = operation.getInputParameterModel();
        List<ModelImpl> outputParameterModel = operation.getOutputParameterModel();
        if (HTTPConstants.HTTP_METHOD_GET.equals(operation.getHttpVerb())) {
            for (ModelImpl input : inputParameterModel) {
                if (input != null && operation.getName().equalsIgnoreCase(input.getName())) {
                    Map<String, Property> properties = input.getProperties();
                    if (properties != null) {
                        for (String property : properties.keySet()) {
                            QueryParameter param = new QueryParameter();
                            param.setName(property);
                            param.setType(properties.get(property).getType());
                            op.addParameter(param);
                        }
                    }
                    inputParameterModel.remove(input);
                    break;
                }
            }
        } else {
            // adding body parameter
            BodyParameter param = new BodyParameter();
            param.setName(APIConstants.OperationParameter.PAYLOAD_PARAM_NAME);
            param.setIn(APIConstants.OperationParameter.PAYLOAD_PARAM_TYPE);
            param.setRequired(true);
            RefModel model = new RefModel();
            model.set$ref(SOAPToRESTConstants.Swagger.DEFINITIONS_ROOT + operation.getName() + SOAPToRESTConstants.Swagger.INPUT_POSTFIX);
            param.setSchema(model);
            op.addParameter(param);
        }
        // adding response
        Response response = new Response();
        RefProperty refProperty = new RefProperty();
        refProperty.set$ref(SOAPToRESTConstants.Swagger.DEFINITIONS_ROOT + operation.getName() + SOAPToRESTConstants.Swagger.OUTPUT_POSTFIX);
        response.setSchema(refProperty);
        response.setDescription(SOAPToRESTConstants.EMPTY_STRING);
        op.addResponse("default", response);
        op.setOperationId(operation.getSoapBindingOpName());
        // setting vendor extensions
        Map<String, String> extensions = new HashMap<>();
        extensions.put(SOAPToRESTConstants.Swagger.SOAP_ACTION, operation.getSoapAction());
        extensions.put(SOAPToRESTConstants.Swagger.SOAP_OPERATION, operation.getSoapBindingOpName());
        extensions.put(SOAPToRESTConstants.Swagger.NAMESPACE, operation.getTargetNamespace());
        if (wsdlInfo.isHasSoap12BindingOperations()) {
            extensions.put(SOAPToRESTConstants.Swagger.SOAP_VERSION, SOAPToRESTConstants.SOAP_VERSION_12);
        } else if (wsdlInfo.hasSoapBindingOperations()) {
            extensions.put(SOAPToRESTConstants.Swagger.SOAP_VERSION, SOAPToRESTConstants.SOAP_VERSION_11);
        }
        extensions.put(SOAPToRESTConstants.Swagger.SOAP_STYLE, operation.getStyle());
        extensions.put(SOAPToRESTConstants.Swagger.SOAP_MESSAGE_TYPE, operation.getMessageType());
        op.setVendorExtension(SOAPToRESTConstants.Swagger.WSO2_SOAP, extensions);
        if (!HTTPConstants.HTTP_METHOD_GET.equals(operation.getHttpVerb())) {
            ModelImpl inputModel = new ModelImpl();
            inputModel.setName(operation.getName() + SOAPToRESTConstants.Swagger.INPUT_POSTFIX);
            inputModel.setType(ObjectProperty.TYPE);
            Map<String, Property> inputPropertyMap = new HashMap<>();
            for (ModelImpl input : inputParameterModel) {
                if (input != null && input.getProperties() != null) {
                    RefProperty inputRefProp;
                    if (input.getProperties().containsKey(input.getName())) {
                        inputRefProp = (RefProperty) input.getProperties().get(input.getName());
                    } else {
                        inputRefProp = new RefProperty();
                        inputRefProp.set$ref(SOAPToRESTConstants.Swagger.DEFINITIONS_ROOT + input.getName());
                    }
                    inputPropertyMap.put(input.getName(), inputRefProp);
                }
            }
            inputModel.setProperties(inputPropertyMap);
            swaggerDoc.addDefinition(operation.getName() + SOAPToRESTConstants.Swagger.INPUT_POSTFIX, inputModel);
        }
        ModelImpl outputModel = new ModelImpl();
        outputModel.setName(operation.getName() + SOAPToRESTConstants.Swagger.OUTPUT_POSTFIX);
        outputModel.setType(ObjectProperty.TYPE);
        Map<String, Property> outputPropertyMap = new HashMap<>();
        for (ModelImpl output : outputParameterModel) {
            if (output != null && output.getProperties() != null) {
                RefProperty outputRefProp;
                if (output.getProperties().containsKey(output.getName())) {
                    outputRefProp = (RefProperty) output.getProperties().get(output.getName());
                } else {
                    outputRefProp = new RefProperty();
                    outputRefProp.set$ref(SOAPToRESTConstants.Swagger.DEFINITIONS_ROOT + output.getName());
                }
                outputPropertyMap.put(output.getName(), outputRefProp);
            }
        }
        outputModel.setProperties(outputPropertyMap);
        swaggerDoc.addDefinition(operation.getName() + SOAPToRESTConstants.Swagger.OUTPUT_POSTFIX, outputModel);
        path.set(operation.getHttpVerb().toLowerCase(), op);
        swaggerDoc.path("/" + operation.getName(), path);
        Info info = new Info();
        info.setVersion(SOAPToRESTConstants.EMPTY_STRING);
        info.setTitle(SOAPToRESTConstants.EMPTY_STRING);
        swaggerDoc.info(info);
    }
    if (paramModelMap != null) {
        for (String propertyName : paramModelMap.keySet()) {
            swaggerDoc.addDefinition(propertyName, paramModelMap.get(propertyName));
        }
    }
    try {
        swaggerStr = Json.pretty(swaggerDoc);
    } catch (Exception e) {
        String msg = "Error occurred while deserialize swagger model.";
        handleException(msg, e);
    }
    if (log.isDebugEnabled()) {
        log.debug(swaggerStr);
    }
    return swaggerStr;
}
Also used : Path(io.swagger.models.Path) QueryParameter(io.swagger.models.parameters.QueryParameter) RefModel(io.swagger.models.RefModel) HashMap(java.util.HashMap) Operation(io.swagger.models.Operation) WSDLSOAPOperation(org.wso2.carbon.apimgt.impl.wsdl.model.WSDLSOAPOperation) BodyParameter(io.swagger.models.parameters.BodyParameter) Info(io.swagger.models.Info) WSDLInfo(org.wso2.carbon.apimgt.impl.wsdl.model.WSDLInfo) APIUtil.handleException(org.wso2.carbon.apimgt.impl.utils.APIUtil.handleException) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException) APIMgtWSDLException(org.wso2.carbon.apimgt.impl.wsdl.exceptions.APIMgtWSDLException) UserStoreException(org.wso2.carbon.user.api.UserStoreException) MalformedURLException(java.net.MalformedURLException) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) RefProperty(io.swagger.models.properties.RefProperty) Response(io.swagger.models.Response) Swagger(io.swagger.models.Swagger) WSDLSOAPOperation(org.wso2.carbon.apimgt.impl.wsdl.model.WSDLSOAPOperation) ModelImpl(io.swagger.models.ModelImpl) ArrayProperty(io.swagger.models.properties.ArrayProperty) Property(io.swagger.models.properties.Property) RefProperty(io.swagger.models.properties.RefProperty) ObjectProperty(io.swagger.models.properties.ObjectProperty)

Example 7 with WSDLSOAPOperation

use of org.wso2.carbon.apimgt.impl.wsdl.model.WSDLSOAPOperation in project carbon-apimgt by wso2.

the class WSDLSOAPOperationExtractorImplTestCase method testGetSwaggerModelForImportedSchemas.

@Test
public void testGetSwaggerModelForImportedSchemas() throws Exception {
    APIMWSDLReader wsdlReader = new APIMWSDLReader(Thread.currentThread().getContextClassLoader().getResource("wsdls/import-schemas/sampleservice.wsdl").toExternalForm());
    byte[] wsdlContent = wsdlReader.getWSDL();
    WSDL11SOAPOperationExtractor processor = SOAPOperationBindingUtils.getWSDL11SOAPOperationExtractor(wsdlContent, wsdlReader);
    Set<WSDLSOAPOperation> operations = processor.getWsdlInfo().getSoapBindingOperations();
    Assert.assertNotNull(operations);
    Map<String, ModelImpl> parameterModelMap = processor.getWsdlInfo().getParameterModelMap();
    Assert.assertNotNull(parameterModelMap);
}
Also used : APIMWSDLReader(org.wso2.carbon.apimgt.impl.utils.APIMWSDLReader) WSDLSOAPOperation(org.wso2.carbon.apimgt.impl.wsdl.model.WSDLSOAPOperation) ModelImpl(io.swagger.models.ModelImpl) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 8 with WSDLSOAPOperation

use of org.wso2.carbon.apimgt.impl.wsdl.model.WSDLSOAPOperation in project carbon-apimgt by wso2.

the class WSDLSOAPOperationExtractorImplTestCase method testGetSwaggerModelForWSDLComplexTypeElement.

@Test
public void testGetSwaggerModelForWSDLComplexTypeElement() throws Exception {
    for (WSDLSOAPOperation operation : operations) {
        List<ModelImpl> inputParameterModel = operation.getInputParameterModel();
        for (ModelImpl model : inputParameterModel) {
            Map<String, Property> properties = model.getProperties();
            for (String property : properties.keySet()) {
                Property currentProp = properties.get(property);
                if ("CheckPhoneNumber".equals(model.getName())) {
                    Assert.assertTrue("PhoneNumber".equals(currentProp.getName()) || "LicenseKey".equals(currentProp.getName()));
                    Assert.assertEquals("string", currentProp.getType());
                }
                if ("CheckPhoneNumbers".equals(model.getName())) {
                    Assert.assertTrue("PhoneNumbers".equals(currentProp.getName()) || "LicenseKey".equals(currentProp.getName()));
                    if ("PhoneNumbers".equals(currentProp.getName())) {
                        Assert.assertEquals("ref", currentProp.getType());
                    } else if ("LicenceKey".equals(currentProp.getName())) {
                        Assert.assertEquals("string", currentProp.getType());
                    }
                }
            }
        }
    }
}
Also used : WSDLSOAPOperation(org.wso2.carbon.apimgt.impl.wsdl.model.WSDLSOAPOperation) ModelImpl(io.swagger.models.ModelImpl) ArrayProperty(io.swagger.models.properties.ArrayProperty) ObjectProperty(io.swagger.models.properties.ObjectProperty) Property(io.swagger.models.properties.Property) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 9 with WSDLSOAPOperation

use of org.wso2.carbon.apimgt.impl.wsdl.model.WSDLSOAPOperation in project carbon-apimgt by wso2.

the class WSDL11SOAPOperationExtractor method getSOAPBindingOperations.

/**
 * Retrieves all the operations defined in the provided Binding.
 *
 * @param binding WSDL binding
 * @return a set of {@link WSDLOperation} defined in the provided Binding
 */
private Set<WSDLSOAPOperation> getSOAPBindingOperations(Binding binding) throws APIMgtWSDLException {
    Set<WSDLSOAPOperation> allBindingOperations = new HashSet<>();
    if (binding.getExtensibilityElements() != null && binding.getExtensibilityElements().size() > 0) {
        List extensibilityElements = binding.getExtensibilityElements();
        for (Object extensibilityElement : extensibilityElements) {
            if (extensibilityElement instanceof SOAPBinding || extensibilityElement instanceof SOAP12Binding) {
                for (Object opObj : binding.getBindingOperations()) {
                    BindingOperation bindingOperation = (BindingOperation) opObj;
                    WSDLSOAPOperation wsdlSoapOperation = getSOAPOperation(bindingOperation);
                    if (wsdlSoapOperation != null) {
                        allBindingOperations.add(wsdlSoapOperation);
                    } else {
                        log.warn("Unable to get soap operation details from binding operation: " + bindingOperation.getName());
                    }
                }
            }
        }
    } else {
        throw new APIMgtWSDLException("Cannot further process to get soap binding operations");
    }
    return allBindingOperations;
}
Also used : SOAP12Binding(javax.wsdl.extensions.soap12.SOAP12Binding) BindingOperation(javax.wsdl.BindingOperation) APIMgtWSDLException(org.wso2.carbon.apimgt.impl.wsdl.exceptions.APIMgtWSDLException) SOAPBinding(javax.wsdl.extensions.soap.SOAPBinding) List(java.util.List) ArrayList(java.util.ArrayList) NodeList(org.w3c.dom.NodeList) WSDLSOAPOperation(org.wso2.carbon.apimgt.impl.wsdl.model.WSDLSOAPOperation) HashSet(java.util.HashSet)

Aggregations

WSDLSOAPOperation (org.wso2.carbon.apimgt.impl.wsdl.model.WSDLSOAPOperation)9 ModelImpl (io.swagger.models.ModelImpl)4 ArrayProperty (io.swagger.models.properties.ArrayProperty)3 ObjectProperty (io.swagger.models.properties.ObjectProperty)3 Property (io.swagger.models.properties.Property)3 Test (org.junit.Test)3 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)3 APIMgtWSDLException (org.wso2.carbon.apimgt.impl.wsdl.exceptions.APIMgtWSDLException)3 RefProperty (io.swagger.models.properties.RefProperty)2 HashSet (java.util.HashSet)2 List (java.util.List)2 SOAPBinding (javax.wsdl.extensions.soap.SOAPBinding)2 SOAP12Binding (javax.wsdl.extensions.soap12.SOAP12Binding)2 NodeList (org.w3c.dom.NodeList)2 APIMWSDLReader (org.wso2.carbon.apimgt.impl.utils.APIMWSDLReader)2 WSDLInfo (org.wso2.carbon.apimgt.impl.wsdl.model.WSDLInfo)2 Info (io.swagger.models.Info)1 Operation (io.swagger.models.Operation)1 Path (io.swagger.models.Path)1 RefModel (io.swagger.models.RefModel)1