use of org.apache.http.nio.NHttpClientConnection 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);
}
use of org.apache.http.nio.NHttpClientConnection 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!");
}
}
use of org.apache.http.nio.NHttpClientConnection 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!");
}
}
use of org.apache.http.nio.NHttpClientConnection 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);
}
use of org.apache.http.nio.NHttpClientConnection in project wso2-synapse by wso2.
the class DeliveryAgent method submit.
/**
* This method queues the message for delivery. If a connection is already existing for
* the destination epr, the message will be delivered immediately. Otherwise message has
* to wait until a connection is established. In this case this method will inform the
* system about the need for a connection.
*
* @param msgContext the message context to be sent
* @param epr the endpoint to which the message should be sent
* @throws AxisFault if an error occurs
*/
public void submit(MessageContext msgContext, EndpointReference epr) throws AxisFault {
try {
URL url = new URL(epr.getAddress());
String scheme = url.getProtocol() != null ? url.getProtocol() : "http";
String hostname = url.getHost();
int port = url.getPort();
if (port == -1) {
// use default
if ("http".equals(scheme)) {
port = 80;
} else if ("https".equals(scheme)) {
port = 443;
}
}
HttpHost target = new HttpHost(hostname, port, scheme);
boolean secure = "https".equalsIgnoreCase(target.getSchemeName());
HttpHost proxy = proxyConfig.selectProxy(target);
msgContext.setProperty(PassThroughConstants.PROXY_PROFILE_TARGET_HOST, target.getHostName());
HttpRoute route;
if (proxy != null) {
route = new HttpRoute(target, null, proxy, secure);
} else {
route = new HttpRoute(target, null, secure);
}
// first we queue the message
Queue<MessageContext> queue = null;
lock.lock();
try {
queue = waitingMessages.get(route);
if (queue == null) {
queue = new ConcurrentLinkedQueue<MessageContext>();
waitingMessages.put(route, queue);
}
if (queue.size() == maxWaitingMessages) {
MessageContext msgCtx = queue.poll();
targetErrorHandler.handleError(msgCtx, ErrorCodes.CONNECTION_TIMEOUT, "Error connecting to the back end", null, ProtocolState.REQUEST_READY);
}
queue.add(msgContext);
} finally {
lock.unlock();
}
NHttpClientConnection conn = targetConnections.getConnection(route);
if (conn != null) {
conn.resetInput();
conn.resetOutput();
MessageContext messageContext = queue.poll();
if (messageContext != null) {
tryNextMessage(messageContext, route, conn);
}
}
} catch (MalformedURLException e) {
handleException("Malformed URL in the target EPR", e);
}
}
Aggregations