use of org.wso2.carbon.apimgt.common.analytics.publishers.dto.Operation in project carbon-apimgt by wso2.
the class ApiMgtDAO method removeAPIFromDefaultVersion.
/**
* Sets/removes default api entry such that api will not represent as default api further.
* If the api's version is the same as the published version, then the whole entry will be removed.
* Otherwise only the default version attribute is set to null.
*
* @param apiIdList
* @param connection
* @return
* @throws APIManagementException
*/
private void removeAPIFromDefaultVersion(List<APIIdentifier> apiIdList, Connection connection) throws APIManagementException {
// TODO: check list empty
try (PreparedStatement prepStmtDefVersionDelete = connection.prepareStatement(SQLConstants.REMOVE_API_DEFAULT_VERSION_SQL)) {
for (APIIdentifier apiId : apiIdList) {
prepStmtDefVersionDelete.setString(1, apiId.getApiName());
prepStmtDefVersionDelete.setString(2, APIUtil.replaceEmailDomainBack(apiId.getProviderName()));
prepStmtDefVersionDelete.addBatch();
}
prepStmtDefVersionDelete.executeBatch();
} catch (SQLException e) {
try {
connection.rollback();
} catch (SQLException e1) {
log.error("Error while rolling back the failed operation", e1);
}
handleException("Error while deleting the API default version entry: " + apiIdList.stream().map(APIIdentifier::getApiName).collect(Collectors.joining(",")) + " from the " + "database", e);
}
}
use of org.wso2.carbon.apimgt.common.analytics.publishers.dto.Operation in project carbon-apimgt by wso2.
the class SequenceGenerator method generateSequencesFromSwagger.
/**
* Generates in/out sequences from the swagger given
*
* @param swaggerStr swagger string
* @throws APIManagementException
*/
public static List<SOAPToRestSequence> generateSequencesFromSwagger(String swaggerStr) throws APIManagementException {
List<SOAPToRestSequence> sequences = new ArrayList<SOAPToRestSequence>();
Swagger swagger = new SwaggerParser().parse(swaggerStr);
Map<String, Model> definitions = swagger.getDefinitions();
// Configure serializers
SimpleModule simpleModule = new SimpleModule().addSerializer(new JsonNodeExampleSerializer());
Json.mapper().registerModule(simpleModule);
Yaml.mapper().registerModule(simpleModule);
Map<String, Path> paths = swagger.getPaths();
for (String pathName : paths.keySet()) {
Path path = paths.get(pathName);
Map<HttpMethod, Operation> operationMap = path.getOperationMap();
for (HttpMethod httpMethod : operationMap.keySet()) {
boolean isResourceFromWSDL = false;
Map<String, String> parameterJsonPathMapping = new HashMap<>();
Map<String, String> queryParameters = new HashMap<>();
Operation operation = operationMap.get(httpMethod);
String operationId = operation.getOperationId();
// get vendor extensions
Map<String, Object> vendorExtensions = operation.getVendorExtensions();
Object vendorExtensionObj = vendorExtensions.get("x-wso2-soap");
String soapAction = SOAPToRESTConstants.EMPTY_STRING;
String namespace = SOAPToRESTConstants.EMPTY_STRING;
String soapVersion = SOAPToRESTConstants.EMPTY_STRING;
if (vendorExtensionObj != null) {
soapAction = (String) ((LinkedHashMap) vendorExtensionObj).get("soap-action");
namespace = (String) ((LinkedHashMap) vendorExtensionObj).get("namespace");
soapVersion = (String) ((LinkedHashMap) vendorExtensionObj).get(SOAPToRESTConstants.Swagger.SOAP_VERSION);
soapMessageType = (String) ((LinkedHashMap) vendorExtensionObj).get(SOAPToRESTConstants.Swagger.SOAP_MESSAGE_TYPE);
soapStyle = (String) ((LinkedHashMap) vendorExtensionObj).get(SOAPToRESTConstants.Swagger.SOAP_STYLE);
isResourceFromWSDL = true;
}
String soapNamespace = SOAPToRESTConstants.SOAP12_NAMSPACE;
if (StringUtils.isNotBlank(soapVersion) && SOAPToRESTConstants.SOAP_VERSION_11.equals(soapVersion)) {
soapNamespace = SOAPToRESTConstants.SOAP11_NAMESPACE;
}
List<Parameter> parameters = operation.getParameters();
for (Parameter parameter : parameters) {
String name = parameter.getName();
if (parameter instanceof BodyParameter) {
Model schema = ((BodyParameter) parameter).getSchema();
if (schema instanceof RefModel) {
String $ref = ((RefModel) schema).get$ref();
if (StringUtils.isNotBlank($ref)) {
String defName = $ref.substring("#/definitions/".length());
Model model = definitions.get(defName);
Example example = ExampleBuilder.fromModel(defName, model, definitions, new HashSet<String>());
replaceNullWithStringExample(example);
String jsonExample = Json.pretty(example);
try {
org.json.JSONObject json = new org.json.JSONObject(jsonExample);
SequenceUtils.listJson(json, parameterJsonPathMapping);
} catch (JSONException e) {
log.error("Error occurred while generating json mapping for the definition", e);
}
}
}
}
if (parameter instanceof QueryParameter) {
String type = ((QueryParameter) parameter).getType();
queryParameters.put(name, type);
}
}
// populates body parameter json paths and query parameters to generate api sequence parameters
populateParametersFromOperation(operation, definitions, parameterJsonPathMapping, queryParameters);
Map<String, String> payloadSequence = createPayloadFacXMLForOperation(parameterJsonPathMapping, queryParameters, namespace, SOAPToRESTConstants.EMPTY_STRING, operationId, definitions);
try {
String[] propAndArgElements = getPropertyAndArgElementsForSequence(parameterJsonPathMapping, queryParameters);
if (log.isDebugEnabled()) {
log.debug("properties string for the generated sequence: " + propAndArgElements[0]);
log.debug("arguments string for the generated sequence: " + propAndArgElements[1]);
}
org.json.simple.JSONArray arraySequenceElements = new org.json.simple.JSONArray();
// gets array elements for the sequence to be used
getArraySequenceElements(arraySequenceElements, parameterJsonPathMapping);
Map<String, String> sequenceMap = new HashMap<>();
sequenceMap.put("args", propAndArgElements[0]);
sequenceMap.put("properties", propAndArgElements[1]);
sequenceMap.put("sequence", payloadSequence.get(operationId));
RESTToSOAPMsgTemplate template = new RESTToSOAPMsgTemplate();
String inSequence = template.getMappingInSequence(sequenceMap, operationId, soapAction, namespace, soapNamespace, arraySequenceElements);
String outSequence = template.getMappingOutSequence();
if (isResourceFromWSDL) {
SOAPToRestSequence inSeq = new SOAPToRestSequence(httpMethod.toString().toLowerCase(), pathName, inSequence, Direction.IN);
sequences.add(inSeq);
SOAPToRestSequence outSeq = new SOAPToRestSequence(httpMethod.toString().toLowerCase(), pathName, outSequence, Direction.OUT);
sequences.add(outSeq);
}
} catch (APIManagementException e) {
handleException("Error when generating sequence property and arg elements for soap operation: " + operationId, e);
}
}
}
return sequences;
}
use of org.wso2.carbon.apimgt.common.analytics.publishers.dto.Operation in project carbon-apimgt by wso2.
the class WSDL11SOAPOperationExtractor method getTargetNamespace.
/**
* Gets the target namespace given the soap binding operation
*
* @param bindingOperation soap operation
* @return target name space
*/
private String getTargetNamespace(BindingOperation bindingOperation) {
Operation operation = bindingOperation.getOperation();
if (operation != null) {
Input input = operation.getInput();
if (input != null) {
Message message = input.getMessage();
if (message != null) {
Map partMap = message.getParts();
for (Object obj : partMap.entrySet()) {
Map.Entry entry = (Map.Entry) obj;
Part part = (Part) entry.getValue();
if (part != null) {
if (part.getElementName() != null) {
return part.getElementName().getNamespaceURI();
}
}
}
}
}
}
return targetNamespace;
}
use of org.wso2.carbon.apimgt.common.analytics.publishers.dto.Operation in project carbon-apimgt by wso2.
the class WSDL11SOAPOperationExtractor method getSOAPOperation.
/**
* Retrieves WSDL operation given the soap binding operation
*
* @param bindingOperation {@link BindingOperation} object
* @return a set of {@link WSDLOperation} defined in the provided Binding
*/
private WSDLSOAPOperation getSOAPOperation(BindingOperation bindingOperation) throws APIMgtWSDLException {
WSDLSOAPOperation wsdlOperation = null;
for (Object boExtElement : bindingOperation.getExtensibilityElements()) {
if (boExtElement instanceof SOAPOperation) {
SOAPOperation soapOperation = (SOAPOperation) boExtElement;
wsdlOperation = new WSDLSOAPOperation();
wsdlOperation.setName(bindingOperation.getName());
wsdlOperation.setSoapAction(soapOperation.getSoapActionURI());
wsdlOperation.setTargetNamespace(getTargetNamespace(bindingOperation));
wsdlOperation.setStyle(soapOperation.getStyle());
wsdlOperation.setInputParameterModel(getSoapInputParameterModel(bindingOperation));
wsdlOperation.setOutputParameterModel(getSoapOutputParameterModel(bindingOperation));
wsdlOperation.setMessageType(getSoapMessageType(bindingOperation));
} else if (boExtElement instanceof SOAP12Operation) {
SOAP12Operation soapOperation = (SOAP12Operation) boExtElement;
wsdlOperation = new WSDLSOAPOperation();
wsdlOperation.setName(bindingOperation.getName());
wsdlOperation.setSoapAction(soapOperation.getSoapActionURI());
wsdlOperation.setTargetNamespace(getTargetNamespace(bindingOperation));
wsdlOperation.setStyle(soapOperation.getStyle());
wsdlOperation.setInputParameterModel(getSoapInputParameterModel(bindingOperation));
wsdlOperation.setOutputParameterModel(getSoapOutputParameterModel(bindingOperation));
wsdlOperation.setMessageType(getSoapMessageType(bindingOperation));
}
}
return wsdlOperation;
}
use of org.wso2.carbon.apimgt.common.analytics.publishers.dto.Operation in project carbon-apimgt by wso2.
the class WSDL11SOAPOperationExtractor method getSoapOutputParameterModel.
/**
* Gets swagger output parameter model for a given soap operation
*
* @param bindingOperation soap operation
* @return list of swagger models for the parameters
* @throws APIMgtWSDLException
*/
private List<ModelImpl> getSoapOutputParameterModel(BindingOperation bindingOperation) throws APIMgtWSDLException {
List<ModelImpl> outputParameterModelList = new ArrayList<>();
Operation operation = bindingOperation.getOperation();
if (operation != null) {
Output output = operation.getOutput();
if (output != null) {
Message message = output.getMessage();
if (message != null) {
Map map = message.getParts();
for (Object obj : map.entrySet()) {
Map.Entry entry = (Map.Entry) obj;
Part part = (Part) entry.getValue();
if (part != null) {
if (part.getElementName() != null) {
outputParameterModelList.add(parameterModelMap.get(part.getElementName().getLocalPart()));
} else {
if (part.getTypeName() != null && parameterModelMap.containsKey(part.getTypeName().getLocalPart())) {
outputParameterModelList.add(parameterModelMap.get(part.getTypeName().getLocalPart()));
} else {
ModelImpl model = new ModelImpl();
model.setType(ObjectProperty.TYPE);
model.setName(message.getQName().getLocalPart());
if (getPropertyFromDataType(part.getTypeName().getLocalPart()) instanceof RefProperty) {
RefProperty property = (RefProperty) getPropertyFromDataType(part.getTypeName().getLocalPart());
property.set$ref(SOAPToRESTConstants.Swagger.DEFINITIONS_ROOT + part.getTypeName().getLocalPart());
model.addProperty(part.getName(), property);
} else {
model.addProperty(part.getName(), getPropertyFromDataType(part.getTypeName().getLocalPart()));
}
parameterModelMap.put(model.getName(), model);
outputParameterModelList.add(model);
}
}
}
}
}
}
}
return outputParameterModelList;
}
Aggregations