Search in sources :

Example 51 with ServletException

use of javax.servlet.ServletException in project jetty.project by eclipse.

the class WebAppObjectInSessionServlet method doGet.

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse httpServletResponse) throws ServletException, IOException {
    try {
        String action = request.getParameter("action");
        if ("set".equals(action)) {
            HttpSession session = request.getSession(true);
            session.setAttribute("staticAttribute", new TestSharedStatic());
            Object staticAttribute = session.getAttribute("staticAttribute");
            Assert.assertTrue(staticAttribute instanceof TestSharedStatic);
        //                session.setAttribute("objectAttribute", new TestSharedNonStatic());
        // The session itself is not shareable, since the implementation class
        // refers to the session manager via the hidden field this$0, and
        // it seems there is no way to mark the hidden field as transient.
        //                session.setAttribute("sessionAttribute", session);
        } else if ("get".equals(action)) {
            HttpSession session = request.getSession(false);
            Object staticAttribute = session.getAttribute("staticAttribute");
            Assert.assertTrue(staticAttribute instanceof TestSharedStatic);
        //                Object objectAttribute = session.getAttribute("objectAttribute");
        //                Assert.assertTrue(objectAttribute instanceof TestSharedNonStatic);
        //                Object sessionAttribute = session.getAttribute("sessionAttribute");
        //                assertTrue(sessionAttribute instanceof HttpSession);
        }
    } catch (Exception e) {
        // e.printStackTrace();
        httpServletResponse.sendError(500, e.toString());
        throw new ServletException(e);
    }
}
Also used : ServletException(javax.servlet.ServletException) HttpSession(javax.servlet.http.HttpSession) ServletException(javax.servlet.ServletException) IOException(java.io.IOException)

Example 52 with ServletException

use of javax.servlet.ServletException in project jetty.project by eclipse.

the class HttpClientTest method test_GET_ResponseWithContent.

@Test
public void test_GET_ResponseWithContent() throws Exception {
    final byte[] data = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7 };
    start(new AbstractHandler() {

        @Override
        public void handle(String target, org.eclipse.jetty.server.Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            response.getOutputStream().write(data);
            baseRequest.setHandled(true);
        }
    });
    client.setConnectBlocking(true);
    ContentResponse response = client.GET(scheme + "://localhost:" + connector.getLocalPort());
    Assert.assertNotNull(response);
    Assert.assertEquals(200, response.getStatus());
    byte[] content = response.getContent();
    Assert.assertArrayEquals(data, content);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) Test(org.junit.Test)

Example 53 with ServletException

use of javax.servlet.ServletException in project jetty.project by eclipse.

the class HttpClientTest method test_PUT_WithParameters.

@Test
public void test_PUT_WithParameters() throws Exception {
    final String paramName = "a";
    final String paramValue = "€";
    final String encodedParamValue = URLEncoder.encode(paramValue, "UTF-8");
    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 value = request.getParameter(paramName);
            if (paramValue.equals(value)) {
                response.setCharacterEncoding("UTF-8");
                response.setContentType("text/plain");
                response.getOutputStream().print(value);
            }
        }
    });
    URI uri = URI.create(scheme + "://localhost:" + connector.getLocalPort() + "/path?" + paramName + "=" + encodedParamValue);
    ContentResponse response = client.newRequest(uri).method(HttpMethod.PUT).timeout(5, TimeUnit.SECONDS).send();
    Assert.assertNotNull(response);
    Assert.assertEquals(200, response.getStatus());
    Assert.assertEquals(paramValue, new String(response.getContent(), StandardCharsets.UTF_8));
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) URI(java.net.URI) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) Test(org.junit.Test)

Example 54 with ServletException

use of javax.servlet.ServletException in project jetty.project by eclipse.

the class HttpClientTest method testUserAgentCanBeRemoved.

@Test
public void testUserAgentCanBeRemoved() 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);
            ArrayList<String> userAgents = Collections.list(request.getHeaders("User-Agent"));
            if ("/ua".equals(target))
                Assert.assertEquals(1, userAgents.size());
            else
                Assert.assertEquals(0, userAgents.size());
        }
    });
    // User agent not specified, use default.
    ContentResponse response = client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).path("/ua").timeout(5, TimeUnit.SECONDS).send();
    Assert.assertEquals(200, response.getStatus());
    // User agent explicitly removed.
    response = client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).agent(null).timeout(5, TimeUnit.SECONDS).send();
    Assert.assertEquals(200, response.getStatus());
    // User agent explicitly removed.
    response = client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).header(HttpHeader.USER_AGENT, null).timeout(5, TimeUnit.SECONDS).send();
    Assert.assertEquals(200, response.getStatus());
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) ArrayList(java.util.ArrayList) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) Test(org.junit.Test)

Example 55 with ServletException

use of javax.servlet.ServletException in project jetty.project by eclipse.

the class HttpClientTest method test_Request_IdleTimeout.

@Slow
@Test
public void test_Request_IdleTimeout() throws Exception {
    final long idleTimeout = 1000;
    start(new AbstractHandler() {

        @Override
        public void handle(String target, org.eclipse.jetty.server.Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            try {
                baseRequest.setHandled(true);
                TimeUnit.MILLISECONDS.sleep(2 * idleTimeout);
            } catch (InterruptedException x) {
                throw new ServletException(x);
            }
        }
    });
    final String host = "localhost";
    final int port = connector.getLocalPort();
    try {
        client.newRequest(host, port).scheme(scheme).idleTimeout(idleTimeout, TimeUnit.MILLISECONDS).timeout(3 * idleTimeout, TimeUnit.MILLISECONDS).send();
        Assert.fail();
    } catch (ExecutionException expected) {
        Assert.assertTrue(expected.getCause() instanceof TimeoutException);
    }
    // Make another request without specifying the idle timeout, should not fail
    ContentResponse response = client.newRequest(host, port).scheme(scheme).timeout(3 * idleTimeout, TimeUnit.MILLISECONDS).send();
    Assert.assertNotNull(response);
    Assert.assertEquals(200, response.getStatus());
}
Also used : ContentResponse(org.eclipse.jetty.client.api.ContentResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) EndPoint(org.eclipse.jetty.io.EndPoint) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) ExecutionException(java.util.concurrent.ExecutionException) TimeoutException(java.util.concurrent.TimeoutException) Test(org.junit.Test) Slow(org.eclipse.jetty.toolchain.test.annotation.Slow)

Aggregations

ServletException (javax.servlet.ServletException)2492 IOException (java.io.IOException)1623 HttpServletRequest (javax.servlet.http.HttpServletRequest)698 HttpServletResponse (javax.servlet.http.HttpServletResponse)658 Test (org.junit.Test)444 PrintWriter (java.io.PrintWriter)238 AbstractHandler (org.eclipse.jetty.server.handler.AbstractHandler)226 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)188 CountDownLatch (java.util.concurrent.CountDownLatch)169 HttpServlet (javax.servlet.http.HttpServlet)151 Request (org.eclipse.jetty.server.Request)148 HashMap (java.util.HashMap)147 InputStream (java.io.InputStream)146 ArrayList (java.util.ArrayList)133 HttpSession (javax.servlet.http.HttpSession)127 SQLException (java.sql.SQLException)124 OutputStream (java.io.OutputStream)116 ServletOutputStream (javax.servlet.ServletOutputStream)113 Properties (java.util.Properties)108 List (java.util.List)107