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);
}
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;
}
Aggregations