Search in sources :

Example 1 with URITemplateParam

use of org.wso2.carbon.apimgt.core.models.URITemplateParam in project carbon-apimgt by wso2.

the class APIDefinitionFromSwagger20 method generateSwaggerFromResources.

/**
 * generate the swagger from uri templates.
 *
 * @param api API Object
 * @return Generated swagger resources as string.
 */
@Override
public String generateSwaggerFromResources(API.APIBuilder api) {
    Swagger swagger = new Swagger();
    Info info = new Info();
    info.setTitle(api.getName());
    info.setDescription(api.getDescription());
    Contact contact = new Contact();
    if (api.getBusinessInformation() != null) {
        BusinessInformation businessInformation = api.getBusinessInformation();
        contact.setName(businessInformation.getBusinessOwner());
        contact.setEmail(businessInformation.getBusinessOwnerEmail());
    }
    info.setContact(contact);
    info.setVersion(api.getVersion());
    swagger.setInfo(info);
    addSecuritySchemeToSwaggerDefinition(swagger, api.build());
    Map<String, Path> stringPathMap = new HashMap();
    for (UriTemplate uriTemplate : api.getUriTemplates().values()) {
        String uriTemplateString = uriTemplate.getUriTemplate();
        List<Parameter> parameterList = getParameters(uriTemplateString);
        if (uriTemplate.getParameters() == null || uriTemplate.getParameters().isEmpty()) {
            if (!HttpMethod.GET.toString().equalsIgnoreCase(uriTemplate.getHttpVerb()) && !HttpMethod.DELETE.toString().equalsIgnoreCase(uriTemplate.getHttpVerb()) && !HttpMethod.OPTIONS.toString().equalsIgnoreCase(uriTemplate.getHttpVerb()) && !HttpMethod.HEAD.toString().equalsIgnoreCase(uriTemplate.getHttpVerb())) {
                parameterList.add(getDefaultBodyParameter());
            }
        } else {
            for (URITemplateParam uriTemplateParam : uriTemplate.getParameters()) {
                Parameter parameter = getParameterFromURITemplateParam(uriTemplateParam);
                parameterList.add(parameter);
            }
        }
        Operation operation = new Operation();
        operation.setParameters(parameterList);
        operation.setOperationId(uriTemplate.getTemplateId());
        // having content types like */* can break swagger definition
        if (!StringUtils.isEmpty(uriTemplate.getContentType()) && !uriTemplate.getContentType().contains("*")) {
            List<String> consumesList = new ArrayList<>();
            consumesList.add(uriTemplate.getContentType());
            operation.setConsumes(consumesList);
        }
        operation.addResponse("200", getDefaultResponse());
        if (!APIMgtConstants.AUTH_NO_AUTHENTICATION.equals(uriTemplate.getAuthType()) && ((api.getSecurityScheme() & 2) == 2)) {
            log.debug("API security scheme : API Key Scheme ---- Resource Auth Type : Not None");
            operation.addSecurity(APIMgtConstants.SWAGGER_APIKEY, null);
        }
        if (!APIMgtConstants.AUTH_NO_AUTHENTICATION.equals(uriTemplate.getAuthType()) && ((api.getSecurityScheme() & 1) == 1)) {
            log.debug("API security scheme : Oauth Scheme ---- Resource Auth Type : Not None");
            operation.addSecurity(APIMgtConstants.SWAGGER_OAUTH2, null);
        }
        if (stringPathMap.containsKey(uriTemplateString)) {
            Path path = stringPathMap.get(uriTemplateString);
            path.set(uriTemplate.getHttpVerb().toLowerCase(), operation);
        } else {
            Path path = new Path();
            path.set(uriTemplate.getHttpVerb().toLowerCase(), operation);
            stringPathMap.put(uriTemplateString, path);
        }
    }
    swagger.setPaths(stringPathMap);
    swagger.setPaths(stringPathMap);
    return Json.pretty(swagger);
}
Also used : Path(io.swagger.models.Path) BusinessInformation(org.wso2.carbon.apimgt.core.models.BusinessInformation) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Operation(io.swagger.models.Operation) Info(io.swagger.models.Info) ServiceMethodInfo(org.wso2.msf4j.ServiceMethodInfo) UriTemplate(org.wso2.carbon.apimgt.core.models.UriTemplate) Contact(io.swagger.models.Contact) Swagger(io.swagger.models.Swagger) FormParameter(io.swagger.models.parameters.FormParameter) PathParameter(io.swagger.models.parameters.PathParameter) Parameter(io.swagger.models.parameters.Parameter) QueryParameter(io.swagger.models.parameters.QueryParameter) BodyParameter(io.swagger.models.parameters.BodyParameter) URITemplateParam(org.wso2.carbon.apimgt.core.models.URITemplateParam)

Example 2 with URITemplateParam

use of org.wso2.carbon.apimgt.core.models.URITemplateParam in project carbon-apimgt by wso2.

the class APIMWSDLUtils method getUriTemplatesForWSDLOperations.

/**
 * Generates URI templates to be assigned to an API from a set of operations extracted from WSDL.
 *
 * @param operations a Set of {@link WSDLOperation} objects
 * @return Map of URI Templates
 */
public static Map<String, UriTemplate> getUriTemplatesForWSDLOperations(Set<WSDLOperation> operations, boolean isHttpBinding) {
    Map<String, UriTemplate> uriTemplateMap = new HashMap<>();
    // add default "POST /" operation if no http binding methods required or operations are not provided
    if (!isHttpBinding || operations == null || operations.isEmpty()) {
        if (log.isDebugEnabled()) {
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append("Adding the POST / operation. ");
            stringBuilder.append("isHttpBinding: ").append(isHttpBinding).append(". ");
            stringBuilder.append("operations are null?: ").append(operations == null).append(". ");
            if (operations != null) {
                stringBuilder.append("operations are empty?: ").append(operations.isEmpty()).append(". ");
            }
            log.debug(stringBuilder.toString());
        }
        UriTemplate.UriTemplateBuilder builderForPOSTRootCtx = new UriTemplate.UriTemplateBuilder();
        builderForPOSTRootCtx.uriTemplate("/");
        builderForPOSTRootCtx.httpVerb("POST");
        builderForPOSTRootCtx.policy(APIUtils.getDefaultAPIPolicy());
        builderForPOSTRootCtx.templateId(APIUtils.generateOperationIdFromPath("/", "POST"));
        uriTemplateMap.put(builderForPOSTRootCtx.getTemplateId(), builderForPOSTRootCtx.build());
        return uriTemplateMap;
    }
    // add URI templates for operations
    for (WSDLOperation operation : operations) {
        if (log.isDebugEnabled()) {
            log.debug("Adding URI template for WSDL operation: " + operation.getVerb() + ", " + operation.getURI());
        }
        UriTemplate.UriTemplateBuilder builder = new UriTemplate.UriTemplateBuilder();
        builder.uriTemplate(operation.getURI().startsWith("/") ? operation.getURI() : "/" + operation.getURI());
        builder.httpVerb(operation.getVerb());
        builder.policy(APIUtils.getDefaultAPIPolicy());
        builder.templateId(APIUtils.generateOperationIdFromPath(builder.getUriTemplate(), operation.getVerb()));
        builder.contentType(operation.getContentType());
        List<URITemplateParam> uriTemplateParams = getUriTemplatesParamsForWSDLOperationParams(operation.getParameters());
        builder.parameters(uriTemplateParams);
        uriTemplateMap.put(builder.getTemplateId(), builder.build());
    }
    return uriTemplateMap;
}
Also used : HashMap(java.util.HashMap) WSDLOperation(org.wso2.carbon.apimgt.core.models.WSDLOperation) URITemplateParam(org.wso2.carbon.apimgt.core.models.URITemplateParam) UriTemplate(org.wso2.carbon.apimgt.core.models.UriTemplate)

Aggregations

HashMap (java.util.HashMap)2 URITemplateParam (org.wso2.carbon.apimgt.core.models.URITemplateParam)2 UriTemplate (org.wso2.carbon.apimgt.core.models.UriTemplate)2 Contact (io.swagger.models.Contact)1 Info (io.swagger.models.Info)1 Operation (io.swagger.models.Operation)1 Path (io.swagger.models.Path)1 Swagger (io.swagger.models.Swagger)1 BodyParameter (io.swagger.models.parameters.BodyParameter)1 FormParameter (io.swagger.models.parameters.FormParameter)1 Parameter (io.swagger.models.parameters.Parameter)1 PathParameter (io.swagger.models.parameters.PathParameter)1 QueryParameter (io.swagger.models.parameters.QueryParameter)1 ArrayList (java.util.ArrayList)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 BusinessInformation (org.wso2.carbon.apimgt.core.models.BusinessInformation)1 WSDLOperation (org.wso2.carbon.apimgt.core.models.WSDLOperation)1 ServiceMethodInfo (org.wso2.msf4j.ServiceMethodInfo)1