/*
* Copyright 2011 Sonian Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.sonian.elasticsearch.http.jetty.error;
import org.eclipse.jetty.http.MimeTypes;
import org.eclipse.jetty.server.Dispatcher;
import org.eclipse.jetty.server.AbstractHttpConnection;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.handler.ErrorHandler;
import org.eclipse.jetty.util.ByteArrayISO8859Writer;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* @author drewr
*/
public class JettyHttpServerErrorHandler extends ErrorHandler {
@Override
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException {
AbstractHttpConnection connection = AbstractHttpConnection.getCurrentConnection();
connection.getRequest().setHandled(true);
String method = request.getMethod();
response.setContentType(MimeTypes.TEXT_PLAIN_8859_1);
ByteArrayISO8859Writer writer= new ByteArrayISO8859Writer(4096);
writer.write(request.getAttribute(Dispatcher.ERROR_STATUS_CODE) + " " +
request.getAttribute(Dispatcher.ERROR_MESSAGE) + " " +
request.getAttribute(Dispatcher.ERROR_REQUEST_URI));
writer.flush();
response.setContentLength(writer.size());
writer.writeTo(response.getOutputStream());
writer.destroy();
}
}