Search in sources :

Example 66 with MessageContext

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

the class URITemplateBasedDispatcherTest method testDefaultDispatch.

public void testDefaultDispatch() throws Exception {
    API api = new API("TestAPI", "/test");
    Resource resource = new Resource();
    resource.setDispatcherHelper(new URITemplateHelper("/"));
    resource.setInSequence(getTestSequence(PROP_NAME, PROP_VALUE));
    api.addResource(resource);
    SynapseConfiguration synapseConfig = new SynapseConfiguration();
    synapseConfig.addAPI(api.getName(), api);
    RESTRequestHandler handler = new RESTRequestHandler();
    MessageContext synCtx = getMessageContext(synapseConfig, false, "/test", "GET");
    handler.process(synCtx);
    assertEquals(PROP_VALUE, synCtx.getProperty(PROP_NAME));
}
Also used : URITemplateHelper(org.apache.synapse.rest.dispatch.URITemplateHelper) MessageContext(org.apache.synapse.MessageContext) SynapseConfiguration(org.apache.synapse.config.SynapseConfiguration)

Example 67 with MessageContext

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

the class URITemplateBasedDispatcherTest method testBasicTemplateDispatch2.

public void testBasicTemplateDispatch2() throws Exception {
    API api = new API("TestAPI", "/");
    Resource resource = new Resource();
    resource.setDispatcherHelper(new URITemplateHelper("/dictionary/{char}/{word}"));
    resource.setInSequence(getTestSequence(PROP_NAME, PROP_VALUE));
    api.addResource(resource);
    SynapseConfiguration synapseConfig = new SynapseConfiguration();
    synapseConfig.addAPI(api.getName(), api);
    RESTRequestHandler handler = new RESTRequestHandler();
    MessageContext synCtx = getMessageContext(synapseConfig, false, "/dictionary/c/cat", "GET");
    handler.process(synCtx);
    assertEquals(PROP_VALUE, synCtx.getProperty(PROP_NAME));
    assertEquals("c", synCtx.getProperty(RESTConstants.REST_URI_VARIABLE_PREFIX + "char"));
    assertEquals("cat", synCtx.getProperty(RESTConstants.REST_URI_VARIABLE_PREFIX + "word"));
    synCtx = getMessageContext(synapseConfig, false, "/dictionary/d/dog/", "GET");
    handler.process(synCtx);
    assertEquals(PROP_VALUE, synCtx.getProperty(PROP_NAME));
    assertEquals("d", synCtx.getProperty(RESTConstants.REST_URI_VARIABLE_PREFIX + "char"));
    assertEquals("dog", synCtx.getProperty(RESTConstants.REST_URI_VARIABLE_PREFIX + "word"));
    synCtx = getMessageContext(synapseConfig, false, "/test/c/cat", "GET");
    handler.process(synCtx);
    assertNull(synCtx.getProperty(PROP_NAME));
    synCtx = getMessageContext(synapseConfig, false, "/dictionary/c", "GET");
    handler.process(synCtx);
    assertNull(synCtx.getProperty(PROP_NAME));
    /* With ESBJAVA-4260 we now support optional query parameters for URITemplates */
    synCtx = getMessageContext(synapseConfig, false, "/dictionary/c/cat?a=5", "GET");
    handler.process(synCtx);
    assertEquals(PROP_VALUE, synCtx.getProperty(PROP_NAME));
    assertEquals("c", synCtx.getProperty(RESTConstants.REST_URI_VARIABLE_PREFIX + "char"));
    assertEquals("cat", synCtx.getProperty(RESTConstants.REST_URI_VARIABLE_PREFIX + "word"));
}
Also used : URITemplateHelper(org.apache.synapse.rest.dispatch.URITemplateHelper) MessageContext(org.apache.synapse.MessageContext) SynapseConfiguration(org.apache.synapse.config.SynapseConfiguration)

Example 68 with MessageContext

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

the class SwitchMediatorTest method setUp.

public void setUp() throws Exception {
    ibmMediator = new TestMediator();
    ibmMediator.setHandler(new TestMediateHandler() {

        public void handle(MessageContext synCtx) {
            setExecutedCase("IBM");
        }
    });
    msftMediator = new TestMediator();
    msftMediator.setHandler(new TestMediateHandler() {

        public void handle(MessageContext synCtx) {
            setExecutedCase("MSFT");
        }
    });
    defaultMediator = new TestMediator();
    defaultMediator.setHandler(new TestMediateHandler() {

        public void handle(MessageContext synCtx) {
            setExecutedCase("DEFAULT");
        }
    });
    // create a new switch mediator
    switchMediator = new SwitchMediator();
    // set xpath condition to select symbol
    SynapseXPath xpath = new SynapseXPath("//wsx:symbol");
    xpath.addNamespace("wsx", "http://www.webserviceX.NET/");
    switchMediator.setSource(xpath);
    SwitchCase caseOne = new SwitchCase();
    caseOne.setRegex(Pattern.compile("IBM"));
    AnonymousListMediator mediatorOne = new AnonymousListMediator();
    mediatorOne.addAll(Arrays.asList(new Mediator[] { ibmMediator }));
    caseOne.setCaseMediator(mediatorOne);
    SwitchCase caseTwo = new SwitchCase();
    caseTwo.setRegex(Pattern.compile("MSFT"));
    AnonymousListMediator mediatorTwo = new AnonymousListMediator();
    mediatorTwo.addAll(Arrays.asList(new Mediator[] { msftMediator }));
    caseTwo.setCaseMediator(mediatorTwo);
    SwitchCase caseDefault = new SwitchCase();
    AnonymousListMediator mediatorDefault = new AnonymousListMediator();
    mediatorDefault.addAll(Arrays.asList(new Mediator[] { defaultMediator }));
    caseDefault.setCaseMediator(mediatorDefault);
    // set ibm mediator to be called for IBM, msft for MSFT and default for others..
    switchMediator.addCase(caseOne);
    switchMediator.addCase(caseTwo);
    switchMediator.setDefaultCase(caseDefault);
}
Also used : SynapseXPath(org.apache.synapse.util.xpath.SynapseXPath) SwitchCase(org.apache.synapse.config.xml.SwitchCase) TestMediateHandler(org.apache.synapse.mediators.TestMediateHandler) AnonymousListMediator(org.apache.synapse.config.xml.AnonymousListMediator) TestMediator(org.apache.synapse.mediators.TestMediator) Mediator(org.apache.synapse.Mediator) AnonymousListMediator(org.apache.synapse.config.xml.AnonymousListMediator) TestMediator(org.apache.synapse.mediators.TestMediator) MessageContext(org.apache.synapse.MessageContext)

Example 69 with MessageContext

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

the class FaultMediatorTest method testSOAP11Fault.

public void testSOAP11Fault() throws Exception {
    FaultMediator faultMediator = new FaultMediator();
    faultMediator.setSoapVersion(FaultMediator.SOAP11);
    faultMediator.setFaultCodeValue(F_CODE);
    faultMediator.setFaultReasonValue(F_STRING);
    faultMediator.setFaultRole(new URI(F_ACTOR_URI));
    faultMediator.setFaultDetail(F_DETAIL);
    // invoke transformation, with static enveope
    MessageContext synCtx = TestUtils.getAxis2MessageContext("<empty/>", new HashMap<String, Entry>());
    faultMediator.mediate(synCtx);
    SOAPEnvelope envelope = synCtx.getEnvelope();
    SOAPFault fault = envelope.getBody().getFault();
    assertTrue(F_CODE.equals(fault.getCode().getTextAsQName()));
    assertTrue(F_STRING.equals(fault.getReason().getText()));
    assertTrue(F_ACTOR_URI.equals(fault.getRole().getRoleValue()));
    assertTrue(F_DETAIL.equals(fault.getDetail().getText()));
    assertEquals(SOAP11Constants.SOAP_ENVELOPE_NAMESPACE_URI, envelope.getNamespace().getNamespaceURI());
}
Also used : Entry(org.apache.synapse.config.Entry) SOAPFault(org.apache.axiom.soap.SOAPFault) MessageContext(org.apache.synapse.MessageContext) SOAPEnvelope(org.apache.axiom.soap.SOAPEnvelope) URI(java.net.URI)

Example 70 with MessageContext

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

the class HeaderMediatorTest method testEmbeddedXml.

public void testEmbeddedXml() throws Exception {
    String simpleHeader = "<header name=\"m:simpleHeader\" value=\"Simple Header\" xmlns:m=\"http://org.synapse.example\"/>";
    String complexHeader = "<header><m:complexHeader xmlns:m=\"http://org.synapse.example\"><property key=\"k1\" value=\"v1\"/><property key=\"k2\" value=\"v2\"/></m:complexHeader></header>";
    String removeHeader = "<header name=\"m:complexHeader\" action=\"remove\" xmlns:m=\"http://org.synapse.example\"/>";
    HeaderMediatorFactory fac = new HeaderMediatorFactory();
    try {
        // Adding headers.
        MessageContext synCtx = TestUtils.getTestContext("<empty/>");
        HeaderMediator headerMediator = (HeaderMediator) fac.createMediator(AXIOMUtil.stringToOM(simpleHeader), new Properties());
        headerMediator.mediate(synCtx);
        headerMediator = (HeaderMediator) fac.createMediator(AXIOMUtil.stringToOM(complexHeader), new Properties());
        headerMediator.mediate(synCtx);
        // Removing headers.
        headerMediator = (HeaderMediator) fac.createMediator(AXIOMUtil.stringToOM(removeHeader), new Properties());
        headerMediator.mediate(synCtx);
    } catch (XMLStreamException e) {
        fail("Error while parsing header mediator configuration: inline XML scenario. " + e.getMessage());
    }
}
Also used : XMLStreamException(javax.xml.stream.XMLStreamException) HeaderMediatorFactory(org.apache.synapse.config.xml.HeaderMediatorFactory) MessageContext(org.apache.synapse.MessageContext) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext) Properties(java.util.Properties)

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