Search in sources :

Example 86 with Axis2MessageContext

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

the class HttpEndpointTest method testQueryParamsWithLegacyEncoding.

/**
 * Test usage of legacy-encoding property where encoded values will be decoded
 * @throws AxisFault
 * @throws XMLStreamException
 */
@Test
public void testQueryParamsWithLegacyEncoding() 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.setLegacySupport(true);
    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%3A123");
    messageContext.setProperty("query.param.user", "john%40G%C3%BCnter");
    // set mocked SynapseEnvironment to message context
    ((Axis2MessageContext) messageContext).getAxis2MessageContext().getConfigurationContext().getAxisConfiguration().addParameter(SynapseConstants.SYNAPSE_ENV, synapseEnvironment);
    messageContext.setEnvironment(synapseEnvironment);
    httpEndpoint.executeEpTypeSpecificFunctions(messageContext);
    Assert.assertEquals("With legacy encoding encoded characters need to be decoded", "http://abc.com?symbol=US:123&amp;user=john@Günter", 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 87 with Axis2MessageContext

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

the class RecipientListEndpointTest method testSendMessageToMembers.

/**
 * Test on Sending messages to different members using RecipientListEndpoint
 */
@Test
public void testSendMessageToMembers() throws Exception {
    // perform init
    Axis2SynapseEnvironment synapseEnvironment = getMockedSynapseEnvironment();
    RecipientListEndpoint recipientListEndpoint = new RecipientListEndpoint();
    recipientListEndpoint.init(synapseEnvironment);
    Mockito.when(synapseEnvironment.createMessageContext()).thenReturn(createMessageContext());
    // set members
    Member member1 = new Member("localhost", 9000);
    Member member2 = new Member("localhost", 9001);
    ArrayList<Member> members = new ArrayList<>(2);
    members.add(member1);
    members.add(member2);
    recipientListEndpoint.setMembers(members);
    // test send message
    String samplePayload = "<test>value</test>";
    Axis2MessageContext messageContext = getMessageContext(samplePayload);
    // message will be sent to EP using this env (which is mocked and do nothing)
    messageContext.setEnvironment(synapseEnvironment);
    messageContext.setTo(new EndpointReference("http://localhost:9000/services/SimpleStockQuoteService"));
    recipientListEndpoint.sendMessage(messageContext);
}
Also used : Axis2SynapseEnvironment(org.apache.synapse.core.axis2.Axis2SynapseEnvironment) ArrayList(java.util.ArrayList) Member(org.apache.axis2.clustering.Member) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext) EndpointReference(org.apache.axis2.addressing.EndpointReference) Test(org.junit.Test)

Example 88 with Axis2MessageContext

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

the class RecipientListEndpointTest 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);
    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) SynapseConfiguration(org.apache.synapse.config.SynapseConfiguration) 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 89 with Axis2MessageContext

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

the class RecipientListEndpointTest method testSendToDynamicMembers.

/**
 * Test on Sending messages to a dynamic EP based on an expression
 *
 * @throws Exception on test failure
 */
@Test
public void testSendToDynamicMembers() throws Exception {
    // perform init
    Axis2SynapseEnvironment synapseEnvironment = getMockedSynapseEnvironment();
    RecipientListEndpoint recipientListEndpoint = new RecipientListEndpoint(2);
    recipientListEndpoint.init(synapseEnvironment);
    Mockito.when(synapseEnvironment.createMessageContext()).thenReturn(createMessageContext());
    // add dynamic EPs
    Value dynamicEPs = new Value(new SynapseXPath("//endpoints"));
    recipientListEndpoint.setDynamicEnpointSet(dynamicEPs);
    // test send message
    String samplePayload = "<test><endpoints>http://localhost:9000/services/SimpleStockQuoteService," + "http://localhost:9001/services/SimpleStockQuoteService" + "</endpoints><body>wso2</body></test>";
    Axis2MessageContext messageContext = getMessageContext(samplePayload);
    // message will be sent to EP using this env (which is mocked and do nothing)
    messageContext.setEnvironment(synapseEnvironment);
    // messageContext.setTo(new EndpointReference("http://localhost:9000/services/SimpleStockQuoteService"));
    recipientListEndpoint.sendMessage(messageContext);
}
Also used : Axis2SynapseEnvironment(org.apache.synapse.core.axis2.Axis2SynapseEnvironment) SynapseXPath(org.apache.synapse.util.xpath.SynapseXPath) Value(org.apache.synapse.mediators.Value) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext) Test(org.junit.Test)

Example 90 with Axis2MessageContext

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

the class HeaderMediatorTest method testSimpleHTTPHeaderSetAndRemove.

public void testSimpleHTTPHeaderSetAndRemove() throws Exception {
    Map transportHeaders;
    String httpHeaderName = "content-type";
    String httpHeaderValue = "application/json";
    HeaderMediator headerMediator = new HeaderMediator();
    headerMediator.setQName(new QName(httpHeaderName));
    headerMediator.setValue(httpHeaderValue);
    headerMediator.setScope(XMLConfigConstants.SCOPE_TRANSPORT);
    // invoke transformation, with static enveope
    MessageContext synCtx = TestUtils.createLightweightSynapseMessageContext("<empty/>");
    headerMediator.mediate(synCtx);
    org.apache.axis2.context.MessageContext axisCtx = ((Axis2MessageContext) synCtx).getAxis2MessageContext();
    transportHeaders = (Map) axisCtx.getProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS);
    assertTrue(httpHeaderValue.equals(transportHeaders.get(httpHeaderName)));
    // set the header mediator as a remove-header
    headerMediator.setAction(HeaderMediator.ACTION_REMOVE);
    headerMediator.mediate(synCtx);
    transportHeaders = (Map) axisCtx.getProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS);
    assertNull(transportHeaders.get(httpHeaderName));
}
Also used : QName(javax.xml.namespace.QName) MessageContext(org.apache.synapse.MessageContext) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext) Map(java.util.Map) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext)

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