use of org.wso2.carbon.apimgt.api.model.Mediation in project carbon-apimgt by wso2.
the class AbstractAPIManagerTestCase method testDeleteApiSpecificMediationPolicy.
@Test
public void testDeleteApiSpecificMediationPolicy() throws RegistryException, APIManagementException {
String resourcePath = "config/mediation/";
Identifier identifier = Mockito.mock(Identifier.class);
AbstractAPIManager abstractAPIManager = new AbstractAPIManagerWrapperExtended(null, null, registry, null);
Mockito.when(registry.resourceExists(Mockito.anyString())).thenReturn(true, false, true, false);
Mockito.doThrow(RegistryException.class).doNothing().when(registry).delete(Mockito.anyString());
try {
abstractAPIManager.deleteApiSpecificMediationPolicy(identifier, resourcePath, SAMPLE_RESOURCE_ID);
Assert.fail("Registry exception not thrown for error scenario");
} catch (APIManagementException e) {
Assert.assertTrue(e.getMessage().contains("Failed to delete specific mediation policy"));
}
Assert.assertFalse(abstractAPIManager.deleteApiSpecificMediationPolicy(identifier, resourcePath, SAMPLE_RESOURCE_ID));
Assert.assertTrue(abstractAPIManager.deleteApiSpecificMediationPolicy(identifier, resourcePath, SAMPLE_RESOURCE_ID));
}
use of org.wso2.carbon.apimgt.api.model.Mediation in project carbon-apimgt by wso2.
the class ImportUtils method retrieveSoapToRestFlowMediations.
public static List<SoapToRestMediationDto> retrieveSoapToRestFlowMediations(String pathToArchive, String type) throws APIManagementException {
List<SoapToRestMediationDto> soapToRestMediationDtoList = new ArrayList<>();
String fileLocation = null;
if (IN.equals(type)) {
fileLocation = pathToArchive + File.separator + SOAPTOREST + File.separator + IN;
} else if (OUT.equals(type)) {
fileLocation = pathToArchive + File.separator + SOAPTOREST + File.separator + OUT;
}
if (CommonUtil.checkFileExistence(fileLocation)) {
Path flowDirectory = Paths.get(fileLocation);
try (DirectoryStream<Path> stream = Files.newDirectoryStream(flowDirectory)) {
for (Path file : stream) {
String fileName = file.getFileName().toString();
String method = "";
String resource = "";
if (fileName.split(".xml").length != 0) {
method = fileName.split(".xml")[0].substring(file.getFileName().toString().lastIndexOf("_") + 1);
resource = fileName.substring(0, fileName.indexOf("_"));
}
try (InputStream inputFlowStream = new FileInputStream(file.toFile())) {
String content = IOUtils.toString(inputFlowStream);
SoapToRestMediationDto soapToRestMediationDto = new SoapToRestMediationDto(resource, method, content);
soapToRestMediationDtoList.add(soapToRestMediationDto);
}
}
} catch (IOException e) {
throw new APIManagementException("Error while reading mediation content", e);
}
}
return soapToRestMediationDtoList;
}
use of org.wso2.carbon.apimgt.api.model.Mediation in project carbon-apimgt by wso2.
the class ImportUtils method addCustomSequencesToRegistry.
/**
* @param sequencesDirectoryPath Location of the sequences directory in the extracted folder of the API
* @param type Sequence type (in/out/fault)
* @param importedApi Imported API
* @param apiProvider API Provider
* @param tenantDomain Tenant domain of the API
* @param existingAPISpecificMediationsList Existing API specific mediations list
* @throws APIManagementException If an error occurs while adding the mediation policy
*/
private static void addCustomSequencesToRegistry(String sequencesDirectoryPath, String type, API importedApi, APIProvider apiProvider, String tenantDomain, List<Mediation> existingAPISpecificMediationsList) throws APIManagementException {
String apiSpecificSequenceFilePath = sequencesDirectoryPath + File.separator + type + ImportExportConstants.SEQUENCE_LOCATION_POSTFIX + File.separator + ImportExportConstants.CUSTOM_TYPE;
if (CommonUtil.checkFileExistence(apiSpecificSequenceFilePath)) {
File apiSpecificSequencesDirectory = new File(apiSpecificSequenceFilePath);
File[] apiSpecificSequencesDirectoryListing = apiSpecificSequencesDirectory.listFiles();
if (apiSpecificSequencesDirectoryListing != null) {
for (File apiSpecificSequence : apiSpecificSequencesDirectoryListing) {
String individualSequenceLocation = apiSpecificSequenceFilePath + File.separator + apiSpecificSequence.getName();
try {
String sequenceContent = retrieveSequenceContentFromLocation(individualSequenceLocation);
PublisherCommonUtils.addMediationPolicyFromFile(sequenceContent, type, apiProvider, importedApi.getUuid(), tenantDomain, existingAPISpecificMediationsList, Boolean.TRUE);
} catch (IOException e) {
log.error("I/O error while writing sequence data to the registry : " + individualSequenceLocation, e);
} catch (Exception e) {
throw new APIManagementException("An Error has occurred while adding mediation policy" + StringUtils.SPACE + e.getMessage(), e);
}
}
}
}
}
use of org.wso2.carbon.apimgt.api.model.Mediation in project carbon-apimgt by wso2.
the class ImportUtils method importMediationLogic.
/**
* Method created to add inflow and outflow mediation logic.
*
* @param sequenceData Inflow and outflow directory
* @param registry Registry
* @param soapToRestLocation Folder location
* @throws APIImportExportException If an error occurs while importing/storing SOAP to REST mediation logic
*/
private static void importMediationLogic(SoapToRestMediationDto sequenceData, Registry registry, String soapToRestLocation) throws APIManagementException {
String fileName = sequenceData.getResource().concat("_").concat(sequenceData.getMethod()).concat(".xml");
try {
byte[] inSeqData = sequenceData.getContent().getBytes();
Resource inSeqResource = registry.newResource();
inSeqResource.setContent(inSeqData);
inSeqResource.addProperty(SOAPToRESTConstants.METHOD, sequenceData.getMethod());
inSeqResource.setMediaType("text/xml");
registry.put(soapToRestLocation + RegistryConstants.PATH_SEPARATOR + fileName, inSeqResource);
} catch (DirectoryIteratorException e) {
throw new APIManagementException("Error in importing SOAP to REST mediation logic", e);
} catch (RegistryException e) {
throw new APIManagementException("Error in storing imported SOAP to REST mediation logic", e);
}
}
use of org.wso2.carbon.apimgt.api.model.Mediation in project carbon-apimgt by wso2.
the class ImportUtils method addAPISequences.
/**
* This method adds API sequences to the imported API. If the sequence is a newly defined one, it is added.
*
* @param pathToArchive Location of the extracted folder of the API
* @param importedApi The imported API object
* @param apiProvider API Provider
* @throws APIManagementException If an error occurs while adding the mediation policy
*/
private static void addAPISequences(String pathToArchive, API importedApi, APIProvider apiProvider) throws APIManagementException {
String tenantDomain = RestApiCommonUtil.getLoggedInUserTenantDomain();
List<Mediation> existingMediationsList = apiProvider.getAllGlobalMediationPolicies();
try {
// Adding in-sequence, if any
String sequenceContent = retrieveSequenceContent(pathToArchive, false, APIConstants.API_CUSTOM_SEQUENCE_TYPE_IN, importedApi.getInSequence());
PublisherCommonUtils.addMediationPolicyFromFile(sequenceContent, APIConstants.API_CUSTOM_SEQUENCE_TYPE_IN, apiProvider, importedApi.getUuid(), tenantDomain, existingMediationsList, Boolean.FALSE);
// Adding out-sequence, if any
sequenceContent = retrieveSequenceContent(pathToArchive, false, APIConstants.API_CUSTOM_SEQUENCE_TYPE_OUT, importedApi.getOutSequence());
PublisherCommonUtils.addMediationPolicyFromFile(sequenceContent, APIConstants.API_CUSTOM_SEQUENCE_TYPE_OUT, apiProvider, importedApi.getUuid(), tenantDomain, existingMediationsList, Boolean.FALSE);
// Adding fault-sequence, if any
sequenceContent = retrieveSequenceContent(pathToArchive, false, APIConstants.API_CUSTOM_SEQUENCE_TYPE_FAULT, importedApi.getFaultSequence());
PublisherCommonUtils.addMediationPolicyFromFile(sequenceContent, APIConstants.API_CUSTOM_SEQUENCE_TYPE_FAULT, apiProvider, importedApi.getUuid(), tenantDomain, existingMediationsList, Boolean.FALSE);
} catch (Exception e) {
throw new APIManagementException("An Error has occurred while adding mediation policy" + StringUtils.SPACE + e.getMessage(), e);
}
}
Aggregations