use of org.eclipse.jetty.client.api.Request in project jetty.project by eclipse.
the class IdleSessionTest method testSessionIdle.
/**
* @throws Exception
*/
@Test
public void testSessionIdle() throws Exception {
String contextPath = "";
String servletMapping = "/server";
int inactivePeriod = 20;
int scavengePeriod = 3;
int evictionSec = 5;
DefaultSessionCacheFactory cacheFactory = new DefaultSessionCacheFactory();
cacheFactory.setEvictionPolicy(evictionSec);
SessionDataStoreFactory storeFactory = new TestSessionDataStoreFactory();
_server1 = new TestServer(0, inactivePeriod, scavengePeriod, cacheFactory, storeFactory);
ServletHolder holder = new ServletHolder(_servlet);
ServletContextHandler contextHandler = _server1.addContext(contextPath);
contextHandler.addServlet(holder, servletMapping);
_server1.start();
int port1 = _server1.getPort();
try (StacklessLogging stackless = new StacklessLogging(Log.getLogger("org.eclipse.jetty.server.session"))) {
HttpClient client = new HttpClient();
client.start();
String url = "http://localhost:" + port1 + contextPath + servletMapping;
//make a request to set up a session on the server
ContentResponse response = client.GET(url + "?action=init");
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=");
//and wait until the session should be idled out
pause(evictionSec * 3);
//check that the session has been idled
String id = TestServer.extractSessionId(sessionCookie);
assertFalse(contextHandler.getSessionHandler().getSessionCache().contains(id));
assertTrue(contextHandler.getSessionHandler().getSessionCache().getSessionDataStore().exists(id));
//make another request to de-idle the session
Request request = client.newRequest(url + "?action=test");
request.getHeaders().add("Cookie", sessionCookie);
ContentResponse response2 = request.send();
assertEquals(HttpServletResponse.SC_OK, response2.getStatus());
//check session de-idled
assertTrue(contextHandler.getSessionHandler().getSessionCache().contains(id));
//wait again for the session to be idled
pause(evictionSec * 3);
//check that it is
assertFalse(contextHandler.getSessionHandler().getSessionCache().contains(id));
assertTrue(contextHandler.getSessionHandler().getSessionCache().getSessionDataStore().exists(id));
//While idle, take some action to ensure that a deidle won't work, like
//deleting the sessions in the store
((TestSessionDataStore) contextHandler.getSessionHandler().getSessionCache().getSessionDataStore())._map.clear();
//make a request
request = client.newRequest(url + "?action=testfail");
request.getHeaders().add("Cookie", sessionCookie);
response2 = request.send();
assertEquals(HttpServletResponse.SC_OK, response2.getStatus());
//Test trying to de-idle an expired session (ie before the scavenger can get to it)
//make a request to set up a session on the server
response = client.GET(url + "?action=init");
assertEquals(HttpServletResponse.SC_OK, response.getStatus());
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=");
id = TestServer.extractSessionId(sessionCookie);
//and wait until the session should be idled out
pause(evictionSec * 3);
//stop the scavenger
if (_server1.getHouseKeeper() != null)
_server1.getHouseKeeper().stop();
//check that the session is idle
assertFalse(contextHandler.getSessionHandler().getSessionCache().contains(id));
assertTrue(contextHandler.getSessionHandler().getSessionCache().getSessionDataStore().exists(id));
//wait until the session should be expired
pause(inactivePeriod + (3 * scavengePeriod));
//make another request to de-idle the session
request = client.newRequest(url + "?action=testfail");
request.getHeaders().add("Cookie", sessionCookie);
response2 = request.send();
assertEquals(HttpServletResponse.SC_OK, response2.getStatus());
assertFalse(contextHandler.getSessionHandler().getSessionCache().contains(id));
assertFalse(contextHandler.getSessionHandler().getSessionCache().getSessionDataStore().exists(id));
} finally {
_server1.stop();
}
}
use of org.eclipse.jetty.client.api.Request in project jetty.project by eclipse.
the class JdbcLoginServiceTest method testHead.
//Head requests to jetty-client are not working: see https://bugs.eclipse.org/bugs/show_bug.cgi?id=394552
@Ignore
public void testHead() throws Exception {
try {
startClient();
Request request = _client.newRequest(_baseUri.resolve("input.txt"));
request.method(HttpMethod.HEAD);
ContentResponse response = request.send();
int responseStatus = response.getStatus();
assertEquals(HttpStatus.OK_200, responseStatus);
} finally {
stopClient();
}
}
use of org.eclipse.jetty.client.api.Request in project druid by druid-io.
the class KerberosJettyHttpClientProvider method get.
@Override
public HttpClient get() {
final HttpClient httpClient = delegateProvider.get();
httpClient.getAuthenticationStore().addAuthentication(new Authentication() {
@Override
public boolean matches(String type, URI uri, String realm) {
return true;
}
@Override
public Result authenticate(final Request request, ContentResponse response, Authentication.HeaderInfo headerInfo, Attributes context) {
return new Result() {
@Override
public URI getURI() {
return request.getURI();
}
@Override
public void apply(Request request) {
try {
// No need to set cookies as they are handled by Jetty Http Client itself.
URI uri = request.getURI();
if (DruidKerberosUtil.needToSendCredentials(httpClient.getCookieStore(), uri)) {
log.debug("No Auth Cookie found for URI[%s]. Existing Cookies[%s] Authenticating... ", uri, httpClient.getCookieStore().getCookies());
final String host = request.getHost();
DruidKerberosUtil.authenticateIfRequired(config);
UserGroupInformation currentUser = UserGroupInformation.getCurrentUser();
String challenge = currentUser.doAs(new PrivilegedExceptionAction<String>() {
@Override
public String run() throws Exception {
return DruidKerberosUtil.kerberosChallenge(host);
}
});
request.getHeaders().add(HttpHeaders.Names.AUTHORIZATION, "Negotiate " + challenge);
} else {
log.debug("Found Auth Cookie found for URI[%s].", uri);
}
} catch (Throwable e) {
Throwables.propagate(e);
}
}
};
}
});
return httpClient;
}
use of org.eclipse.jetty.client.api.Request in project jetty.project by eclipse.
the class HttpClientTest method test_QueuedRequest_IsSent_WhenPreviousRequestSucceeded.
@Test
public void test_QueuedRequest_IsSent_WhenPreviousRequestSucceeded() throws Exception {
start(new EmptyServerHandler());
client.setMaxConnectionsPerDestination(1);
final CountDownLatch latch = new CountDownLatch(1);
final CountDownLatch successLatch = new CountDownLatch(2);
client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).onRequestBegin(request -> {
try {
latch.await();
} catch (InterruptedException x) {
x.printStackTrace();
}
}).send(new Response.Listener.Adapter() {
@Override
public void onSuccess(Response response) {
Assert.assertEquals(200, response.getStatus());
successLatch.countDown();
}
});
client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).onRequestQueued(request -> latch.countDown()).send(new Response.Listener.Adapter() {
@Override
public void onSuccess(Response response) {
Assert.assertEquals(200, response.getStatus());
successLatch.countDown();
}
});
Assert.assertTrue(successLatch.await(5, TimeUnit.SECONDS));
}
use of org.eclipse.jetty.client.api.Request in project jetty.project by eclipse.
the class HttpClientTest method test_POST_WithContent_NotifiesRequestContentListener.
@Test
public void test_POST_WithContent_NotifiesRequestContentListener() throws Exception {
start(new AbstractHandler() {
@Override
public void handle(String target, org.eclipse.jetty.server.Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
baseRequest.setHandled(true);
consume(request.getInputStream(), true);
}
});
final byte[] content = { 0, 1, 2, 3 };
ContentResponse response = client.POST(scheme + "://localhost:" + connector.getLocalPort()).onRequestContent((request, buffer) -> {
byte[] bytes = new byte[buffer.remaining()];
buffer.get(bytes);
if (!Arrays.equals(content, bytes))
request.abort(new Exception());
}).content(new BytesContentProvider(content)).timeout(5, TimeUnit.SECONDS).send();
Assert.assertNotNull(response);
Assert.assertEquals(200, response.getStatus());
}
Aggregations