Search in sources :

Example 91 with MessageContext

use of org.apache.synapse.MessageContext in project wso2-synapse by wso2.

the class EndpointViewTest method createEndPointView.

/**
 * Creates a basic EndpointView with a given number of children added to the endpoint.
 *
 * @param numberOfChildren the child endpoints inside the endpoint attached to the end point view. If
 *                         specified as 0, the children list will be null
 * @return the created end point view
 */
private EndpointView createEndPointView(int numberOfChildren) {
    AbstractEndpoint endpoint = new AddressEndpoint();
    MessageContext synCtx = new TestMessageContext();
    synCtx.setConfiguration(new SynapseConfiguration());
    SynapseEnvironment environment = new Axis2SynapseEnvironment(new ConfigurationContext(new AxisConfiguration()), synCtx.getConfiguration());
    if (numberOfChildren > 0) {
        List<Endpoint> children = new ArrayList<>(numberOfChildren);
        for (int i = 0; i < numberOfChildren; i++) {
            AbstractEndpoint child = new AddressEndpoint();
            child.init(environment);
            child.setEnableMBeanStats(true);
            child.setName("endpoint" + i);
            children.add(child);
        }
        endpoint.setChildren(children);
    }
    endpoint.init(environment);
    return new EndpointView("endpoint", endpoint);
}
Also used : ConfigurationContext(org.apache.axis2.context.ConfigurationContext) AxisConfiguration(org.apache.axis2.engine.AxisConfiguration) SynapseEnvironment(org.apache.synapse.core.SynapseEnvironment) Axis2SynapseEnvironment(org.apache.synapse.core.axis2.Axis2SynapseEnvironment) ArrayList(java.util.ArrayList) SynapseConfiguration(org.apache.synapse.config.SynapseConfiguration) Axis2SynapseEnvironment(org.apache.synapse.core.axis2.Axis2SynapseEnvironment) TestMessageContext(org.apache.synapse.TestMessageContext) TestMessageContext(org.apache.synapse.TestMessageContext) MessageContext(org.apache.synapse.MessageContext)

Example 92 with MessageContext

use of org.apache.synapse.MessageContext in project wso2-synapse by wso2.

the class HttpEndpointTest method testQueryParamsAsUnreservedChars.

/**
 * Tests sending sample characters that may be unreserved (@,: etc) as query parameter content
 */
@Test
public void testQueryParamsAsUnreservedChars() throws AxisFault, XMLStreamException {
    HTTPEndpointFactory factory = new HTTPEndpointFactory();
    OMElement em = AXIOMUtil.stringToOM("<endpoint><http method=\"GET\" uri-template=\"http://abc.com?symbol={+query.param.symbol}&amp;user={+query.param.user}\"/></endpoint>");
    EndpointDefinition ep1 = factory.createEndpointDefinition(em);
    HTTPEndpoint httpEndpoint = new HTTPEndpoint();
    httpEndpoint.setHttpMethod("GET");
    httpEndpoint.setDefinition(ep1);
    httpEndpoint.setUriTemplate(UriTemplate.fromTemplate("http://abc.com?symbol={+query.param.symbol}&amp;user={+query.param.user}"));
    SynapseEnvironment synapseEnvironment = getMockedSynapseEnvironment();
    httpEndpoint.init(getMockedSynapseEnvironment());
    MessageContext messageContext = createMessageContext();
    messageContext.setProperty("query.param.symbol", "US:123");
    messageContext.setProperty("query.param.user", "john@gmail");
    // set mocked SynapseEnvironment to message context
    ((Axis2MessageContext) messageContext).getAxis2MessageContext().getConfigurationContext().getAxisConfiguration().addParameter(SynapseConstants.SYNAPSE_ENV, synapseEnvironment);
    messageContext.setEnvironment(synapseEnvironment);
    httpEndpoint.executeEpTypeSpecificFunctions(messageContext);
    Assert.assertEquals("Reserved characters need to be sent in default format without encoding", "http://abc.com?symbol=US:123&amp;user=john@gmail", messageContext.getTo().getAddress().toString());
}
Also used : HTTPEndpointFactory(org.apache.synapse.config.xml.endpoints.HTTPEndpointFactory) Axis2SynapseEnvironment(org.apache.synapse.core.axis2.Axis2SynapseEnvironment) SynapseEnvironment(org.apache.synapse.core.SynapseEnvironment) OMElement(org.apache.axiom.om.OMElement) MessageContext(org.apache.synapse.MessageContext) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext) Test(org.junit.Test)

Example 93 with MessageContext

use of org.apache.synapse.MessageContext in project wso2-synapse by wso2.

the class HttpEndpointTest method testQueryParamsAsReservedChars.

/**
 * Tests sending sample reserved characters(@,:) as query parameter content
 * @throws AxisFault
 * @throws XMLStreamException
 */
@Test
public void testQueryParamsAsReservedChars() throws AxisFault, XMLStreamException {
    HTTPEndpointFactory factory = new HTTPEndpointFactory();
    OMElement em = AXIOMUtil.stringToOM("<endpoint><http method=\"GET\" uri-template=\"http://abc.com?symbol={query.param.symbol}&amp;user={query.param.user}\"/></endpoint>");
    EndpointDefinition ep1 = factory.createEndpointDefinition(em);
    HTTPEndpoint httpEndpoint = new HTTPEndpoint();
    httpEndpoint.setHttpMethod("GET");
    httpEndpoint.setDefinition(ep1);
    httpEndpoint.setUriTemplate(UriTemplate.fromTemplate("http://abc.com?symbol={query.param.symbol}&amp;user={query.param.user}"));
    SynapseEnvironment synapseEnvironment = getMockedSynapseEnvironment();
    httpEndpoint.init(getMockedSynapseEnvironment());
    MessageContext messageContext = createMessageContext();
    messageContext.setProperty("query.param.symbol", "US:123");
    messageContext.setProperty("query.param.user", "john@gmail");
    // set mocked SynapseEnvironment to message context
    ((Axis2MessageContext) messageContext).getAxis2MessageContext().getConfigurationContext().getAxisConfiguration().addParameter(SynapseConstants.SYNAPSE_ENV, synapseEnvironment);
    messageContext.setEnvironment(synapseEnvironment);
    httpEndpoint.executeEpTypeSpecificFunctions(messageContext);
    Assert.assertEquals("Reserved characters need to be encoded", "http://abc.com?symbol=US%3A123&amp;user=john%40gmail", messageContext.getTo().getAddress().toString());
}
Also used : HTTPEndpointFactory(org.apache.synapse.config.xml.endpoints.HTTPEndpointFactory) Axis2SynapseEnvironment(org.apache.synapse.core.axis2.Axis2SynapseEnvironment) SynapseEnvironment(org.apache.synapse.core.SynapseEnvironment) OMElement(org.apache.axiom.om.OMElement) MessageContext(org.apache.synapse.MessageContext) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext) Test(org.junit.Test)

Example 94 with MessageContext

use of org.apache.synapse.MessageContext in project wso2-synapse by wso2.

the class WeightedRRLCAlgorithmTest method testNextEndpoint.

public void testNextEndpoint() {
    MessageContext messageContext = createMessageContext();
    LoadbalanceEndpoint endpoint = createLoadBalanceEndpoint();
    String[] firstTwoRoundsExpected = { "6", "6", "6", "6", "6", "6", "5", "5", "5", "5", "5", "3", "3", "3", "2", "2", "1", "6", "6", "6", "6", "6", "6", "5", "5", "5", "5", "5", "3", "3", "3", "2", "2", "1" };
    LoadbalanceAlgorithm algo = endpoint.getAlgorithm();
    String[] firstTwoRoundsResults = new String[34];
    for (int i = 0; i < 34; i++) {
        Endpoint epr = algo.getNextEndpoint(messageContext, null);
        if (epr instanceof AddressEndpoint) {
            firstTwoRoundsResults[i] = ((AddressEndpoint) epr).getProperty(WeightedRRLCAlgorithm.LB_WEIGHTED_RRLC_WEIGHT).getValue();
        }
    }
    for (int i = 0; i < 34; i++) {
        assertEquals(firstTwoRoundsExpected[i], firstTwoRoundsResults[i]);
    }
    String[] secondTwoRoundsExpected = { "6", "6", "6", "6", "5", "5", "5", "5", "5", "5", "5", "3", "3", "3", "3", "2", "2", "2", "2", "1", "1", "6", "6", "6", "6", "5", "5", "5", "5", "5", "5", "5", "3", "3" };
    String[] secondTwoRoundsResults = new String[34];
    for (int i = 0; i < 34; i++) {
        Endpoint epr = algo.getNextEndpoint(messageContext, null);
        if (epr instanceof AddressEndpoint) {
            secondTwoRoundsResults[i] = ((AddressEndpoint) epr).getProperty(WeightedRRLCAlgorithm.LB_WEIGHTED_RRLC_WEIGHT).getValue();
        }
    }
    for (int i = 0; i < 34; i++) {
        assertEquals(secondTwoRoundsExpected[i], secondTwoRoundsResults[i]);
    }
}
Also used : LoadbalanceEndpoint(org.apache.synapse.endpoints.LoadbalanceEndpoint) AddressEndpoint(org.apache.synapse.endpoints.AddressEndpoint) AddressEndpoint(org.apache.synapse.endpoints.AddressEndpoint) Endpoint(org.apache.synapse.endpoints.Endpoint) LoadbalanceEndpoint(org.apache.synapse.endpoints.LoadbalanceEndpoint) MessageContext(org.apache.synapse.MessageContext) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext) AddressEndpoint(org.apache.synapse.endpoints.AddressEndpoint) Endpoint(org.apache.synapse.endpoints.Endpoint) LoadbalanceEndpoint(org.apache.synapse.endpoints.LoadbalanceEndpoint)

Example 95 with MessageContext

use of org.apache.synapse.MessageContext in project wso2-synapse by wso2.

the class SALSessionsTest method testUpdateWithOldSessionSameName.

/**
 * Test updating session with session id and old session where the old session id is same as current session id
 * @throws Exception
 */
@Test
public void testUpdateWithOldSessionSameName() throws Exception {
    BasicConfigurator.configure();
    SynapseConfiguration synapseConfiguration = new SynapseConfiguration();
    AxisConfiguration axisConfiguration = synapseConfiguration.getAxisConfiguration();
    ConfigurationContext cfgCtx = new ConfigurationContext(axisConfiguration);
    SynapseEnvironment synapseEnvironment = new Axis2SynapseEnvironment(cfgCtx, synapseConfiguration);
    Axis2MessageContext axis2MessageContext = new Axis2MessageContext(new org.apache.axis2.context.MessageContext(), synapseConfiguration, synapseEnvironment);
    MessageContext messageContext = axis2MessageContext;
    Endpoint endpoint = new AddressEndpoint();
    List<Endpoint> endpoints = new ArrayList<>();
    endpoints.add(endpoint);
    messageContext.setProperty(SynapseConstants.PROP_SAL_ENDPOINT_ENDPOINT_LIST, endpoints);
    SALSessions salSessions = SALSessions.getInstance();
    salSessions.initialize(false, cfgCtx);
    SessionInformation oldSessionInfo = new SessionInformation("testSession3", endpoints, 30000);
    messageContext.setProperty(SynapseConstants.PROP_SAL_CURRENT_SESSION_INFORMATION, oldSessionInfo);
    salSessions.updateSession(messageContext, "testSession3");
    SessionInformation sessionInformation = salSessions.getSession("testSession3");
    Assert.assertEquals("Session not updated!", "testSession3", sessionInformation.getId());
}
Also used : AxisConfiguration(org.apache.axis2.engine.AxisConfiguration) ConfigurationContext(org.apache.axis2.context.ConfigurationContext) Axis2SynapseEnvironment(org.apache.synapse.core.axis2.Axis2SynapseEnvironment) SynapseEnvironment(org.apache.synapse.core.SynapseEnvironment) ArrayList(java.util.ArrayList) SynapseConfiguration(org.apache.synapse.config.SynapseConfiguration) Axis2SynapseEnvironment(org.apache.synapse.core.axis2.Axis2SynapseEnvironment) AddressEndpoint(org.apache.synapse.endpoints.AddressEndpoint) AddressEndpoint(org.apache.synapse.endpoints.AddressEndpoint) Endpoint(org.apache.synapse.endpoints.Endpoint) MessageContext(org.apache.synapse.MessageContext) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext) Test(org.junit.Test)

Aggregations

MessageContext (org.apache.synapse.MessageContext)220 Axis2MessageContext (org.apache.synapse.core.axis2.Axis2MessageContext)86 SynapseConfiguration (org.apache.synapse.config.SynapseConfiguration)54 SynapseException (org.apache.synapse.SynapseException)29 TestMessageContextBuilder (org.apache.synapse.TestMessageContextBuilder)26 ArrayList (java.util.ArrayList)24 Axis2SynapseEnvironment (org.apache.synapse.core.axis2.Axis2SynapseEnvironment)24 SynapseXPath (org.apache.synapse.util.xpath.SynapseXPath)24 ConfigurationContext (org.apache.axis2.context.ConfigurationContext)18 SynapseEnvironment (org.apache.synapse.core.SynapseEnvironment)18 OMElement (org.apache.axiom.om.OMElement)17 AxisConfiguration (org.apache.axis2.engine.AxisConfiguration)17 HashMap (java.util.HashMap)16 Mediator (org.apache.synapse.Mediator)16 TestMessageContext (org.apache.synapse.TestMessageContext)16 Map (java.util.Map)15 Properties (java.util.Properties)15 Test (org.junit.Test)15 SOAPEnvelope (org.apache.axiom.soap.SOAPEnvelope)14 QName (javax.xml.namespace.QName)13