use of org.wso2.carbon.apimgt.api.gateway.GatewayAPIDTO in project carbon-apimgt by wso2.
the class APIGatewayAdmin method deployAPI.
public boolean deployAPI(GatewayAPIDTO gatewayAPIDTO) throws AxisFault {
CertificateManager certificateManager = CertificateManagerImpl.getInstance();
SequenceAdminServiceProxy sequenceAdminServiceProxy = getSequenceAdminServiceClient(gatewayAPIDTO.getTenantDomain());
RESTAPIAdminServiceProxy restapiAdminServiceProxy = getRestapiAdminClient(gatewayAPIDTO.getTenantDomain());
LocalEntryServiceProxy localEntryServiceProxy = new LocalEntryServiceProxy(gatewayAPIDTO.getTenantDomain());
EndpointAdminServiceProxy endpointAdminServiceProxy = new EndpointAdminServiceProxy(gatewayAPIDTO.getTenantDomain());
MediationSecurityAdminServiceProxy mediationSecurityAdminServiceProxy = new MediationSecurityAdminServiceProxy(gatewayAPIDTO.getTenantDomain());
if (log.isDebugEnabled()) {
log.debug("Start to undeploy API" + gatewayAPIDTO.getName() + ":" + gatewayAPIDTO.getVersion());
}
unDeployAPI(certificateManager, sequenceAdminServiceProxy, restapiAdminServiceProxy, localEntryServiceProxy, endpointAdminServiceProxy, gatewayAPIDTO, mediationSecurityAdminServiceProxy);
if (log.isDebugEnabled()) {
log.debug(gatewayAPIDTO.getName() + ":" + gatewayAPIDTO.getVersion() + " undeployed");
log.debug("Start to deploy Local entries" + gatewayAPIDTO.getName() + ":" + gatewayAPIDTO.getVersion());
}
// Add Local Entries
if (gatewayAPIDTO.getLocalEntriesToBeAdd() != null) {
for (GatewayContentDTO localEntry : gatewayAPIDTO.getLocalEntriesToBeAdd()) {
if (localEntryServiceProxy.isEntryExists(localEntry.getName())) {
localEntryServiceProxy.deleteEntry(localEntry.getName());
localEntryServiceProxy.addLocalEntry(localEntry.getContent());
} else {
localEntryServiceProxy.addLocalEntry(localEntry.getContent());
}
}
}
if (log.isDebugEnabled()) {
log.debug(gatewayAPIDTO.getName() + ":" + gatewayAPIDTO.getVersion() + " Local Entries deployed");
log.debug("Start to deploy Endpoint entries" + gatewayAPIDTO.getName() + ":" + gatewayAPIDTO.getVersion());
}
// Add Endpoints
if (gatewayAPIDTO.getEndpointEntriesToBeAdd() != null) {
for (GatewayContentDTO endpointEntry : gatewayAPIDTO.getEndpointEntriesToBeAdd()) {
if (endpointAdminServiceProxy.isEndpointExist(endpointEntry.getName())) {
endpointAdminServiceProxy.deleteEndpoint(endpointEntry.getName());
endpointAdminServiceProxy.addEndpoint(endpointEntry.getContent());
} else {
endpointAdminServiceProxy.addEndpoint(endpointEntry.getContent());
}
}
}
if (log.isDebugEnabled()) {
log.debug(gatewayAPIDTO.getName() + ":" + gatewayAPIDTO.getVersion() + " Endpoints deployed");
log.debug("Start to deploy Client certificates" + gatewayAPIDTO.getName() + ":" + gatewayAPIDTO.getVersion());
}
// Add Client Certificates
if (gatewayAPIDTO.getClientCertificatesToBeAdd() != null) {
for (GatewayContentDTO certificate : gatewayAPIDTO.getClientCertificatesToBeAdd()) {
certificateManager.addClientCertificateToGateway(certificate.getContent(), certificate.getName());
}
}
if (log.isDebugEnabled()) {
log.debug(gatewayAPIDTO.getName() + ":" + gatewayAPIDTO.getVersion() + " client certificates deployed");
log.debug("Start to add vault entries " + gatewayAPIDTO.getName() + ":" + gatewayAPIDTO.getVersion());
}
// Add vault entries
if (gatewayAPIDTO.getCredentialsToBeAdd() != null) {
for (CredentialDto certificate : gatewayAPIDTO.getCredentialsToBeAdd()) {
try {
String encryptedValue = mediationSecurityAdminServiceProxy.doEncryption(certificate.getPassword());
if (mediationSecurityAdminServiceProxy.isAliasExist(certificate.getAlias())) {
setRegistryProperty(gatewayAPIDTO.getTenantDomain(), certificate.getAlias(), encryptedValue);
} else {
setRegistryProperty(gatewayAPIDTO.getTenantDomain(), certificate.getAlias(), encryptedValue);
}
} catch (APIManagementException e) {
log.error("Exception occurred while encrypting password.", e);
throw new AxisFault(e.getMessage());
}
}
}
if (log.isDebugEnabled()) {
log.debug(gatewayAPIDTO.getName() + ":" + gatewayAPIDTO.getVersion() + " Vault Entries Added successfully");
log.debug("Start to deploy custom sequences" + gatewayAPIDTO.getName() + ":" + gatewayAPIDTO.getVersion());
}
// Add Sequences
if (gatewayAPIDTO.getSequenceToBeAdd() != null) {
for (GatewayContentDTO sequence : gatewayAPIDTO.getSequenceToBeAdd()) {
OMElement element;
try {
element = AXIOMUtil.stringToOM(sequence.getContent());
} catch (XMLStreamException e) {
log.error("Exception occurred while converting String to an OM.", e);
throw new AxisFault(e.getMessage());
}
if (sequenceAdminServiceProxy.isExistingSequence(sequence.getName())) {
sequenceAdminServiceProxy.deleteSequence(sequence.getName());
sequenceAdminServiceProxy.addSequence(element);
} else {
sequenceAdminServiceProxy.addSequence(element);
}
}
}
if (log.isDebugEnabled()) {
log.debug(gatewayAPIDTO.getName() + ":" + gatewayAPIDTO.getVersion() + " custom sequences deployed");
log.debug("Start to deploy API Definition" + gatewayAPIDTO.getName() + ":" + gatewayAPIDTO.getVersion());
}
// Add API
if (StringUtils.isNotEmpty(gatewayAPIDTO.getApiDefinition())) {
restapiAdminServiceProxy.addApi(gatewayAPIDTO.getApiDefinition());
}
if (log.isDebugEnabled()) {
log.debug(gatewayAPIDTO.getName() + ":" + gatewayAPIDTO.getVersion() + " API Definition deployed");
log.debug("Start to deploy Default API Definition" + gatewayAPIDTO.getName() + ":" + gatewayAPIDTO.getVersion());
}
if (log.isDebugEnabled()) {
log.debug(gatewayAPIDTO.getName() + ":" + gatewayAPIDTO.getVersion() + " Default API Definition deployed");
log.debug(gatewayAPIDTO.getName() + ":" + gatewayAPIDTO.getVersion() + "Deployed successfully");
}
return true;
}
use of org.wso2.carbon.apimgt.api.gateway.GatewayAPIDTO in project carbon-apimgt by wso2.
the class APIGatewayAdmin method unDeployAPI.
public boolean unDeployAPI(GatewayAPIDTO gatewayAPIDTO) throws AxisFault {
CertificateManager certificateManager = CertificateManagerImpl.getInstance();
SequenceAdminServiceProxy sequenceAdminServiceProxy = getSequenceAdminServiceClient(gatewayAPIDTO.getTenantDomain());
RESTAPIAdminServiceProxy restapiAdminServiceProxy = getRestapiAdminClient(gatewayAPIDTO.getTenantDomain());
LocalEntryServiceProxy localEntryServiceProxy = new LocalEntryServiceProxy(gatewayAPIDTO.getTenantDomain());
EndpointAdminServiceProxy endpointAdminServiceProxy = new EndpointAdminServiceProxy(gatewayAPIDTO.getTenantDomain());
MediationSecurityAdminServiceProxy mediationSecurityAdminServiceProxy = new MediationSecurityAdminServiceProxy(gatewayAPIDTO.getTenantDomain());
unDeployAPI(certificateManager, sequenceAdminServiceProxy, restapiAdminServiceProxy, localEntryServiceProxy, endpointAdminServiceProxy, gatewayAPIDTO, mediationSecurityAdminServiceProxy);
return true;
}
use of org.wso2.carbon.apimgt.api.gateway.GatewayAPIDTO in project carbon-apimgt by wso2.
the class TemplateBuilderUtilTest method addAddGqlWebSocketResourceEndpoints.
@Test
public void addAddGqlWebSocketResourceEndpoints() throws Exception {
APITemplateBuilder apiTemplateBuilder = Mockito.mock(APITemplateBuilder.class);
API api = new API(new APIIdentifier("admin", "API", "1.0"));
Set<URITemplate> uriTemplates = new HashSet<>();
URITemplate template = new URITemplate();
template.setAuthType("Any");
template.setHTTPVerb("POST");
template.setHttpVerbs("POST");
template.setUriTemplate(wildCardResource);
uriTemplates.add(template);
template = new URITemplate();
template.setUriTemplate(wildCardResource);
uriTemplates.add(template);
api.setUriTemplates(uriTemplates);
Map<String, Map<String, String>> perTopicMappings = new HashMap<>();
Map<String, String> endpoints = new HashMap<>();
endpoints.put(APIConstants.GATEWAY_ENV_TYPE_PRODUCTION, wsProdEndpoint);
endpoints.put(APIConstants.GATEWAY_ENV_TYPE_SANDBOX, wsSandEndpoint);
perTopicMappings.put(wildCardResource, endpoints);
WebSocketTopicMappingConfiguration webSocketTopicMappingConfiguration = new WebSocketTopicMappingConfiguration(perTopicMappings);
webSocketTopicMappingConfiguration.setResourceKey(wildCardResource, mappingWildCard);
api.setWebSocketTopicMappingConfiguration(webSocketTopicMappingConfiguration);
api.setType(APIConstants.GRAPHQL_API);
GatewayAPIDTO gatewayAPIDTO = new GatewayAPIDTO();
GatewayContentDTO dummyHttpProdEpContentDTO = new GatewayContentDTO();
dummyHttpProdEpContentDTO.setName("API--v1.0_APIproductionEndpoint");
dummyHttpProdEpContentDTO.setContent("dummy content");
GatewayContentDTO[] gatewayContentDTOS = new GatewayContentDTO[1];
gatewayContentDTOS[0] = dummyHttpProdEpContentDTO;
gatewayAPIDTO.setEndpointEntriesToBeAdd(gatewayContentDTOS);
String dummyProdEndpointConfig = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><endpoint name=\"" + wsProdEpName + "\">dummy prod content</endpoint>";
String dummySandboxEndpointConfig = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><endpoint name=\"" + wsSandEpName + "\">dummy sandbox content</endpoint>";
Mockito.when(apiTemplateBuilder.getConfigStringForWebSocketEndpointTemplate(APIConstants.GATEWAY_ENV_TYPE_PRODUCTION, mappingWildCard, wsProdEndpoint)).thenReturn(dummyProdEndpointConfig);
Mockito.when(apiTemplateBuilder.getConfigStringForWebSocketEndpointTemplate(APIConstants.GATEWAY_ENV_TYPE_SANDBOX, mappingWildCard, wsSandEndpoint)).thenReturn(dummySandboxEndpointConfig);
TemplateBuilderUtil.addWebSocketResourceEndpoints(api, apiTemplateBuilder, gatewayAPIDTO);
GatewayContentDTO[] endpointEntries = gatewayAPIDTO.getEndpointEntriesToBeAdd();
Assert.assertEquals(endpointEntries.length, 3);
boolean httpEpConfigPresent = false;
boolean wsProdEpConfigPresent = false;
boolean wsSandEPConfigPresent = false;
for (int i = 0; i < 3; i++) {
if (dummyHttpProdEpContentDTO.getName().equals(endpointEntries[i].getName())) {
httpEpConfigPresent = true;
Assert.assertEquals(endpointEntries[i].getContent(), dummyHttpProdEpContentDTO.getContent());
}
if (wsProdEpName.equals(endpointEntries[i].getName())) {
wsProdEpConfigPresent = true;
Assert.assertEquals(endpointEntries[i].getContent(), dummyProdEndpointConfig);
}
if (wsSandEpName.equals(endpointEntries[i].getName())) {
wsSandEPConfigPresent = true;
Assert.assertEquals(endpointEntries[i].getContent(), dummySandboxEndpointConfig);
}
}
Assert.assertTrue(wsProdEpConfigPresent);
Assert.assertTrue(wsSandEPConfigPresent);
Assert.assertTrue(httpEpConfigPresent);
}
use of org.wso2.carbon.apimgt.api.gateway.GatewayAPIDTO in project carbon-apimgt by wso2.
the class TemplateBuilderUtil method addWebSocketResourceEndpoints.
public static void addWebSocketResourceEndpoints(API api, APITemplateBuilder builder, GatewayAPIDTO gatewayAPIDTO) throws APITemplateException, XMLStreamException {
Set<URITemplate> uriTemplates = api.getUriTemplates();
Map<String, Map<String, String>> topicMappings = api.getWebSocketTopicMappingConfiguration().getMappings();
List<GatewayContentDTO> endpointsToAdd = new ArrayList<>();
for (URITemplate resource : uriTemplates) {
String topic = resource.getUriTemplate();
Map<String, String> endpoints = topicMappings.get(topic);
// Production and Sandbox endpoints
for (Map.Entry<String, String> endpointData : endpoints.entrySet()) {
if (!"resourceKey".equals(endpointData.getKey())) {
String endpointType = endpointData.getKey();
String endpointUrl = endpointData.getValue();
String endpointConfigContext = builder.getConfigStringForWebSocketEndpointTemplate(endpointType, getWebsocketResourceKey(topic), endpointUrl);
GatewayContentDTO endpoint = new GatewayContentDTO();
// For WS APIs, resource type is not applicable,
// so we can just use the uriTemplate/uriMapping to identify the resource
endpoint.setName(getEndpointName(endpointConfigContext));
endpoint.setContent(endpointConfigContext);
endpointsToAdd.add(endpoint);
}
}
// once through resources is enough.
if (APIConstants.GRAPHQL_API.equals(api.getType())) {
break;
}
}
gatewayAPIDTO.setEndpointEntriesToBeAdd(addGatewayContentsToList(endpointsToAdd, gatewayAPIDTO.getEndpointEntriesToBeAdd()));
}
use of org.wso2.carbon.apimgt.api.gateway.GatewayAPIDTO in project carbon-apimgt by wso2.
the class TemplateBuilderUtil method setAPIFaultSequencesToBeAdded.
private static void setAPIFaultSequencesToBeAdded(API api, GatewayAPIDTO gatewayAPIDTO, String extractedPath, APIDTO apidto) throws APIManagementException {
String faultSeqExt = APIUtil.getSequenceExtensionName(api) + APIConstants.API_CUSTOM_SEQ_FAULT_EXT;
if (!APIConstants.APITransportType.HTTP.toString().equals(api.getType())) {
gatewayAPIDTO.setSequencesToBeRemove(GatewayUtils.addStringToList(faultSeqExt, gatewayAPIDTO.getSequencesToBeRemove()));
List<MediationPolicyDTO> mediationPolicies = apidto.getMediationPolicies();
GatewayContentDTO faultSequenceContent = retrieveSequence(extractedPath, mediationPolicies, APIConstants.API_CUSTOM_SEQUENCE_TYPE_FAULT, api);
if (faultSequenceContent != null) {
gatewayAPIDTO.setSequenceToBeAdd(addGatewayContentToList(faultSequenceContent, gatewayAPIDTO.getSequenceToBeAdd()));
}
}
gatewayAPIDTO.setSequencesToBeRemove(GatewayUtils.addStringToList(faultSeqExt, gatewayAPIDTO.getSequencesToBeRemove()));
}
Aggregations