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);
}
}
}
}
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;
}
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");
}
}
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;
}
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;
}
Aggregations