Search in sources :

Example 11 with HttpClient

use of org.eclipse.jetty.client.HttpClient in project jetty.project by eclipse.

the class FastCGIProxyServletTest method prepare.

public void prepare(HttpServlet servlet) throws Exception {
    QueuedThreadPool serverThreads = new QueuedThreadPool();
    serverThreads.setName("server");
    server = new Server(serverThreads);
    httpConnector = new ServerConnector(server);
    server.addConnector(httpConnector);
    fcgiConnector = new ServerConnector(server, new ServerFCGIConnectionFactory(new HttpConfiguration(), sendStatus200));
    server.addConnector(fcgiConnector);
    final String contextPath = "/";
    context = new ServletContextHandler(server, contextPath);
    final String servletPath = "/script";
    FastCGIProxyServlet fcgiServlet = new FastCGIProxyServlet() {

        @Override
        protected String rewriteTarget(HttpServletRequest request) {
            return "http://localhost:" + fcgiConnector.getLocalPort() + servletPath + request.getServletPath();
        }
    };
    ServletHolder fcgiServletHolder = new ServletHolder(fcgiServlet);
    fcgiServletHolder.setName("fcgi");
    fcgiServletHolder.setInitParameter(FastCGIProxyServlet.SCRIPT_ROOT_INIT_PARAM, "/scriptRoot");
    fcgiServletHolder.setInitParameter("proxyTo", "http://localhost");
    fcgiServletHolder.setInitParameter(FastCGIProxyServlet.SCRIPT_PATTERN_INIT_PARAM, "(.+?\\.php)");
    context.addServlet(fcgiServletHolder, "*.php");
    context.addServlet(new ServletHolder(servlet), servletPath + "/*");
    QueuedThreadPool clientThreads = new QueuedThreadPool();
    clientThreads.setName("client");
    client = new HttpClient();
    client.setExecutor(clientThreads);
    server.addBean(client);
    server.start();
}
Also used : ServerConnector(org.eclipse.jetty.server.ServerConnector) HttpServletRequest(javax.servlet.http.HttpServletRequest) Server(org.eclipse.jetty.server.Server) QueuedThreadPool(org.eclipse.jetty.util.thread.QueuedThreadPool) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) HttpClient(org.eclipse.jetty.client.HttpClient) HttpConfiguration(org.eclipse.jetty.server.HttpConfiguration) ServerFCGIConnectionFactory(org.eclipse.jetty.fcgi.server.ServerFCGIConnectionFactory) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler)

Example 12 with HttpClient

use of org.eclipse.jetty.client.HttpClient in project jetty.project by eclipse.

the class TestJettyOSGiBootContextAsService method testContextHandlerAsOSGiService.

/**
     */
@Test
public void testContextHandlerAsOSGiService() throws Exception {
    // now test the context
    HttpClient client = new HttpClient();
    try {
        client.start();
        ContentResponse response = client.GET("http://127.0.0.1:" + TestJettyOSGiBootCore.DEFAULT_HTTP_PORT + "/acme/index.html");
        assertEquals(HttpStatus.OK_200, response.getStatus());
        String content = new String(response.getContent());
        assertTrue(content.indexOf("<h1>Test OSGi Context</h1>") != -1);
    } finally {
        client.stop();
    }
    ServiceReference[] refs = bundleContext.getServiceReferences(ContextHandler.class.getName(), null);
    assertNotNull(refs);
    assertEquals(1, refs.length);
    ContextHandler ch = (ContextHandler) bundleContext.getService(refs[0]);
    assertEquals("/acme", ch.getContextPath());
    // Stop the bundle with the ContextHandler in it and check the jetty
    // Context is destroyed for it.
    // TODO: think of a better way to communicate this to the test, other
    // than checking stderr output
    Bundle testWebBundle = TestOSGiUtil.getBundle(bundleContext, "org.eclipse.jetty.osgi.testcontext");
    assertNotNull("Could not find the org.eclipse.jetty.test-jetty-osgi-context.jar bundle", testWebBundle);
    assertTrue("The bundle org.eclipse.jetty.testcontext is not correctly resolved", testWebBundle.getState() == Bundle.ACTIVE);
    testWebBundle.stop();
}
Also used : ContextHandler(org.eclipse.jetty.server.handler.ContextHandler) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) Bundle(org.osgi.framework.Bundle) CoreOptions.mavenBundle(org.ops4j.pax.exam.CoreOptions.mavenBundle) HttpClient(org.eclipse.jetty.client.HttpClient) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test)

Example 13 with HttpClient

use of org.eclipse.jetty.client.HttpClient in project jetty.project by eclipse.

the class TestJettyOSGiBootWithJsp method testJspDump.

@Test
public void testJspDump() throws Exception {
    // TestOSGiUtil.debugBundles(bundleContext);
    HttpClient client = new HttpClient();
    try {
        client.start();
        ContentResponse response = client.GET("http://127.0.0.1:" + TestJettyOSGiBootCore.DEFAULT_HTTP_PORT + "/jsp/jstl.jsp");
        assertEquals(HttpStatus.OK_200, response.getStatus());
        String content = new String(response.getContent());
        assertTrue(content.contains("JSTL Example"));
    } finally {
        client.stop();
    }
}
Also used : ContentResponse(org.eclipse.jetty.client.api.ContentResponse) HttpClient(org.eclipse.jetty.client.HttpClient) Test(org.junit.Test)

Example 14 with HttpClient

use of org.eclipse.jetty.client.HttpClient in project jetty.project by eclipse.

the class TestOSGiUtil method testHttpServiceGreetings.

protected static void testHttpServiceGreetings(BundleContext bundleContext, String protocol, int port) throws Exception {
    assertActiveBundle(bundleContext, "org.eclipse.jetty.osgi.boot");
    assertActiveBundle(bundleContext, "org.eclipse.jetty.osgi.httpservice");
    assertActiveBundle(bundleContext, "org.eclipse.equinox.http.servlet");
    // in the OSGi world this would be bad code and we should use a bundle
    // tracker.
    // here we purposely want to make sure that the httpService is actually
    // ready.
    ServiceReference sr = bundleContext.getServiceReference(HttpService.class.getName());
    Assert.assertNotNull("The httpServiceOSGiBundle is started and should " + "have deployed a service reference for HttpService", sr);
    HttpService http = (HttpService) bundleContext.getService(sr);
    http.registerServlet("/greetings", new HttpServlet() {

        private static final long serialVersionUID = 1L;

        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            resp.getWriter().write("Hello");
        }
    }, null, null);
    // now test the servlet
    HttpClient client = protocol.equals("https") ? new HttpClient(newSslContextFactory()) : new HttpClient();
    try {
        client.start();
        ContentResponse response = client.GET(protocol + "://127.0.0.1:" + port + "/greetings");
        Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
        String content = new String(response.getContent());
        Assert.assertEquals("Hello", content);
    } finally {
        client.stop();
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) HttpService(org.osgi.service.http.HttpService) HttpServlet(javax.servlet.http.HttpServlet) HttpClient(org.eclipse.jetty.client.HttpClient) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) ServiceReference(org.osgi.framework.ServiceReference)

Example 15 with HttpClient

use of org.eclipse.jetty.client.HttpClient in project jetty.project by eclipse.

the class DigestPostTest method testServerWithHttpClientStreamContent.

@Test
public void testServerWithHttpClientStreamContent() throws Exception {
    String srvUrl = "http://127.0.0.1:" + ((NetworkConnector) _server.getConnectors()[0]).getLocalPort() + "/test/";
    HttpClient client = new HttpClient();
    try {
        AuthenticationStore authStore = client.getAuthenticationStore();
        authStore.addAuthentication(new DigestAuthentication(new URI(srvUrl), "test", "testuser", "password"));
        client.start();
        String sent = IO.toString(new FileInputStream("src/test/resources/message.txt"));
        Request request = client.newRequest(srvUrl);
        request.method(HttpMethod.POST);
        request.content(new StringContentProvider(sent));
        _received = null;
        request = request.timeout(5, TimeUnit.SECONDS);
        ContentResponse response = request.send();
        Assert.assertEquals(200, response.getStatus());
        Assert.assertEquals(sent, _received);
    } finally {
        client.stop();
    }
}
Also used : StringContentProvider(org.eclipse.jetty.client.util.StringContentProvider) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) HttpClient(org.eclipse.jetty.client.HttpClient) Request(org.eclipse.jetty.client.api.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) NetworkConnector(org.eclipse.jetty.server.NetworkConnector) DigestAuthentication(org.eclipse.jetty.client.util.DigestAuthentication) URI(java.net.URI) FileInputStream(java.io.FileInputStream) AuthenticationStore(org.eclipse.jetty.client.api.AuthenticationStore) Test(org.junit.Test)

Aggregations

HttpClient (org.eclipse.jetty.client.HttpClient)135 Test (org.junit.Test)90 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)70 Request (org.eclipse.jetty.client.api.Request)44 HttpServletRequest (javax.servlet.http.HttpServletRequest)42 ServletContextHandler (org.eclipse.jetty.servlet.ServletContextHandler)40 ServletHolder (org.eclipse.jetty.servlet.ServletHolder)23 QueuedThreadPool (org.eclipse.jetty.util.thread.QueuedThreadPool)18 HTTP2Client (org.eclipse.jetty.http2.client.HTTP2Client)8 StacklessLogging (org.eclipse.jetty.util.log.StacklessLogging)8 SslContextFactory (org.eclipse.jetty.util.ssl.SslContextFactory)8 URI (java.net.URI)7 CountDownLatch (java.util.concurrent.CountDownLatch)7 HttpProxy (org.eclipse.jetty.client.HttpProxy)7 Before (org.junit.Before)7 HttpServletResponse (javax.servlet.http.HttpServletResponse)6 ByteBufferPool (org.eclipse.jetty.io.ByteBufferPool)6 InputStream (java.io.InputStream)5 File (java.io.File)4 ContentExchange (org.eclipse.jetty.client.ContentExchange)4