Search in sources :

Example 1 with Value

use of org.apache.synapse.mediators.Value in project wso2-synapse by wso2.

the class JmsStore method resolveSecureVaultExpressions.

/**
 * Use secure vault to secure password in JMS Message Store.
 *
 * @param value Value of password from JMS Message Store
 * @return the actual password from the Secure Vault Password Management.
 */
private String resolveSecureVaultExpressions(String value) {
    // Password can be null, it is optional
    if (value == null) {
        return null;
    }
    Matcher lookupMatcher = vaultLookupPattern.matcher(value);
    String resolvedValue = value;
    if (lookupMatcher.find()) {
        Value expression = null;
        // getting the expression with out curly brackets
        String expressionStr = lookupMatcher.group(1);
        try {
            expression = new Value(new SynapseXPath(expressionStr));
        } catch (JaxenException e) {
            throw new SynapseException("Error while building the expression : " + expressionStr, e);
        }
        resolvedValue = expression.evaluateValue(synapseEnvironment.createMessageContext());
        if (StringUtils.isEmpty(resolvedValue)) {
            log.warn("Found Empty value for expression : " + expression.getExpression());
            resolvedValue = "";
        }
    }
    return resolvedValue;
}
Also used : SynapseXPath(org.apache.synapse.util.xpath.SynapseXPath) SynapseException(org.apache.synapse.SynapseException) Matcher(java.util.regex.Matcher) JaxenException(org.jaxen.JaxenException) Value(org.apache.synapse.mediators.Value)

Example 2 with Value

use of org.apache.synapse.mediators.Value in project wso2-synapse by wso2.

the class XSLTMediatorTest method testMultipleKeys.

/**
 * Test with multiple keys including static and dynamic keys
 *
 * @param num number from 0 to 2
 * @throws Exception Exception in case of an error in tests
 */
private void testMultipleKeys(int num) throws Exception {
    String xsltKeyValue = null;
    String path;
    SynapseXPath xpath;
    Value xsltKey;
    XSLTMediator transformMediator = new XSLTMediator();
    // default source, xsltFile, and state of key (dynamic or static)
    String source = "";
    String xsltFile = "";
    boolean isDynamicKey = true;
    // based on source, different XSLTFiles can be used
    if (num == 0) {
        source = SOURCE_STATIC_KEY;
        xsltFile = "static_key.xslt";
        xsltKeyValue = "StaticXsltKey";
        isDynamicKey = false;
    } else if (num == 1) {
        source = SOURCE_DYNAMIC_KEY1;
        xsltFile = "dynamic_key_1.xslt";
        xsltKeyValue = "DynamicXsltKey1";
        isDynamicKey = true;
    } else if (num == 2) {
        source = SOURCE_DYNAMIC_KEY2;
        xsltFile = "dynamic_key_2.xslt";
        xsltKeyValue = "DynamicXsltKey2";
        isDynamicKey = true;
    }
    if (isDynamicKey) {
        path = "//m0:CheckPriceRequest/m0:" + xsltKeyValue;
        xpath = new SynapseXPath(path);
        xpath.addNamespace("m0", "http://services.samples/xsd");
        // Create key from dynamic key (xpath)
        xsltKey = new Value(xpath);
        // set XSLT transformation URL (Xpath)
        transformMediator.setXsltKey(xsltKey);
    } else {
        // static key
        path = xsltKeyValue;
        // set XSLT transformation URL (static)
        setXsltTransformationURL(transformMediator, path);
    }
    // Mediate twice for synCtx
    MessageContext synCtx = new TestMessageContextBuilder().addEntry(xsltKeyValue, getClass().getResource(xsltFile)).setBodyFromString(source).addTextAroundBody().build();
    transformMediator.mediate(synCtx);
    synCtx = new TestMessageContextBuilder().addEntry(xsltKeyValue, getClass().getResource(xsltFile)).setBodyFromString(source).addTextAroundBody().build();
    transformMediator.mediate(synCtx);
}
Also used : SynapseXPath(org.apache.synapse.util.xpath.SynapseXPath) TestMessageContextBuilder(org.apache.synapse.TestMessageContextBuilder) Value(org.apache.synapse.mediators.Value) MessageContext(org.apache.synapse.MessageContext)

Example 3 with Value

use of org.apache.synapse.mediators.Value in project wso2-synapse by wso2.

the class ValidateMediatorTest method createKeyListFromMoreKeys.

/**
 * Create a Value list with given set of static keys
 *
 * @param keyNames Set of static keys to create list
 * @return Key list
 */
private List<Value> createKeyListFromMoreKeys(String... keyNames) {
    List<Value> keyList = new ArrayList<Value>();
    for (String keyName : keyNames) {
        // create static key using given string key name
        Value xsdKey = new Value(keyName);
        keyList.add(xsdKey);
    }
    return keyList;
}
Also used : Value(org.apache.synapse.mediators.Value) ArrayList(java.util.ArrayList)

Example 4 with Value

use of org.apache.synapse.mediators.Value in project wso2-synapse by wso2.

the class PayloadFactoryMediatorSerializerTest method testSerializeSpecificMediator3.

/**
 * Test SerializeSpecificMediator method with Dynamic format added for payloadFactory mediator
 * and assert that dynamic format is set.
 */
@Test
public void testSerializeSpecificMediator3() {
    PayloadFactoryMediatorSerializer serializer = new PayloadFactoryMediatorSerializer();
    PayloadFactoryMediator payloadFactoryMediator = new PayloadFactoryMediator();
    payloadFactoryMediator.setFormat(format);
    payloadFactoryMediator.setFormatDynamic(true);
    payloadFactoryMediator.setFormatKey(new Value("testKey"));
    OMElement element = serializer.serializeSpecificMediator(payloadFactoryMediator);
    Assert.assertNotNull(element);
    MediatorFactory mediatorFactory = new PayloadFactoryMediatorFactory();
    Mediator mediator = mediatorFactory.createMediator(element, null);
    Assert.assertTrue("Dynamic Format set for Mediator is not serialized", ((PayloadFactoryMediator) mediator).isFormatDynamic());
}
Also used : PayloadFactoryMediator(org.apache.synapse.mediators.transform.PayloadFactoryMediator) Value(org.apache.synapse.mediators.Value) OMElement(org.apache.axiom.om.OMElement) PayloadFactoryMediator(org.apache.synapse.mediators.transform.PayloadFactoryMediator) Mediator(org.apache.synapse.Mediator) Test(org.junit.Test)

Example 5 with Value

use of org.apache.synapse.mediators.Value in project wso2-synapse by wso2.

the class EJBMediatorTest method testInitAndMediate.

/**
 * Initializing EJBMediator and Mediating a messageContext
 */
@Test
public void testInitAndMediate() throws Exception {
    SynapseEnvironment synapseEnvironment = Mockito.mock(SynapseEnvironment.class);
    ServerConfigurationInformation configurationInformation = new ServerConfigurationInformation();
    ServerContextInformation contextInformation = new ServerContextInformation(configurationInformation);
    Mockito.when(synapseEnvironment.getServerContextInformation()).thenReturn(contextInformation);
    try {
        ejbMediator.init(synapseEnvironment);
        Assert.fail("executed successfully when exception is expected");
    } catch (Exception ex) {
        Assert.assertEquals("assert exception class", SynapseException.class, ex.getClass());
        Assert.assertEquals("assert exception message", "Initialization failed. BeanstalkManager not found.", ex.getMessage());
    }
    ejbMediator.setBeanstalkName(BEANSTALK_NAME);
    EnterpriseBeanstalkManager beanstalkManager = Mockito.mock(EnterpriseBeanstalkManager.class);
    contextInformation.addProperty(EnterpriseBeanstalkConstants.BEANSTALK_MANAGER_PROP_NAME, beanstalkManager);
    try {
        ejbMediator.init(synapseEnvironment);
        Assert.fail("executed successfully when exception is expected");
    } catch (Exception ex) {
        Assert.assertEquals("assert exception class", SynapseException.class, ex.getClass());
        Assert.assertEquals("assert exception message", "Initialization failed. '" + BEANSTALK_NAME + "' " + "beanstalk not found.", ex.getMessage());
    }
    beanstalk = PowerMockito.mock(EnterpriseBeanstalk.class);
    Value beanId = new Value(VALUE);
    ejbMediator.setBeanId(beanId);
    Object obj = new Object();
    PowerMockito.when(beanstalk.getEnterpriseBean(any(String.class), any(String.class), any(String.class))).thenReturn(obj);
    PowerMockito.when(beanstalkManager.getBeanstalk(BEANSTALK_NAME)).thenReturn(beanstalk);
    PowerMockito.mockStatic(BeanUtils.class);
    PowerMockito.when(BeanUtils.invokeInstanceMethod(any(Object.class), any(Method.class), any(Object[].class))).thenReturn(new Object());
    ejbMediator.init(synapseEnvironment);
    String samplePayload = "<test>value</test>";
    Map<String, Entry> properties = new HashMap<>();
    Axis2MessageContext messageContext = TestUtils.getAxis2MessageContext(samplePayload, properties);
    ejbMediator.setClassName(CLASS_NAME);
    ejbMediator.setJndiName(JNDI_NAME);
    Assert.assertTrue("mediation must be successful", ejbMediator.mediate(messageContext));
}
Also used : SynapseException(org.apache.synapse.SynapseException) HashMap(java.util.HashMap) SynapseEnvironment(org.apache.synapse.core.SynapseEnvironment) EnterpriseBeanstalkManager(org.apache.synapse.commons.beanstalk.enterprise.EnterpriseBeanstalkManager) ServerConfigurationInformation(org.apache.synapse.ServerConfigurationInformation) Method(java.lang.reflect.Method) ExpectedException(org.junit.rules.ExpectedException) SynapseException(org.apache.synapse.SynapseException) EnterpriseBeanstalk(org.apache.synapse.commons.beanstalk.enterprise.EnterpriseBeanstalk) Entry(org.apache.synapse.config.Entry) ServerContextInformation(org.apache.synapse.ServerContextInformation) Value(org.apache.synapse.mediators.Value) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

Value (org.apache.synapse.mediators.Value)38 OMElement (org.apache.axiom.om.OMElement)18 OMAttribute (org.apache.axiom.om.OMAttribute)12 QName (javax.xml.namespace.QName)9 SynapseXPath (org.apache.synapse.util.xpath.SynapseXPath)9 SynapseException (org.apache.synapse.SynapseException)7 Iterator (java.util.Iterator)6 Axis2MessageContext (org.apache.synapse.core.axis2.Axis2MessageContext)6 JaxenException (org.jaxen.JaxenException)6 MessageContext (org.apache.synapse.MessageContext)5 Entry (org.apache.synapse.config.Entry)5 ValueFactory (org.apache.synapse.config.xml.ValueFactory)5 Test (org.junit.Test)4 DataHandler (javax.activation.DataHandler)3 OMText (org.apache.axiom.om.OMText)3 Endpoint (org.apache.synapse.endpoints.Endpoint)3 IOException (java.io.IOException)2 InputStreamReader (java.io.InputStreamReader)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2