Search in sources :

Example 1 with TargetConfiguration

use of org.apache.synapse.transport.passthru.config.TargetConfiguration in project wso2-synapse by wso2.

the class TargetHandlerTest method testRequestReady.

/**
 * Testing whether request-ready connection is processed
 *
 * @throws Exception
 */
@Test
public void testRequestReady() throws Exception {
    DeliveryAgent deliveryAgent = mock(DeliveryAgent.class);
    ClientConnFactory connFactory = mock(ClientConnFactory.class);
    TargetConfiguration configuration = mock(TargetConfiguration.class);
    TargetHandler targetHandler = new TargetHandler(deliveryAgent, connFactory, configuration);
    NHttpClientConnection conn = mock(NHttpClientConnection.class, Mockito.RETURNS_DEEP_STUBS);
    HttpContext context = mock(HttpContext.class);
    when(conn.getContext()).thenReturn(context);
    mockStatic(TargetContext.class);
    when(TargetContext.getState(any(NHttpClientConnection.class))).thenReturn(ProtocolState.REQUEST_READY);
    targetHandler.requestReady(conn);
}
Also used : TargetConfiguration(org.apache.synapse.transport.passthru.config.TargetConfiguration) HttpContext(org.apache.http.protocol.HttpContext) ClientConnFactory(org.apache.synapse.transport.http.conn.ClientConnFactory) NHttpClientConnection(org.apache.http.nio.NHttpClientConnection) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 2 with TargetConfiguration

use of org.apache.synapse.transport.passthru.config.TargetConfiguration in project wso2-synapse by wso2.

the class TargetResponseTest method testTrue.

/**
 * Testing the starting of target response when response body is expected
 *
 * @throws Exception
 */
@Test
public void testTrue() throws Exception {
    ConfigurationContext configurationContext = new ConfigurationContext(new AxisConfiguration());
    WorkerPool workerPool = new NativeWorkerPool(3, 4, 5, 5, "name", "id");
    PassThroughTransportMetricsCollector metrics = new PassThroughTransportMetricsCollector(true, "testScheme");
    TargetConfiguration targetConfiguration = new TargetConfiguration(configurationContext, null, workerPool, metrics, null);
    targetConfiguration.build();
    HttpResponse response = PowerMockito.mock(HttpResponse.class, Mockito.RETURNS_DEEP_STUBS);
    NHttpClientConnection conn = PowerMockito.mock(NHttpClientConnection.class, Mockito.RETURNS_DEEP_STUBS);
    PowerMockito.mockStatic(TargetContext.class);
    TargetContext cntxt = new TargetContext(targetConfiguration);
    PowerMockito.when(TargetContext.get(any(NHttpClientConnection.class))).thenReturn(cntxt);
    TargetResponse targetResponse = new TargetResponse(targetConfiguration, response, conn, true, false);
    try {
        targetResponse.start(conn);
    } catch (Exception e) {
        logger.error(e);
        Assert.fail("Unable to start the target response!");
    }
}
Also used : ConfigurationContext(org.apache.axis2.context.ConfigurationContext) AxisConfiguration(org.apache.axis2.engine.AxisConfiguration) WorkerPool(org.apache.axis2.transport.base.threads.WorkerPool) NativeWorkerPool(org.apache.axis2.transport.base.threads.NativeWorkerPool) TargetConfiguration(org.apache.synapse.transport.passthru.config.TargetConfiguration) PassThroughTransportMetricsCollector(org.apache.synapse.transport.passthru.jmx.PassThroughTransportMetricsCollector) NativeWorkerPool(org.apache.axis2.transport.base.threads.NativeWorkerPool) HttpResponse(org.apache.http.HttpResponse) NHttpClientConnection(org.apache.http.nio.NHttpClientConnection) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 3 with TargetConfiguration

use of org.apache.synapse.transport.passthru.config.TargetConfiguration in project wso2-synapse by wso2.

the class TargetResponseTest method testFalse.

/**
 * Testing the starting of target response when response body is not expected
 *
 * @throws Exception
 */
@Test
public void testFalse() throws Exception {
    ConfigurationContext configurationContext = new ConfigurationContext(new AxisConfiguration());
    WorkerPool workerPool = new NativeWorkerPool(3, 4, 5, 5, "name", "id");
    PassThroughTransportMetricsCollector metrics = new PassThroughTransportMetricsCollector(true, "testScheme");
    TargetConfiguration targetConfiguration = new TargetConfiguration(configurationContext, null, workerPool, metrics, null);
    HttpResponse response = PowerMockito.mock(HttpResponse.class, Mockito.RETURNS_DEEP_STUBS);
    NHttpClientConnection conn = PowerMockito.mock(NHttpClientConnection.class, Mockito.RETURNS_DEEP_STUBS);
    TargetConnections connections = PowerMockito.mock(TargetConnections.class);
    targetConfiguration.setConnections(connections);
    PowerMockito.mockStatic(TargetContext.class);
    TargetResponse targetResponse = new TargetResponse(targetConfiguration, response, conn, false, false);
    try {
        targetResponse.start(conn);
    } catch (Exception e) {
        logger.error(e);
        Assert.fail("Unable to start the target response!");
    }
}
Also used : ConfigurationContext(org.apache.axis2.context.ConfigurationContext) AxisConfiguration(org.apache.axis2.engine.AxisConfiguration) WorkerPool(org.apache.axis2.transport.base.threads.WorkerPool) NativeWorkerPool(org.apache.axis2.transport.base.threads.NativeWorkerPool) TargetConfiguration(org.apache.synapse.transport.passthru.config.TargetConfiguration) PassThroughTransportMetricsCollector(org.apache.synapse.transport.passthru.jmx.PassThroughTransportMetricsCollector) NativeWorkerPool(org.apache.axis2.transport.base.threads.NativeWorkerPool) HttpResponse(org.apache.http.HttpResponse) TargetConnections(org.apache.synapse.transport.passthru.connections.TargetConnections) NHttpClientConnection(org.apache.http.nio.NHttpClientConnection) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 4 with TargetConfiguration

use of org.apache.synapse.transport.passthru.config.TargetConfiguration in project wso2-synapse by wso2.

the class TargetResponseTest method testRead.

/**
 * Testing reading from a pipe
 * @throws Exception
 */
@Test
public void testRead() throws Exception {
    ConfigurationContext configurationContext = new ConfigurationContext(new AxisConfiguration());
    WorkerPool workerPool = new NativeWorkerPool(3, 4, 5, 5, "name", "id");
    PassThroughTransportMetricsCollector metrics = new PassThroughTransportMetricsCollector(true, "testScheme");
    TargetConfiguration targetConfiguration = new TargetConfiguration(configurationContext, null, workerPool, metrics, null);
    targetConfiguration.build();
    HttpResponse response = PowerMockito.mock(HttpResponse.class, Mockito.RETURNS_DEEP_STUBS);
    NHttpClientConnection conn = PowerMockito.mock(NHttpClientConnection.class, Mockito.RETURNS_DEEP_STUBS);
    ContentDecoder decoder = PowerMockito.mock(ContentDecoder.class);
    TargetConnections connections = PowerMockito.mock(TargetConnections.class);
    targetConfiguration.setConnections(connections);
    PowerMockito.mockStatic(TargetContext.class);
    TargetContext cntxt = new TargetContext(targetConfiguration);
    PowerMockito.when(TargetContext.get(any(NHttpClientConnection.class))).thenReturn(cntxt);
    PowerMockito.when(decoder.read(any(ByteBuffer.class))).thenReturn(12);
    PowerMockito.when(decoder.isCompleted()).thenReturn(true);
    TargetResponse targetResponse = new TargetResponse(targetConfiguration, response, conn, true, false);
    targetResponse.start(conn);
    int result = targetResponse.read(conn, decoder);
    Assert.assertEquals(12, result);
}
Also used : ConfigurationContext(org.apache.axis2.context.ConfigurationContext) AxisConfiguration(org.apache.axis2.engine.AxisConfiguration) TargetConfiguration(org.apache.synapse.transport.passthru.config.TargetConfiguration) PassThroughTransportMetricsCollector(org.apache.synapse.transport.passthru.jmx.PassThroughTransportMetricsCollector) NativeWorkerPool(org.apache.axis2.transport.base.threads.NativeWorkerPool) ContentDecoder(org.apache.http.nio.ContentDecoder) HttpResponse(org.apache.http.HttpResponse) NHttpClientConnection(org.apache.http.nio.NHttpClientConnection) ByteBuffer(java.nio.ByteBuffer) WorkerPool(org.apache.axis2.transport.base.threads.WorkerPool) NativeWorkerPool(org.apache.axis2.transport.base.threads.NativeWorkerPool) TargetConnections(org.apache.synapse.transport.passthru.connections.TargetConnections) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 5 with TargetConfiguration

use of org.apache.synapse.transport.passthru.config.TargetConfiguration in project wso2-synapse by wso2.

the class PassThroughHttpSender method init.

public void init(ConfigurationContext configurationContext, TransportOutDescription transportOutDescription) throws AxisFault {
    log.info("Initializing Pass-through HTTP/S Sender...");
    namePrefix = transportOutDescription.getName().toUpperCase(Locale.US);
    scheme = getScheme();
    WorkerPool workerPool = null;
    Object obj = configurationContext.getProperty(PassThroughConstants.PASS_THROUGH_TRANSPORT_WORKER_POOL);
    if (obj != null) {
        workerPool = (WorkerPool) obj;
    }
    PassThroughTransportMetricsCollector metrics = new PassThroughTransportMetricsCollector(false, scheme.getName());
    TransportView view = new TransportView(null, this, metrics, null);
    MBeanRegistrar.getInstance().registerMBean(view, "Transport", "passthru-" + namePrefix.toLowerCase() + "-sender");
    proxyConfig = new ProxyConfigBuilder().build(transportOutDescription);
    log.info(proxyConfig.logProxyConfig());
    targetConfiguration = new TargetConfiguration(configurationContext, transportOutDescription, workerPool, metrics, proxyConfig.createProxyAuthenticator());
    targetConfiguration.build();
    PassThroughSenderManager.registerPassThroughHttpSender(this);
    configurationContext.setProperty(PassThroughConstants.PASS_THROUGH_TRANSPORT_WORKER_POOL, targetConfiguration.getWorkerPool());
    ClientConnFactoryBuilder connFactoryBuilder = initConnFactoryBuilder(transportOutDescription);
    connFactory = connFactoryBuilder.createConnFactory(targetConfiguration.getHttpParams());
    try {
        String prefix = namePrefix + "-Sender I/O dispatcher";
        ioReactor = new DefaultConnectingIOReactor(targetConfiguration.getIOReactorConfig(), new NativeThreadFactory(new ThreadGroup(prefix + " Thread Group"), prefix));
        ioReactor.setExceptionHandler(new IOReactorExceptionHandler() {

            public boolean handle(IOException ioException) {
                log.warn("System may be unstable: " + namePrefix + " ConnectingIOReactor encountered a checked exception : " + ioException.getMessage(), ioException);
                return true;
            }

            public boolean handle(RuntimeException runtimeException) {
                log.warn("System may be unstable: " + namePrefix + " ConnectingIOReactor encountered a runtime exception : " + runtimeException.getMessage(), runtimeException);
                return true;
            }
        });
    } catch (IOReactorException e) {
        handleException("Error starting " + namePrefix + " ConnectingIOReactor", e);
    }
    ConnectCallback connectCallback = new ConnectCallback();
    targetConnections = new TargetConnections(ioReactor, targetConfiguration, connectCallback);
    targetConfiguration.setConnections(targetConnections);
    // create the delivery agent to hand over messages
    deliveryAgent = new DeliveryAgent(targetConfiguration, targetConnections, proxyConfig);
    // we need to set the delivery agent
    connectCallback.setDeliveryAgent(deliveryAgent);
    handler = new TargetHandler(deliveryAgent, connFactory, targetConfiguration);
    ioEventDispatch = new ClientIODispatch(handler, connFactory);
    // start the sender in a separate thread
    Thread t = new Thread(new Runnable() {

        public void run() {
            try {
                ioReactor.execute(ioEventDispatch);
            } catch (Exception ex) {
                log.fatal("Exception encountered in the " + namePrefix + " Sender. " + "No more connections will be initiated by this transport", ex);
            }
            log.info(namePrefix + " Sender shutdown");
        }
    }, "PassThrough" + namePrefix + "Sender");
    t.start();
    state = BaseConstants.STARTED;
    log.info("Pass-through " + namePrefix + " Sender started...");
}
Also used : IOReactorExceptionHandler(org.apache.http.nio.reactor.IOReactorExceptionHandler) ProxyConfigBuilder(org.apache.synapse.transport.nhttp.config.ProxyConfigBuilder) TargetConfiguration(org.apache.synapse.transport.passthru.config.TargetConfiguration) ClientConnFactoryBuilder(org.apache.synapse.transport.nhttp.config.ClientConnFactoryBuilder) PassThroughTransportMetricsCollector(org.apache.synapse.transport.passthru.jmx.PassThroughTransportMetricsCollector) NativeThreadFactory(org.apache.axis2.transport.base.threads.NativeThreadFactory) IOException(java.io.IOException) XMLStreamException(javax.xml.stream.XMLStreamException) RemoteException(java.rmi.RemoteException) HttpException(org.apache.http.HttpException) IOException(java.io.IOException) IOReactorException(org.apache.http.nio.reactor.IOReactorException) DefaultConnectingIOReactor(org.apache.http.impl.nio.reactor.DefaultConnectingIOReactor) IOReactorException(org.apache.http.nio.reactor.IOReactorException) WorkerPool(org.apache.axis2.transport.base.threads.WorkerPool) TransportView(org.apache.synapse.transport.passthru.jmx.TransportView) TargetConnections(org.apache.synapse.transport.passthru.connections.TargetConnections)

Aggregations

TargetConfiguration (org.apache.synapse.transport.passthru.config.TargetConfiguration)8 Test (org.junit.Test)7 WorkerPool (org.apache.axis2.transport.base.threads.WorkerPool)6 NHttpClientConnection (org.apache.http.nio.NHttpClientConnection)6 PassThroughTransportMetricsCollector (org.apache.synapse.transport.passthru.jmx.PassThroughTransportMetricsCollector)6 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)6 ConfigurationContext (org.apache.axis2.context.ConfigurationContext)5 AxisConfiguration (org.apache.axis2.engine.AxisConfiguration)5 NativeWorkerPool (org.apache.axis2.transport.base.threads.NativeWorkerPool)5 TargetConnections (org.apache.synapse.transport.passthru.connections.TargetConnections)4 MessageContext (org.apache.axis2.context.MessageContext)3 HttpResponse (org.apache.http.HttpResponse)3 ClientConnFactory (org.apache.synapse.transport.http.conn.ClientConnFactory)3 ContentDecoder (org.apache.http.nio.ContentDecoder)2 IOException (java.io.IOException)1 ByteBuffer (java.nio.ByteBuffer)1 RemoteException (java.rmi.RemoteException)1 XMLStreamException (javax.xml.stream.XMLStreamException)1 EndpointReference (org.apache.axis2.addressing.EndpointReference)1 OperationContext (org.apache.axis2.context.OperationContext)1