org.apache.synapse.mediators.bsf

## Class ScriptMediator

- java.lang.Object
- - org.apache.synapse.mediators.AbstractMediator
  - - org.apache.synapse.mediators.bsf.ScriptMediator

- All Implemented Interfaces:
    org.apache.synapse.aspects.AspectConfigurable, org.apache.synapse.Mediator, org.apache.synapse.SynapseArtifact

      public class ScriptMediator
      extends org.apache.synapse.mediators.AbstractMediator

  A Synapse mediator that calls a function in any scripting language supported by the BSF. The ScriptMediator supports scripts specified in-line or those loaded through a registry

          <script [key="entry-key"]
            [function="script-function-name"] language="nashornJs|javascript|groovy|ruby">
            (text | xml)?
          </script>

  The function is an optional attribute defining the name of the script function to call, if not specified it defaults to a function named 'mediate'. The function takes a single parameter which is the Synapse MessageContext. The function may return a boolean, if it does not then true is assumed.

- - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `protected javax.script.ScriptEngine` | `jsEngine` The BSF engine created to validate each JSON payload
    `protected javax.script.ScriptEngine` | `scriptEngine` The BSF engine created to process each message through the script

    - ### Fields inherited from class org.apache.synapse.mediators.AbstractMediator

      `log, trace, traceState`

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `ScriptMediator(java.lang.String language, java.util.Map<org.apache.synapse.mediators.Value,java.lang.Object> includeKeysMap, org.apache.synapse.mediators.Value key, java.lang.String function, java.lang.ClassLoader classLoader)` Create a script mediator for the given language and given script entry key and function.
    `ScriptMediator(java.lang.String language, java.lang.String scriptSourceCode, java.lang.ClassLoader classLoader)` Create a script mediator for the given language and given script source.

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `java.lang.String` | `getFunction()`
    `java.util.Map<org.apache.synapse.mediators.Value,java.lang.Object>` | `getIncludeMap()`
    `org.apache.synapse.mediators.Value` | `getKey()`
    `java.lang.String` | `getLanguage()`
    `java.lang.ClassLoader` | `getLoader()`
    `ScriptEngineWrapper` | `getNewScriptEngine()`
    `java.lang.String` | `getScriptSrc()`
    `protected void` | `initInlineScript()` Initialise the Mediator for the inline script
    `protected void` | `initScriptEngine()`
    `boolean` | `isContentAltering()`
    `boolean` | `mediate(org.apache.synapse.MessageContext synCtx)` Perform Script mediation.
    `protected ScriptEngineWrapper` | `prepareExternalScript(org.apache.synapse.MessageContext synCtx)` Prepares the mediator for the invocation of an external script
    `void` | `setLoader(java.lang.ClassLoader loader)`

    - ### Methods inherited from class org.apache.synapse.mediators.AbstractMediator

      `auditLog, auditWarn, configure, disableStatistics, disableTracing, divertMediationRoute, enableStatistics, enableTracing, getAspectConfiguration, getCommentsList, getDescription, getInputType, getLastSequenceFaultHandler, getLog, getMediatorName, getMediatorPosition, getOutputType, getRegisteredMediationFlowPoint, getShortDescription, getTraceState, getType, handleException, handleException, isBreakPoint, isContentAware, isSkipEnabled, isStatisticsEnable, isTraceOn, isTraceOrDebugOn, isTracingEnabled, registerMediationFlowPoint, reportCloseStatistics, reportOpenStatistics, setBreakPoint, setCommentsList, setComponentStatisticsId, setDescription, setEffectiveTraceState, setMediatorPosition, setShortDescription, setSkipEnabled, setTraceState, shouldCaptureTracing, shouldTrace, shouldTrace, traceOrDebug, traceOrDebugWarn, unregisterMediationFlowPoint`

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Field Detail

    - #### scriptEngine

          protected javax.script.ScriptEngine scriptEngine

      The BSF engine created to process each message through the script

    - #### jsEngine

          protected javax.script.ScriptEngine jsEngine

      The BSF engine created to validate each JSON payload

  - ### Constructor Detail

    - #### ScriptMediator

          public ScriptMediator(java.lang.String language,
                                java.lang.String scriptSourceCode,
                                java.lang.ClassLoader classLoader)

      Create a script mediator for the given language and given script source.
      Parameters:
        `language` - the BSF language
        `scriptSourceCode` - the source code of the script

    - #### ScriptMediator

          public ScriptMediator(java.lang.String language,
                                java.util.Map<org.apache.synapse.mediators.Value,java.lang.Object> includeKeysMap,
                                org.apache.synapse.mediators.Value key,
                                java.lang.String function,
                                java.lang.ClassLoader classLoader)

      Create a script mediator for the given language and given script entry key and function.
      Parameters:
        `language` - the BSF language
        `includeKeysMap` - Include script keys
        `key` - the registry entry key to load the script
        `function` - the function to be invoked

  - ### Method Detail

    - #### mediate

          public boolean mediate(org.apache.synapse.MessageContext synCtx)

      Perform Script mediation.
      Parameters:
        `synCtx` - the Synapse message context
      Returns:
        the boolean result from the script invocation

    - #### initInlineScript

          protected void initInlineScript()

      Initialise the Mediator for the inline script

    - #### prepareExternalScript

          protected ScriptEngineWrapper prepareExternalScript(org.apache.synapse.MessageContext synCtx)
                                                       throws javax.script.ScriptException

      Prepares the mediator for the invocation of an external script
      Parameters:
        `synCtx` - MessageContext script
      Throws:
        `javax.script.ScriptException` - For any errors , when compile the script

    - #### initScriptEngine

          protected void initScriptEngine()

    - #### getLanguage

          public java.lang.String getLanguage()

    - #### getKey

          public org.apache.synapse.mediators.Value getKey()

    - #### getFunction

          public java.lang.String getFunction()

    - #### getScriptSrc

          public java.lang.String getScriptSrc()

    - #### getIncludeMap

          public java.util.Map<org.apache.synapse.mediators.Value,java.lang.Object> getIncludeMap()

    - #### getLoader

          public java.lang.ClassLoader getLoader()

    - #### setLoader

          public void setLoader(java.lang.ClassLoader loader)

    - #### getNewScriptEngine

          public ScriptEngineWrapper getNewScriptEngine()

    - #### isContentAltering

          public boolean isContentAltering()

      Specified by:
        `isContentAltering` in interface `org.apache.synapse.Mediator`
      Overrides:
        `isContentAltering` in class `org.apache.synapse.mediators.AbstractMediator`

Copyright © 2005-2021 Apache Software Foundation. All Rights Reserved.