use of org.wso2.carbon.apimgt.impl.wsdl.WSDLProcessor in project carbon-apimgt by wso2.
the class APIPublisherImpl method updateAPIWSDL.
@Override
public String updateAPIWSDL(String apiId, InputStream inputStream) throws APIMgtDAOException, APIMgtWSDLException {
byte[] wsdlContent;
try {
wsdlContent = IOUtils.toByteArray(inputStream);
WSDLProcessor processor = WSDLProcessFactory.getInstance().getWSDLProcessor(wsdlContent);
if (!processor.canProcess()) {
throw new APIMgtWSDLException("Unable to process WSDL by the processor " + processor.getClass().getName(), ExceptionCodes.CANNOT_PROCESS_WSDL_CONTENT);
}
if (log.isDebugEnabled()) {
log.debug("Successfully validated the content of WSDL. API uuid: " + apiId);
}
getApiDAO().addOrUpdateWSDL(apiId, wsdlContent, getUsername());
if (log.isDebugEnabled()) {
log.debug("Successfully added WSDL to the DB. API uuid: " + apiId);
}
return new String(wsdlContent, APIMgtConstants.ENCODING_UTF_8);
} catch (IOException e) {
throw new APIMgtWSDLException("Error while updating WSDL of API " + apiId, e, ExceptionCodes.INTERNAL_WSDL_EXCEPTION);
}
}
use of org.wso2.carbon.apimgt.impl.wsdl.WSDLProcessor in project carbon-apimgt by wso2.
the class APIProviderImpl method addWSDLResource.
@Override
public void addWSDLResource(String apiId, ResourceFile resource, String url, String organization) throws APIManagementException {
if (!StringUtils.isEmpty(url)) {
URL wsdlUrl;
try {
wsdlUrl = new URL(url);
} catch (MalformedURLException e) {
throw new APIManagementException("Invalid/Malformed WSDL URL : " + url, 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);
org.wso2.carbon.apimgt.persistence.dto.ResourceFile wsdlResourceFile = new org.wso2.carbon.apimgt.persistence.dto.ResourceFile(wsdlContent, null);
try {
apiPersistenceInstance.saveWSDL(new Organization(organization), apiId, wsdlResourceFile);
} catch (WSDLPersistenceException e) {
throw new APIManagementException("Error while adding WSDL to api " + apiId, e);
}
} else if (resource != null) {
org.wso2.carbon.apimgt.persistence.dto.ResourceFile wsdlResourceFile = new org.wso2.carbon.apimgt.persistence.dto.ResourceFile(resource.getContent(), resource.getContentType());
try {
apiPersistenceInstance.saveWSDL(new Organization(organization), apiId, wsdlResourceFile);
} catch (WSDLPersistenceException e) {
throw new APIManagementException("Error while adding WSDL to api " + apiId, e);
}
}
}
use of org.wso2.carbon.apimgt.impl.wsdl.WSDLProcessor 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);
}
}
use of org.wso2.carbon.apimgt.impl.wsdl.WSDLProcessor in project carbon-apimgt by wso2.
the class APIMWSDLReader method getWSDLProcessorForUrl.
/**
* Returns the appropriate WSDL 1.1/WSDL 2.0 based on the url {@code url}.
*
* @param url WSDL url
* @return WSDL 1.1/2.0 processor for the provided content
* @throws APIManagementException If an error occurs while determining the processor
*/
public static WSDLProcessor getWSDLProcessorForUrl(URL url) throws APIManagementException {
WSDLProcessor wsdl11Processor = new WSDL11ProcessorImpl();
WSDLProcessor wsdl20Processor = new WSDL20ProcessorImpl();
try {
if (wsdl11Processor.canProcess(url)) {
wsdl11Processor.init(url);
return wsdl11Processor;
} else if (wsdl20Processor.canProcess(url)) {
wsdl20Processor.init(url);
return wsdl20Processor;
} else {
// no processors found if this line reaches
throw new APIManagementException("No WSDL processor found to process WSDL url: " + url, ExceptionCodes.URL_NOT_RECOGNIZED_AS_WSDL);
}
} catch (APIMgtWSDLException e) {
throw new APIManagementException("Error while instantiating wsdl processor class", e);
}
}
use of org.wso2.carbon.apimgt.impl.wsdl.WSDLProcessor in project carbon-apimgt by wso2.
the class APIMWSDLReader method getWSDLProcessor.
/**
* Returns the appropriate WSDL 1.1/WSDL 2.0 based on the file path {@code wsdlPath}.
*
* @param wsdlPath File path containing WSDL files and dependant files
* @return WSDL 1.1/2.0 processor for the provided content
* @throws APIManagementException If an error occurs while determining the processor
*/
public static WSDLProcessor getWSDLProcessor(String wsdlPath) throws APIManagementException {
WSDLProcessor wsdl11Processor = new WSDL11ProcessorImpl();
WSDLProcessor wsdl20Processor = new WSDL20ProcessorImpl();
byte[] wsdlContent;
APIMWSDLReader wsdlReader;
if (wsdlPath.endsWith(".wsdl") || wsdlPath.endsWith("?wsdl")) {
wsdlReader = new APIMWSDLReader(wsdlPath);
wsdlContent = wsdlReader.getWSDL();
return getWSDLProcessor(wsdlContent);
} else {
try {
if (wsdl11Processor.canProcess(wsdlPath)) {
wsdl11Processor.initPath(wsdlPath);
return wsdl11Processor;
} else if (wsdl20Processor.canProcess(wsdlPath)) {
wsdl20Processor.initPath(wsdlPath);
return wsdl20Processor;
} else {
// no processors found if this line reaches
throw new APIManagementException("No WSDL processor found to process WSDL content.", ExceptionCodes.CONTENT_NOT_RECOGNIZED_AS_WSDL);
}
} catch (APIMgtWSDLException e) {
throw new APIManagementException("Error while instantiating wsdl processor class", e);
}
}
}
Aggregations