use of org.eclipse.jetty.client.api.ContentResponse in project jetty.project by eclipse.
the class HttpClientProxyTest method testProxyAuthenticationWithServerAuthentication.
@Test
public void testProxyAuthenticationWithServerAuthentication() throws Exception {
String proxyRealm = "proxyRealm";
String serverRealm = "serverRealm";
int status = HttpStatus.NO_CONTENT_204;
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);
String authorization = request.getHeader(HttpHeader.PROXY_AUTHORIZATION.asString());
if (authorization == null) {
response.setStatus(HttpStatus.PROXY_AUTHENTICATION_REQUIRED_407);
response.setHeader(HttpHeader.PROXY_AUTHENTICATE.asString(), "Basic realm=\"" + proxyRealm + "\"");
} else {
authorization = request.getHeader(HttpHeader.AUTHORIZATION.asString());
if (authorization == null) {
response.setStatus(HttpStatus.UNAUTHORIZED_401);
response.setHeader(HttpHeader.WWW_AUTHENTICATE.asString(), "Basic realm=\"" + serverRealm + "\"");
} else {
response.setStatus(status);
}
}
}
});
String proxyHost = "localhost";
int proxyPort = connector.getLocalPort();
String serverHost = "server";
int serverPort = proxyPort + 1;
URI proxyURI = URI.create(scheme + "://" + proxyHost + ":" + proxyPort);
client.getAuthenticationStore().addAuthentication(new BasicAuthentication(proxyURI, proxyRealm, "proxyUser", "proxyPassword"));
URI serverURI = URI.create(scheme + "://" + serverHost + ":" + serverPort);
client.getAuthenticationStore().addAuthentication(new BasicAuthentication(serverURI, serverRealm, "serverUser", "serverPassword"));
client.getProxyConfiguration().getProxies().add(new HttpProxy(proxyHost, proxyPort));
final AtomicInteger requests = new AtomicInteger();
client.getRequestListeners().add(new Request.Listener.Adapter() {
@Override
public void onSuccess(Request request) {
requests.incrementAndGet();
}
});
// Make a request, expect 407 + 401 + 204.
ContentResponse response1 = client.newRequest(serverHost, serverPort).scheme(scheme).timeout(5, TimeUnit.SECONDS).send();
Assert.assertEquals(status, response1.getStatus());
Assert.assertEquals(3, requests.get());
// Make again the request, authentication is cached, expect 204.
requests.set(0);
ContentResponse response2 = client.newRequest(serverHost, serverPort).scheme(scheme).timeout(5, TimeUnit.SECONDS).send();
Assert.assertEquals(status, response2.getStatus());
Assert.assertEquals(1, requests.get());
}
use of org.eclipse.jetty.client.api.ContentResponse in project jetty.project by eclipse.
the class HttpClientRedirectTest method testMethodRedirect.
private void testMethodRedirect(final HttpMethod requestMethod, final HttpMethod redirectMethod, int redirectCode) throws Exception {
start(new RedirectHandler());
final AtomicInteger passes = new AtomicInteger();
client.getRequestListeners().add(new org.eclipse.jetty.client.api.Request.Listener.Adapter() {
@Override
public void onBegin(org.eclipse.jetty.client.api.Request request) {
int pass = passes.incrementAndGet();
if (pass == 1) {
if (!requestMethod.is(request.getMethod()))
request.abort(new Exception());
} else if (pass == 2) {
if (!redirectMethod.is(request.getMethod()))
request.abort(new Exception());
} else {
request.abort(new Exception());
}
}
});
ContentResponse response = client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).method(requestMethod).path("/" + redirectCode + "/localhost/done").timeout(5, TimeUnit.SECONDS).send();
Assert.assertEquals(200, response.getStatus());
}
use of org.eclipse.jetty.client.api.ContentResponse in project jetty.project by eclipse.
the class HttpClientRedirectTest method test_307_WithRequestContent.
@Test
public void test_307_WithRequestContent() throws Exception {
start(new RedirectHandler());
byte[] data = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7 };
ContentResponse response = client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).method(HttpMethod.POST).path("/307/localhost/done").content(new ByteBufferContentProvider(ByteBuffer.wrap(data))).timeout(5, TimeUnit.SECONDS).send();
Assert.assertNotNull(response);
Assert.assertEquals(200, response.getStatus());
Assert.assertFalse(response.getHeaders().containsKey(HttpHeader.LOCATION.asString()));
Assert.assertArrayEquals(data, response.getContent());
}
use of org.eclipse.jetty.client.api.ContentResponse 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.api.ContentResponse in project jetty.project by eclipse.
the class JavaMonitorAction method sendRequest.
/* ------------------------------------------------------------ */
/**
* @param request
* @return
* @throws Exception
*/
private Properties sendRequest(Properties request) throws Exception {
ByteArrayOutputStream reqStream = null;
ByteArrayInputStream resStream = null;
Properties response = null;
try {
reqStream = new ByteArrayOutputStream();
request.storeToXML(reqStream, null);
ContentResponse r3sponse = _client.POST(_url).header("Connection", "close").content(new BytesContentProvider(reqStream.toByteArray())).send();
if (r3sponse.getStatus() == HttpStatus.OK_200) {
response = new Properties();
resStream = new ByteArrayInputStream(r3sponse.getContent());
response.loadFromXML(resStream);
}
} finally {
try {
if (reqStream != null)
reqStream.close();
} catch (IOException ex) {
LOG.ignore(ex);
}
try {
if (resStream != null)
resStream.close();
} catch (IOException ex) {
LOG.ignore(ex);
}
}
return response;
}
Aggregations