Search in sources :

Example 31 with Axis2MessageContext

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

the class DynamicLoadBalanceEndpointTest method createMessageContext.

/**
 * Create a empty message context
 *
 * @return A context with empty message
 * @throws AxisFault on an error creating a context
 */
private MessageContext createMessageContext() throws AxisFault {
    Axis2SynapseEnvironment synapseEnvironment = new Axis2SynapseEnvironment(new SynapseConfiguration());
    org.apache.axis2.context.MessageContext axis2MC = new org.apache.axis2.context.MessageContext();
    axis2MC.setConfigurationContext(new ConfigurationContext(new AxisConfiguration()));
    ServiceContext svcCtx = new ServiceContext();
    OperationContext opCtx = new OperationContext(new InOutAxisOperation(), svcCtx);
    axis2MC.setServiceContext(svcCtx);
    axis2MC.setOperationContext(opCtx);
    axis2MC.setTransportIn(new TransportInDescription("http"));
    axis2MC.setTo(new EndpointReference("http://localhost:9000/services/SimpleStockQuoteService"));
    MessageContext mc = new Axis2MessageContext(axis2MC, new SynapseConfiguration(), synapseEnvironment);
    mc.setMessageID(UIDGenerator.generateURNString());
    mc.setEnvelope(OMAbstractFactory.getSOAP12Factory().createSOAPEnvelope());
    mc.getEnvelope().addChild(OMAbstractFactory.getSOAP12Factory().createSOAPBody());
    return mc;
}
Also used : OperationContext(org.apache.axis2.context.OperationContext) ConfigurationContext(org.apache.axis2.context.ConfigurationContext) AxisConfiguration(org.apache.axis2.engine.AxisConfiguration) ServiceContext(org.apache.axis2.context.ServiceContext) TransportInDescription(org.apache.axis2.description.TransportInDescription) SynapseConfiguration(org.apache.synapse.config.SynapseConfiguration) EndpointReference(org.apache.axis2.addressing.EndpointReference) Axis2SynapseEnvironment(org.apache.synapse.core.axis2.Axis2SynapseEnvironment) MessageContext(org.apache.synapse.MessageContext) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext) InOutAxisOperation(org.apache.axis2.description.InOutAxisOperation) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext)

Example 32 with Axis2MessageContext

use of org.apache.synapse.core.axis2.Axis2MessageContext 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 33 with Axis2MessageContext

use of org.apache.synapse.core.axis2.Axis2MessageContext 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 34 with Axis2MessageContext

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

the class RecipientListEndpointTest method getMessageContext.

/**
 * Create a sample synapse message context with a simple payload
 *
 * @param payload payload of the envelope of message context
 * @return Axis2MessageContext with payload and parameters
 * @throws Exception on creating the context
 */
private Axis2MessageContext getMessageContext(String payload) throws Exception {
    Map<String, Entry> properties = new HashMap<>();
    Axis2MessageContext messageContext = TestUtils.getAxis2MessageContext(payload, properties);
    messageContext.getAxis2MessageContext().setTransportIn(new TransportInDescription("http"));
    return messageContext;
}
Also used : Entry(org.apache.synapse.config.Entry) HashMap(java.util.HashMap) TransportInDescription(org.apache.axis2.description.TransportInDescription) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext)

Example 35 with Axis2MessageContext

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

the class RecipientListEndpointTest method testSendToEndpointList.

/**
 * Create a RecipientListEndpoint by config and test sending a message
 * @throws Exception on an issue sending out the message
 */
@Test
public void testSendToEndpointList() throws Exception {
    OMElement omBody = AXIOMUtil.stringToOM("<endpoint><recipientlist xmlns=\"http://ws.apache.org/ns/synapse\">\n" + "    <endpoint xmlns=\"http://ws.apache.org/ns/synapse\">\n" + "            <address uri=\"http://localhost:9000/services/SimpleStockQuoteService\"/>\n" + "    </endpoint>\n" + "    <endpoint xmlns=\"http://ws.apache.org/ns/synapse\">\n" + "            <address uri=\"http://localhost:9001/services/SimpleStockQuoteService\"/>\n" + "    </endpoint>\n" + "</recipientlist></endpoint>");
    RecipientListEndpoint recipientListEndpoint = (RecipientListEndpoint) EndpointFactory.getEndpointFromElement(omBody, true, null);
    Axis2SynapseEnvironment synapseEnvironment = getMockedSynapseEnvironment();
    Mockito.when(synapseEnvironment.createMessageContext()).thenReturn(createMessageContext());
    // test send message
    String samplePayload = "<test><a>WSO2</a></test>";
    Axis2MessageContext messageContext = getMessageContext(samplePayload);
    // message will be sent to EP using this env (which is mocked and do nothing)
    messageContext.setEnvironment(synapseEnvironment);
    recipientListEndpoint.init(synapseEnvironment);
    recipientListEndpoint.sendMessage(messageContext);
}
Also used : Axis2SynapseEnvironment(org.apache.synapse.core.axis2.Axis2SynapseEnvironment) OMElement(org.apache.axiom.om.OMElement) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext) 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