public void init() throws Exception { client = new HttpClient(); client.setConnectorType(HttpClient.CONNECTOR_SELECT_CHANNEL); client.setThreadPool(new ExecutorThreadPool(4, 256, timeout, TimeUnit.SECONDS)); client.setTimeout(timeout); client.start(); }
@Test public void testResponseHeadersAreNotRemoved() throws Exception { prepareProxy(); proxyContext.stop(); final String headerName = "X-Test"; final String headerValue = "test-value"; proxyContext.addFilter( new FilterHolder( new Filter() { @Override public void init(FilterConfig filterConfig) throws ServletException {} @Override public void doFilter( ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { ((HttpServletResponse) response).addHeader(headerName, headerValue); chain.doFilter(request, response); } @Override public void destroy() {} }), "/*", EnumSet.of(DispatcherType.REQUEST)); proxyContext.start(); prepareServer(new EmptyHttpServlet()); HttpClient client = prepareClient(); ContentResponse response = client.newRequest("localhost", serverConnector.getLocalPort()).send(); Assert.assertEquals(200, response.getStatus()); Assert.assertEquals(headerValue, response.getHeaders().get(headerName)); }
/** * Create a http client * * @return * @throws Exception */ private static BayeuxClient makeClient() throws Exception { HttpClient httpClient = new HttpClient(); httpClient.setConnectTimeout(connectionTimeout); httpClient.setTimeout(readTimeout); // TODO optional parameters httpClient.start(); SoapLoginUtil.login(httpClient, userName, password); final String sessionId = SoapLoginUtil.getSessionId(); String endpoint = SoapLoginUtil.getEndpoint(); System.out.println("Login successful!\nEndpoint: " + endpoint + "\nSessionid=" + sessionId); Map<String, Object> options = new HashMap<String, Object>(); options.put(ClientTransport.TIMEOUT_OPTION, readTimeout); LongPollingTransport transport = new LongPollingTransport(options, httpClient) { @Override protected void customize(ContentExchange exchange) { super.customize(exchange); exchange.addRequestHeader("Authorization", "OAuth " + sessionId); } }; BayeuxClient client = new BayeuxClient(salesforceStreamingEndpoint(endpoint), transport); if (useCookies) establishCookies(client, userName, sessionId); return client; }
@Test public void testClientExcludedHosts() throws Exception { prepareProxy(); prepareServer( new HttpServlet() { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { if (req.getHeader("Via") != null) resp.addHeader(PROXIED_HEADER, "true"); } }); int port = serverConnector.getLocalPort(); client .getProxyConfiguration() .getProxies() .get(0) .getExcludedAddresses() .add("127.0.0.1:" + port); // Try with a proxied host ContentResponse response = client.newRequest("localhost", port).timeout(5, TimeUnit.SECONDS).send(); Assert.assertEquals(200, response.getStatus()); Assert.assertTrue(response.getHeaders().containsKey(PROXIED_HEADER)); // Try again with an excluded host response = client.newRequest("127.0.0.1", port).timeout(5, TimeUnit.SECONDS).send(); Assert.assertEquals(200, response.getStatus()); Assert.assertFalse(response.getHeaders().containsKey(PROXIED_HEADER)); }
/** Initialize the client object */ public StockManagerHTTPProxy(String serverAddress) throws Exception { setServerAddress(serverAddress); client = new HttpClient(); client.setConnectorType(HttpClient.CONNECTOR_SELECT_CHANNEL); client.setMaxConnectionsPerAddress( BookStoreClientConstants.CLIENT_MAX_CONNECTION_ADDRESS); // max // concurrent // connections // to // every // address client.setThreadPool( new QueuedThreadPool(BookStoreClientConstants.CLIENT_MAX_THREADSPOOL_THREADS)); // max // threads client.setTimeout(BookStoreClientConstants.CLIENT_MAX_TIMEOUT_MILLISECS); // seconds // timeout; // if // no // server // reply, // the // request // expires client.start(); }
@Test public void testSessionMigration() throws Exception { String contextPath = ""; String servletMapping = "/server"; AbstractTestServer server1 = createServer(0); server1.addContext(contextPath).addServlet(TestServlet.class, servletMapping); try { server1.start(); int port1 = server1.getPort(); AbstractTestServer server2 = createServer(0); server2.addContext(contextPath).addServlet(TestServlet.class, servletMapping); try { server2.start(); int port2 = server2.getPort(); HttpClient client = new HttpClient(); client.start(); try { // Perform one request to server1 to create a session int value = 1; Request request1 = client.POST( "http://localhost:" + port1 + contextPath + servletMapping + "?action=set&value=" + value); ContentResponse response1 = request1.send(); assertEquals(HttpServletResponse.SC_OK, response1.getStatus()); String sessionCookie = response1.getHeaders().get("Set-Cookie"); assertTrue(sessionCookie != null); // Mangle the cookie, replacing Path with $Path, etc. sessionCookie = sessionCookie.replaceFirst("(\\W)(P|p)ath=", "$1\\$Path="); // Perform a request to server2 using the session cookie from the previous request // This should migrate the session from server1 to server2. Request request2 = client.newRequest( "http://localhost:" + port2 + contextPath + servletMapping + "?action=get"); request2.header("Cookie", sessionCookie); ContentResponse response2 = request2.send(); assertEquals(HttpServletResponse.SC_OK, response2.getStatus()); String response = response2.getContentAsString(); assertEquals(response.trim(), String.valueOf(value)); } finally { client.stop(); } } finally { server2.stop(); } } finally { server1.stop(); } }
@Test public void testAttributeNamesWithDots() throws Exception { String contextPath = ""; String servletMapping = "/server"; int maxInactivePeriod = 10000; int scavengePeriod = 20000; AbstractTestServer server1 = createServer(0, maxInactivePeriod, scavengePeriod); server1.addContext(contextPath).addServlet(TestServlet.class, servletMapping); server1.start(); int port1 = server1.getPort(); AbstractTestServer server2 = createServer(0, maxInactivePeriod, scavengePeriod); server2.addContext(contextPath).addServlet(TestServlet.class, servletMapping); server2.start(); int port2 = server2.getPort(); try { HttpClient client = new HttpClient(); client.start(); try { // Perform one request to server1 to create a session with attribute with dotted name ContentResponse response = client.GET("http://localhost:" + port1 + contextPath + servletMapping + "?action=init"); assertEquals(HttpServletResponse.SC_OK, response.getStatus()); String resp = response.getContentAsString(); String[] sessionTestResponse = resp.split("/"); assertEquals("a.b.c", sessionTestResponse[0]); String sessionCookie = response.getHeaders().get(HttpHeader.SET_COOKIE); assertTrue(sessionCookie != null); // Mangle the cookie, replacing Path with $Path, etc. sessionCookie = sessionCookie.replaceFirst("(\\W)(P|p)ath=", "$1\\$Path="); // Make a request to the 2nd server which will do a refresh, use TestServlet to ensure that // the // session attribute with dotted name is not removed Request request2 = client.newRequest( "http://localhost:" + port2 + contextPath + servletMapping + "?action=get"); request2.header("Cookie", sessionCookie); ContentResponse response2 = request2.send(); assertEquals(HttpServletResponse.SC_OK, response2.getStatus()); } finally { client.stop(); } } finally { server1.stop(); server2.stop(); } }
private HttpClient prepareClient() throws Exception { HttpClient result = new HttpClient(); result .getProxyConfiguration() .getProxies() .add(new HttpProxy("localhost", proxyConnector.getLocalPort())); result.start(); return result; }
private static HttpClient httpClient() throws Exception { if (_httpClient == null) { _httpClient = new HttpClient(); _httpClient.setConnectorType(CONNECTOR_SELECT_CHANNEL); _httpClient.setConnectTimeout(TIMEOUT_CONNECT); _httpClient.start(); } return _httpClient; }
// TODO: client.start() could potentially have some side effects. // Need to understand more of what's going on under the hood. @Provides HttpClient provideHttpClient() { LOGGER.info("Providing http client for connection name " + connectionName); HttpClient client = new HttpClient(); client.setConnectorType(HttpClient.CONNECTOR_SELECT_CHANNEL); client.setConnectTimeout(timeoutOption); client.setTimeout(timeoutOption); return client; }
@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); 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: { break; } case DONE: { assert generator.isEnd(); callback.succeeded(); return; } default: { throw new IllegalStateException(); } } } } catch (Exception x) { LOG.debug(x); callback.failed(x); } }
/** * If nodeA creates a session, and just afterwards crashes, it is the only node that knows about * the session. We want to test that the session data is gone after scavenging. */ @Test public void testOrphanedSession() throws Exception { // Disable scavenging for the first server, so that we simulate its "crash". String contextPath = ""; String servletMapping = "/server"; int inactivePeriod = 5; AbstractTestServer server1 = createServer(0, inactivePeriod, -1); server1.addContext(contextPath).addServlet(TestServlet.class, servletMapping); try { server1.start(); int port1 = server1.getPort(); int scavengePeriod = 2; AbstractTestServer server2 = createServer(0, inactivePeriod, scavengePeriod); server2.addContext(contextPath).addServlet(TestServlet.class, servletMapping); try { server2.start(); int port2 = server2.getPort(); HttpClient client = new HttpClient(); client.start(); try { // Connect to server1 to create a session and get its session cookie ContentResponse response1 = client.GET( "http://localhost:" + port1 + contextPath + servletMapping + "?action=init"); assertEquals(HttpServletResponse.SC_OK, response1.getStatus()); String sessionCookie = response1.getHeaders().getStringField("Set-Cookie"); assertTrue(sessionCookie != null); // Mangle the cookie, replacing Path with $Path, etc. sessionCookie = sessionCookie.replaceFirst("(\\W)(P|p)ath=", "$1\\$Path="); // Wait for the session to expire. // The first node does not do any scavenging, but the session // must be removed by scavenging done in the other node. Thread.sleep(TimeUnit.SECONDS.toMillis(inactivePeriod + 2L * scavengePeriod)); // Perform one request to server2 to be sure that the session has been expired Request request = client.newRequest( "http://localhost:" + port2 + contextPath + servletMapping + "?action=check"); request.header("Cookie", sessionCookie); ContentResponse response2 = request.send(); assertEquals(HttpServletResponse.SC_OK, response2.getStatus()); } finally { client.stop(); } } finally { server2.stop(); } } finally { server1.stop(); } }
/** @throws Exception */ public void testSessionRenewal() throws Exception { String contextPath = ""; String servletMapping = "/server"; int maxInactive = 1; int scavengePeriod = 3; _server = createServer(0, maxInactive, scavengePeriod, SessionCache.NEVER_EVICT); WebAppContext context = _server.addWebAppContext(".", contextPath); context.setParentLoaderPriority(true); context.addServlet(TestServlet.class, servletMapping); TestHttpSessionIdListener testListener = new TestHttpSessionIdListener(); context.addEventListener(testListener); HttpClient client = new HttpClient(); try { _server.start(); int port = _server.getPort(); client.start(); // make a request to create a session ContentResponse response = client.GET("http://localhost:" + port + contextPath + servletMapping + "?action=create"); assertEquals(HttpServletResponse.SC_OK, response.getStatus()); String sessionCookie = response.getHeaders().get("Set-Cookie"); assertTrue(sessionCookie != null); assertFalse(testListener.isCalled()); // make a request to change the sessionid Request request = client.newRequest( "http://localhost:" + port + contextPath + servletMapping + "?action=renew"); request.header("Cookie", sessionCookie); ContentResponse renewResponse = request.send(); assertEquals(HttpServletResponse.SC_OK, renewResponse.getStatus()); String renewSessionCookie = renewResponse.getHeaders().get("Set-Cookie"); assertNotNull(renewSessionCookie); assertNotSame(sessionCookie, renewSessionCookie); assertTrue(testListener.isCalled()); assertTrue( verifyChange( context, AbstractTestServer.extractSessionId(sessionCookie), AbstractTestServer.extractSessionId(renewSessionCookie))); } finally { client.stop(); _server.stop(); } }
@Test public void testProxyWithRequestContentAndResponseContent() throws Exception { prepareProxy(); prepareServer( new HttpServlet() { @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { if (req.getHeader("Via") != null) resp.addHeader(PROXIED_HEADER, "true"); IO.copy(req.getInputStream(), resp.getOutputStream()); } }); byte[] content = new byte[1024]; new Random().nextBytes(content); ContentResponse response = client .newRequest("localhost", serverConnector.getLocalPort()) .method(HttpMethod.POST) .content(new BytesContentProvider(content)) .timeout(5, TimeUnit.SECONDS) .send(); Assert.assertEquals(200, response.getStatus()); Assert.assertTrue(response.getHeaders().containsKey(PROXIED_HEADER)); Assert.assertArrayEquals(content, response.getContent()); }
@BeforeClass public static void startServer() throws Exception { server = new Server(); ServerConnector connector = new ServerConnector(server); connector.setIdleTimeout(30000); server.addConnector(connector); ServletContextHandler context = new ServletContextHandler(server, "/"); // CometD servlet ServletHolder cometdServletHolder = new ServletHolder(CometDServlet.class); cometdServletHolder.setInitParameter("timeout", "10000"); cometdServletHolder.setInitParameter("multiSessionInterval", "2000"); cometdServletHolder.setInitOrder(1); String cometdServletPath = "/cometd"; context.addServlet(cometdServletHolder, cometdServletPath + "/*"); server.start(); int port = connector.getLocalPort(); cometdURL = "http://localhost:" + port + cometdServletPath; httpClient = new HttpClient(); httpClient.start(); }
@Test public void testProxyWithBigRequestContentConsumed() throws Exception { final byte[] content = new byte[128 * 1024]; new Random().nextBytes(content); prepareProxy(); prepareServer( new HttpServlet() { @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { if (req.getHeader("Via") != null) resp.addHeader(PROXIED_HEADER, "true"); InputStream input = req.getInputStream(); int index = 0; while (true) { int value = input.read(); if (value < 0) break; Assert.assertEquals( "Content mismatch at index=" + index, content[index] & 0xFF, value); ++index; } } }); ContentResponse response = client .newRequest("localhost", serverConnector.getLocalPort()) .method(HttpMethod.POST) .content(new BytesContentProvider(content)) .timeout(5, TimeUnit.SECONDS) .send(); Assert.assertEquals(200, response.getStatus()); Assert.assertTrue(response.getHeaders().containsKey(PROXIED_HEADER)); }
@After public void dispose() throws Exception { server1.stop(); server2.stop(); balancer.stop(); client.stop(); }
@Test public void testProxyBlackList() throws Exception { prepareProxy(); prepareServer(new EmptyHttpServlet()); int port = serverConnector.getLocalPort(); proxyServlet.getBlackListHosts().add("localhost:" + port); // Try with the wrong host ContentResponse response = client.newRequest("localhost", port).timeout(5, TimeUnit.SECONDS).send(); Assert.assertEquals(403, response.getStatus()); // Try again with the right host response = client.newRequest("127.0.0.1", port).timeout(5, TimeUnit.SECONDS).send(); Assert.assertEquals(200, response.getStatus()); }
@Test public void testTransparentProxyWithoutPrefix() throws Exception { final String target = "/test"; prepareServer( new HttpServlet() { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { if (req.getHeader("Via") != null) resp.addHeader(PROXIED_HEADER, "true"); resp.setStatus(target.equals(req.getRequestURI()) ? 200 : 404); } }); final String proxyTo = "http://localhost:" + serverConnector.getLocalPort(); proxyServlet = new ProxyServlet.Transparent(); Map<String, String> initParams = new HashMap<>(); initParams.put("proxyTo", proxyTo); prepareProxy(initParams); // Make the request to the proxy, it should transparently forward to the server ContentResponse response = client .newRequest("localhost", proxyConnector.getLocalPort()) .path(target) .timeout(5, TimeUnit.SECONDS) .send(); Assert.assertEquals(200, response.getStatus()); Assert.assertTrue(response.getHeaders().containsKey(PROXIED_HEADER)); }
@Test public void testGZIPContentIsProxied() throws Exception { final byte[] content = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; prepareProxy(); prepareServer( new HttpServlet() { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { if (req.getHeader("Via") != null) resp.addHeader(PROXIED_HEADER, "true"); resp.addHeader("Content-Encoding", "gzip"); GZIPOutputStream gzipOutputStream = new GZIPOutputStream(resp.getOutputStream()); gzipOutputStream.write(content); gzipOutputStream.close(); } }); ContentResponse response = client .newRequest("localhost", serverConnector.getLocalPort()) .timeout(5, TimeUnit.SECONDS) .send(); Assert.assertEquals(200, response.getStatus()); Assert.assertTrue(response.getHeaders().containsKey(PROXIED_HEADER)); Assert.assertArrayEquals(content, response.getContent()); }
public void requestControl( final Player player, Figure.Type type, int x, int y, final Joystick joystick, List<Plot> plots) throws IOException { ContentExchange exchange = new MyContentExchange(joystick, player); exchange.setMethod("GET"); String callbackUrl = player.getCallbackUrl().endsWith("/") ? player.getCallbackUrl() : player.getCallbackUrl() + "/"; StringBuilder sb = exportGlassState(plots); String url = callbackUrl + "?figure=" + type + "&x=" + x + "&y=" + y + "&glass=" + URLEncoder.encode(sb.toString(), "UTF-8"); exchange.setURL(url); client.send(exchange); }
@AfterClass public static void stopServer() throws Exception { httpClient.stop(); server.stop(); server.join(); }
protected void startClient() throws Exception { if (httpClient != null) { stopClient(); } httpClient = new HttpClient(); httpClient.start(); }
@Override public void tearDown() throws Exception { // if (log.isInfoEnabled()) log.warn("tearing down test: " + getName()); if (m_fixture != null) { m_fixture.stop(); m_fixture = null; } if (m_indexManager != null && namespace != null) { dropTripleStore(m_indexManager, namespace); m_indexManager = null; } namespace = null; m_rootURL = null; m_serviceURL = null; m_repo.close(); m_client.stop(); log.info("tear down done"); super.tearDown(); }
@Test public void createsMetricsForTheHandler() throws Exception { final ContentResponse response = client.GET("http://localhost:" + connector.getLocalPort() + "/hello"); assertThat(response.getStatus()).isEqualTo(404); assertThat(registry.getNames()) .containsOnly( "org.eclipse.jetty.server.handler.DefaultHandler.handler.1xx-responses", "org.eclipse.jetty.server.handler.DefaultHandler.handler.2xx-responses", "org.eclipse.jetty.server.handler.DefaultHandler.handler.3xx-responses", "org.eclipse.jetty.server.handler.DefaultHandler.handler.4xx-responses", "org.eclipse.jetty.server.handler.DefaultHandler.handler.5xx-responses", "org.eclipse.jetty.server.handler.DefaultHandler.handler.requests", "org.eclipse.jetty.server.handler.DefaultHandler.handler.active-suspended", "org.eclipse.jetty.server.handler.DefaultHandler.handler.async-dispatches", "org.eclipse.jetty.server.handler.DefaultHandler.handler.async-timeouts", "org.eclipse.jetty.server.handler.DefaultHandler.handler.get-requests", "org.eclipse.jetty.server.handler.DefaultHandler.handler.put-requests", "org.eclipse.jetty.server.handler.DefaultHandler.handler.active-dispatches", "org.eclipse.jetty.server.handler.DefaultHandler.handler.trace-requests", "org.eclipse.jetty.server.handler.DefaultHandler.handler.other-requests", "org.eclipse.jetty.server.handler.DefaultHandler.handler.connect-requests", "org.eclipse.jetty.server.handler.DefaultHandler.handler.dispatches", "org.eclipse.jetty.server.handler.DefaultHandler.handler.head-requests", "org.eclipse.jetty.server.handler.DefaultHandler.handler.post-requests", "org.eclipse.jetty.server.handler.DefaultHandler.handler.options-requests", "org.eclipse.jetty.server.handler.DefaultHandler.handler.active-requests", "org.eclipse.jetty.server.handler.DefaultHandler.handler.delete-requests", "org.eclipse.jetty.server.handler.DefaultHandler.handler.move-requests"); }
protected void performTest(MonitorAction action, long count, long interval) throws Exception { _monitor.addActions(action); for (long cnt = 0; cnt < count; cnt++) { try { ContentExchange getExchange = new ContentExchange(); getExchange.setURL(_requestUrl); getExchange.setMethod(HttpMethods.GET); _client.send(getExchange); int state = getExchange.waitForDone(); String content = ""; int responseStatus = getExchange.getResponseStatus(); if (responseStatus == HttpStatus.OK_200) { content = getExchange.getResponseContent(); } assertEquals(HttpStatus.OK_200, responseStatus); Thread.sleep(interval); } catch (InterruptedException ex) { break; } } Thread.sleep(interval); _monitor.removeActions(action); }
@Override public void close() { try { client.stop(); } catch (final Exception e) { throw new ProcessingException("Failed to stop the client.", e); } }
public void stop() { try { client.stop(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } }
protected byte[] sendRequestToBalancer(String path) throws Exception { ContentResponse response = client .newRequest("localhost", getServerPort(balancer)) .path(CONTEXT_PATH + SERVLET_PATH + path) .send() .get(5, TimeUnit.SECONDS); return response.getContent(); }
@Before public void setUp() throws Exception { handler.setName("handler"); handler.setHandler(new DefaultHandler()); server.addConnector(connector); server.setHandler(handler); server.start(); client.start(); }