|
| 1 | +/* |
| 2 | + * Copyright 2002-2023 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.web.util; |
| 18 | + |
| 19 | +import java.util.Set; |
| 20 | + |
| 21 | +import org.apache.commons.logging.Log; |
| 22 | +import org.apache.commons.logging.LogFactory; |
| 23 | + |
| 24 | +import org.springframework.core.NestedExceptionUtils; |
| 25 | +import org.springframework.util.Assert; |
| 26 | + |
| 27 | +/** |
| 28 | + * Utility methods to assist with identifying and logging exceptions that indicate |
| 29 | + * the client has gone away. Such exceptions fill logs with unnecessary stack |
| 30 | + * traces. The utility methods help to log a single line message at DEBUG level, |
| 31 | + * and a full stacktrace at TRACE level. |
| 32 | + * |
| 33 | + * @author Rossen Stoyanchev |
| 34 | + * @since 6.1 |
| 35 | + */ |
| 36 | +public class DisconnectedClientHelper { |
| 37 | + |
| 38 | + private static final Set<String> EXCEPTION_PHRASES = |
| 39 | + Set.of("broken pipe", "connection reset by peer"); |
| 40 | + |
| 41 | + private static final Set<String> EXCEPTION_TYPE_NAMES = |
| 42 | + Set.of("AbortedException", "ClientAbortException", "EOFException", "EofException"); |
| 43 | + |
| 44 | + private final Log logger; |
| 45 | + |
| 46 | + |
| 47 | + public DisconnectedClientHelper(String logCategory) { |
| 48 | + Assert.notNull(logCategory, "'logCategory' is required"); |
| 49 | + this.logger = LogFactory.getLog(logCategory); |
| 50 | + } |
| 51 | + |
| 52 | + |
| 53 | + /** |
| 54 | + * Check via {@link #isClientDisconnectedException} if the exception |
| 55 | + * indicates the remote client disconnected, and if so log a single line |
| 56 | + * message when DEBUG is on, and a full stacktrace when TRACE is on for |
| 57 | + * the configured logger. |
| 58 | + */ |
| 59 | + public boolean checkAndLogClientDisconnectedException(Throwable ex) { |
| 60 | + if (isClientDisconnectedException(ex)) { |
| 61 | + if (logger.isTraceEnabled()) { |
| 62 | + logger.trace("Looks like the client has gone away", ex); |
| 63 | + } |
| 64 | + else if (logger.isDebugEnabled()) { |
| 65 | + logger.debug("Looks like the client has gone away: " + ex + |
| 66 | + " (For a full stack trace, set the log category '" + logger + "' to TRACE level.)"); |
| 67 | + } |
| 68 | + return true; |
| 69 | + } |
| 70 | + return false; |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Whether the given exception indicates the client has gone away. |
| 75 | + * Known cases covered: |
| 76 | + * <ul> |
| 77 | + * <li>ClientAbortException or EOFException for Tomcat |
| 78 | + * <li>EofException for Jetty |
| 79 | + * <li>IOException "Broken pipe" or "connection reset by peer" |
| 80 | + * </ul> |
| 81 | + */ |
| 82 | + public static boolean isClientDisconnectedException(Throwable ex) { |
| 83 | + String message = NestedExceptionUtils.getMostSpecificCause(ex).getMessage(); |
| 84 | + if (message != null) { |
| 85 | + String text = message.toLowerCase(); |
| 86 | + for (String phrase : EXCEPTION_PHRASES) { |
| 87 | + if (text.contains(phrase)) { |
| 88 | + return true; |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + return EXCEPTION_TYPE_NAMES.contains(ex.getClass().getSimpleName()); |
| 93 | + } |
| 94 | + |
| 95 | +} |
0 commit comments