Search in sources :

Example 6 with SOAPToRestSequence

use of org.wso2.carbon.apimgt.api.model.SOAPToRestSequence in project carbon-apimgt by wso2.

the class RegistryPersistenceImpl method setSoapToRestSequences.

protected void setSoapToRestSequences(PublisherAPI publisherAPI, Registry registry) throws RegistryException {
    if (publisherAPI.getSoapToRestSequences() != null && !publisherAPI.getSoapToRestSequences().isEmpty()) {
        List<SOAPToRestSequence> sequence = publisherAPI.getSoapToRestSequences();
        for (SOAPToRestSequence soapToRestSequence : sequence) {
            String apiResourceName = soapToRestSequence.getPath();
            if (apiResourceName.startsWith("/")) {
                apiResourceName = apiResourceName.substring(1);
            }
            String resourcePath = APIConstants.API_ROOT_LOCATION + RegistryConstants.PATH_SEPARATOR + RegistryPersistenceUtil.replaceEmailDomain(publisherAPI.getProviderName()) + RegistryConstants.PATH_SEPARATOR + publisherAPI.getApiName() + RegistryConstants.PATH_SEPARATOR + publisherAPI.getVersion() + RegistryConstants.PATH_SEPARATOR;
            if (soapToRestSequence.getDirection() == Direction.OUT) {
                resourcePath = resourcePath + "soap_to_rest" + RegistryConstants.PATH_SEPARATOR + "out" + RegistryConstants.PATH_SEPARATOR;
            } else {
                resourcePath = resourcePath + "soap_to_rest" + RegistryConstants.PATH_SEPARATOR + "in" + RegistryConstants.PATH_SEPARATOR;
            }
            resourcePath = resourcePath + apiResourceName + "_" + soapToRestSequence.getMethod() + ".xml";
            Resource regResource;
            if (!registry.resourceExists(resourcePath)) {
                regResource = registry.newResource();
                regResource.setContent(soapToRestSequence.getContent());
                regResource.addProperty("method", soapToRestSequence.getMethod());
                if (regResource.getProperty("resourcePath") != null) {
                    regResource.removeProperty("resourcePath");
                }
                regResource.addProperty("resourcePath", apiResourceName);
                regResource.setMediaType("text/xml");
                registry.put(resourcePath, regResource);
            }
        }
    }
}
Also used : Resource(org.wso2.carbon.registry.core.Resource) SOAPToRestSequence(org.wso2.carbon.apimgt.api.model.SOAPToRestSequence)

Example 7 with SOAPToRestSequence

use of org.wso2.carbon.apimgt.api.model.SOAPToRestSequence in project carbon-apimgt by wso2.

the class RegistryPersistenceImpl method getSoapToRestSequences.

protected List<SOAPToRestSequence> getSoapToRestSequences(Registry registry, API api, Direction direction) throws RegistryException, APIPersistenceException {
    String resourcePath = APIConstants.API_LOCATION + RegistryConstants.PATH_SEPARATOR + RegistryPersistenceUtil.replaceEmailDomain(api.getId().getProviderName()) + RegistryConstants.PATH_SEPARATOR + api.getId().getName() + RegistryConstants.PATH_SEPARATOR + api.getId().getVersion() + RegistryConstants.PATH_SEPARATOR + "soap_to_rest" + RegistryConstants.PATH_SEPARATOR;
    if (direction == Direction.IN) {
        resourcePath = resourcePath + "in";
    } else if (direction == Direction.OUT) {
        resourcePath = resourcePath + "out";
    } else {
        throw new APIPersistenceException("Invalid sequence type");
    }
    List<SOAPToRestSequence> sequences = new ArrayList<SOAPToRestSequence>();
    if (registry.resourceExists(resourcePath)) {
        Collection collection = (Collection) registry.get(resourcePath);
        String[] resources = collection.getChildren();
        for (String path : resources) {
            Resource resource = registry.get(path);
            String content = new String((byte[]) resource.getContent(), Charset.defaultCharset());
            String resourceName;
            if (resource.getProperty("resourcePath") != null) {
                resourceName = resource.getProperty("resourcePath");
            } else {
                resourceName = ((ResourceImpl) resource).getName();
            }
            resourceName = resourceName.replaceAll("\\.xml", "");
            resourceName = resourceName.split("_")[0];
            String httpMethod = resource.getProperty("method");
            SOAPToRestSequence seq = new SOAPToRestSequence(httpMethod, resourceName, content, direction);
            seq.setUuid(resource.getUUID());
            sequences.add(seq);
        }
    }
    return sequences;
}
Also used : APIPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException) ArrayList(java.util.ArrayList) Resource(org.wso2.carbon.registry.core.Resource) Collection(org.wso2.carbon.registry.core.Collection) SOAPToRestSequence(org.wso2.carbon.apimgt.api.model.SOAPToRestSequence)

Example 8 with SOAPToRestSequence

use of org.wso2.carbon.apimgt.api.model.SOAPToRestSequence in project carbon-apimgt by wso2.

the class SequenceUtilsTestCase method testGetRestToSoapConvertedSequence.

public void testGetRestToSoapConvertedSequence() throws Exception {
    String provider = "admin";
    String apiName = "test-api";
    String version = "1.0.0";
    String seqType = "in";
    API api = new API(new APIIdentifier(provider, apiName, version));
    List<SOAPToRestSequence> soapToRestSequences = new ArrayList<SOAPToRestSequence>();
    SOAPToRestSequence seq = new SOAPToRestSequence("post", "test", "<>", Direction.IN);
    soapToRestSequences.add(seq);
    SOAPToRestSequence seq2 = new SOAPToRestSequence("post", "test", "<>", Direction.OUT);
    soapToRestSequences.add(seq2);
    api.setSoapToRestSequences(soapToRestSequences);
    String resourceName = "test_get.xml";
    Resource resource = Mockito.mock(Resource.class);
    ResourceImpl resourceImpl = Mockito.mock(ResourceImpl.class);
    Collection collection = Mockito.mock(Collection.class);
    String[] paths = new String[0];
    byte[] content = new byte[1];
    PowerMockito.when(MultitenantUtils.getTenantDomain(Mockito.anyString())).thenReturn("carbon.super");
    PowerMockito.when(serviceReferenceHolder.getRegistryService()).thenReturn(registryService);
    Mockito.when(((Collection) userRegistry.get(Mockito.anyString()))).thenReturn(collection);
    Mockito.when(collection.getChildren()).thenReturn(paths);
    Mockito.when(userRegistry.get(Mockito.anyString())).thenReturn(resource);
    Mockito.when(resource.getContent()).thenReturn(content);
    Mockito.when(resourceImpl.getName()).thenReturn(resourceName);
    Mockito.when(tenantManager.getTenantId(Mockito.anyString())).thenReturn(-1);
    try {
        SequenceUtils.getRestToSoapConvertedSequence(api, seqType);
    } catch (APIManagementException e) {
        Assert.fail("Failed to get the sequences from the registry");
    }
}
Also used : ResourceImpl(org.wso2.carbon.registry.core.ResourceImpl) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) ArrayList(java.util.ArrayList) Resource(org.wso2.carbon.registry.core.Resource) Collection(org.wso2.carbon.registry.core.Collection) API(org.wso2.carbon.apimgt.api.model.API) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) SOAPToRestSequence(org.wso2.carbon.apimgt.api.model.SOAPToRestSequence)

Example 9 with SOAPToRestSequence

use of org.wso2.carbon.apimgt.api.model.SOAPToRestSequence in project carbon-apimgt by wso2.

the class ApisApiServiceImpl method updateAPIResourcePoliciesByPolicyId.

/**
 * Update the resource policies(inflow/outflow) given the resource id.
 *
 * @param apiId  API ID
 * @param resourcePolicyId resource policy id
 * @param body resource policy content
 * @param ifMatch If-Match header value
 * @return json response of the updated sequence content
 */
@Override
public Response updateAPIResourcePoliciesByPolicyId(String apiId, String resourcePolicyId, ResourcePolicyInfoDTO body, String ifMatch, MessageContext messageContext) {
    try {
        String organization = RestApiUtil.getValidatedOrganization(messageContext);
        APIProvider provider = RestApiCommonUtil.getLoggedInUserProvider();
        API api = provider.getAPIbyUUID(apiId, organization);
        if (api == null) {
            throw new APIMgtResourceNotFoundException("Couldn't retrieve existing API with API UUID: " + apiId, ExceptionCodes.from(ExceptionCodes.API_NOT_FOUND, apiId));
        }
        // validate API update operation permitted based on the LC state
        validateAPIOperationsPerLC(api.getStatus());
        if (APIConstants.API_TYPE_SOAPTOREST.equals(api.getType())) {
            if (StringUtils.isEmpty(resourcePolicyId)) {
                String errorMessage = "Resource id should not be empty to update a resource policy.";
                RestApiUtil.handleBadRequest(errorMessage, log);
            }
            boolean isValidSchema = RestApiPublisherUtils.validateXMLSchema(body.getContent());
            if (isValidSchema) {
                List<SOAPToRestSequence> sequence = api.getSoapToRestSequences();
                for (SOAPToRestSequence soapToRestSequence : sequence) {
                    if (soapToRestSequence.getUuid().equals(resourcePolicyId)) {
                        soapToRestSequence.setContent(body.getContent());
                        break;
                    }
                }
                API originalAPI = provider.getAPIbyUUID(apiId, organization);
                provider.updateAPI(api, originalAPI);
                SequenceUtils.updateResourcePolicyFromRegistryResourceId(api.getId(), resourcePolicyId, body.getContent());
                String updatedPolicyContent = SequenceUtils.getResourcePolicyFromRegistryResourceId(api, resourcePolicyId);
                ResourcePolicyInfoDTO resourcePolicyInfoDTO = APIMappingUtil.fromResourcePolicyStrToInfoDTO(updatedPolicyContent);
                return Response.ok().entity(resourcePolicyInfoDTO).build();
            } else {
                String errorMessage = "Error while validating the resource policy xml content for the API : " + apiId;
                RestApiUtil.handleInternalServerError(errorMessage, log);
            }
        } else {
            String errorMessage = "The provided api with id: " + apiId + " is not a soap to rest converted api.";
            RestApiUtil.handleBadRequest(errorMessage, log);
        }
    } catch (APIManagementException | FaultGatewaysException e) {
        String errorMessage = "Error while retrieving the API : " + apiId;
        RestApiUtil.handleInternalServerError(errorMessage, e, log);
    }
    return null;
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) FaultGatewaysException(org.wso2.carbon.apimgt.api.FaultGatewaysException) API(org.wso2.carbon.apimgt.api.model.API) ImportExportAPI(org.wso2.carbon.apimgt.impl.importexport.ImportExportAPI) SubscribedAPI(org.wso2.carbon.apimgt.api.model.SubscribedAPI) ResourcePolicyInfoDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.ResourcePolicyInfoDTO) APIMgtResourceNotFoundException(org.wso2.carbon.apimgt.api.APIMgtResourceNotFoundException) APIProvider(org.wso2.carbon.apimgt.api.APIProvider) SOAPToRestSequence(org.wso2.carbon.apimgt.api.model.SOAPToRestSequence)

Example 10 with SOAPToRestSequence

use of org.wso2.carbon.apimgt.api.model.SOAPToRestSequence in project carbon-apimgt by wso2.

the class SequenceUtils method getResourcePolicyFromRegistryResourceId.

/**
 * Gets resource policy resource for the given resource id from the registry.
 *
 * @param API  api
 * @param resourceId Resource identifier
 * @return resource policy string for the given resource id
 * @throws APIManagementException
 */
public static String getResourcePolicyFromRegistryResourceId(API api, String resourceId) throws APIManagementException {
    String response = null;
    List<SOAPToRestSequence> sequences = api.getSoapToRestSequences();
    if (sequences == null) {
        handleException("Cannot find any resource policies for the api " + api.getUuid());
    }
    boolean found = false;
    for (SOAPToRestSequence soapToRestSequence : sequences) {
        if (soapToRestSequence.getUuid().equals(resourceId)) {
            JSONObject resultJson = new JSONObject();
            String content = soapToRestSequence.getContent();
            String resourceName = soapToRestSequence.getPath();
            String httpMethod = soapToRestSequence.getMethod();
            Map<String, String> resourceMap = new HashMap<>();
            resourceMap.put(SOAPToRESTConstants.RESOURCE_ID, soapToRestSequence.getUuid());
            resourceMap.put(SOAPToRESTConstants.METHOD, httpMethod);
            resourceMap.put(SOAPToRESTConstants.CONTENT, content);
            resultJson.put(resourceName, resourceMap);
            response = resultJson.toJSONString();
            found = true;
            break;
        }
    }
    if (!found) {
        handleException("Cannot find any resource policies with policy id : " + resourceId);
    }
    return response;
}
Also used : JSONObject(org.json.simple.JSONObject) HashMap(java.util.HashMap) SOAPToRestSequence(org.wso2.carbon.apimgt.api.model.SOAPToRestSequence)

Aggregations

SOAPToRestSequence (org.wso2.carbon.apimgt.api.model.SOAPToRestSequence)10 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)5 API (org.wso2.carbon.apimgt.api.model.API)5 Resource (org.wso2.carbon.registry.core.Resource)4 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 JSONObject (org.json.simple.JSONObject)2 APIMgtResourceNotFoundException (org.wso2.carbon.apimgt.api.APIMgtResourceNotFoundException)2 APIProvider (org.wso2.carbon.apimgt.api.APIProvider)2 APIPersistenceException (org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException)2 Collection (org.wso2.carbon.registry.core.Collection)2 SimpleModule (com.fasterxml.jackson.databind.module.SimpleModule)1 ArrayExample (io.swagger.inflector.examples.models.ArrayExample)1 Example (io.swagger.inflector.examples.models.Example)1 ObjectExample (io.swagger.inflector.examples.models.ObjectExample)1 StringExample (io.swagger.inflector.examples.models.StringExample)1 JsonNodeExampleSerializer (io.swagger.inflector.processors.JsonNodeExampleSerializer)1 HttpMethod (io.swagger.models.HttpMethod)1 Model (io.swagger.models.Model)1 Operation (io.swagger.models.Operation)1