Search in sources :

Example 81 with SynapseConfiguration

use of org.apache.synapse.config.SynapseConfiguration in project wso2-synapse by wso2.

the class URLMappingBasedDispatcherTest method testMultipleResourceDispatch.

public void testMultipleResourceDispatch() throws Exception {
    API api = new API("TestAPI", "/test");
    Resource resource1 = new Resource();
    resource1.setDispatcherHelper(new URLMappingHelper("/foo/*"));
    resource1.setInSequence(getTestSequence(PROP_NAME, "resource1"));
    Resource resource2 = new Resource();
    resource2.setDispatcherHelper(new URLMappingHelper("/foo/bar/*"));
    resource2.setInSequence(getTestSequence(PROP_NAME, "resource2"));
    Resource resource3 = new Resource();
    resource3.setDispatcherHelper(new URLMappingHelper("*.jsp"));
    resource3.setInSequence(getTestSequence(PROP_NAME, "resource3"));
    api.addResource(resource1);
    api.addResource(resource2);
    api.addResource(resource3);
    SynapseConfiguration synapseConfig = new SynapseConfiguration();
    synapseConfig.addAPI(api.getName(), api);
    RESTRequestHandler handler = new RESTRequestHandler();
    MessageContext synCtx = getMessageContext(synapseConfig, false, "/test/foo/", "GET");
    handler.process(synCtx);
    assertEquals("resource1", synCtx.getProperty(PROP_NAME));
    synCtx = getMessageContext(synapseConfig, false, "/test/foo/index.html?a=5", "GET");
    handler.process(synCtx);
    assertEquals("resource1", synCtx.getProperty(PROP_NAME));
    synCtx = getMessageContext(synapseConfig, false, "/test/foo/bars", "GET");
    handler.process(synCtx);
    assertEquals("resource1", synCtx.getProperty(PROP_NAME));
    synCtx = getMessageContext(synapseConfig, false, "/test/foo/index.jsp", "GET");
    handler.process(synCtx);
    assertEquals("resource1", synCtx.getProperty(PROP_NAME));
    synCtx = getMessageContext(synapseConfig, false, "/test/foo/bar/", "GET");
    handler.process(synCtx);
    assertEquals("resource2", synCtx.getProperty(PROP_NAME));
    synCtx = getMessageContext(synapseConfig, false, "/test/foo/bar/index.html?a=5", "GET");
    handler.process(synCtx);
    assertEquals("resource2", synCtx.getProperty(PROP_NAME));
    synCtx = getMessageContext(synapseConfig, false, "/test/foo/bar/hello", "GET");
    handler.process(synCtx);
    assertEquals("resource2", synCtx.getProperty(PROP_NAME));
    synCtx = getMessageContext(synapseConfig, false, "/test/foo/bar/index.jsp", "GET");
    handler.process(synCtx);
    assertEquals("resource2", synCtx.getProperty(PROP_NAME));
    synCtx = getMessageContext(synapseConfig, false, "/test/index.jsp", "GET");
    handler.process(synCtx);
    assertEquals("resource3", synCtx.getProperty(PROP_NAME));
    synCtx = getMessageContext(synapseConfig, false, "/test/hello/index.jsp?a=5", "GET");
    handler.process(synCtx);
    assertEquals("resource3", synCtx.getProperty(PROP_NAME));
    synCtx = getMessageContext(synapseConfig, false, "/test/foolish/bars/index.jsp", "GET");
    handler.process(synCtx);
    assertEquals("resource3", synCtx.getProperty(PROP_NAME));
    synCtx = getMessageContext(synapseConfig, false, "/test/foolish/index.html", "GET");
    handler.process(synCtx);
    assertNull(synCtx.getProperty(PROP_NAME));
}
Also used : URLMappingHelper(org.apache.synapse.rest.dispatch.URLMappingHelper) MessageContext(org.apache.synapse.MessageContext) SynapseConfiguration(org.apache.synapse.config.SynapseConfiguration)

Example 82 with SynapseConfiguration

use of org.apache.synapse.config.SynapseConfiguration in project wso2-synapse by wso2.

the class URLMappingBasedDispatcherTest method testResponseDispatch.

public void testResponseDispatch() throws Exception {
    API api = new API("TestAPI", "/test");
    Resource resource = new Resource();
    resource.setDispatcherHelper(new URLMappingHelper("/foo/bar/*"));
    resource.setOutSequence(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/foo/bar", "GET");
    synCtx.setProperty(RESTConstants.SYNAPSE_REST_API, api.getName());
    synCtx.setResponse(true);
    handler.process(synCtx);
    assertNull(synCtx.getProperty(PROP_NAME));
    synCtx.setProperty(RESTConstants.SYNAPSE_RESOURCE, resource.getName());
    handler.process(synCtx);
    assertEquals(PROP_VALUE, synCtx.getProperty(PROP_NAME));
}
Also used : URLMappingHelper(org.apache.synapse.rest.dispatch.URLMappingHelper) MessageContext(org.apache.synapse.MessageContext) SynapseConfiguration(org.apache.synapse.config.SynapseConfiguration)

Example 83 with SynapseConfiguration

use of org.apache.synapse.config.SynapseConfiguration in project wso2-synapse by wso2.

the class TestMessageContextBuilder method build.

/**
 * Build the test message context.
 * This method returns a new (and independent) instance on every invocation.
 *
 * @return
 * @throws Exception
 */
public MessageContext build() throws Exception {
    SynapseConfiguration testConfig = new SynapseConfiguration();
    // TODO: check whether we need a SynapseEnvironment in all cases
    SynapseEnvironment synEnv = new Axis2SynapseEnvironment(new ConfigurationContext(new AxisConfiguration()), testConfig);
    MessageContext synCtx;
    if (requireAxis2MessageContext) {
        synCtx = new Axis2MessageContext(new org.apache.axis2.context.MessageContext(), testConfig, synEnv);
    } else {
        synCtx = new TestMessageContext();
        synCtx.setEnvironment(synEnv);
        synCtx.setConfiguration(testConfig);
    }
    for (Map.Entry<String, Entry> mapEntry : entries.entrySet()) {
        testConfig.addEntry(mapEntry.getKey(), mapEntry.getValue());
    }
    XMLStreamReader parser = null;
    if (contentString != null) {
        parser = StAXUtils.createXMLStreamReader(new StringReader(contentString));
    } else if (contentFile != null) {
        parser = StAXUtils.createXMLStreamReader(new FileInputStream(contentFile));
    } else if (contentStringJson != null) {
        // synCtx = new Axis2MessageContext(null, testConfig, synEnv);
        SOAPEnvelope envelope = OMAbstractFactory.getSOAP11Factory().getDefaultEnvelope();
        synCtx.setEnvelope(envelope);
        JsonUtil.getNewJsonPayload(((Axis2MessageContext) synCtx).getAxis2MessageContext(), contentStringJson, true, true);
        return synCtx;
    }
    SOAPEnvelope envelope;
    if (parser != null) {
        if (contentIsEnvelope) {
            envelope = new StAXSOAPModelBuilder(parser).getSOAPEnvelope();
        } else {
            envelope = OMAbstractFactory.getSOAP11Factory().getDefaultEnvelope();
            // TODO: don't know why this is here, but without it some unit tests fail...
            OMDocument omDoc = OMAbstractFactory.getSOAP11Factory().createOMDocument();
            omDoc.addChild(envelope);
            SOAPBody body = envelope.getBody();
            StAXOMBuilder builder = new StAXOMBuilder(parser);
            OMElement bodyElement = builder.getDocumentElement();
            if (addTextAroundBody) {
                OMFactory fac = OMAbstractFactory.getOMFactory();
                body.addChild(fac.createOMText("\n"));
                body.addChild(bodyElement);
                body.addChild(fac.createOMText("\n"));
            } else {
                body.addChild(bodyElement);
            }
        }
    } else {
        envelope = OMAbstractFactory.getSOAP11Factory().getDefaultEnvelope();
    }
    synCtx.setEnvelope(envelope);
    return synCtx;
}
Also used : ConfigurationContext(org.apache.axis2.context.ConfigurationContext) AxisConfiguration(org.apache.axis2.engine.AxisConfiguration) XMLStreamReader(javax.xml.stream.XMLStreamReader) Axis2SynapseEnvironment(org.apache.synapse.core.axis2.Axis2SynapseEnvironment) SynapseEnvironment(org.apache.synapse.core.SynapseEnvironment) OMElement(org.apache.axiom.om.OMElement) SOAPEnvelope(org.apache.axiom.soap.SOAPEnvelope) SynapseConfiguration(org.apache.synapse.config.SynapseConfiguration) FileInputStream(java.io.FileInputStream) OMDocument(org.apache.axiom.om.OMDocument) OMFactory(org.apache.axiom.om.OMFactory) Axis2SynapseEnvironment(org.apache.synapse.core.axis2.Axis2SynapseEnvironment) Entry(org.apache.synapse.config.Entry) SOAPBody(org.apache.axiom.soap.SOAPBody) StAXSOAPModelBuilder(org.apache.axiom.soap.impl.builder.StAXSOAPModelBuilder) StringReader(java.io.StringReader) StAXOMBuilder(org.apache.axiom.om.impl.builder.StAXOMBuilder) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext) HashMap(java.util.HashMap) Map(java.util.Map) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext)

Example 84 with SynapseConfiguration

use of org.apache.synapse.config.SynapseConfiguration in project wso2-synapse by wso2.

the class StatisticIdentityGeneratorTest method init.

@BeforeClass
public static void init() {
    synapseConfiguration = new SynapseConfiguration();
    StatisticIdentityGenerator.setSynapseConfiguration(synapseConfiguration);
    artifactHolder = new ArtifactHolder();
    ArrayList<StructuringElement> structuringElementList = new ArrayList<>();
    structuringElement = new StructuringElement(COMPONENT_ID, ComponentType.ENDPOINT);
    structuringElementList.add(structuringElement);
    artifactHolder.setList(structuringElementList);
    artifactHolder.setParent(PARENT);
    artifactHolder.setHashCode(HASH_CODE);
    Stack<StructuringElement> structuringElementStack = new Stack<>();
    structuringElementStack.add(structuringElement);
    artifactHolder.setStack(structuringElementStack);
    artifactHolder.setId(0);
}
Also used : StructuringElement(org.apache.synapse.aspects.flow.statistics.structuring.StructuringElement) ArtifactHolder(org.apache.synapse.aspects.flow.statistics.data.artifact.ArtifactHolder) ArrayList(java.util.ArrayList) SynapseConfiguration(org.apache.synapse.config.SynapseConfiguration) Stack(java.util.Stack) BeforeClass(org.junit.BeforeClass)

Example 85 with SynapseConfiguration

use of org.apache.synapse.config.SynapseConfiguration in project wso2-synapse by wso2.

the class MultiXMLConfigurationBuilderTest method testConfigurationBuilder.

public void testConfigurationBuilder() throws Exception {
    URL u = this.getClass().getClassLoader().getResource("synapse-config");
    String root = new File(u.toURI()).getAbsolutePath();
    System.out.println("Using SYNAPSE_CONFIG_HOME=" + root);
    SynapseConfiguration synapseConfig = MultiXMLConfigurationBuilder.getConfiguration(root, new Properties());
    assertNotNull(synapseConfig.getDefinedSequences().get("main"));
    assertNotNull(synapseConfig.getDefinedSequences().get("fault"));
    SequenceMediator foo = synapseConfig.getDefinedSequences().get("foo");
    SequenceMediator seq1 = synapseConfig.getDefinedSequences().get("synapse_xml_seq1");
    assertNotNull(foo);
    assertNotNull(seq1);
    assertEquals("foo.xml", foo.getFileName());
    assertNull(seq1.getFileName());
    assertNull(synapseConfig.getDefinedSequences().get("bar"));
    assertNotNull(synapseConfig.getDefinedEndpoints().get("epr1"));
    assertNotNull(synapseConfig.getDefinedEndpoints().get("synapse_xml_epr1"));
    assertNotNull(synapseConfig.getProxyService("proxy1"));
    assertNotNull(synapseConfig.getStartup("task1"));
    assertNotNull(synapseConfig.getRegistry());
    assertTrue(JavaUtils.isTrueExplicitly(synapseConfig.getProperty(MultiXMLConfigurationBuilder.SEPARATE_REGISTRY_DEFINITION)));
}
Also used : SequenceMediator(org.apache.synapse.mediators.base.SequenceMediator) SynapseConfiguration(org.apache.synapse.config.SynapseConfiguration) Properties(java.util.Properties) File(java.io.File) URL(java.net.URL)

Aggregations

SynapseConfiguration (org.apache.synapse.config.SynapseConfiguration)145 Axis2SynapseEnvironment (org.apache.synapse.core.axis2.Axis2SynapseEnvironment)64 AxisConfiguration (org.apache.axis2.engine.AxisConfiguration)59 MessageContext (org.apache.synapse.MessageContext)56 Test (org.junit.Test)56 ConfigurationContext (org.apache.axis2.context.ConfigurationContext)50 SynapseEnvironment (org.apache.synapse.core.SynapseEnvironment)49 OMElement (org.apache.axiom.om.OMElement)41 Parameter (org.apache.axis2.description.Parameter)29 Axis2MessageContext (org.apache.synapse.core.axis2.Axis2MessageContext)27 TestMessageContext (org.apache.synapse.TestMessageContext)16 Properties (java.util.Properties)15 SynapseException (org.apache.synapse.SynapseException)13 Mediator (org.apache.synapse.Mediator)12 AddressEndpoint (org.apache.synapse.endpoints.AddressEndpoint)11 File (java.io.File)10 ArrayList (java.util.ArrayList)10 HashMap (java.util.HashMap)9 SOAPEnvelope (org.apache.axiom.soap.SOAPEnvelope)7 Endpoint (org.apache.synapse.endpoints.Endpoint)7