Search in sources :

Example 1 with Registry

use of org.apache.synapse.registry.Registry in project wso2-synapse by wso2.

the class PropertyMediator method mediate.

/**
 * Sets a property into the current (local) Synapse Context or into the Axis Message Context
 * or into Transports Header and removes above properties from the corresponding locations.
 *
 * @param synCtx the message context
 * @return true always
 */
public boolean mediate(MessageContext synCtx) {
    if (synCtx.getEnvironment().isDebuggerEnabled()) {
        if (super.divertMediationRoute(synCtx)) {
            return true;
        }
    }
    SynapseLog synLog = getLog(synCtx);
    if (synLog.isTraceOrDebugEnabled()) {
        synLog.traceOrDebug("Start : Property mediator");
        if (synLog.isTraceTraceEnabled()) {
            synLog.traceTrace("Message : " + synCtx.getEnvelope());
        }
    }
    if (action == ACTION_SET) {
        Object resultValue = getResultValue(synCtx);
        // choose part of it
        if (resultValue instanceof String && pattern != null) {
            resultValue = getMatchedValue((String) resultValue, synLog);
        }
        if (synLog.isTraceOrDebugEnabled()) {
            synLog.traceOrDebug("Setting property : " + name + " at scope : " + (scope == null ? "default" : scope) + " to : " + resultValue + " (i.e. " + (value != null ? "constant : " + value : "result of expression : " + expression) + ")");
        }
        if (scope == null || XMLConfigConstants.SCOPE_DEFAULT.equals(scope)) {
            // Setting property into the  Synapse Context
            if (resultValue != null && resultValue instanceof OMElement) {
                ((OMElement) resultValue).build();
            }
            synCtx.setProperty(name, resultValue);
        } else if (XMLConfigConstants.SCOPE_AXIS2.equals(scope) && synCtx instanceof Axis2MessageContext) {
            // Setting property into the  Axis2 Message Context
            Axis2MessageContext axis2smc = (Axis2MessageContext) synCtx;
            org.apache.axis2.context.MessageContext axis2MessageCtx = axis2smc.getAxis2MessageContext();
            axis2MessageCtx.setProperty(name, resultValue);
            MediatorPropertyUtils.handleSpecialProperties(name, resultValue, axis2MessageCtx);
        } else if (XMLConfigConstants.SCOPE_CLIENT.equals(scope) && synCtx instanceof Axis2MessageContext) {
            // Setting property into the  Axis2 Message Context client options
            Axis2MessageContext axis2smc = (Axis2MessageContext) synCtx;
            org.apache.axis2.context.MessageContext axis2MessageCtx = axis2smc.getAxis2MessageContext();
            axis2MessageCtx.getOptions().setProperty(name, resultValue);
        } else if (XMLConfigConstants.SCOPE_TRANSPORT.equals(scope) && synCtx instanceof Axis2MessageContext) {
            // Setting Transport Headers
            Axis2MessageContext axis2smc = (Axis2MessageContext) synCtx;
            org.apache.axis2.context.MessageContext axis2MessageCtx = axis2smc.getAxis2MessageContext();
            Object headers = axis2MessageCtx.getProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS);
            /*
                 * if null is passed as header value at AbstractHTTPSender in Axis2 when header
                 * value is read causes a null-pointer issue
                 */
            if (resultValue == null) {
                resultValue = "";
            }
            if (headers != null && headers instanceof Map) {
                Map headersMap = (Map) headers;
                headersMap.put(name, resultValue);
            }
            if (headers == null) {
                Map headersMap = new HashMap();
                headersMap.put(name, resultValue);
                axis2MessageCtx.setProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS, headersMap);
            }
        } else if (XMLConfigConstants.SCOPE_OPERATION.equals(scope) && synCtx instanceof Axis2MessageContext) {
            // Setting Transport Headers
            Axis2MessageContext axis2smc = (Axis2MessageContext) synCtx;
            org.apache.axis2.context.MessageContext axis2MessageCtx = axis2smc.getAxis2MessageContext();
            axis2smc.getAxis2MessageContext().getOperationContext().setProperty(name, resultValue);
        } else if (XMLConfigConstants.SCOPE_REGISTRY.equals(scope) && synCtx instanceof Axis2MessageContext) {
            String[] args = name.split("@");
            String path = "";
            String propertyName = "";
            // with the property mentioned and the value as its value
            if (args.length == 1) {
                path = args[0];
            } else if (args.length == 2) {
                path = args[0];
                propertyName = args[1];
            }
            Registry registry = synCtx.getConfiguration().getRegistry();
            registry.newNonEmptyResource(path, false, CONTENT_TYPE, resultValue.toString(), propertyName);
        }
    } else {
        if (synLog.isTraceOrDebugEnabled()) {
            synLog.traceOrDebug("Removing property : " + name + " (scope:" + (scope == null ? "default" : scope) + ")");
        }
        if (scope == null || XMLConfigConstants.SCOPE_DEFAULT.equals(scope)) {
            // Removing property from the  Synapse Context
            Set pros = synCtx.getPropertyKeySet();
            if (pros != null) {
                pros.remove(name);
            }
        } else if (XMLConfigConstants.SCOPE_AXIS2.equals(scope) && synCtx instanceof Axis2MessageContext) {
            // Removing property from the Axis2 Message Context
            Axis2MessageContext axis2smc = (Axis2MessageContext) synCtx;
            org.apache.axis2.context.MessageContext axis2MessageCtx = axis2smc.getAxis2MessageContext();
            axis2MessageCtx.removeProperty(name);
        } else if (XMLConfigConstants.SCOPE_CLIENT.equals(scope) && synCtx instanceof Axis2MessageContext) {
            // Removing property from the Axis2-client Message Context
            Axis2MessageContext axis2smc = (Axis2MessageContext) synCtx;
            org.apache.axis2.context.MessageContext axis2MessageCtx = axis2smc.getAxis2MessageContext();
            // Property value is set to null since axis2MessageCtx.getOptions()
            // does not have an option to remove properties
            axis2MessageCtx.getOptions().setProperty(name, null);
        } else if (XMLConfigConstants.SCOPE_TRANSPORT.equals(scope) && synCtx instanceof Axis2MessageContext) {
            // Removing transport headers
            Axis2MessageContext axis2smc = (Axis2MessageContext) synCtx;
            org.apache.axis2.context.MessageContext axis2MessageCtx = axis2smc.getAxis2MessageContext();
            Object headers = axis2MessageCtx.getProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS);
            if (headers != null && headers instanceof Map) {
                Map headersMap = (Map) headers;
                headersMap.remove(name);
            } else {
                synLog.traceOrDebug("No transport headers found for the message");
            }
        } else if (XMLConfigConstants.SCOPE_OPERATION.equals(scope) && synCtx instanceof Axis2MessageContext) {
            // Removing operation scope headers
            Axis2MessageContext axis2smc = (Axis2MessageContext) synCtx;
            org.apache.axis2.context.MessageContext axis2MessageCtx = axis2smc.getAxis2MessageContext();
            OperationContext axis2oc = axis2MessageCtx.getOperationContext();
            axis2oc.removeProperty(name);
        }
    }
    synLog.traceOrDebug("End : Property mediator");
    return true;
}
Also used : OperationContext(org.apache.axis2.context.OperationContext) Set(java.util.Set) HashMap(java.util.HashMap) OMElement(org.apache.axiom.om.OMElement) Registry(org.apache.synapse.registry.Registry) SynapseLog(org.apache.synapse.SynapseLog) MessageContext(org.apache.synapse.MessageContext) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext) HashMap(java.util.HashMap) Map(java.util.Map) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext)

Example 2 with Registry

use of org.apache.synapse.registry.Registry in project wso2-synapse by wso2.

the class GetPropertyFunction method evaluate.

/**
 * Returns the string value of the property using arg one as key and arg two as scope
 *
 * @param scopeObject scope will decide from where property will be picked up from
 *        i.e. axis2, transport, default/synapse
 * @param keyObject the key of the property
 * @param navigator object model which can be used for navigation around
 * @param dateformat The dateformat that need to convert
 * @return The String value of property using arg one as key and arg two as scope
 */
public Object evaluate(Object scopeObject, Object keyObject, Object dateformat, Navigator navigator) {
    boolean traceOn = synCtx.getTracingState() == SynapseConstants.TRACING_ON;
    boolean traceOrDebugOn = traceOn || log.isDebugEnabled();
    String scope = StringFunction.evaluate(scopeObject, navigator);
    String key = StringFunction.evaluate(keyObject, navigator);
    if (key == null || "".equals(key)) {
        if (traceOrDebugOn) {
            traceOrDebug(traceOn, "property-name should be provided when executing synapse:get-property" + "(scope,prop-name) or synapse:get-property(prop-name) Xpath function");
        }
        return NULL_STRING;
    }
    if (SynapseConstants.SYSTEM_DATE.equals(key)) {
        if (dateformat != null) {
            Format formatter = new SimpleDateFormat(dateformat.toString());
            return formatter.format(new java.util.Date());
        } else {
            Format formatter = new SimpleDateFormat();
            return formatter.format(new java.util.Date());
        }
    }
    // return the current system time as a string , don't care scope
    if (SynapseConstants.SYSTEM_TIME.equals(key)) {
        return Long.toString(System.currentTimeMillis());
    }
    if (XMLConfigConstants.SCOPE_DEFAULT.equals(scope)) {
        if (SynapseConstants.HEADER_TO.equals(key)) {
            EndpointReference toEPR = synCtx.getTo();
            if (toEPR != null) {
                return toEPR.getAddress();
            } else {
                return NULL_STRING;
            }
        } else if (SynapseConstants.HEADER_FROM.equals(key)) {
            EndpointReference fromEPR = synCtx.getFrom();
            if (fromEPR != null) {
                return fromEPR.getAddress();
            } else {
                return NULL_STRING;
            }
        } else if (SynapseConstants.HEADER_ACTION.equals(key)) {
            String wsaAction = synCtx.getWSAAction();
            if (wsaAction != null) {
                return wsaAction;
            } else {
                return NULL_STRING;
            }
        } else if (SynapseConstants.HEADER_FAULT.equals(key)) {
            EndpointReference faultEPR = synCtx.getFaultTo();
            if (faultEPR != null) {
                return faultEPR.getAddress();
            } else {
                return NULL_STRING;
            }
        } else if (SynapseConstants.HEADER_REPLY_TO.equals(key)) {
            EndpointReference replyToEPR = synCtx.getReplyTo();
            if (replyToEPR != null) {
                return replyToEPR.getAddress();
            } else {
                return NULL_STRING;
            }
        } else if (SynapseConstants.HEADER_RELATES_TO.equals(key)) {
            RelatesTo relatesTo = synCtx.getRelatesTo();
            if (relatesTo != null) {
                return relatesTo.getValue();
            } else {
                return NULL_STRING;
            }
        } else if (SynapseConstants.HEADER_MESSAGE_ID.equals(key)) {
            String messageID = synCtx.getMessageID();
            if (messageID != null) {
                return messageID;
            } else {
                return NULL_STRING;
            }
        } else if (SynapseConstants.PROPERTY_FAULT.equals(key)) {
            if (synCtx.getEnvelope().hasFault()) {
                return SynapseConstants.TRUE;
            } else if (synCtx instanceof Axis2MessageContext) {
                org.apache.axis2.context.MessageContext axis2MessageContext = ((Axis2MessageContext) synCtx).getAxis2MessageContext();
                if (axis2MessageContext.getProperty(BaseConstants.FAULT_MESSAGE) != null && SynapseConstants.TRUE.equals(axis2MessageContext.getProperty(BaseConstants.FAULT_MESSAGE))) {
                    return SynapseConstants.TRUE;
                }
            } else {
                return NULL_STRING;
            }
        } else if (SynapseConstants.PROPERTY_MESSAGE_FORMAT.equals(key)) {
            if (synCtx.isDoingPOX())
                return SynapseConstants.FORMAT_POX;
            else if (synCtx.isDoingGET())
                return SynapseConstants.FORMAT_GET;
            else if (synCtx.isSOAP11())
                return SynapseConstants.FORMAT_SOAP11;
            else
                return SynapseConstants.FORMAT_SOAP12;
        } else if (SynapseConstants.PROPERTY_OPERATION_NAME.equals(key) || SynapseConstants.PROPERTY_OPERATION_NAMESPACE.equals(key)) {
            if (synCtx instanceof Axis2MessageContext) {
                AxisOperation axisOperation = ((Axis2MessageContext) synCtx).getAxis2MessageContext().getAxisOperation();
                if (axisOperation != null) {
                    if (SynapseConstants.PROPERTY_OPERATION_NAMESPACE.equals(key)) {
                        return axisOperation.getName().getNamespaceURI();
                    } else {
                        return axisOperation.getName().getLocalPart();
                    }
                }
            }
        } else {
            Object result = synCtx.getProperty(key);
            if (result != null) {
                return result;
            } else {
                return synCtx.getLocalEntry(key);
            }
        }
    } else if (XMLConfigConstants.SCOPE_AXIS2.equals(scope) && synCtx instanceof Axis2MessageContext) {
        org.apache.axis2.context.MessageContext axis2MessageContext = ((Axis2MessageContext) synCtx).getAxis2MessageContext();
        return axis2MessageContext.getProperty(key);
    } else if (XMLConfigConstants.SCOPE_FUNC.equals(scope)) {
        Stack<TemplateContext> functionStack = (Stack) synCtx.getProperty(SynapseConstants.SYNAPSE__FUNCTION__STACK);
        TemplateContext topCtxt = functionStack.peek();
        if (topCtxt != null) {
            return topCtxt.getParameterValue(key);
        }
    } else if (XMLConfigConstants.SCOPE_TRANSPORT.equals(scope) && synCtx instanceof Axis2MessageContext) {
        org.apache.axis2.context.MessageContext axis2MessageContext = ((Axis2MessageContext) synCtx).getAxis2MessageContext();
        Object headers = axis2MessageContext.getProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS);
        if (headers != null && headers instanceof Map) {
            Map headersMap = (Map) headers;
            return headersMap.get(key);
        }
    } else if (XMLConfigConstants.SCOPE_OPERATION.equals(scope) && synCtx instanceof Axis2MessageContext) {
        Axis2MessageContext axis2smc = (Axis2MessageContext) synCtx;
        org.apache.axis2.context.MessageContext axis2MessageCtx = axis2smc.getAxis2MessageContext();
        return axis2smc.getAxis2MessageContext().getOperationContext().getProperty(key);
    } else if (XMLConfigConstants.SCOPE_REGISTRY.equals(scope)) {
        String[] regParam = key.split("@");
        String regPath = null;
        String propName = null;
        if (regParam.length == 2) {
            regPath = regParam[0];
            propName = regParam[1];
        } else if (regParam.length == 1) {
            regPath = regParam[0];
        }
        Entry propEntry = synCtx.getConfiguration().getEntryDefinition(regPath);
        if (propEntry == null) {
            propEntry = new Entry();
            propEntry.setType(Entry.REMOTE_ENTRY);
            propEntry.setKey(key);
        }
        Registry registry = synCtx.getConfiguration().getRegistry();
        if (registry != null) {
            registry.getResource(propEntry, new Properties());
            if (propName != null) {
                Properties reqProperties = propEntry.getEntryProperties();
                if (reqProperties != null) {
                    if (reqProperties.get(propName) != null) {
                        return reqProperties.getProperty(propName);
                    }
                }
            } else if (propEntry.getValue() != null) {
                if (propEntry.getValue() instanceof OMText) {
                    return ((OMText) propEntry.getValue()).getText();
                }
                return propEntry.getValue().toString();
            }
        }
    } else if (XMLConfigConstants.SCOPE_SYSTEM.equals(scope)) {
        String propVal = System.getProperty(key);
        if (propVal != null) {
            return propVal;
        } else {
            if (traceOrDebugOn) {
                traceOrDebug(traceOn, "System property " + key + " not found");
            }
            return NULL_STRING;
        }
    } else {
        if (traceOrDebugOn) {
            traceOrDebug(traceOn, "Invalid scope : '" + scope + "' has been set for the " + "synapse:get-property(scope,prop-name) XPath function");
        }
    }
    return NULL_STRING;
}
Also used : AxisOperation(org.apache.axis2.description.AxisOperation) Properties(java.util.Properties) RelatesTo(org.apache.axis2.addressing.RelatesTo) Entry(org.apache.synapse.config.Entry) Format(java.text.Format) SimpleDateFormat(java.text.SimpleDateFormat) OMText(org.apache.axiom.om.OMText) MessageContext(org.apache.synapse.MessageContext) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext) Registry(org.apache.synapse.registry.Registry) TemplateContext(org.apache.synapse.mediators.template.TemplateContext) EndpointReference(org.apache.axis2.addressing.EndpointReference) Stack(java.util.Stack) SimpleDateFormat(java.text.SimpleDateFormat) Map(java.util.Map)

Example 3 with Registry

use of org.apache.synapse.registry.Registry in project wso2-synapse by wso2.

the class SimpleURLRegistryTest method testLargeFile.

public void testLargeFile() throws Exception {
    Registry reg = new SimpleURLRegistry();
    Properties props = new Properties();
    props.put("root", "file:./");
    props.put("cachableDuration", "1500");
    reg.init(props);
    OMNode node = reg.lookup(FILE2);
    node.serialize(new NullOutputStream());
}
Also used : Registry(org.apache.synapse.registry.Registry) Properties(java.util.Properties) NullOutputStream(org.apache.commons.io.output.NullOutputStream)

Example 4 with Registry

use of org.apache.synapse.registry.Registry in project wso2-synapse by wso2.

the class SynapseXMLConfigurationSerializerTest method testSerializeConfiguration2.

/**
 * Test serializeConfigurationMethod with registry set for SynapseConfiguration and assert OMElement returned
 */
@Test
public void testSerializeConfiguration2() {
    SynapseXMLConfigurationSerializer serializer = new SynapseXMLConfigurationSerializer();
    SynapseConfiguration synapseConfiguration = new SynapseConfiguration();
    Map<String, OMNode> data = new HashMap<>();
    data.put(KEY_DYNAMIC_ENDPOINT_1, TestUtils.createOMElement(DYNAMIC_ENDPOINT_1));
    data.put(KEY_DYNAMIC_SEQUENCE_1, TestUtils.createOMElement(DYNAMIC_SEQUENCE_1));
    Registry registry = new SimpleInMemoryRegistry(data, 8000L);
    synapseConfiguration.setRegistry(registry);
    OMElement element = serializer.serializeConfiguration(synapseConfiguration);
    Assert.assertNotNull("OMElement is not returned", element);
    Assert.assertEquals("definitions", element.getLocalName());
    Assert.assertTrue("Registry added is not serialized.", element.getChildren().next().toString().contains("registry"));
}
Also used : OMNode(org.apache.axiom.om.OMNode) HashMap(java.util.HashMap) SimpleInMemoryRegistry(org.apache.synapse.registry.SimpleInMemoryRegistry) OMElement(org.apache.axiom.om.OMElement) Registry(org.apache.synapse.registry.Registry) SimpleInMemoryRegistry(org.apache.synapse.registry.SimpleInMemoryRegistry) SynapseConfiguration(org.apache.synapse.config.SynapseConfiguration) Test(org.junit.Test)

Example 5 with Registry

use of org.apache.synapse.registry.Registry in project wso2-synapse by wso2.

the class RegistryFactory method createRegistry.

public static Registry createRegistry(OMElement elem, Properties properties) {
    OMAttribute prov = elem.getAttribute(PROVIDER_Q);
    if (prov != null) {
        try {
            Class provider = Class.forName(prov.getAttributeValue());
            Registry registry = (Registry) provider.newInstance();
            registry.init(getProperties(elem, properties));
            return registry;
        } catch (ClassNotFoundException e) {
            handleException("Cannot locate registry provider class : " + prov.getAttributeValue(), e);
        } catch (IllegalAccessException e) {
            handleException("Error instantiating registry provider : " + prov.getAttributeValue(), e);
        } catch (InstantiationException e) {
            handleException("Error instantiating registry provider : " + prov.getAttributeValue(), e);
        }
    } else {
        handleException("The registry 'provider' " + "attribute is required for a registry definition");
    }
    return null;
}
Also used : Registry(org.apache.synapse.registry.Registry) OMAttribute(org.apache.axiom.om.OMAttribute)

Aggregations

Registry (org.apache.synapse.registry.Registry)10 Properties (java.util.Properties)4 HashMap (java.util.HashMap)3 OMElement (org.apache.axiom.om.OMElement)3 OMNode (org.apache.axiom.om.OMNode)3 File (java.io.File)2 Map (java.util.Map)2 MessageContext (org.apache.synapse.MessageContext)2 Entry (org.apache.synapse.config.Entry)2 SynapseConfiguration (org.apache.synapse.config.SynapseConfiguration)2 Axis2MessageContext (org.apache.synapse.core.axis2.Axis2MessageContext)2 SimpleInMemoryRegistry (org.apache.synapse.registry.SimpleInMemoryRegistry)2 Test (org.junit.Test)2 FileInputStream (java.io.FileInputStream)1 Format (java.text.Format)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Set (java.util.Set)1 Stack (java.util.Stack)1 XMLStreamException (javax.xml.stream.XMLStreamException)1 OMAttribute (org.apache.axiom.om.OMAttribute)1