use of org.eclipse.jetty.server.handler.ErrorHandler in project jetty.project by eclipse.
the class ProxyConnectionTest method init.
@Before
public void init() throws Exception {
_server = new Server();
HttpConnectionFactory http = new HttpConnectionFactory();
http.getHttpConfiguration().setRequestHeaderSize(1024);
http.getHttpConfiguration().setResponseHeaderSize(1024);
ProxyConnectionFactory proxy = new ProxyConnectionFactory();
_connector = new LocalConnector(_server, null, null, null, 1, proxy, http);
_connector.setIdleTimeout(1000);
_server.addConnector(_connector);
_server.setHandler(new DumpHandler());
ErrorHandler eh = new ErrorHandler();
eh.setServer(_server);
_server.addBean(eh);
_server.start();
}
use of org.eclipse.jetty.server.handler.ErrorHandler in project jetty.project by eclipse.
the class ErrorHandlerTest method before.
@Before
public void before() throws Exception {
server = new Server();
connector = new LocalConnector(server);
server.addConnector(connector);
server.addBean(new ErrorHandler() {
@Override
protected void generateAcceptableResponse(Request baseRequest, HttpServletRequest request, HttpServletResponse response, int code, String message, String mimeType) throws IOException {
switch(mimeType) {
case "text/json":
case "application/json":
{
baseRequest.setHandled(true);
response.setContentType(mimeType);
response.getWriter().append("{").append("code: \"").append(Integer.toString(code)).append("\",").append("message: \"").append(message).append('"').append("}");
break;
}
default:
super.generateAcceptableResponse(baseRequest, request, response, code, message, mimeType);
}
}
});
server.start();
}
use of org.eclipse.jetty.server.handler.ErrorHandler in project jetty.project by eclipse.
the class Response method sendError.
@Override
public void sendError(int code, String message) throws IOException {
if (isIncluding())
return;
if (isCommitted()) {
if (LOG.isDebugEnabled())
LOG.debug("Aborting on sendError on committed response {} {}", code, message);
code = -1;
} else
resetBuffer();
switch(code) {
case -1:
_channel.abort(new IOException());
return;
case 102:
sendProcessing();
return;
default:
break;
}
_mimeType = null;
_characterEncoding = null;
_outputType = OutputType.NONE;
setHeader(HttpHeader.EXPIRES, null);
setHeader(HttpHeader.LAST_MODIFIED, null);
setHeader(HttpHeader.CACHE_CONTROL, null);
setHeader(HttpHeader.CONTENT_TYPE, null);
setHeader(HttpHeader.CONTENT_LENGTH, null);
setStatus(code);
Request request = _channel.getRequest();
Throwable cause = (Throwable) request.getAttribute(Dispatcher.ERROR_EXCEPTION);
if (message == null) {
_reason = HttpStatus.getMessage(code);
message = cause == null ? _reason : cause.toString();
} else
_reason = message;
// If we are allowed to have a body, then produce the error page.
if (code != SC_NO_CONTENT && code != SC_NOT_MODIFIED && code != SC_PARTIAL_CONTENT && code >= SC_OK) {
ContextHandler.Context context = request.getContext();
ContextHandler contextHandler = context == null ? _channel.getState().getContextHandler() : context.getContextHandler();
request.setAttribute(RequestDispatcher.ERROR_STATUS_CODE, code);
request.setAttribute(RequestDispatcher.ERROR_MESSAGE, message);
request.setAttribute(RequestDispatcher.ERROR_REQUEST_URI, request.getRequestURI());
request.setAttribute(RequestDispatcher.ERROR_SERVLET_NAME, request.getServletName());
ErrorHandler error_handler = ErrorHandler.getErrorHandler(_channel.getServer(), contextHandler);
if (error_handler != null)
error_handler.handle(null, request, request, this);
else
closeOutput();
}
}
use of org.eclipse.jetty.server.handler.ErrorHandler in project jetty.project by eclipse.
the class Server method doStart.
/* ------------------------------------------------------------ */
@Override
protected void doStart() throws Exception {
// Create an error handler if there is none
if (_errorHandler == null)
_errorHandler = getBean(ErrorHandler.class);
if (_errorHandler == null)
setErrorHandler(new ErrorHandler());
if (_errorHandler instanceof ErrorHandler.ErrorPageMapper)
LOG.warn("ErrorPageMapper not supported for Server level Error Handling");
//with the shutdown handler thread.
if (getStopAtShutdown())
ShutdownThread.register(this);
//Register the Server with the handler thread for receiving
//remote stop commands
ShutdownMonitor.register(this);
//Start a thread waiting to receive "stop" commands.
// initialize
ShutdownMonitor.getInstance().start();
LOG.info("jetty-" + getVersion());
if (!Jetty.STABLE) {
LOG.warn("THIS IS NOT A STABLE RELEASE! DO NOT USE IN PRODUCTION!");
LOG.warn("Download a stable release from http://download.eclipse.org/jetty/");
}
HttpGenerator.setJettyVersion(HttpConfiguration.SERVER_VERSION);
// Check that the thread pool size is enough.
SizedThreadPool pool = getBean(SizedThreadPool.class);
int max = pool == null ? -1 : pool.getMaxThreads();
int selectors = 0;
int acceptors = 0;
for (Connector connector : _connectors) {
if (connector instanceof AbstractConnector) {
AbstractConnector abstractConnector = (AbstractConnector) connector;
Executor connectorExecutor = connector.getExecutor();
if (connectorExecutor != pool) {
// the server level, because the connector uses a dedicated executor.
continue;
}
acceptors += abstractConnector.getAcceptors();
if (connector instanceof ServerConnector) {
// The SelectorManager uses 2 threads for each selector,
// one for the normal and one for the low priority strategies.
selectors += 2 * ((ServerConnector) connector).getSelectorManager().getSelectorCount();
}
}
}
int needed = 1 + selectors + acceptors;
if (max > 0 && needed > max)
throw new IllegalStateException(String.format("Insufficient threads: max=%d < needed(acceptors=%d + selectors=%d + request=1)", max, acceptors, selectors));
MultiException mex = new MultiException();
try {
super.doStart();
} catch (Throwable e) {
mex.add(e);
}
// start connectors last
for (Connector connector : _connectors) {
try {
connector.start();
} catch (Throwable e) {
mex.add(e);
}
}
if (isDumpAfterStart())
dumpStdErr();
mex.ifExceptionThrow();
LOG.info(String.format("Started @%dms", Uptime.getUptime()));
}
use of org.eclipse.jetty.server.handler.ErrorHandler in project camel by apache.
the class JettyHttpComponent method createServer.
protected Server createServer() {
Server s = null;
ThreadPool tp = threadPool;
QueuedThreadPool qtp = null;
// configure thread pool if min/max given
if (minThreads != null || maxThreads != null) {
if (getThreadPool() != null) {
throw new IllegalArgumentException("You cannot configure both minThreads/maxThreads and a custom threadPool on JettyHttpComponent: " + this);
}
qtp = new QueuedThreadPool();
if (minThreads != null) {
qtp.setMinThreads(minThreads.intValue());
}
if (maxThreads != null) {
qtp.setMaxThreads(maxThreads.intValue());
}
tp = qtp;
}
if (tp != null) {
try {
if (!Server.getVersion().startsWith("8")) {
s = Server.class.getConstructor(ThreadPool.class).newInstance(tp);
} else {
s = new Server();
if (isEnableJmx()) {
enableJmx(s);
}
Server.class.getMethod("setThreadPool", ThreadPool.class).invoke(s, tp);
}
} catch (Exception e) {
//ignore
}
}
if (s == null) {
s = new Server();
}
if (qtp != null) {
// let the thread names indicate they are from the server
qtp.setName("CamelJettyServer(" + ObjectHelper.getIdentityHashCode(s) + ")");
try {
qtp.start();
} catch (Exception e) {
throw new RuntimeCamelException("Error starting JettyServer thread pool: " + qtp, e);
}
}
ContextHandlerCollection collection = new ContextHandlerCollection();
s.setHandler(collection);
// setup the error handler if it set to Jetty component
if (getErrorHandler() != null) {
s.addBean(getErrorHandler());
} else if (!Server.getVersion().startsWith("8")) {
//need an error handler that won't leak information about the exception
//back to the client.
ErrorHandler eh = new ErrorHandler() {
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException {
String msg = HttpStatus.getMessage(response.getStatus());
request.setAttribute(RequestDispatcher.ERROR_MESSAGE, msg);
if (response instanceof Response) {
//need to use the deprecated method to support compiling with Jetty 8
((Response) response).setStatus(response.getStatus(), msg);
}
super.handle(target, baseRequest, request, response);
}
protected void writeErrorPage(HttpServletRequest request, Writer writer, int code, String message, boolean showStacks) throws IOException {
super.writeErrorPage(request, writer, code, message, false);
}
};
s.addBean(eh, false);
}
return s;
}
Aggregations