Search in sources :

Example 26 with Axis2MessageContext

use of org.apache.synapse.core.axis2.Axis2MessageContext in project wso2-synapse by wso2.

the class RESTMediationTestCase method getMessageContext.

protected MessageContext getMessageContext(SynapseConfiguration synapseConfig, boolean https, String url, String method) throws Exception {
    MessageContext synCtx = TestUtils.createSynapseMessageContext("<foo/>", synapseConfig);
    org.apache.axis2.context.MessageContext msgCtx = ((Axis2MessageContext) synCtx).getAxis2MessageContext();
    if (https) {
        msgCtx.setIncomingTransportName("https");
    } else {
        msgCtx.setIncomingTransportName("http");
    }
    msgCtx.setProperty(Constants.Configuration.HTTP_METHOD, method);
    msgCtx.setProperty(Constants.Configuration.TRANSPORT_IN_URL, url);
    msgCtx.setProperty(NhttpConstants.REST_URL_POSTFIX, url.substring(1));
    return synCtx;
}
Also used : MessageContext(org.apache.synapse.MessageContext) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext)

Example 27 with Axis2MessageContext

use of org.apache.synapse.core.axis2.Axis2MessageContext in project wso2-synapse by wso2.

the class RESTMediationTestCase method addHttpHeader.

protected void addHttpHeader(String name, String value, MessageContext synCtx) {
    org.apache.axis2.context.MessageContext msgCtx = ((Axis2MessageContext) synCtx).getAxis2MessageContext();
    Object obj = msgCtx.getProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS);
    if (obj != null) {
        ((Map) obj).put(name, value);
    } else {
        Map map = new HashMap();
        map.put(name, value);
        msgCtx.setProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS, map);
    }
}
Also used : HashMap(java.util.HashMap) Map(java.util.Map) HashMap(java.util.HashMap) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext)

Example 28 with Axis2MessageContext

use of org.apache.synapse.core.axis2.Axis2MessageContext in project wso2-synapse by wso2.

the class SynapseXPathTest method testTransportHeaders.

public void testTransportHeaders() throws Exception {
    Axis2MessageContext synCtx = TestUtils.getAxis2MessageContext("<test/>", null);
    org.apache.axis2.context.MessageContext axis2MessageCtx = synCtx.getAxis2MessageContext();
    Object headers = axis2MessageCtx.getProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS);
    String name = "MyHeader";
    String resultValue = "TestValue";
    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);
    }
    assertEquals(resultValue, new SynapseXPath("$trp:" + name).evaluate(synCtx));
}
Also used : SynapseXPath(org.apache.synapse.util.xpath.SynapseXPath) HashMap(java.util.HashMap) Map(java.util.Map) HashMap(java.util.HashMap) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext)

Example 29 with Axis2MessageContext

use of org.apache.synapse.core.axis2.Axis2MessageContext in project wso2-synapse by wso2.

the class NashornJavaScriptMessageContext method removeProperty.

/**
 * Remove property from the message.
 *
 * @param key unique identifier of property
 * @param scope scope of the property
 */
public void removeProperty(String key, String scope) {
    if (scope == null || XMLConfigConstants.SCOPE_DEFAULT.equals(scope)) {
        Set pros = mc.getPropertyKeySet();
        if (pros != null) {
            pros.remove(key);
        }
    } else if (XMLConfigConstants.SCOPE_AXIS2.equals(scope)) {
        // Removing property from the Axis2 Message Context
        Axis2MessageContext axis2smc = (Axis2MessageContext) mc;
        org.apache.axis2.context.MessageContext axis2MessageCtx = axis2smc.getAxis2MessageContext();
        axis2MessageCtx.removeProperty(key);
    } else if (XMLConfigConstants.SCOPE_TRANSPORT.equals(scope)) {
        // Removing transport headers
        Axis2MessageContext axis2smc = (Axis2MessageContext) mc;
        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(key);
        }
    } else if (XMLConfigConstants.SCOPE_OPERATION.equals(scope)) {
        // Removing operation scope headers
        Axis2MessageContext axis2smc = (Axis2MessageContext) mc;
        org.apache.axis2.context.MessageContext axis2MessageCtx = axis2smc.getAxis2MessageContext();
        OperationContext axis2oc = axis2MessageCtx.getOperationContext();
        axis2oc.removeProperty(key);
    }
}
Also used : OperationContext(org.apache.axis2.context.OperationContext) Set(java.util.Set) 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 30 with Axis2MessageContext

use of org.apache.synapse.core.axis2.Axis2MessageContext in project wso2-synapse by wso2.

the class DynamicLoadBalanceEndpointTest method testSend.

/**
 * Test sending a message to a load balance EP
 *
 * @throws AxisFault on an issue sending message to DynamicLoadBalanceEndpoint
 */
@Test
public void testSend() throws AxisFault {
    DynamicLoadbalanceEndpoint dynamicLoadbalanceEndpoint = new DynamicLoadbalanceEndpoint();
    SynapseEnvironment synapseEnvironment = getMockedSynapseEnvironment();
    dynamicLoadbalanceEndpoint.init(synapseEnvironment);
    MessageContext messageContext = createMessageContext();
    // Mock a LoadBalanceMembershipHandler and set
    LoadBalanceMembershipHandler loadBalanceMembershipHandler = PowerMockito.mock(LoadBalanceMembershipHandler.class);
    Member member1 = new Member("localhost", 9000);
    Mockito.when(loadBalanceMembershipHandler.getNextApplicationMember(any(AlgorithmContext.class))).thenReturn(member1);
    dynamicLoadbalanceEndpoint.setLoadBalanceMembershipHandler(loadBalanceMembershipHandler);
    // set mocked SynapseEnvironment to message context
    ((Axis2MessageContext) messageContext).getAxis2MessageContext().getConfigurationContext().getAxisConfiguration().addParameter(SynapseConstants.SYNAPSE_ENV, synapseEnvironment);
    messageContext.setEnvironment(synapseEnvironment);
    // send message
    dynamicLoadbalanceEndpoint.send(messageContext);
    Assert.assertNotNull("SynapseConstants.LAST_ENDPOINT should return a not null value", messageContext.getProperty(SynapseConstants.LAST_ENDPOINT));
}
Also used : AlgorithmContext(org.apache.synapse.endpoints.algorithms.AlgorithmContext) Axis2SynapseEnvironment(org.apache.synapse.core.axis2.Axis2SynapseEnvironment) SynapseEnvironment(org.apache.synapse.core.SynapseEnvironment) LoadBalanceMembershipHandler(org.apache.synapse.core.LoadBalanceMembershipHandler) MessageContext(org.apache.synapse.MessageContext) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext) Member(org.apache.axis2.clustering.Member) Test(org.junit.Test)

Aggregations

Axis2MessageContext (org.apache.synapse.core.axis2.Axis2MessageContext)92 MessageContext (org.apache.synapse.MessageContext)50 Axis2SynapseEnvironment (org.apache.synapse.core.axis2.Axis2SynapseEnvironment)24 Map (java.util.Map)23 HashMap (java.util.HashMap)20 SynapseConfiguration (org.apache.synapse.config.SynapseConfiguration)19 SynapseEnvironment (org.apache.synapse.core.SynapseEnvironment)18 Test (org.junit.Test)16 OMElement (org.apache.axiom.om.OMElement)15 ConfigurationContext (org.apache.axis2.context.ConfigurationContext)14 SynapseException (org.apache.synapse.SynapseException)14 AxisConfiguration (org.apache.axis2.engine.AxisConfiguration)13 ArrayList (java.util.ArrayList)12 SOAPEnvelope (org.apache.axiom.soap.SOAPEnvelope)11 EndpointReference (org.apache.axis2.addressing.EndpointReference)10 SynapseLog (org.apache.synapse.SynapseLog)9 Endpoint (org.apache.synapse.endpoints.Endpoint)9 OperationContext (org.apache.axis2.context.OperationContext)8 AxisFault (org.apache.axis2.AxisFault)7 Entry (org.apache.synapse.config.Entry)7