use of org.wso2.ballerinalang.compiler.semantics.model.iterable.Operation in project carbon-apimgt by wso2.
the class CompositeApisApiServiceImpl method compositeApisApiIdDedicatedGatewayGet.
/**
* Retrieve Dedicated Gateway of an API
*
* @param apiId UUID of API
* @param ifNoneMatch ifNoneMatch header value
* @param ifModifiedSince If-Modified-Since header value
* @param request msf4j request object
* @return 200 OK if the operation was successful
* @throws NotFoundException when the particular resource does not exist
*/
@Override
public Response compositeApisApiIdDedicatedGatewayGet(String apiId, String ifNoneMatch, String ifModifiedSince, Request request) throws NotFoundException {
String username = RestApiUtil.getLoggedInUsername(request);
try {
APIStore apiStore = RestApiUtil.getConsumer(username);
if (!apiStore.isCompositeAPIExist(apiId)) {
String errorMessage = "API not found : " + apiId;
APIMgtResourceNotFoundException e = new APIMgtResourceNotFoundException(errorMessage, ExceptionCodes.API_NOT_FOUND);
HashMap<String, String> paramList = new HashMap<String, String>();
paramList.put(APIMgtConstants.ExceptionsConstants.API_ID, apiId);
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
}
String existingFingerprint = compositeApisApiIdGetFingerprint(apiId, ifNoneMatch, ifModifiedSince, request);
if (!StringUtils.isEmpty(ifNoneMatch) && !StringUtils.isEmpty(existingFingerprint) && ifNoneMatch.contains(existingFingerprint)) {
return Response.notModified().build();
}
DedicatedGateway dedicatedGateway = apiStore.getDedicatedGateway(apiId);
if (dedicatedGateway != null) {
DedicatedGatewayDTO dedicatedGatewayDTO = DedicatedGatewayMappingUtil.toDedicatedGatewayDTO(dedicatedGateway);
return Response.ok().header(HttpHeaders.ETAG, "\"" + existingFingerprint + "\"").entity(dedicatedGatewayDTO).build();
} else {
String msg = "Dedicated Gateway not found for " + apiId;
APIMgtResourceNotFoundException e = new APIMgtResourceNotFoundException(msg, ExceptionCodes.DEDICATED_GATEWAY_DETAILS_NOT_FOUND);
HashMap<String, String> paramList = new HashMap<String, String>();
paramList.put(APIMgtConstants.ExceptionsConstants.API_ID, apiId);
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
log.error(msg, e);
return Response.status(Response.Status.NOT_FOUND).entity(errorDTO).build();
}
} catch (APIManagementException e) {
String errorMessage = "Error while retrieving dedicated gateway of the API : " + apiId;
HashMap<String, String> paramList = new HashMap<String, String>();
paramList.put(APIMgtConstants.ExceptionsConstants.API_ID, apiId);
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
log.error(errorMessage, e);
return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
}
}
use of org.wso2.ballerinalang.compiler.semantics.model.iterable.Operation in project carbon-apimgt by wso2.
the class CompositeApisApiServiceImpl method compositeApisApiIdDedicatedGatewayPut.
/**
* Add or update Dedicated Gateway of an API
*
* @param apiId UUID of API
* @param body DedicatedGatewayDTO
* @param ifMatch If-Match header value
* @param ifUnmodifiedSince If-Unmodified-Since header value
* @param request msf4j request object
* @return 200 OK if the operation was successful
* @throws NotFoundException when the particular resource does not exist
*/
@Override
public Response compositeApisApiIdDedicatedGatewayPut(String apiId, DedicatedGatewayDTO body, String ifMatch, String ifUnmodifiedSince, Request request) throws NotFoundException {
String username = RestApiUtil.getLoggedInUsername(request);
try {
APIStore apiStore = RestApiUtil.getConsumer(username);
if (!apiStore.isCompositeAPIExist(apiId)) {
String errorMessage = "API not found : " + apiId;
APIMgtResourceNotFoundException e = new APIMgtResourceNotFoundException(errorMessage, ExceptionCodes.API_NOT_FOUND);
HashMap<String, String> paramList = new HashMap<String, String>();
paramList.put(APIMgtConstants.ExceptionsConstants.API_ID, apiId);
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
log.error(errorMessage, e);
return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
}
String existingFingerprint = compositeApisApiIdGetFingerprint(apiId, null, null, request);
if (!StringUtils.isEmpty(ifMatch) && !StringUtils.isEmpty(existingFingerprint) && !ifMatch.contains(existingFingerprint)) {
return Response.status(Response.Status.PRECONDITION_FAILED).build();
}
DedicatedGateway dedicatedGateway = DedicatedGatewayMappingUtil.fromDTOtoDedicatedGateway(body, apiId, username);
apiStore.updateDedicatedGateway(dedicatedGateway);
DedicatedGateway updatedDedicatedGateway = apiStore.getDedicatedGateway(apiId);
String newFingerprint = compositeApisApiIdGetFingerprint(apiId, null, null, request);
return Response.ok().header(HttpHeaders.ETAG, "\"" + newFingerprint + "\"").entity(DedicatedGatewayMappingUtil.toDedicatedGatewayDTO(updatedDedicatedGateway)).build();
} catch (APIManagementException e) {
String errorMessage = "Error while updating dedicated gateway of the composite API : " + apiId;
HashMap<String, String> paramList = new HashMap<String, String>();
paramList.put(APIMgtConstants.ExceptionsConstants.API_ID, apiId);
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
log.error(errorMessage, e);
return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
}
}
use of org.wso2.ballerinalang.compiler.semantics.model.iterable.Operation in project carbon-apimgt by wso2.
the class APIDefinitionFromSwagger20 method parseSwaggerAPIResources.
@Override
public List<APIResource> parseSwaggerAPIResources(StringBuilder resourceConfigsJSON) throws APIManagementException {
List<APIResource> apiResources = new ArrayList<>();
SwaggerParser swaggerParser = new SwaggerParser();
Swagger swagger = swaggerParser.parse(resourceConfigsJSON.toString());
Map<String, Path> resourceList = swagger.getPaths();
String securityName = getOauthSecurityName(swagger);
if (swagger.getSecurityDefinitions() != null) {
for (Map.Entry<String, Path> resourceEntry : resourceList.entrySet()) {
Path resource = resourceEntry.getValue();
UriTemplate.UriTemplateBuilder uriTemplateBuilder = new UriTemplate.UriTemplateBuilder();
uriTemplateBuilder.uriTemplate(resourceEntry.getKey());
for (Map.Entry<HttpMethod, Operation> operationEntry : resource.getOperationMap().entrySet()) {
APIResource.Builder apiResourceBuilder = setApiResourceBuilderProperties(operationEntry, uriTemplateBuilder, resourceEntry.getKey());
List<Map<String, List<String>>> security = operationEntry.getValue().getSecurity();
if (security != null) {
for (Map<String, List<String>> securityMap : security) {
if (securityMap.containsKey(securityName)) {
apiResourceBuilder.scopes(securityMap.get(securityName));
}
}
}
uriTemplateBuilder.httpVerb(operationEntry.getKey().name());
apiResourceBuilder.uriTemplate(uriTemplateBuilder.build());
apiResources.add(apiResourceBuilder.build());
}
}
}
resourceConfigsJSON.setLength(0);
resourceConfigsJSON.append(Json.pretty(swagger));
return apiResources;
}
use of org.wso2.ballerinalang.compiler.semantics.model.iterable.Operation in project carbon-apimgt by wso2.
the class APIDefinitionFromSwagger20 method generateMergedResourceDefinition.
@Override
public String generateMergedResourceDefinition(String resourceConfigJson, API api) {
SwaggerParser swaggerParser = new SwaggerParser();
Swagger swagger = swaggerParser.parse(resourceConfigJson);
addSecuritySchemeToSwaggerDefinition(swagger, api);
String securityName = getOauthSecurityName(swagger);
if (!StringUtils.isEmpty(securityName)) {
List<SecurityRequirement> securityRequirements = swagger.getSecurity();
if (securityRequirements != null) {
for (SecurityRequirement securityRequirement : securityRequirements) {
Map<String, List<String>> requirementMap = securityRequirement.getRequirements();
if (requirementMap.containsKey(securityName)) {
requirementMap.replace(securityName, api.getScopes());
} else {
if (!api.getScopes().isEmpty()) {
requirementMap.put(securityName, api.getScopes());
}
}
}
} else {
if (!api.getScopes().isEmpty()) {
SecurityRequirement securityRequirement = new SecurityRequirement();
securityRequirement.setRequirements(securityName, api.getScopes());
swagger.addSecurity(securityRequirement);
}
}
Map<String, UriTemplate> uriTemplateMap = new HashMap<>(api.getUriTemplates());
swagger.getPaths().entrySet().removeIf(entry -> isPathNotExist(uriTemplateMap, securityName, entry));
uriTemplateMap.forEach((k, v) -> {
Path path = swagger.getPath(v.getUriTemplate());
if (path == null) {
path = new Path();
swagger.path(v.getUriTemplate(), path);
}
Map<HttpMethod, Operation> operationMap = path.getOperationMap();
Operation operation = operationMap.get(getHttpMethodForVerb(v.getHttpVerb().toUpperCase()));
if (operation != null) {
assignScopesToOperation(operation, securityName, v.getScopes());
} else {
Operation operationTocCreate = new Operation();
operationTocCreate.addSecurity(securityName, v.getScopes());
operationTocCreate.addResponse("200", getDefaultResponse());
List<Parameter> parameterList = getParameters(v.getUriTemplate());
if (!HttpMethod.GET.toString().equalsIgnoreCase(v.getHttpVerb()) && !HttpMethod.DELETE.toString().equalsIgnoreCase(v.getHttpVerb()) && !HttpMethod.OPTIONS.toString().equalsIgnoreCase(v.getHttpVerb()) && !HttpMethod.HEAD.toString().equalsIgnoreCase(v.getHttpVerb())) {
parameterList.add(getDefaultBodyParameter());
}
operationTocCreate.setParameters(parameterList);
path.set(v.getHttpVerb().toLowerCase(), operationTocCreate);
}
});
}
return Json.pretty(swagger);
}
use of org.wso2.ballerinalang.compiler.semantics.model.iterable.Operation 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);
}
Aggregations