use of javax.servlet.ServletException in project jetty.project by eclipse.
the class HttpServerTestBase method testExceptionThrownInHandler.
@Test
public void testExceptionThrownInHandler() throws Exception {
configureServer(new AbstractHandler.ErrorDispatchHandler() {
@Override
public void doNonErrorHandle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
throw new QuietServletException("TEST handler exception");
}
});
StringBuffer request = new StringBuffer("GET / HTTP/1.0\r\n");
request.append("Host: localhost\r\n\r\n");
Socket client = newSocket(_serverURI.getHost(), _serverURI.getPort());
OutputStream os = client.getOutputStream();
try (StacklessLogging stackless = new StacklessLogging(HttpChannel.class)) {
Log.getLogger(HttpChannel.class).info("Expecting ServletException: TEST handler exception...");
os.write(request.toString().getBytes());
os.flush();
String response = readResponse(client);
assertThat(response, Matchers.containsString(" 500 "));
}
}
use of javax.servlet.ServletException in project jetty.project by eclipse.
the class HttpServerTestBase method testExceptionThrownInHandlerLoop.
@Test
public void testExceptionThrownInHandlerLoop() throws Exception {
configureServer(new AbstractHandler() {
@Override
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
throw new QuietServletException("TEST handler exception");
}
});
StringBuffer request = new StringBuffer("GET / HTTP/1.0\r\n");
request.append("Host: localhost\r\n\r\n");
Socket client = newSocket(_serverURI.getHost(), _serverURI.getPort());
OutputStream os = client.getOutputStream();
try (StacklessLogging stackless = new StacklessLogging(HttpChannel.class)) {
Log.getLogger(HttpChannel.class).info("Expecting ServletException: TEST handler exception...");
os.write(request.toString().getBytes());
os.flush();
String response = readResponse(client);
assertThat(response, Matchers.containsString(" 500 "));
}
}
use of javax.servlet.ServletException in project jetty.project by eclipse.
the class SslConnectionFactoryTest method before.
@Before
public void before() throws Exception {
String keystorePath = "src/test/resources/keystore";
File keystoreFile = new File(keystorePath);
if (!keystoreFile.exists())
throw new FileNotFoundException(keystoreFile.getAbsolutePath());
_server = new Server();
HttpConfiguration http_config = new HttpConfiguration();
http_config.setSecureScheme("https");
http_config.setSecurePort(8443);
http_config.setOutputBufferSize(32768);
HttpConfiguration https_config = new HttpConfiguration(http_config);
https_config.addCustomizer(new SecureRequestCustomizer());
SslContextFactory sslContextFactory = new SslContextFactory();
sslContextFactory.setKeyStorePath(keystoreFile.getAbsolutePath());
sslContextFactory.setKeyStorePassword("OBF:1vny1zlo1x8e1vnw1vn61x8g1zlu1vn4");
sslContextFactory.setKeyManagerPassword("OBF:1u2u1wml1z7s1z7a1wnl1u2g");
ServerConnector https = _connector = new ServerConnector(_server, new SslConnectionFactory(sslContextFactory, HttpVersion.HTTP_1_1.asString()), new HttpConnectionFactory(https_config));
https.setPort(0);
https.setIdleTimeout(30000);
_server.addConnector(https);
_server.setHandler(new AbstractHandler() {
@Override
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
response.setStatus(200);
response.getWriter().write("url=" + request.getRequestURI() + "\nhost=" + request.getServerName());
response.flushBuffer();
}
});
_server.start();
_port = https.getLocalPort();
}
use of javax.servlet.ServletException in project jetty.project by eclipse.
the class ServletHolder method initServlet.
/* ------------------------------------------------------------ */
private void initServlet() throws ServletException {
Object old_run_as = null;
try {
if (_servlet == null)
_servlet = newInstance();
if (_config == null)
_config = new Config();
// Handle run as
if (_identityService != null) {
old_run_as = _identityService.setRunAs(_identityService.getSystemUserIdentity(), _runAsToken);
}
// Handle configuring servlets that implement org.apache.jasper.servlet.JspServlet
if (isJspServlet()) {
initJspServlet();
detectJspContainer();
} else if (_forcedPath != null)
detectJspContainer();
initMultiPart();
if (LOG.isDebugEnabled())
LOG.debug("Servlet.init {} for {}", _servlet, getName());
_servlet.init(_config);
} catch (UnavailableException e) {
makeUnavailable(e);
_servlet = null;
_config = null;
throw e;
} catch (ServletException e) {
makeUnavailable(e.getCause() == null ? e : e.getCause());
_servlet = null;
_config = null;
throw e;
} catch (Exception e) {
makeUnavailable(e);
_servlet = null;
_config = null;
throw new ServletException(this.toString(), e);
} finally {
// pop run-as role
if (_identityService != null)
_identityService.unsetRunAs(old_run_as);
}
}
use of javax.servlet.ServletException in project jetty.project by eclipse.
the class StatisticsHandlerTest method testSuspendResume.
@Test
public void testSuspendResume() throws Exception {
final long dispatchTime = 10;
final long requestTime = 50;
final AtomicReference<AsyncContext> asyncHolder = new AtomicReference<>();
final CyclicBarrier[] barrier = { new CyclicBarrier(2), new CyclicBarrier(2), new CyclicBarrier(2) };
_statsHandler.setHandler(new AbstractHandler() {
@Override
public void handle(String path, Request request, HttpServletRequest httpRequest, HttpServletResponse httpResponse) throws IOException, ServletException {
request.setHandled(true);
try {
barrier[0].await();
Thread.sleep(dispatchTime);
if (asyncHolder.get() == null)
asyncHolder.set(request.startAsync());
} catch (Exception x) {
throw new ServletException(x);
} finally {
try {
barrier[1].await();
} catch (Exception ignored) {
}
}
}
});
_server.start();
String request = "GET / HTTP/1.1\r\n" + "Host: localhost\r\n" + "\r\n";
_connector.executeRequest(request);
barrier[0].await();
assertEquals(1, _statistics.getConnections());
assertEquals(1, _statsHandler.getRequests());
assertEquals(1, _statsHandler.getRequestsActive());
assertEquals(1, _statsHandler.getDispatched());
assertEquals(1, _statsHandler.getDispatchedActive());
barrier[1].await();
assertTrue(_latchHandler.await());
assertNotNull(asyncHolder.get());
assertEquals(1, _statsHandler.getRequests());
assertEquals(1, _statsHandler.getRequestsActive());
assertEquals(1, _statsHandler.getDispatched());
assertEquals(0, _statsHandler.getDispatchedActive());
_latchHandler.reset();
barrier[0].reset();
barrier[1].reset();
Thread.sleep(requestTime);
asyncHolder.get().addListener(new AsyncListener() {
@Override
public void onTimeout(AsyncEvent event) throws IOException {
}
@Override
public void onStartAsync(AsyncEvent event) throws IOException {
}
@Override
public void onError(AsyncEvent event) throws IOException {
}
@Override
public void onComplete(AsyncEvent event) throws IOException {
try {
barrier[2].await();
} catch (Exception ignored) {
}
}
});
asyncHolder.get().dispatch();
// entered app handler
barrier[0].await();
assertEquals(1, _statistics.getConnections());
assertEquals(1, _statsHandler.getRequests());
assertEquals(1, _statsHandler.getRequestsActive());
assertEquals(2, _statsHandler.getDispatched());
assertEquals(1, _statsHandler.getDispatchedActive());
// exiting app handler
barrier[1].await();
// exited stats handler
assertTrue(_latchHandler.await());
// onComplete called
barrier[2].await();
assertEquals(1, _statsHandler.getRequests());
assertEquals(0, _statsHandler.getRequestsActive());
assertEquals(2, _statsHandler.getDispatched());
assertEquals(0, _statsHandler.getDispatchedActive());
assertEquals(1, _statsHandler.getAsyncRequests());
assertEquals(1, _statsHandler.getAsyncDispatches());
assertEquals(0, _statsHandler.getExpires());
assertEquals(1, _statsHandler.getResponses2xx());
assertThat(_statsHandler.getRequestTimeTotal(), greaterThanOrEqualTo(requestTime * 3 / 4));
assertEquals(_statsHandler.getRequestTimeTotal(), _statsHandler.getRequestTimeMax());
assertEquals(_statsHandler.getRequestTimeTotal(), _statsHandler.getRequestTimeMean(), 0.01);
assertThat(_statsHandler.getDispatchedTimeTotal(), greaterThanOrEqualTo(dispatchTime * 2 * 3 / 4));
assertTrue(_statsHandler.getDispatchedTimeMean() + dispatchTime <= _statsHandler.getDispatchedTimeTotal());
assertTrue(_statsHandler.getDispatchedTimeMax() + dispatchTime <= _statsHandler.getDispatchedTimeTotal());
}
Aggregations