use of org.wso2.carbon.apimgt.impl.utils.APIMWSDLReader in project carbon-apimgt by wso2.
the class WSDLSOAPOperationExtractorImplTestCase method testGetSwaggerModelForImportedSchemas.
@Test
public void testGetSwaggerModelForImportedSchemas() throws Exception {
APIMWSDLReader wsdlReader = new APIMWSDLReader(Thread.currentThread().getContextClassLoader().getResource("wsdls/import-schemas/sampleservice.wsdl").toExternalForm());
byte[] wsdlContent = wsdlReader.getWSDL();
WSDL11SOAPOperationExtractor processor = SOAPOperationBindingUtils.getWSDL11SOAPOperationExtractor(wsdlContent, wsdlReader);
Set<WSDLSOAPOperation> operations = processor.getWsdlInfo().getSoapBindingOperations();
Assert.assertNotNull(operations);
Map<String, ModelImpl> parameterModelMap = processor.getWsdlInfo().getParameterModelMap();
Assert.assertNotNull(parameterModelMap);
}
use of org.wso2.carbon.apimgt.impl.utils.APIMWSDLReader in project carbon-apimgt by wso2.
the class WSDLSOAPOperationExtractorImplTestCase method setup.
@Before
public void setup() throws Exception {
APIMWSDLReader wsdlReader = new APIMWSDLReader(Thread.currentThread().getContextClassLoader().getResource("wsdls/phoneverify.wsdl").toExternalForm());
byte[] wsdlContent = wsdlReader.getWSDL();
WSDL11SOAPOperationExtractor processor = SOAPOperationBindingUtils.getWSDL11SOAPOperationExtractor(wsdlContent, wsdlReader);
operations = processor.getWsdlInfo().getSoapBindingOperations();
}
use of org.wso2.carbon.apimgt.impl.utils.APIMWSDLReader in project carbon-apimgt by wso2.
the class WSDL11SOAPOperationExtractor method loadXSDsfromNamespaces.
/**
* Load the schemas into the list of based schemas from the namespaces.
*
* @param ns namespace
* @return document
* @throws APIManagementException
*/
public Document loadXSDsfromNamespaces(String ns) throws APIManagementException {
Collection<File> foundXSDFiles = new java.util.LinkedList<>();
foundXSDFiles.addAll(getStandardBaseXSDs());
Document doc = null;
APIMWSDLReader reader = new APIMWSDLReader(ns + ".xsd");
for (File file : foundXSDFiles) {
String absWSDLPath = file.getAbsolutePath();
if (log.isDebugEnabled()) {
log.debug("Processing xsd file: " + absWSDLPath);
}
doc = reader.getSecuredParsedDocument(absWSDLPath);
Node namespace = doc.getDocumentElement().getAttributes().getNamedItem("targetNamespace");
if (namespace != null) {
basedSchemas.put(namespace.getNodeValue(), doc);
}
}
return doc;
}
use of org.wso2.carbon.apimgt.impl.utils.APIMWSDLReader in project carbon-apimgt by wso2.
the class APIConsumerImpl method getUpdatedWSDLByEnvironment.
/**
* This method is used to get the updated wsdl with the respective environment apis are published
*
* @param wsdlResourcePath registry resource path to the wsdl
* @param wsdlContent wsdl resource content as byte array
* @param environmentType gateway environment type
* @return updated wsdl content with environment endpoints
* @throws APIManagementException
*/
private byte[] getUpdatedWSDLByEnvironment(String wsdlResourcePath, byte[] wsdlContent, String environmentName, String environmentType, String apiName, String apiVersion, String apiProvider) throws APIManagementException {
APIMWSDLReader apimwsdlReader = new APIMWSDLReader(wsdlResourcePath);
Definition definition = apimwsdlReader.getWSDLDefinitionFromByteContent(wsdlContent, false);
byte[] updatedWSDLContent = null;
boolean isTenantFlowStarted = false;
try {
String tenantDomain = MultitenantUtils.getTenantDomain(APIUtil.replaceEmailDomainBack(apiProvider));
if (tenantDomain != null && !MultitenantConstants.SUPER_TENANT_DOMAIN_NAME.equals(tenantDomain)) {
isTenantFlowStarted = true;
PrivilegedCarbonContext.startTenantFlow();
PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(tenantDomain, true);
}
RegistryService registryService = ServiceReferenceHolder.getInstance().getRegistryService();
int tenantId;
UserRegistry registry;
try {
tenantId = ServiceReferenceHolder.getInstance().getRealmService().getTenantManager().getTenantId(tenantDomain);
APIUtil.loadTenantRegistry(tenantId);
registry = registryService.getGovernanceSystemRegistry(tenantId);
API api = null;
if (!StringUtils.isEmpty(apiName) && !StringUtils.isEmpty(apiVersion)) {
APIIdentifier apiIdentifier = new APIIdentifier(APIUtil.replaceEmailDomain(apiProvider), apiName, apiVersion);
if (log.isDebugEnabled()) {
log.debug("Api identifier for the soap api artifact: " + apiIdentifier + "for api name: " + apiName + ", version: " + apiVersion);
}
GenericArtifact apiArtifact = APIUtil.getAPIArtifact(apiIdentifier, registry);
api = APIUtil.getAPI(apiArtifact);
if (log.isDebugEnabled()) {
if (api != null) {
log.debug("Api context for the artifact with id:" + api.getId() + " is " + api.getContext());
} else {
log.debug("Api does not exist for api name: " + apiIdentifier.getApiName());
}
}
} else {
handleException("Artifact does not exist in the registry for api name: " + apiName + " and version: " + apiVersion);
}
if (api != null) {
try {
apimwsdlReader.setServiceDefinition(definition, api, environmentName, environmentType);
if (log.isDebugEnabled()) {
log.debug("Soap api with context:" + api.getContext() + " in " + environmentName + " with environment type" + environmentType);
}
updatedWSDLContent = apimwsdlReader.getWSDL(definition);
} catch (APIManagementException e) {
handleException("Error occurred while processing the wsdl for api: [" + api.getId() + "]", e);
}
} else {
handleException("Error while getting API object for wsdl artifact");
}
} catch (UserStoreException e) {
handleException("Error while reading tenant information", e);
} catch (RegistryException e) {
handleException("Error when create registry instance", e);
}
} finally {
if (isTenantFlowStarted) {
PrivilegedCarbonContext.endTenantFlow();
}
}
return updatedWSDLContent;
}
use of org.wso2.carbon.apimgt.impl.utils.APIMWSDLReader in project carbon-apimgt by wso2.
the class APIUtil method createWSDL.
/**
* Crate an WSDL from given wsdl url. Reset the endpoint details to gateway node
* *
*
* @param registry - Governance Registry space to save the WSDL
* @param api -API instance
* @return Path of the created resource
* @throws APIManagementException If an error occurs while adding the WSDL
*/
public static String createWSDL(Registry registry, API api) throws RegistryException, APIManagementException {
try {
APIIdentifier apiId = api.getId();
String apiPath = APIUtil.getAPIPath(apiId);
int prependIndex = apiPath.indexOf(apiId.getVersion()) + apiId.getVersion().length();
String apiSourcePath = apiPath.substring(0, prependIndex);
String wsdlResourcePath = apiSourcePath + RegistryConstants.PATH_SEPARATOR + createWsdlFileName(api.getId().getProviderName(), api.getId().getApiName(), api.getId().getVersion());
String absoluteWSDLResourcePath = RegistryUtils.getAbsolutePath(RegistryContext.getBaseInstance(), RegistryConstants.GOVERNANCE_REGISTRY_BASE_PATH) + wsdlResourcePath;
APIMWSDLReader wsdlReader = new APIMWSDLReader();
OMElement wsdlContentEle;
String wsdlRegistryPath;
String tenantDomain = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain();
if (org.wso2.carbon.utils.multitenancy.MultitenantConstants.SUPER_TENANT_DOMAIN_NAME.equalsIgnoreCase(tenantDomain)) {
wsdlRegistryPath = RegistryConstants.PATH_SEPARATOR + "registry" + RegistryConstants.PATH_SEPARATOR + "resource" + absoluteWSDLResourcePath;
} else {
wsdlRegistryPath = "/t/" + tenantDomain + RegistryConstants.PATH_SEPARATOR + "registry" + RegistryConstants.PATH_SEPARATOR + "resource" + absoluteWSDLResourcePath;
}
Resource wsdlResource = registry.newResource();
// Hence if this is a registry url, should not go in to the following if block
if (!api.getWsdlUrl().matches(wsdlRegistryPath) && (api.getWsdlUrl().startsWith("http:") || api.getWsdlUrl().startsWith("https:") || api.getWsdlUrl().startsWith("file:") || api.getWsdlUrl().startsWith("/t"))) {
URL wsdlUrl;
try {
wsdlUrl = new URL(api.getWsdlUrl());
} catch (MalformedURLException e) {
throw new APIManagementException("Invalid/Malformed WSDL URL : " + api.getWsdlUrl(), e, ExceptionCodes.INVALID_WSDL_URL_EXCEPTION);
}
// Get the WSDL 1.1 or 2.0 processor and process the content based on the version
WSDLProcessor wsdlProcessor = APIMWSDLReader.getWSDLProcessorForUrl(wsdlUrl);
InputStream wsdlContent = wsdlProcessor.getWSDL();
wsdlResource.setContentStream(wsdlContent);
} else {
byte[] wsdl = (byte[]) registry.get(wsdlResourcePath).getContent();
if (isWSDL2Resource(wsdl)) {
wsdlContentEle = wsdlReader.updateWSDL2(wsdl, api);
wsdlResource.setContent(wsdlContentEle.toString());
} else {
wsdlContentEle = wsdlReader.updateWSDL(wsdl, api);
wsdlResource.setContent(wsdlContentEle.toString());
}
}
registry.put(wsdlResourcePath, wsdlResource);
// set the anonymous role for wsld resource to avoid basicauth security.
String[] visibleRoles = null;
if (api.getVisibleRoles() != null) {
visibleRoles = api.getVisibleRoles().split(",");
}
setResourcePermissions(api.getId().getProviderName(), api.getVisibility(), visibleRoles, wsdlResourcePath);
// Delete any WSDL archives if exists
String wsdlArchivePath = APIUtil.getWsdlArchivePath(api.getId());
if (registry.resourceExists(wsdlArchivePath)) {
registry.delete(wsdlArchivePath);
}
// set the wsdl resource permlink as the wsdlURL.
api.setWsdlUrl(getRegistryResourceHTTPPermlink(absoluteWSDLResourcePath));
return wsdlRegistryPath;
} catch (RegistryException e) {
String msg = "Failed to add WSDL " + api.getWsdlUrl() + " to the registry";
log.error(msg, e);
throw new RegistryException(msg, e);
} catch (APIManagementException e) {
String msg = "Failed to process the WSDL : " + api.getWsdlUrl();
log.error(msg, e);
throw new APIManagementException(msg, e);
}
}
Aggregations