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);
}
}
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);
}
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));
}
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());
}
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());
}
Aggregations