Hi Standa,

On Aug 2, 2013, at 5:38 AM, Standa Mitrega <[email protected]> wrote:

> Hi
> 
> I'm trying to control message properties in messages processed by Apache 
> Synapse server from script executed in script mediator.
> 
> Using properties it is possible to set property for 4 different scopes:
> <property name="MsgPropertyDef" value="28" scope="default" type="INTEGER"/>
> <property name="MsgPropertyTran" value="28" scope="transport" type="INTEGER"/>
> <property name="MsgPropertyAx" value="28" scope="axis2" type="INTEGER"/>
> <property name="MsgPropertyAxCl" value="28" scope="axis2-client" 
> type="INTEGER"/>
> 
> 
> When using script mediator with groovy script:
> """
> import org.apache.synapse.MessageContext;
> import java.io.File;
> import java.util.Map;
> def test1(MessageContext mc) {
>    File f
>    f = new File("/tmp/out.log")
> 
>    Map<String,Object> ce = mc.getContextEntries()
> 
>    Set sp = mc.getPropertyKeySet()
>    f << "PropertyKeySet size: " + sp.size() + "\n"
>    sp.each() { key -> f << "${key} == " + mc.getProperty(key).toString() + 
> "\n" }
> }
> """
> 
> I am able to get to property set by:
> <property name="MsgPropertyDef" value="28" scope="default" type="INTEGER"/>
> But I am not able to get to properties defined in other scopes - Axis2, Axis2 
> - client, Transport.
> 
> Question:
> Is it possible from scipt mediator get to properties in scope transport / 
> axis2 / axis2-client?
> 
> 
> 
> What I found:
> In 
> 'http://www.docjar.org/html/api/org/apache/synapse/mediators/builtin/PropertyMediator.java.html'
>  there is following code:
>   87               if (scope == null || 
> XMLConfigConstants.SCOPE_DEFAULT.equals(scope)) {
>   88                   //Setting property into the Synapse Context
>   89                   synCtx.setProperty(name, resultValue);
>   90
>   91               } else if (XMLConfigConstants.SCOPE_AXIS2.equals(scope)
>   92                       && synCtx instanceof Axis2MessageContext) {
>   93                   //Setting property into the  Axis2 Message Context
>   94                   Axis2MessageContext axis2smc = (Axis2MessageContext) 
> synCtx;
>   95 org.apache.axis2.context.MessageContext axis2MessageCtx =
>   96 axis2smc.getAxis2MessageContext();
>   97                   axis2MessageCtx.setProperty(name, resultValue);
>   98
>   99               } else if (XMLConfigConstants.SCOPE_CLIENT.equals(scope)
>  100                       && synCtx instanceof Axis2MessageContext) {
>  101                   //Setting property into the  Axis2 Message Context 
> client options
>  102                   Axis2MessageContext axis2smc = (Axis2MessageContext) 
> synCtx;
>  103 org.apache.axis2.context.MessageContext axis2MessageCtx =
>  104 axis2smc.getAxis2MessageContext();
>  105 axis2MessageCtx.getOptions().setProperty(name, resultValue);
>  106
>  107               } else if (XMLConfigConstants.SCOPE_TRANSPORT.equals(scope)
>  108                       && synCtx instanceof Axis2MessageContext) {
>  109                   //Setting Transport Headers
>  110                   Axis2MessageContext axis2smc = (Axis2MessageContext) 
> synCtx;
>  111 org.apache.axis2.context.MessageContext axis2MessageCtx =
>  112 axis2smc.getAxis2MessageContext();
>  113                   Object headers = axis2MessageCtx.getProperty(
>  114 org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS);
>  115
>  116                   if (headers != null && headers instanceof Map) {
>  117                       Map headersMap = (HashMap) headers;
>  118                       headersMap.put(name, resultValue);
>  119                   }
>  120                   if (headers == null) {
>  121                       Map headersMap = new HashMap();
>  122                       headersMap.put(name, resultValue);
>  123                       axis2MessageCtx.setProperty(
>  124 org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS,
>  125                               headersMap);
>  126                   }
>  127               }
> 
> 
> IMO in the code I can see how one can get to various scopes - SCOPE_DEFAULT, 
> SCOPE_AXIS2, SCOPE_CLIENT, SCOPE_TRANSPORT. BUT:
> 1. default scope:
>   89                   synCtx.setProperty(name, resultValue);
> synCtx is instance of: org.apache.synapse.MessageContext;
> 2. other scopes:
> for all other scopes 'synCtx' must be instance of 
> org.apache.synapse.core.axis2.Axis2MessageContext.
> BUT in script mediator (see sample at the top) 'mc' is instance of 
> org.apache.synapse.MessageContext. But it is not an instance of 
> org.apache.synapse.core.axis2.Axis2MessageContext. It is instance of 
> org.apache.synapse.mediators.bsf.ScriptMessageContext so there is not 
> available IMHO key method 'axis2smc.getAxis2MessageContext()'.
> Is it possible to get to 'axis2smc.getAxis2MessageContext()' different way in 
> script mediator?

Your analysis correct. It looks like only the properties set in the default 
context can be manipulated using the script mediator. If it's a requirement to 
access other scopes from the script mediator, we can certainly include that in 
the next release. You can report an issue for this at 
https://issues.apache.org/jira/browse/SYNAPSE

Thanks,
Hiranya

> 
> Best regards
> Standa
> 

--
Hiranya Jayathilaka
Mayhem Lab/RACE Lab;
Dept. of Computer Science, UCSB;  http://cs.ucsb.edu
E-mail: [email protected];  Mobile: +1 (805) 895-7443
Blog: http://techfeast-hiranya.blogspot.com

Reply via email to