use of org.eclipse.jetty.client.HttpClient in project jetty.project by eclipse.
the class HttpChannelAssociationTest method testAssociationFailedAbortsRequest.
@Test
public void testAssociationFailedAbortsRequest() throws Exception {
startServer(new EmptyServerHandler());
client = new HttpClient(newHttpClientTransport(transport, exchange -> false), sslContextFactory);
QueuedThreadPool clientThreads = new QueuedThreadPool();
clientThreads.setName("client");
client.setExecutor(clientThreads);
client.start();
CountDownLatch latch = new CountDownLatch(1);
client.newRequest(newURI()).send(result -> {
if (result.isFailed())
latch.countDown();
});
Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
}
use of org.eclipse.jetty.client.HttpClient in project jetty.project by eclipse.
the class HttpReceiverOverHTTP method releaseBuffer.
private void releaseBuffer() {
if (buffer == null)
throw new IllegalStateException();
if (BufferUtil.hasContent(buffer))
throw new IllegalStateException();
HttpClient client = getHttpDestination().getHttpClient();
ByteBufferPool bufferPool = client.getByteBufferPool();
bufferPool.release(buffer);
buffer = null;
}
use of org.eclipse.jetty.client.HttpClient in project jetty.project by eclipse.
the class HttpSenderOverHTTP method sendContent.
@Override
protected void sendContent(HttpExchange exchange, HttpContent content, Callback callback) {
try {
HttpClient client = getHttpChannel().getHttpDestination().getHttpClient();
ByteBufferPool bufferPool = client.getByteBufferPool();
ByteBuffer chunk = null;
while (true) {
ByteBuffer contentBuffer = content.getByteBuffer();
boolean lastContent = content.isLast();
HttpGenerator.Result result = generator.generateRequest(null, null, chunk, contentBuffer, lastContent);
if (LOG.isDebugEnabled())
LOG.debug("Generated content ({} bytes) - {}/{}", contentBuffer == null ? -1 : contentBuffer.remaining(), result, generator);
switch(result) {
case NEED_CHUNK:
{
chunk = bufferPool.acquire(HttpGenerator.CHUNK_SIZE, false);
break;
}
case FLUSH:
{
EndPoint endPoint = getHttpChannel().getHttpConnection().getEndPoint();
if (chunk != null)
endPoint.write(new ByteBufferRecyclerCallback(callback, bufferPool, chunk), chunk, contentBuffer);
else
endPoint.write(callback, contentBuffer);
return;
}
case SHUTDOWN_OUT:
{
shutdownOutput();
break;
}
case CONTINUE:
{
if (lastContent)
break;
callback.succeeded();
return;
}
case DONE:
{
callback.succeeded();
return;
}
default:
{
throw new IllegalStateException(result.toString());
}
}
}
} catch (Throwable x) {
if (LOG.isDebugEnabled())
LOG.debug(x);
callback.failed(x);
}
}
use of org.eclipse.jetty.client.HttpClient in project jetty.project by eclipse.
the class TestMemcachedSessions method testMemcached.
@Test
public void testMemcached() throws Exception {
String contextPath = "/";
Server server = new Server(0);
ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
context.setContextPath("/");
context.setResourceBase(System.getProperty("java.io.tmpdir"));
server.setHandler(context);
NullSessionCache dsc = new NullSessionCache(context.getSessionHandler());
dsc.setSessionDataStore(new CachingSessionDataStore(new MemcachedSessionDataMap("localhost", "11211"), new NullSessionDataStore()));
context.getSessionHandler().setSessionCache(dsc);
// Add a test servlet
ServletHolder h = new ServletHolder();
h.setServlet(new TestServlet());
context.addServlet(h, "/");
try {
server.start();
int port = ((NetworkConnector) server.getConnectors()[0]).getLocalPort();
HttpClient client = new HttpClient();
client.start();
try {
int value = 42;
ContentResponse response = client.GET("http://localhost:" + port + contextPath + "?action=set&value=" + value);
assertEquals(HttpServletResponse.SC_OK, response.getStatus());
String sessionCookie = response.getHeaders().get("Set-Cookie");
assertTrue(sessionCookie != null);
// Mangle the cookie, replacing Path with $Path, etc.
sessionCookie = sessionCookie.replaceFirst("(\\W)(P|p)ath=", "$1\\$Path=");
String resp = response.getContentAsString();
assertEquals(resp.trim(), String.valueOf(value));
// Be sure the session value is still there
Request request = client.newRequest("http://localhost:" + port + contextPath + "?action=get");
request.header("Cookie", sessionCookie);
response = request.send();
assertEquals(HttpServletResponse.SC_OK, response.getStatus());
resp = response.getContentAsString();
assertEquals(String.valueOf(value), resp.trim());
//Delete the session
request = client.newRequest("http://localhost:" + port + contextPath + "?action=del");
request.header("Cookie", sessionCookie);
response = request.send();
assertEquals(HttpServletResponse.SC_OK, response.getStatus());
//Check that the session is gone
request = client.newRequest("http://localhost:" + port + contextPath + "?action=get");
request.header("Cookie", sessionCookie);
response = request.send();
assertEquals(HttpServletResponse.SC_OK, response.getStatus());
resp = response.getContentAsString();
assertEquals("No session", resp.trim());
} finally {
client.stop();
}
} finally {
server.stop();
}
}
use of org.eclipse.jetty.client.HttpClient in project jetty.project by eclipse.
the class WebSocketClientInitTest method testInit_HttpClient_StartedOutside.
/**
* This is the new Jetty 9.4 advanced usage mode of WebSocketClient,
* that allows for more robust HTTP configurations (such as authentication,
* cookies, and proxies)
*
* @throws Exception
* on test failure
*/
@Test
public void testInit_HttpClient_StartedOutside() throws Exception {
HttpClient http = new HttpClient();
http.start();
try {
WebSocketClient ws = new WebSocketClient(http);
ws.start();
try {
assertThat("HttpClient", ws.getHttpClient(), is(http));
assertThat("WebSocketClient started", ws.isStarted(), is(true));
assertThat("HttpClient started", http.isStarted(), is(true));
HttpClient httpBean = ws.getBean(HttpClient.class);
assertThat("HttpClient should not be found in WebSocketClient", httpBean, nullValue());
assertThat("HttpClient bean is managed", ws.isManaged(httpBean), is(false));
assertThat("WebSocketClient should not be found in HttpClient", http.getBean(WebSocketClient.class), nullValue());
} finally {
ws.stop();
}
assertThat("WebSocketClient stopped", ws.isStopped(), is(true));
assertThat("HttpClient stopped", http.isStopped(), is(false));
} finally {
http.stop();
}
assertThat("HttpClient stopped", http.isStopped(), is(true));
}
Aggregations