use of org.wso2.carbon.apimgt.core.models.CorsConfiguration in project carbon-apimgt by wso2.
the class MappingUtil method toAPI.
/**
* This method converts the API model object from the DTO object.
*
* @param apidto APIDTO object with API data
* @return APIBuilder object
*/
public static API.APIBuilder toAPI(APIDTO apidto) throws JsonProcessingException {
BusinessInformation businessInformation = new BusinessInformation();
API_businessInformationDTO apiBusinessInformationDTO = apidto.getBusinessInformation();
if (apiBusinessInformationDTO != null) {
businessInformation.setBusinessOwner(apiBusinessInformationDTO.getBusinessOwner());
businessInformation.setBusinessOwnerEmail(apiBusinessInformationDTO.getBusinessOwnerEmail());
businessInformation.setTechnicalOwner(apiBusinessInformationDTO.getTechnicalOwner());
businessInformation.setTechnicalOwnerEmail(apiBusinessInformationDTO.getTechnicalOwnerEmail());
}
API_corsConfigurationDTO apiCorsConfigurationDTO = apidto.getCorsConfiguration();
CorsConfiguration corsConfiguration = new CorsConfiguration();
if (apiCorsConfigurationDTO != null) {
corsConfiguration.setAllowCredentials(apiCorsConfigurationDTO.getAccessControlAllowCredentials());
corsConfiguration.setAllowHeaders(apiCorsConfigurationDTO.getAccessControlAllowHeaders());
corsConfiguration.setAllowMethods(apiCorsConfigurationDTO.getAccessControlAllowMethods());
corsConfiguration.setAllowOrigins(apiCorsConfigurationDTO.getAccessControlAllowOrigins());
corsConfiguration.setEnabled(apiCorsConfigurationDTO.getCorsConfigurationEnabled());
}
List<API_operationsDTO> operationList = apidto.getOperations();
Map<String, UriTemplate> uriTemplateList = new HashMap<>();
for (API_operationsDTO operationsDTO : operationList) {
UriTemplate.UriTemplateBuilder uriTemplateBuilder = new UriTemplate.UriTemplateBuilder();
uriTemplateBuilder.uriTemplate(operationsDTO.getUritemplate());
uriTemplateBuilder.authType(operationsDTO.getAuthType());
uriTemplateBuilder.httpVerb(operationsDTO.getHttpVerb());
uriTemplateBuilder.policy(new APIPolicy(operationsDTO.getPolicy()));
uriTemplateBuilder.scopes(operationsDTO.getScopes());
if (operationsDTO.getEndpoint() != null && !operationsDTO.getEndpoint().isEmpty()) {
uriTemplateBuilder.endpoint(fromEndpointListToMap(operationsDTO.getEndpoint()));
}
if (operationsDTO.getId() != null) {
uriTemplateBuilder.templateId(operationsDTO.getId());
} else {
uriTemplateBuilder.templateId(APIUtils.generateOperationIdFromPath(operationsDTO.getUritemplate(), operationsDTO.getHttpVerb()));
}
uriTemplateList.put(uriTemplateBuilder.getTemplateId(), uriTemplateBuilder.build());
}
Set<Policy> subscriptionPolicies = new HashSet<>();
apidto.getPolicies().forEach(v -> subscriptionPolicies.add(new SubscriptionPolicy(v)));
API.APIBuilder apiBuilder = new API.APIBuilder(apidto.getProvider(), apidto.getName(), apidto.getVersion()).id(apidto.getId()).context(apidto.getContext()).description(apidto.getDescription()).lifeCycleStatus(apidto.getLifeCycleStatus()).endpoint(fromEndpointListToMap(apidto.getEndpoint())).visibleRoles(new HashSet<>(apidto.getVisibleRoles())).policies(subscriptionPolicies).apiPermission(apidto.getPermission()).tags(new HashSet<>(apidto.getTags())).labels(new ArrayList<>(apidto.getLabels())).transport(new HashSet<>(apidto.getTransport())).isResponseCachingEnabled(Boolean.valueOf(apidto.getResponseCaching())).businessInformation(businessInformation).uriTemplates(uriTemplateList).corsConfiguration(corsConfiguration).wsdlUri(apidto.getWsdlUri()).scopes(apidto.getScopes()).securityScheme(mapSecuritySchemeListToInt(apidto.getSecurityScheme()));
if (apidto.getIsDefaultVersion() != null) {
apiBuilder.isDefaultVersion(apidto.getIsDefaultVersion());
}
if (apidto.getVisibility() != null) {
apiBuilder.visibility(API.Visibility.valueOf(apidto.getVisibility().toString()));
}
if (apidto.getCacheTimeout() != null) {
apiBuilder.cacheTimeout(apidto.getCacheTimeout());
}
if (apidto.getApiPolicy() != null) {
Policy policy = new APIPolicy(apidto.getApiPolicy());
apiBuilder.apiPolicy(policy);
}
if (apidto.getHasOwnGateway() != null) {
apiBuilder.hasOwnGateway(apidto.getHasOwnGateway());
}
if (apidto.getThreatProtectionPolicies() != null) {
API_threatProtectionPoliciesDTO threatProtectionPoliciesDTO = apidto.getThreatProtectionPolicies();
List<API_threatProtectionPolicies_listDTO> threatProtectionPolicies_listDTO = threatProtectionPoliciesDTO.getList();
Set<String> policyIdSet = new HashSet<>();
for (API_threatProtectionPolicies_listDTO listDTO : threatProtectionPolicies_listDTO) {
policyIdSet.add(listDTO.getPolicyId());
}
apiBuilder.threatProtectionPolicies(policyIdSet);
}
return apiBuilder;
}
use of org.wso2.carbon.apimgt.core.models.CorsConfiguration in project carbon-apimgt by wso2.
the class SampleTestObjectCreator method createDefaultAPI.
public static API.APIBuilder createDefaultAPI() {
Set<String> transport = new HashSet<>();
transport.add(HTTP);
transport.add(HTTPS);
Set<String> tags = new HashSet<>();
tags.add(TAG_CLIMATE);
Set<Policy> policies = new HashSet<>();
policies.add(goldSubscriptionPolicy);
policies.add(silverSubscriptionPolicy);
policies.add(bronzeSubscriptionPolicy);
BusinessInformation businessInformation = new BusinessInformation();
businessInformation.setBusinessOwner(NAME_BUSINESS_OWNER_1);
businessInformation.setBusinessOwnerEmail(EMAIL_BUSINESS_OWNER_1);
businessInformation.setTechnicalOwner(NAME_TECHNICAL_OWNER_1);
businessInformation.setTechnicalOwnerEmail(EMAIL_TECHNICAL_OWNER_1);
String permissionJson = "[{\"groupId\" : \"developer\", \"permission\" : " + "[\"READ\",\"UPDATE\"]},{\"groupId\" : \"admin\", \"permission\" : [\"READ\",\"UPDATE\"," + "\"DELETE\", \"MANAGE_SUBSCRIPTION\"]}]";
Set<String> visibleRoles = new HashSet<>();
visibleRoles.add("testRple");
List<String> labels = new ArrayList<>();
labels.add("testLabel");
CorsConfiguration corsConfiguration = new CorsConfiguration();
corsConfiguration.setEnabled(true);
corsConfiguration.setAllowMethods(Arrays.asList(APIMgtConstants.FunctionsConstants.GET, APIMgtConstants.FunctionsConstants.POST, APIMgtConstants.FunctionsConstants.DELETE));
corsConfiguration.setAllowHeaders(Arrays.asList(ALLOWED_HEADER_AUTHORIZATION, ALLOWED_HEADER_CUSTOM));
corsConfiguration.setAllowCredentials(true);
corsConfiguration.setAllowOrigins(Arrays.asList("*"));
Map<String, Endpoint> endpointMap = new HashMap<>();
endpointMap.put("TestEndpoint", createMockEndpoint());
API.APIBuilder apiBuilder = new API.APIBuilder(ADMIN, "WeatherAPI", API_VERSION).id(UUID.randomUUID().toString()).context("weather").description("Get Weather Info").lifeCycleStatus(APIStatus.CREATED.getStatus()).lifecycleInstanceId(UUID.randomUUID().toString()).endpoint(Collections.emptyMap()).wsdlUri("http://localhost:9443/echo?wsdl").isResponseCachingEnabled(false).cacheTimeout(60).isDefaultVersion(false).apiPolicy(unlimitedApiPolicy).transport(transport).tags(tags).policies(policies).visibility(API.Visibility.PUBLIC).visibleRoles(visibleRoles).businessInformation(businessInformation).corsConfiguration(corsConfiguration).createdTime(LocalDateTime.now()).createdBy(ADMIN).updatedBy(ADMIN).lastUpdatedTime(LocalDateTime.now()).apiPermission(permissionJson).uriTemplates(getMockUriTemplates()).apiDefinition(apiDefinition).workflowStatus(WORKFLOW_STATUS).labels(labels).endpoint(endpointMap);
Map map = new HashMap();
map.put(DEVELOPER_ROLE_ID, 6);
map.put(ADMIN_ROLE_ID, 15);
apiBuilder.permissionMap(map);
return apiBuilder;
}
use of org.wso2.carbon.apimgt.core.models.CorsConfiguration in project carbon-apimgt by wso2.
the class SampleTestObjectCreator method createUniqueAPI.
/**
* Create an unique API.
*
* @return APIBuilder with unique API details
*/
public static API.APIBuilder createUniqueAPI() {
Set<String> transport = new HashSet<>();
transport.add(HTTP);
Set<String> tags = new HashSet<>();
tags.add(TAG_FOOD);
tags.add(TAG_BEVERAGE);
Set<Policy> policies = new HashSet<>();
policies.add(silverSubscriptionPolicy);
policies.add(bronzeSubscriptionPolicy);
BusinessInformation businessInformation = new BusinessInformation();
businessInformation.setBusinessOwner(NAME_BUSINESS_OWNER_1);
businessInformation.setBusinessOwnerEmail(EMAIL_BUSINESS_OWNER_1);
businessInformation.setTechnicalOwner(NAME_BUSINESS_OWNER_2);
businessInformation.setBusinessOwnerEmail(EMAIL_BUSINESS_OWNER_2);
CorsConfiguration corsConfiguration = new CorsConfiguration();
corsConfiguration.setEnabled(true);
corsConfiguration.setAllowMethods(Arrays.asList(APIMgtConstants.FunctionsConstants.GET, APIMgtConstants.FunctionsConstants.POST, APIMgtConstants.FunctionsConstants.DELETE));
corsConfiguration.setAllowHeaders(Arrays.asList(ALLOWED_HEADER_AUTHORIZATION, ALLOWED_HEADER_CUSTOM));
corsConfiguration.setAllowCredentials(true);
corsConfiguration.setAllowOrigins(Arrays.asList("*"));
String permissionJson = "[{\"groupId\" : \"developer\", \"permission\" : " + "[\"READ\",\"UPDATE\"]},{\"groupId\" : \"admin\", \"permission\" : [\"READ\",\"UPDATE\"," + "\"DELETE\"]}]";
Map permissionMap = new HashMap();
permissionMap.put(DEVELOPER_ROLE_ID, 6);
permissionMap.put(ADMIN_ROLE_ID, 7);
API.APIBuilder apiBuilder = new API.APIBuilder(UUID.randomUUID().toString(), UUID.randomUUID().toString(), API_VERSION).id(UUID.randomUUID().toString()).context(UUID.randomUUID().toString()).description("Get Food & Beverage Info").lifeCycleStatus(APIStatus.CREATED.getStatus()).endpoint(Collections.emptyMap()).wsdlUri(SAMPLE_API_WSDL).isResponseCachingEnabled(true).cacheTimeout(120).isDefaultVersion(true).apiPolicy(goldApiPolicy).transport(transport).tags(tags).policies(policies).visibility(API.Visibility.RESTRICTED).visibleRoles(new HashSet<>(Arrays.asList(CUSTOMER_ROLE, MANAGER_ROLE, EMPLOYEE_ROLE))).businessInformation(businessInformation).corsConfiguration(corsConfiguration).apiPermission(permissionJson).permissionMap(permissionMap).createdTime(LocalDateTime.now()).createdBy(API_CREATOR).uriTemplates(Collections.emptyMap()).apiDefinition(apiDefinition).lastUpdatedTime(LocalDateTime.now()).threatProtectionPolicies(new HashSet<>(Arrays.asList("a", "b")));
return apiBuilder;
}
use of org.wso2.carbon.apimgt.core.models.CorsConfiguration in project carbon-apimgt by wso2.
the class APIPublisherImpl method addApiFromDefinition.
/**
* Create api from Definition
*
* @param apiDefinition API definition stream.
* @return UUID of the added API.
* @throws APIManagementException If failed to add the API.
*/
@Override
public String addApiFromDefinition(InputStream apiDefinition) throws APIManagementException {
try {
String apiDefinitionString = IOUtils.toString(apiDefinition);
validateScope(apiDefinitionString);
API.APIBuilder apiBuilder = apiDefinitionFromSwagger20.generateApiFromSwaggerResource(getUsername(), apiDefinitionString);
Map<String, String> scopes = apiDefinitionFromSwagger20.getScopesFromSecurityDefinition(apiDefinitionString);
for (Map.Entry<String, String> scopeEntry : scopes.entrySet()) {
getKeyManager().registerScope(new Scope(scopeEntry.getKey(), scopeEntry.getValue()));
}
apiBuilder.corsConfiguration(new CorsConfiguration());
apiBuilder.apiDefinition(apiDefinitionString);
addAPI(apiBuilder);
return apiBuilder.getId();
} catch (IOException e) {
throw new APIManagementException("Couldn't Generate ApiDefinition from file", ExceptionCodes.API_DEFINITION_MALFORMED);
}
}
use of org.wso2.carbon.apimgt.core.models.CorsConfiguration in project carbon-apimgt by wso2.
the class SampleTestObjectCreator method createDefaultAPI.
public static API.APIBuilder createDefaultAPI() {
Set<String> transport = new HashSet<>();
transport.add(HTTP);
transport.add(HTTPS);
Set<String> tags = new HashSet<>();
tags.add(TAG_CLIMATE);
Set<Policy> policies = new HashSet<>();
policies.add(goldSubscriptionPolicy);
policies.add(silverSubscriptionPolicy);
policies.add(bronzeSubscriptionPolicy);
BusinessInformation businessInformation = new BusinessInformation();
businessInformation.setBusinessOwner(NAME_BUSINESS_OWNER_1);
businessInformation.setBusinessOwnerEmail(EMAIL_BUSINESS_OWNER_1);
businessInformation.setTechnicalOwner(NAME_TECHNICAL_OWNER_1);
businessInformation.setTechnicalOwnerEmail(EMAIL_TECHNICAL_OWNER_1);
String permissionJson = "[{\"groupId\" : \"developer\", \"permission\" : " + "[\"READ\",\"UPDATE\"]},{\"groupId\" : \"admin\", \"permission\" : [\"READ\",\"UPDATE\"," + "\"DELETE\", \"MANAGE_SUBSCRIPTION\"]}]";
Set<String> visibleRoles = new HashSet<>();
visibleRoles.add("testRple");
List<String> labels = new ArrayList<>();
labels.add("testLabel");
CorsConfiguration corsConfiguration = new CorsConfiguration();
corsConfiguration.setEnabled(true);
corsConfiguration.setAllowMethods(Arrays.asList(APIMgtConstants.FunctionsConstants.GET, APIMgtConstants.FunctionsConstants.POST, APIMgtConstants.FunctionsConstants.DELETE));
corsConfiguration.setAllowHeaders(Arrays.asList(ALLOWED_HEADER_AUTHORIZATION, ALLOWED_HEADER_CUSTOM));
corsConfiguration.setAllowCredentials(true);
corsConfiguration.setAllowOrigins(Arrays.asList("*"));
Map<String, Endpoint> endpointMap = new HashMap<>();
endpointMap.put("TestEndpoint", createMockEndpoint());
API.APIBuilder apiBuilder = new API.APIBuilder(ADMIN, "WeatherAPI", API_VERSION).id(UUID.randomUUID().toString()).context("weather").description("Get Weather Info").lifeCycleStatus(APIStatus.CREATED.getStatus()).lifecycleInstanceId(UUID.randomUUID().toString()).endpoint(Collections.emptyMap()).wsdlUri("http://localhost:9443/echo?wsdl").isResponseCachingEnabled(false).cacheTimeout(60).isDefaultVersion(false).apiPolicy(unlimitedApiPolicy).transport(transport).tags(tags).policies(policies).visibility(API.Visibility.PUBLIC).visibleRoles(visibleRoles).businessInformation(businessInformation).corsConfiguration(corsConfiguration).createdTime(LocalDateTime.now()).createdBy(ADMIN).updatedBy(ADMIN).lastUpdatedTime(LocalDateTime.now()).apiPermission(permissionJson).uriTemplates(getMockUriTemplates()).apiDefinition(apiDefinition).workflowStatus(WORKFLOW_STATUS).labels(labels).endpoint(endpointMap);
Map map = new HashMap();
map.put(DEVELOPER_ROLE_ID, 6);
map.put(ADMIN_ROLE_ID, 15);
apiBuilder.permissionMap(map);
return apiBuilder;
}
Aggregations