use of org.wso2.carbon.apimgt.api.model.Wsdl in project carbon-apimgt by wso2.
the class AbstractWSDLProcessor method getWSDLArchive.
/**
* Creates a zip for from the files of the provided path and returns an InputStream for the created zip file.
*
* @param wsdlArchiveFilePath file path where the WSDL archive is extracted
* @return ByteArrayInputStream object of the WSDL archive zip file
* @throws APIMgtWSDLException when error occurred when creating the zip and returning the InputStream
*/
ByteArrayInputStream getWSDLArchive(String wsdlArchiveFilePath) throws APIMgtWSDLException {
try {
Collection<File> allFiles = FileUtils.listFiles(new File(wsdlArchiveFilePath), null, true);
String outputZipFilePath = wsdlArchiveFilePath + File.separator + APIConstants.WSDL_ARCHIVE_UPDATED_ZIP_FILE;
ZIPUtils.zipFiles(outputZipFilePath, allFiles);
File outputZipFile = new File(outputZipFilePath);
return new ByteArrayInputStream(FileUtils.readFileToByteArray(outputZipFile));
} catch (APIManagementException | IOException e) {
throw new APIMgtWSDLException("General error occurred while creating WSDL archive", e, ExceptionCodes.ERROR_WHILE_CREATING_WSDL_ARCHIVE);
}
}
use of org.wso2.carbon.apimgt.api.model.Wsdl in project carbon-apimgt by wso2.
the class AbstractWSDLProcessor method getSecuredParsedDocumentFromURL.
/**
* Returns an "XXE safe" built DOM XML object by reading the content from the provided URL.
*
* @param url URL to fetch the content
* @return an "XXE safe" built DOM XML object by reading the content from the provided URL
* @throws APIMgtWSDLException When error occurred while reading from URL
*/
Document getSecuredParsedDocumentFromURL(URL url) throws APIMgtWSDLException {
InputStream inputStream = null;
try {
DocumentBuilderFactory factory = getSecuredDocumentBuilder();
DocumentBuilder builder = factory.newDocumentBuilder();
inputStream = url.openStream();
return builder.parse(inputStream);
} catch (ParserConfigurationException | IOException | SAXException e) {
throw new APIMgtWSDLException("Error while reading WSDL document", e);
} finally {
IOUtils.closeQuietly(inputStream);
}
}
use of org.wso2.carbon.apimgt.api.model.Wsdl in project carbon-apimgt by wso2.
the class WSDL11ProcessorImpl method init.
@Override
public boolean init(byte[] wsdlContent) throws APIMgtWSDLException {
setMode(Mode.SINGLE);
WSDLReader wsdlReader = getWsdlFactoryInstance().newWSDLReader();
// switch off the verbose mode
wsdlReader.setFeature(JAVAX_WSDL_VERBOSE_MODE, false);
wsdlReader.setFeature(JAVAX_WSDL_IMPORT_DOCUMENTS, false);
try {
wsdlDefinition = wsdlReader.readWSDL(null, getSecuredParsedDocumentFromContent(wsdlContent));
if (log.isDebugEnabled()) {
log.debug("Successfully initialized an instance of " + this.getClass().getSimpleName() + " with a single WSDL.");
}
} catch (WSDLException | APIManagementException e) {
// This implementation class cannot process the WSDL.
log.debug("Cannot process the WSDL by " + this.getClass().getName(), e);
setError(new ErrorItem(ExceptionCodes.CANNOT_PROCESS_WSDL_CONTENT.getErrorMessage(), e.getMessage(), ExceptionCodes.CANNOT_PROCESS_WSDL_CONTENT.getErrorCode(), ExceptionCodes.CANNOT_PROCESS_WSDL_CONTENT.getHttpStatusCode()));
}
return !hasError;
}
use of org.wso2.carbon.apimgt.api.model.Wsdl in project carbon-apimgt by wso2.
the class WSDL11ProcessorImpl method initPath.
@Override
public boolean initPath(String path) throws APIMgtWSDLException {
setMode(Mode.ARCHIVE);
pathToDefinitionMap = new HashMap<>();
wsdlArchiveExtractedPath = path;
WSDLReader wsdlReader = getWsdlFactoryInstance().newWSDLReader();
try {
// switch off the verbose mode
wsdlReader.setFeature(JAVAX_WSDL_VERBOSE_MODE, false);
wsdlReader.setFeature(JAVAX_WSDL_IMPORT_DOCUMENTS, false);
File folderToImport = new File(path);
Collection<File> foundWSDLFiles = APIFileUtil.searchFilesWithMatchingExtension(folderToImport, "wsdl");
if (log.isDebugEnabled()) {
log.debug("Found " + foundWSDLFiles.size() + " WSDL file(s) in path " + path);
}
for (File file : foundWSDLFiles) {
String absWSDLPath = file.getAbsolutePath();
if (log.isDebugEnabled()) {
log.debug("Processing WSDL file: " + absWSDLPath);
}
Definition definition = wsdlReader.readWSDL(path, getSecuredParsedDocumentFromPath(absWSDLPath));
pathToDefinitionMap.put(absWSDLPath, definition);
// set the first found WSDL as wsdlDefinition variable assuming that it is the root WSDL
if (wsdlDefinition == null) {
wsdlDefinition = definition;
}
}
if (foundWSDLFiles.isEmpty()) {
setError(ExceptionCodes.NO_WSDL_FOUND_IN_WSDL_ARCHIVE);
}
if (log.isDebugEnabled()) {
log.debug("Successfully processed all WSDL files in path " + path);
}
} catch (WSDLException | APIManagementException e) {
// This implementation class cannot process the WSDL. Continuing after setting canProcess = false
log.debug(this.getClass().getName() + " was unable to process the WSDL Files for the path: " + path, e);
setError(new ErrorItem(ExceptionCodes.CANNOT_PROCESS_WSDL_CONTENT.getErrorMessage(), e.getMessage(), ExceptionCodes.CANNOT_PROCESS_WSDL_CONTENT.getErrorCode(), ExceptionCodes.CANNOT_PROCESS_WSDL_CONTENT.getHttpStatusCode()));
}
return !hasError;
}
use of org.wso2.carbon.apimgt.api.model.Wsdl in project carbon-apimgt by wso2.
the class WSDL11ProcessorImpl method updateEndpointsOfSingleWSDL.
/**
* Update the endpoint information of the provided WSDL definition when an API and the environment details are
* provided
*
* @param api API
* @param environmentName name of the gateway environment
* @param environmentType type of the gateway environment
* @param wsdlDefinition WSDL 1.1 definition
* @throws APIMgtWSDLException when error occurred while updating the endpoints
*/
private void updateEndpointsOfSingleWSDL(API api, String environmentName, String environmentType, Definition wsdlDefinition) throws APIMgtWSDLException {
Map serviceMap = wsdlDefinition.getAllServices();
URL addressURI;
String organization = api.getOrganization();
for (Object entry : serviceMap.entrySet()) {
Map.Entry svcEntry = (Map.Entry) entry;
Service svc = (Service) svcEntry.getValue();
Map portMap = svc.getPorts();
for (Object o : portMap.entrySet()) {
Map.Entry portEntry = (Map.Entry) o;
Port port = (Port) portEntry.getValue();
List<ExtensibilityElement> extensibilityElementList = port.getExtensibilityElements();
String endpointTransport;
for (ExtensibilityElement extensibilityElement : extensibilityElementList) {
try {
addressURI = new URL(getAddressUrl(extensibilityElement));
endpointTransport = determineURLTransport(addressURI.getProtocol(), api.getTransports());
if (log.isDebugEnabled()) {
log.debug("Address URI for the port:" + port.getName() + " is " + addressURI.toString());
}
} catch (MalformedURLException e) {
if (log.isDebugEnabled()) {
log.debug("Error occurred while getting the wsdl address location [" + getAddressUrl(extensibilityElement) + "]");
}
endpointTransport = determineURLTransport("https", api.getTransports());
// This string to URL conversion done in order to identify URL transport eg - http or https.
// Here if there is a conversion failure , consider "https" as default protocol
}
try {
setAddressUrl(extensibilityElement, endpointTransport, api.getContext(), environmentName, environmentType, organization);
} catch (APIManagementException e) {
throw new APIMgtWSDLException("Error while setting gateway access URLs in the WSDL", e);
}
}
}
}
}
Aggregations