use of org.wso2.carbon.apimgt.core.template.APIConfigContext in project carbon-apimgt by wso2.
the class APIStoreImpl method setGatewayDefinitionSource.
private void setGatewayDefinitionSource(CompositeAPI.Builder apiBuilder) throws APIManagementException {
List<UriTemplate> list = new ArrayList<>(apiBuilder.getUriTemplates().values());
List<TemplateBuilderDTO> resourceList = new ArrayList<>();
String appId = null;
List<CompositeAPIEndpointDTO> endpointDTOs = new ArrayList<CompositeAPIEndpointDTO>();
try {
appId = apiBuilder.getApplicationId();
List<Subscription> subscriptions = getApiSubscriptionDAO().getAPISubscriptionsByApplication(apiBuilder.getApplicationId(), ApiType.STANDARD);
for (Subscription subscription : subscriptions) {
CompositeAPIEndpointDTO endpointDTO = new CompositeAPIEndpointDTO();
API api = subscription.getApi();
endpointDTO.setEndpointName(api.getName());
// TODO: currently only HTTPS endpoint considered. Websocket APIs and http transport should considered
endpointDTO.setTransportType(APIMgtConstants.HTTPS);
// TODO: replace host with gateway domain host
String endpointUrl = APIMgtConstants.HTTPS + APIMgtConstants.WEB_PROTOCOL_SUFFIX + config.getHostname() + "/" + api.getContext() + "/" + api.getVersion();
endpointDTO.setEndpointUrl(endpointUrl);
endpointDTOs.add(endpointDTO);
}
} catch (APIMgtDAOException e) {
String errorMsg = "Error while getting subscriptions of the application " + appId;
log.error(errorMsg, e);
throw new APIManagementException(errorMsg, e, e.getErrorHandler());
}
for (UriTemplate uriTemplate : list) {
TemplateBuilderDTO dto = new TemplateBuilderDTO();
dto.setTemplateId(uriTemplate.getTemplateId());
dto.setUriTemplate(uriTemplate.getUriTemplate());
dto.setHttpVerb(uriTemplate.getHttpVerb());
resourceList.add(dto);
}
GatewaySourceGenerator gatewaySourceGenerator = getGatewaySourceGenerator();
APIConfigContext apiConfigContext = new APIConfigContext(apiBuilder.build(), config.getGatewayPackageName());
gatewaySourceGenerator.setApiConfigContext(apiConfigContext);
String gatewayConfig = gatewaySourceGenerator.getCompositeAPIConfigStringFromTemplate(resourceList, endpointDTOs);
if (log.isDebugEnabled()) {
log.debug("API " + apiBuilder.getName() + "gateway config: " + gatewayConfig);
}
apiBuilder.gatewayConfig(gatewayConfig);
}
use of org.wso2.carbon.apimgt.core.template.APIConfigContext in project carbon-apimgt by wso2.
the class EndpointBckConfigContextTest method testEndpointBckConfigContext.
@Test
public void testEndpointBckConfigContext() throws Exception {
API api = new API(new APIIdentifier("admin", "TestAPI", "1.0.0"));
api.setStatus(APIConstants.CREATED);
api.setContextTemplate("/");
String url = "http://maps.googleapis.com/maps/api/geocode/json?address=Colombo";
api.setUrl(url);
api.setSandboxUrl(url);
ConfigContext configcontext = new APIConfigContext(api);
EndpointBckConfigContext endpointBckConfigContext = new EndpointBckConfigContext(configcontext, api);
Assert.assertTrue(api.getEndpointConfig().contains(url));
// setting an empty string as the endpoint config and checking the value which is returned
api.setEndpointConfig("");
String endpointConfig = "{\"production_endpoints\":{\"url\":\"" + api.getUrl() + "\", \"config\":null}," + "\"sandbox_endpoint\":{\"url\":\"" + api.getSandboxUrl() + "\"," + "\"config\":null},\"endpoint_type\":\"http\"}";
EndpointBckConfigContext secondEndpointBckConfigContext = new EndpointBckConfigContext(configcontext, api);
Assert.assertTrue(api.getEndpointConfig().contains(endpointConfig));
// setting null as the endpoint config and checking the value which is returned
api.setEndpointConfig(null);
EndpointBckConfigContext thirdEndpointBckConfigContext = new EndpointBckConfigContext(configcontext, api);
Assert.assertTrue(api.getEndpointConfig().contains(endpointConfig));
}
use of org.wso2.carbon.apimgt.core.template.APIConfigContext in project carbon-apimgt by wso2.
the class EnvironmentConfigContextTest method testEnvironmentConfigContext.
@Test
public void testEnvironmentConfigContext() throws Exception {
API api = new API(new APIIdentifier("admin", "TestAPI", "1.0.0"));
api.setStatus(APIConstants.CREATED);
api.setContextTemplate("/");
String url = "http://maps.googleapis.com/maps/api/geocode/json?address=Colombo";
String endpointConfig = "{\"production_endpoints\":{\"url\":\"" + url + "\", \"config\":null}," + "\"sandbox_endpoint\":{\"url\":\"" + url + "\",\"config\":null},\"endpoint_type\":\"http\"}";
api.setEndpointConfig(endpointConfig);
api.setUrl(url);
api.setSandboxUrl(url);
ConfigContext configcontext = new APIConfigContext(api);
Environment environment = new Environment();
environment.setType("production");
EnvironmentConfigContext environmentConfigContext = new EnvironmentConfigContext(configcontext, environment);
Assert.assertNotNull(environmentConfigContext.getContext().get("environment"));
Assert.assertNotNull(environmentConfigContext.getContext().get("environmentType"));
}
use of org.wso2.carbon.apimgt.core.template.APIConfigContext in project carbon-apimgt by wso2.
the class ResourceConfigContextTest method testResourceConfigContextForAPI.
@Test
public void testResourceConfigContextForAPI() throws Exception {
API api = new API(new APIIdentifier("admin", "TestAPI", "1.0.0"));
api.setStatus(APIConstants.CREATED);
api.setContextTemplate("/");
api.setUriTemplates(setAPIUriTemplates());
ConfigContext configcontext = new APIConfigContext(api);
ResourceConfigContext resourceConfigContext = new ResourceConfigContext(configcontext, api);
resourceConfigContext.validate();
Assert.assertNotNull(resourceConfigContext.getContext().get("resources"));
// assign an empty URITemplate set and check the result
Set<URITemplate> uriTemplates = new LinkedHashSet<URITemplate>();
api.setUriTemplates(uriTemplates);
configcontext = new APIConfigContext(api);
resourceConfigContext = new ResourceConfigContext(configcontext, api);
String errorClass = "org.wso2.carbon.apimgt.api.APIManagementException";
String expectedErrorMessage = "At least one resource is required";
try {
resourceConfigContext.validate();
} catch (APIManagementException e) {
Assert.assertTrue(errorClass.equalsIgnoreCase(e.getClass().getName()));
Assert.assertTrue(expectedErrorMessage.equalsIgnoreCase(e.getMessage()));
}
// set a null value for URITemplate and check the result
api.setUriTemplates(null);
configcontext = new APIConfigContext(api);
resourceConfigContext = new ResourceConfigContext(configcontext, api);
try {
resourceConfigContext.validate();
} catch (APIManagementException e) {
Assert.assertTrue(errorClass.equalsIgnoreCase(e.getClass().getName()));
Assert.assertTrue(expectedErrorMessage.equalsIgnoreCase(e.getMessage()));
}
}
use of org.wso2.carbon.apimgt.core.template.APIConfigContext in project carbon-apimgt by wso2.
the class SecurityConfigContextTest method testSecurityConfigContextOauth.
@Test
public void testSecurityConfigContextOauth() throws Exception {
String json = "{\"endpoint_security\":{\n" + " \"production\":{\n" + " \"enabled\":true,\n" + " \"type\":\"oauth\",\n" + " \"clientId\":\"123-456\",\n" + " \"clientSecret\":\"admin\",\n" + " \"grantType\":\"client_credentials\"\n" + " },\n" + " \"sandbox\":{\n" + " \"enabled\":true,\n" + " \"type\":\"oauth\",\n" + " \"clientId\":\"123-4567\",\n" + " \"clientSecret\":\"admin\",\n" + " \"grantType\":\"client_credentials\"\n" + " }\n" + " }\n" + "}";
API api = new API(new APIIdentifier("admin", "TestAPI", "1.0.0"));
api.setUuid(UUID.randomUUID().toString());
api.setStatus(APIConstants.CREATED);
api.setContextTemplate("/");
api.setTransports(Constants.TRANSPORT_HTTP);
api.setEndpointConfig(json);
ConfigContext configcontext = new APIConfigContext(api);
Mockito.when(apiManagerConfiguration.getFirstProperty(APIConstants.API_SECUREVAULT_ENABLE)).thenReturn("true");
SecurityConfigContext securityConfigContext = new SecurityConfigContextWrapper(configcontext, api, apiManagerConfiguration);
securityConfigContext.validate();
VelocityContext velocityContext = securityConfigContext.getContext();
Assert.assertNotNull(velocityContext.get("endpoint_security"));
Map<String, EndpointSecurityModel> endpointSecurityModelMap = (Map<String, EndpointSecurityModel>) velocityContext.get("endpoint_security");
EndpointSecurityModel production = endpointSecurityModelMap.get("production");
Assert.assertTrue("Property enabled cannot be false.", production.isEnabled());
Assert.assertTrue("Property type cannot be other.", production.getType().equalsIgnoreCase("oauth"));
Assert.assertTrue("Property clientid does not match.", "123-456".equals(production.getClientId()));
Assert.assertEquals(production.getClientSecretAlias(), "TestAPI--v1.0.0--oauth--clientSecret--production");
EndpointSecurityModel sandbox = endpointSecurityModelMap.get("sandbox");
Assert.assertTrue("Property enabled cannot be false.", sandbox.isEnabled());
Assert.assertTrue("Property type cannot be other.", sandbox.getType().equalsIgnoreCase("oauth"));
Assert.assertTrue("Property username does not match.", "123-4567".equals(sandbox.getClientId()));
Assert.assertEquals(sandbox.getClientSecretAlias(), "TestAPI--v1.0.0--oauth--clientSecret--sandbox");
Assert.assertTrue("Property isSecureVaultEnabled cannot be false. ", velocityContext.get("isSecureVaultEnabled").equals(true));
}
Aggregations