diff --git a/components/mediators/datamapper/org.wso2.carbon.mediator.datamapper/src/main/java/org/wso2/carbon/mediator/datamapper/DataMapperMediator.java b/components/mediators/datamapper/org.wso2.carbon.mediator.datamapper/src/main/java/org/wso2/carbon/mediator/datamapper/DataMapperMediator.java index 5872d3fcd3e..0ea8e45855f 100644 --- a/components/mediators/datamapper/org.wso2.carbon.mediator.datamapper/src/main/java/org/wso2/carbon/mediator/datamapper/DataMapperMediator.java +++ b/components/mediators/datamapper/org.wso2.carbon.mediator.datamapper/src/main/java/org/wso2/carbon/mediator/datamapper/DataMapperMediator.java @@ -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; @@ -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; @@ -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; } /** @@ -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; } /** @@ -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); } @@ -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"); @@ -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()) { @@ -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) { @@ -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"); + } + } + } } diff --git a/components/mediators/datamapper/org.wso2.carbon.mediator.datamapper/src/main/java/org/wso2/carbon/mediator/datamapper/config/xml/DataMapperMediatorFactory.java b/components/mediators/datamapper/org.wso2.carbon.mediator.datamapper/src/main/java/org/wso2/carbon/mediator/datamapper/config/xml/DataMapperMediatorFactory.java index fcf818c7ba6..f37e8e12568 100644 --- a/components/mediators/datamapper/org.wso2.carbon.mediator.datamapper/src/main/java/org/wso2/carbon/mediator/datamapper/config/xml/DataMapperMediatorFactory.java +++ b/components/mediators/datamapper/org.wso2.carbon.mediator.datamapper/src/main/java/org/wso2/carbon/mediator/datamapper/config/xml/DataMapperMediatorFactory.java @@ -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) { diff --git a/components/mediators/datamapper/org.wso2.carbon.mediator.datamapper/src/main/java/org/wso2/carbon/mediator/datamapper/config/xml/DataMapperMediatorSerializer.java b/components/mediators/datamapper/org.wso2.carbon.mediator.datamapper/src/main/java/org/wso2/carbon/mediator/datamapper/config/xml/DataMapperMediatorSerializer.java index ee5b9a13f35..4245d3e2b87 100644 --- a/components/mediators/datamapper/org.wso2.carbon.mediator.datamapper/src/main/java/org/wso2/carbon/mediator/datamapper/config/xml/DataMapperMediatorSerializer.java +++ b/components/mediators/datamapper/org.wso2.carbon.mediator.datamapper/src/main/java/org/wso2/carbon/mediator/datamapper/config/xml/DataMapperMediatorSerializer.java @@ -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());