use of org.wso2.carbon.apimgt.api.model.Wsdl in project carbon-apimgt by wso2.
the class APIProviderImpl method createNewAPIVersion.
public API createNewAPIVersion(String existingApiId, String newVersion, Boolean isDefaultVersion, String organization) throws APIManagementException {
API existingAPI = getAPIbyUUID(existingApiId, organization);
if (existingAPI == null) {
throw new APIMgtResourceNotFoundException("API not found for id " + existingApiId, ExceptionCodes.from(ExceptionCodes.API_NOT_FOUND, existingApiId));
}
if (newVersion.equals(existingAPI.getId().getVersion())) {
throw new APIMgtResourceAlreadyExistsException("Version " + newVersion + " exists for api " + existingAPI.getId().getApiName());
}
existingAPI.setOrganization(organization);
APIIdentifier existingAPIId = existingAPI.getId();
String existingAPICreatedTime = existingAPI.getCreatedTime();
String existingAPIStatus = existingAPI.getStatus();
boolean isExsitingAPIdefaultVersion = existingAPI.isDefaultVersion();
String existingContext = existingAPI.getContext();
String existingVersionTimestamp = existingAPI.getVersionTimestamp();
APIIdentifier newApiId = new APIIdentifier(existingAPI.getId().getProviderName(), existingAPI.getId().getApiName(), newVersion);
existingAPI.setUuid(null);
existingAPI.setId(newApiId);
existingAPI.setStatus(APIConstants.CREATED);
existingAPI.setDefaultVersion(isDefaultVersion);
existingAPI.setVersionTimestamp("");
// We need to change the context by setting the new version
// This is a change that is coming with the context version strategy
String existingAPIContextTemplate = existingAPI.getContextTemplate();
existingAPI.setContext(existingAPIContextTemplate.replace("{version}", newVersion));
Map<String, List<OperationPolicy>> operationPoliciesMap = extractAndDropOperationPoliciesFromURITemplate(existingAPI.getUriTemplates());
API newAPI = addAPI(existingAPI);
String newAPIId = newAPI.getUuid();
if (!operationPoliciesMap.isEmpty()) {
// clone common or API specific operation policy.
Map<String, String> clonedOperationPolicyMap = cloneOperationPoliciesToAPI(existingApiId, newAPI, operationPoliciesMap);
// attach policy to uri template.
attachOperationPoliciesToAPI(newAPI, clonedOperationPolicyMap, operationPoliciesMap);
}
// copy docs
List<Documentation> existingDocs = getAllDocumentation(existingApiId, organization);
if (existingDocs != null) {
for (Documentation documentation : existingDocs) {
Documentation newDoc = addDocumentation(newAPIId, documentation, organization);
DocumentationContent content = getDocumentationContent(existingApiId, documentation.getId(), // TODO see whether we can optimize this
organization);
if (content != null) {
addDocumentationContent(newAPIId, newDoc.getId(), organization, content);
}
}
}
// copy icon
ResourceFile icon = getIcon(existingApiId, organization);
if (icon != null) {
setThumbnailToAPI(newAPIId, icon, organization);
}
// copy sequences
List<Mediation> mediationPolicies = getAllApiSpecificMediationPolicies(existingApiId, organization);
if (mediationPolicies != null) {
for (Mediation mediation : mediationPolicies) {
Mediation policy = getApiSpecificMediationPolicyByPolicyId(existingApiId, mediation.getUuid(), organization);
addApiSpecificMediationPolicy(newAPIId, policy, organization);
}
}
// copy wsdl
if (!APIConstants.API_TYPE_SOAPTOREST.equals(existingAPI.getType()) && existingAPI.getWsdlUrl() != null) {
ResourceFile wsdl = getWSDL(existingApiId, organization);
if (wsdl != null) {
addWSDLResource(newAPIId, wsdl, null, organization);
}
}
// copy graphql definition
String graphQLSchema = getGraphqlSchemaDefinition(existingApiId, organization);
if (graphQLSchema != null) {
saveGraphqlSchemaDefinition(newAPIId, graphQLSchema, organization);
}
// update old api
// revert back to old values before update.
existingAPI.setUuid(existingApiId);
existingAPI.setStatus(existingAPIStatus);
existingAPI.setId(existingAPIId);
existingAPI.setContext(existingContext);
existingAPI.setCreatedTime(existingAPICreatedTime);
// update existing api with the original timestamp
existingAPI.setVersionTimestamp(existingVersionTimestamp);
if (isDefaultVersion) {
existingAPI.setDefaultVersion(false);
} else {
existingAPI.setDefaultVersion(isExsitingAPIdefaultVersion);
}
try {
apiPersistenceInstance.updateAPI(new Organization(organization), APIMapper.INSTANCE.toPublisherApi(existingAPI));
} catch (APIPersistenceException e) {
throw new APIManagementException("Error while updating API details", e);
}
return getAPIbyUUID(newAPIId, organization);
}
use of org.wso2.carbon.apimgt.api.model.Wsdl in project carbon-apimgt by wso2.
the class APIUtil method saveWSDLResource.
/**
* Save the provided wsdl archive file to the registry for the api
*
* @param registry Governance Registry space to save the WSDL
* @param api API instance
* @return
* @throws RegistryException
* @throws APIManagementException
*/
public static String saveWSDLResource(Registry registry, API api) throws RegistryException, APIManagementException {
ResourceFile wsdlResource = api.getWsdlResource();
String wsdlResourcePath;
boolean isZip = false;
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 wsdlResourcePathArchive = apiSourcePath + RegistryConstants.PATH_SEPARATOR + APIConstants.API_WSDL_ARCHIVE_LOCATION + api.getId().getProviderName() + APIConstants.WSDL_PROVIDER_SEPERATOR + api.getId().getApiName() + api.getId().getVersion() + APIConstants.ZIP_FILE_EXTENSION;
String wsdlResourcePathFile = apiSourcePath + RegistryConstants.PATH_SEPARATOR + createWsdlFileName(api.getId().getProviderName(), api.getId().getApiName(), api.getId().getVersion());
if (wsdlResource.getContentType().equals(APIConstants.APPLICATION_ZIP)) {
wsdlResourcePath = wsdlResourcePathArchive;
isZip = true;
} else {
wsdlResourcePath = wsdlResourcePathFile;
}
String absoluteWSDLResourcePath = RegistryUtils.getAbsolutePath(RegistryContext.getBaseInstance(), RegistryConstants.GOVERNANCE_REGISTRY_BASE_PATH) + wsdlResourcePath;
try {
Resource wsdlResourceToUpdate = registry.newResource();
wsdlResourceToUpdate.setContentStream(api.getWsdlResource().getContent());
wsdlResourceToUpdate.setMediaType(api.getWsdlResource().getContentType());
registry.put(wsdlResourcePath, wsdlResourceToUpdate);
String[] visibleRoles = null;
if (api.getVisibleRoles() != null) {
visibleRoles = api.getVisibleRoles().split(",");
}
setResourcePermissions(api.getId().getProviderName(), api.getVisibility(), visibleRoles, wsdlResourcePath);
if (isZip) {
// Delete any WSDL file if exists
if (registry.resourceExists(wsdlResourcePathFile)) {
registry.delete(wsdlResourcePathFile);
}
} else {
// Delete any WSDL archives if exists
if (registry.resourceExists(wsdlResourcePathArchive)) {
registry.delete(wsdlResourcePathArchive);
}
}
api.setWsdlUrl(getRegistryResourceHTTPPermlink(absoluteWSDLResourcePath));
} catch (RegistryException e) {
String msg = "Failed to add WSDL Archive " + api.getWsdlUrl() + " to the registry";
log.error(msg, e);
throw new RegistryException(msg, e);
} catch (APIManagementException e) {
String msg = "Failed to process the WSDL Archive: " + api.getWsdlUrl();
log.error(msg, e);
throw new APIManagementException(msg, e);
}
return wsdlResourcePath;
}
use of org.wso2.carbon.apimgt.api.model.Wsdl in project carbon-apimgt by wso2.
the class APIMWSDLReader method updateWSDL.
/**
* Update WSDL 1.0 service definitions saved in registry
*
* @param wsdl byte array of registry content
* @param api API object
* @return the OMElemnt of the new WSDL content
* @throws APIManagementException
*/
public OMElement updateWSDL(byte[] wsdl, API api) throws APIManagementException {
try {
// Generate wsdl document from registry data
WSDLReader wsdlReader = getWsdlFactoryInstance().newWSDLReader();
// switch off the verbose mode
wsdlReader.setFeature(JAVAX_WSDL_VERBOSE_MODE, false);
wsdlReader.setFeature(JAVAX_WSDL_IMPORT_DOCUMENTS, false);
if (wsdlReader instanceof WSDLReaderImpl) {
((WSDLReaderImpl) wsdlReader).setIgnoreSchemaContent(true);
}
Definition wsdlDefinition = wsdlReader.readWSDL(null, getSecuredParsedDocumentFromContent(wsdl));
// Update transports
setServiceDefinition(wsdlDefinition, api);
WSDLWriter writer = getWsdlFactoryInstance().newWSDLWriter();
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
writer.writeWSDL(wsdlDefinition, byteArrayOutputStream);
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
return APIUtil.buildOMElement(byteArrayInputStream);
} catch (Exception e) {
String msg = " Error occurs when updating WSDL ";
log.error(msg);
throw new APIManagementException(msg, e);
}
}
use of org.wso2.carbon.apimgt.api.model.Wsdl in project carbon-apimgt by wso2.
the class APIMWSDLReader method getWSDLProcessor.
/**
* Gets WSDL processor WSDL 1.1/WSDL 2.0 based on the content {@code content}.
*
* @param content WSDL content
* @return {@link WSDLProcessor}
* @throws APIManagementException
*/
public static WSDLProcessor getWSDLProcessor(byte[] content) throws APIManagementException {
WSDLProcessor wsdl11Processor = new WSDL11ProcessorImpl();
WSDLProcessor wsdl20Processor = new WSDL20ProcessorImpl();
try {
if (wsdl11Processor.canProcess(content)) {
wsdl11Processor.init(content);
return wsdl11Processor;
} else if (wsdl20Processor.canProcess(content)) {
wsdl20Processor.init(content);
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);
}
}
use of org.wso2.carbon.apimgt.api.model.Wsdl in project carbon-apimgt by wso2.
the class APIMWSDLReader method validateBaseURI.
/**
* Validate the base URI of the WSDL reader
*
* @throws APIManagementException When error occurred while parsing the content from the URL
*/
@Deprecated
public void validateBaseURI() throws APIManagementException {
if (baseURI.startsWith(APIConstants.WSDL_REGISTRY_LOCATION_PREFIX)) {
baseURI = APIUtil.getServerURL() + baseURI;
}
boolean isWsdl20 = false;
boolean isWsdl11 = false;
BufferedReader in = null;
try {
String inputLine;
StringBuilder urlContent = new StringBuilder();
URL wsdl = new URL(baseURI);
in = new BufferedReader(new InputStreamReader(wsdl.openStream(), Charset.defaultCharset()));
while ((inputLine = in.readLine()) != null) {
urlContent.append(inputLine);
isWsdl20 = urlContent.indexOf(WSDL20_NAMESPACE) > 0;
isWsdl11 = urlContent.indexOf(WSDL11_NAMESPACE) > 0;
}
} catch (IOException e) {
throw new APIManagementException("Error while reading WSDL from base URI " + baseURI, e);
} finally {
IOUtils.closeQuietly(in);
}
try {
if (isWsdl11) {
readAndValidateWSDL11();
} else if (isWsdl20) {
readAndValidateWSDL20();
} else {
throw new APIManagementException("URL is not in format of wsdl1.1 or wsdl2.0");
}
} catch (WSDLException e) {
throw new APIManagementException("Error while parsing WSDL content", e);
} catch (org.apache.woden.WSDLException e) {
throw new APIManagementException("Error while parsing WSDL content", e);
}
}
Aggregations