Skip to content

Commit

Permalink
Improve datamapper to read input/output type from json schema
Browse files Browse the repository at this point in the history
  • Loading branch information
arunans23 committed Jul 10, 2024
1 parent 750c645 commit 55e6b6f
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
import org.wso2.carbon.mediator.datamapper.engine.core.mapper.MappingResource;
import org.wso2.carbon.mediator.datamapper.engine.core.mapper.XSLTMappingHandler;
import org.wso2.carbon.mediator.datamapper.engine.core.mapper.XSLTMappingResource;
import org.wso2.carbon.mediator.datamapper.engine.core.schemas.Schema;
import org.wso2.carbon.mediator.datamapper.engine.utils.DataMapperEngineConstants;
import org.wso2.carbon.mediator.datamapper.engine.utils.InputOutputDataType;
import org.xml.sax.SAXException;
Expand Down Expand Up @@ -78,7 +79,9 @@
import static org.wso2.carbon.mediator.datamapper.config.xml.DataMapperMediatorConstants.DEFAULT_CONTEXT;
import static org.wso2.carbon.mediator.datamapper.config.xml.DataMapperMediatorConstants.EMPTY_STRING;
import static org.wso2.carbon.mediator.datamapper.config.xml.DataMapperMediatorConstants.FUNCTION_CONTEXT;
import static org.wso2.carbon.mediator.datamapper.config.xml.DataMapperMediatorConstants.INPUT_TYPE;
import static org.wso2.carbon.mediator.datamapper.config.xml.DataMapperMediatorConstants.OPERATIONS_CONTEXT;
import static org.wso2.carbon.mediator.datamapper.config.xml.DataMapperMediatorConstants.OUTPUT_TYPE;
import static org.wso2.carbon.mediator.datamapper.config.xml.DataMapperMediatorConstants.SYNAPSE_CONTEXT;
import static org.wso2.carbon.mediator.datamapper.config.xml.DataMapperMediatorConstants.TRANSPORT_CONTEXT;
import static org.wso2.carbon.mediator.datamapper.config.xml.DataMapperMediatorConstants.TRANSPORT_HEADERS;
Expand Down Expand Up @@ -217,12 +220,7 @@ public void setOutputSchemaKey(Value dataMapperOutSchemaKey) {
* @return the input data type
*/
public String getInputType() {
if (inputType != null) {
return inputType;
} else {
log.warn("Input data type not found. Set to default value : " + InputOutputDataType.XML);
return InputOutputDataType.XML.toString();
}
return inputType;
}

/**
Expand All @@ -240,12 +238,7 @@ public void setInputType(String type) {
* @return the output data type
*/
public String getOutputType() {
if (outputType != null) {
return outputType;
} else {
log.warn("Output data type not found. Set to default value : " + InputOutputDataType.XML);
return InputOutputDataType.XML.toString();
}
return outputType;
}

/**
Expand Down Expand Up @@ -303,6 +296,7 @@ public boolean mediate(MessageContext synCtx) {
// mapping resources needed to get the final output
try {
mappingResource = getMappingResource(synCtx, configKey, inSchemaKey, outSchemaKey);
initializeInputOutputType();
} catch (IOException e) {
handleException("DataMapper mediator mapping resource generation failed", e, synCtx);
}
Expand All @@ -311,9 +305,9 @@ public boolean mediate(MessageContext synCtx) {
}

// Does message conversion and gives the final result
transform(synCtx, getInputType(), getOutputType());
transform(synCtx);
//setting output type in the axis2 message context
switch (getOutputType()) {
switch (outputType) {
case "JSON":
((Axis2MessageContext) synCtx).getAxis2MessageContext().setProperty("messageType", "application/json");
((Axis2MessageContext) synCtx).getAxis2MessageContext().setProperty("ContentType", "application/json");
Expand All @@ -327,7 +321,7 @@ public boolean mediate(MessageContext synCtx) {
((Axis2MessageContext) synCtx).getAxis2MessageContext().setProperty("ContentType", "text/xml");
break;
default:
throw new SynapseException("Unsupported output data type found : " + getOutputType());
throw new SynapseException("Unsupported output data type found : " + outputType);
}

if (synLog.isTraceOrDebugEnabled()) {
Expand Down Expand Up @@ -371,10 +365,8 @@ private void checkForXSLTTransformation(MessageContext synCtx) {
* Does message conversion and gives the output message as the final result
*
* @param synCtx the message synCtx
* @param configKey registry location of the mapping configuration
* @param inSchemaKey registry location of the input schema
*/
private void transform(MessageContext synCtx, String configKey, String inSchemaKey) {
private void transform(MessageContext synCtx) {
try {
String outputResult;
if (usingXSLTMapping) {
Expand Down Expand Up @@ -757,4 +749,27 @@ private ScriptEngine getScriptExecutor() {
}
return new ScriptEngineManager().getEngineByName(DataMapperEngineConstants.GRAALJS_ENGINE_NAME);
}

private void initializeInputOutputType() {
if (inputType == null) {
Schema inputSchema = mappingResource.getInputSchema();
if (inputSchema != null && inputSchema.getSchemaMap() != null &&
inputSchema.getSchemaMap().containsKey(INPUT_TYPE)) {
inputType = inputSchema.getSchemaMap().get(INPUT_TYPE).toString();
} else {
log.error("Input type is not defined in the input schema/synapse configuration");
throw new SynapseException("Input type is not defined in the input schema/synapse configuration");
}
}
if (outputType == null) {
Schema outputSchema = mappingResource.getOutputSchema();
if (outputSchema != null && outputSchema.getSchemaMap() != null &&
outputSchema.getSchemaMap().containsKey(OUTPUT_TYPE)) {
outputType = outputSchema.getSchemaMap().get(OUTPUT_TYPE).toString();
} else {
log.error("Output type is not defined in the output schema/synapse configuration");
throw new SynapseException("Output type is not defined in the output schema/synapse configuration");
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -108,14 +108,10 @@ public class DataMapperMediatorFactory extends AbstractMediatorFactory {

if (inputTypeAttribute != null) {
datamapperMediator.setInputType(inputTypeAttribute.getAttributeValue());
} else {
handleException("The input DataType is required for the DataMapper mediator");
}

if (outputTypeAttribute != null) {
datamapperMediator.setOutputType(outputTypeAttribute.getAttributeValue());
} else {
handleException("The output DataType is required for the DataMapper mediator");
}

if (xsltStyleSheetKeyAttribute != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,17 @@ public class DataMapperMediatorSerializer extends AbstractMediatorSerializer {
dataMapperElement);
}

dataMapperElement.addAttribute(fac.createOMAttribute(DataMapperMediatorConstants.INPUT_TYPE, nullNS,
dataMapperMediator.getInputType()));
String inputType = dataMapperMediator.getInputType();
if (inputType != null) {
dataMapperElement.addAttribute(fac.createOMAttribute(DataMapperMediatorConstants.INPUT_TYPE, nullNS,
inputType));
}

dataMapperElement.addAttribute(fac.createOMAttribute(DataMapperMediatorConstants.OUTPUT_TYPE, nullNS,
dataMapperMediator.getOutputType()));
String outputType = dataMapperMediator.getOutputType();
if (outputType != null) {
dataMapperElement.addAttribute(fac.createOMAttribute(DataMapperMediatorConstants.OUTPUT_TYPE, nullNS,
outputType));
}

saveTracingState(dataMapperElement, dataMapperMediator);
serializeComments(dataMapperElement, dataMapperMediator.getCommentsList());
Expand Down

0 comments on commit 55e6b6f

Please sign in to comment.