Line data Source code
1 : // Copyright (C) 2013 The Android Open Source Project 2 : // 3 : // Licensed under the Apache License, Version 2.0 (the "License"); 4 : // you may not use this file except in compliance with the License. 5 : // You may obtain a copy of the License at 6 : // 7 : // http://www.apache.org/licenses/LICENSE-2.0 8 : // 9 : // Unless required by applicable law or agreed to in writing, software 10 : // distributed under the License is distributed on an "AS IS" BASIS, 11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 : // See the License for the specific language governing permissions and 13 : // limitations under the License. 14 : 15 : package com.google.gerrit.pgm.http.jetty; 16 : 17 : import static java.nio.charset.StandardCharsets.ISO_8859_1; 18 : 19 : import com.google.common.base.Strings; 20 : import com.google.common.flogger.FluentLogger; 21 : import com.google.gerrit.util.http.CacheHeaders; 22 : import java.io.IOException; 23 : import javax.servlet.ServletOutputStream; 24 : import javax.servlet.http.HttpServletRequest; 25 : import javax.servlet.http.HttpServletResponse; 26 : import org.eclipse.jetty.http.HttpHeader; 27 : import org.eclipse.jetty.http.HttpStatus; 28 : import org.eclipse.jetty.server.HttpConnection; 29 : import org.eclipse.jetty.server.Request; 30 : import org.eclipse.jetty.server.handler.ErrorHandler; 31 : 32 99 : class HiddenErrorHandler extends ErrorHandler { 33 99 : private static final FluentLogger logger = FluentLogger.forEnclosingClass(); 34 : 35 : @Override 36 : public void handle( 37 : String target, Request baseRequest, HttpServletRequest req, HttpServletResponse res) 38 : throws IOException { 39 24 : HttpConnection conn = HttpConnection.getCurrentConnection(); 40 24 : baseRequest.setHandled(true); 41 : try { 42 24 : log(req); 43 : } finally { 44 24 : reply(conn, res); 45 : } 46 24 : } 47 : 48 : private void reply(HttpConnection conn, HttpServletResponse res) throws IOException { 49 24 : byte[] msg = message(conn); 50 24 : res.setHeader(HttpHeader.CONTENT_TYPE.asString(), "text/plain; charset=ISO-8859-1"); 51 24 : res.setContentLength(msg.length); 52 : try { 53 24 : CacheHeaders.setNotCacheable(res); 54 : } finally { 55 24 : try (ServletOutputStream out = res.getOutputStream()) { 56 24 : out.write(msg); 57 : } 58 : } 59 24 : } 60 : 61 : private static byte[] message(HttpConnection conn) { 62 : String msg; 63 24 : if (conn == null) { 64 0 : msg = ""; 65 : } else { 66 24 : msg = conn.getHttpChannel().getResponse().getReason(); 67 24 : if (msg == null) { 68 24 : msg = HttpStatus.getMessage(conn.getHttpChannel().getResponse().getStatus()); 69 : } 70 : } 71 24 : return msg.getBytes(ISO_8859_1); 72 : } 73 : 74 : private static void log(HttpServletRequest req) { 75 24 : Throwable err = (Throwable) req.getAttribute("javax.servlet.error.exception"); 76 24 : if (err != null) { 77 0 : String uri = req.getRequestURI(); 78 0 : if (!Strings.isNullOrEmpty(req.getQueryString())) { 79 0 : uri += "?" + req.getQueryString(); 80 : } 81 0 : logger.atSevere().withCause(err).log("Error in %s %s", req.getMethod(), uri); 82 : } 83 24 : } 84 : }