Search in sources :

Example 36 with Mediation

use of org.wso2.carbon.apimgt.persistence.dto.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));
}
Also used : APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) APIProductIdentifier(org.wso2.carbon.apimgt.api.model.APIProductIdentifier) Identifier(org.wso2.carbon.apimgt.api.model.Identifier) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 37 with Mediation

use of org.wso2.carbon.apimgt.persistence.dto.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;
}
Also used : Path(java.nio.file.Path) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) SoapToRestMediationDto(org.wso2.carbon.apimgt.impl.dto.SoapToRestMediationDto) ArrayList(java.util.ArrayList) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream)

Example 38 with Mediation

use of org.wso2.carbon.apimgt.persistence.dto.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);
                }
            }
        }
    }
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) IOException(java.io.IOException) File(java.io.File) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException) DirectoryIteratorException(java.nio.file.DirectoryIteratorException) APIImportExportException(org.wso2.carbon.apimgt.impl.importexport.APIImportExportException) FaultGatewaysException(org.wso2.carbon.apimgt.api.FaultGatewaysException) IOException(java.io.IOException) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) FileNotFoundException(java.io.FileNotFoundException) SAXException(org.xml.sax.SAXException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) JsonParseException(com.google.gson.JsonParseException) APIMgtAuthorizationFailedException(org.wso2.carbon.apimgt.api.APIMgtAuthorizationFailedException) ParseException(org.json.simple.parser.ParseException) CryptoException(org.wso2.carbon.core.util.CryptoException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) APIMgtResourceNotFoundException(org.wso2.carbon.apimgt.api.APIMgtResourceNotFoundException)

Example 39 with Mediation

use of org.wso2.carbon.apimgt.persistence.dto.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);
    }
}
Also used : DirectoryIteratorException(java.nio.file.DirectoryIteratorException) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) Resource(org.wso2.carbon.registry.core.Resource) APIProductResource(org.wso2.carbon.apimgt.api.model.APIProductResource) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException)

Example 40 with Mediation

use of org.wso2.carbon.apimgt.persistence.dto.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);
    }
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) Mediation(org.wso2.carbon.apimgt.api.model.Mediation) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException) DirectoryIteratorException(java.nio.file.DirectoryIteratorException) APIImportExportException(org.wso2.carbon.apimgt.impl.importexport.APIImportExportException) FaultGatewaysException(org.wso2.carbon.apimgt.api.FaultGatewaysException) IOException(java.io.IOException) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) FileNotFoundException(java.io.FileNotFoundException) SAXException(org.xml.sax.SAXException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) JsonParseException(com.google.gson.JsonParseException) APIMgtAuthorizationFailedException(org.wso2.carbon.apimgt.api.APIMgtAuthorizationFailedException) ParseException(org.json.simple.parser.ParseException) CryptoException(org.wso2.carbon.core.util.CryptoException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) APIMgtResourceNotFoundException(org.wso2.carbon.apimgt.api.APIMgtResourceNotFoundException)

Aggregations

APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)35 Mediation (org.wso2.carbon.apimgt.api.model.Mediation)23 RegistryException (org.wso2.carbon.registry.core.exceptions.RegistryException)22 IOException (java.io.IOException)21 Resource (org.wso2.carbon.registry.core.Resource)19 XMLStreamException (javax.xml.stream.XMLStreamException)12 QName (javax.xml.namespace.QName)11 OMElement (org.apache.axiom.om.OMElement)11 APIProductResource (org.wso2.carbon.apimgt.api.model.APIProductResource)11 MediationPolicyPersistenceException (org.wso2.carbon.apimgt.persistence.exceptions.MediationPolicyPersistenceException)11 InputStream (java.io.InputStream)10 APIMgtResourceNotFoundException (org.wso2.carbon.apimgt.api.APIMgtResourceNotFoundException)10 ArrayList (java.util.ArrayList)9 Collection (org.wso2.carbon.registry.core.Collection)9 APIProvider (org.wso2.carbon.apimgt.api.APIProvider)8 OMAttribute (org.apache.axiom.om.OMAttribute)7 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)6 Organization (org.wso2.carbon.apimgt.persistence.dto.Organization)6 APIPersistenceException (org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException)6 UserRegistry (org.wso2.carbon.registry.core.session.UserRegistry)6