Skip to content

Commit dfe60e4

Browse files
authored
Controlled logging of IOExceptions (#101)
1 parent ddd8894 commit dfe60e4

File tree

3 files changed

+103
-0
lines changed

3 files changed

+103
-0
lines changed

src/main/java/org/cryptomator/webdav/core/filters/LoggingFilter.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,17 @@
1919
public class LoggingFilter implements HttpFilter {
2020

2121
private static final Logger LOG = LoggerFactory.getLogger(LoggingFilter.class);
22+
23+
public static final String REQUEST_ID_ATTR_NAME = "org.cryptomator.requestId";
24+
2225
private final AtomicLong REQUEST_ID_GEN = new AtomicLong();
2326

2427
@Override
2528
public void doFilterHttp(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws IOException, ServletException {
2629
if (LOG.isDebugEnabled()) {
2730
long requestId = REQUEST_ID_GEN.getAndIncrement();
2831
LOG.debug("REQUEST {}:\n{} {} {}\n{}", requestId, request.getMethod(), request.getRequestURI(), request.getProtocol(), headers(request));
32+
request.setAttribute(REQUEST_ID_ATTR_NAME, requestId);
2933
chain.doFilter(request, response);
3034
LOG.debug("RESPONSE {}:\n{}\n{}", requestId, response.getStatus(), headers(response));
3135
} else {

src/main/java/org/cryptomator/webdav/core/servlet/AbstractNioWebDavServlet.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,13 @@
1717
import org.apache.jackrabbit.webdav.lock.Scope;
1818
import org.apache.jackrabbit.webdav.lock.Type;
1919
import org.apache.jackrabbit.webdav.server.AbstractWebdavServlet;
20+
import org.cryptomator.webdav.core.filters.LoggingFilter;
2021
import org.slf4j.Logger;
2122
import org.slf4j.LoggerFactory;
2223

2324
import javax.servlet.ServletException;
2425
import java.io.IOException;
26+
import java.io.UncheckedIOException;
2527
import java.nio.file.Path;
2628
import java.util.Set;
2729

@@ -96,12 +98,21 @@ protected boolean execute(WebdavRequest request, WebdavResponse response, int me
9698
} catch (UncheckedDavException e) {
9799
throw e.toDavException();
98100
}
101+
} catch (IOException | UncheckedIOException e) {
102+
if (LOG.isDebugEnabled()) {
103+
LOG.debug("REQUEST {}: Returning 500 due to IO exception.", request.getAttribute(LoggingFilter.REQUEST_ID_ATTR_NAME), e);
104+
} else {
105+
LOG.warn("{} request failed, returning 500. Reason: {}", DavMethodsUtil.getName(method), e.getClass().getName());
106+
}
107+
response.sendError(DavServletResponse.SC_INTERNAL_SERVER_ERROR, e.getMessage());
99108
} catch (DavException e) {
100109
if (e.getErrorCode() == DavServletResponse.SC_INTERNAL_SERVER_ERROR) {
101110
LOG.error("Unexpected DavException.", e);
102111
}
112+
LOG.debug("REQUEST {}: Returning {} due to exception.", request.getAttribute(LoggingFilter.REQUEST_ID_ATTR_NAME), e.getErrorCode(), e);
103113
throw e;
104114
}
115+
return true;
105116
}
106117

107118
/* GET stuff */
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package org.cryptomator.webdav.core.servlet;
2+
3+
import org.apache.jackrabbit.webdav.DavMethods;
4+
5+
class DavMethodsUtil {
6+
7+
private DavMethodsUtil() {}
8+
9+
/**
10+
* Returns for the given DAV method code the method name.
11+
*
12+
* @param code DAV method code as defined in {@link DavMethods}
13+
* @return DAV method name
14+
*/
15+
static String getName(int code) {
16+
switch (code) {
17+
case DavMethods.DAV_GET:
18+
return DavMethods.METHOD_GET;
19+
case DavMethods.DAV_HEAD:
20+
return DavMethods.METHOD_HEAD;
21+
case DavMethods.DAV_PROPFIND:
22+
return DavMethods.METHOD_PROPFIND;
23+
case DavMethods.DAV_PROPPATCH:
24+
return DavMethods.METHOD_PROPPATCH;
25+
case DavMethods.DAV_POST:
26+
return DavMethods.METHOD_POST;
27+
case DavMethods.DAV_PUT:
28+
return DavMethods.METHOD_PUT;
29+
case DavMethods.DAV_DELETE:
30+
return DavMethods.METHOD_DELETE;
31+
case DavMethods.DAV_COPY:
32+
return DavMethods.METHOD_COPY;
33+
case DavMethods.DAV_MOVE:
34+
return DavMethods.METHOD_MOVE;
35+
case DavMethods.DAV_MKCOL:
36+
return DavMethods.METHOD_MKCOL;
37+
case DavMethods.DAV_OPTIONS:
38+
return DavMethods.METHOD_OPTIONS;
39+
case DavMethods.DAV_LOCK:
40+
return DavMethods.METHOD_LOCK;
41+
case DavMethods.DAV_UNLOCK:
42+
return DavMethods.METHOD_UNLOCK;
43+
case DavMethods.DAV_ORDERPATCH:
44+
return DavMethods.METHOD_ORDERPATCH;
45+
case DavMethods.DAV_SUBSCRIBE:
46+
return DavMethods.METHOD_SUBSCRIBE;
47+
case DavMethods.DAV_UNSUBSCRIBE:
48+
return DavMethods.METHOD_UNSUBSCRIBE;
49+
case DavMethods.DAV_POLL:
50+
return DavMethods.METHOD_POLL;
51+
case DavMethods.DAV_SEARCH:
52+
return DavMethods.METHOD_SEARCH;
53+
case DavMethods.DAV_VERSION_CONTROL:
54+
return DavMethods.METHOD_VERSION_CONTROL;
55+
case DavMethods.DAV_LABEL:
56+
return DavMethods.METHOD_LABEL;
57+
case DavMethods.DAV_REPORT:
58+
return DavMethods.METHOD_REPORT;
59+
case DavMethods.DAV_CHECKIN:
60+
return DavMethods.METHOD_CHECKIN;
61+
case DavMethods.DAV_CHECKOUT:
62+
return DavMethods.METHOD_CHECKOUT;
63+
case DavMethods.DAV_UNCHECKOUT:
64+
return DavMethods.METHOD_UNCHECKOUT;
65+
case DavMethods.DAV_MERGE:
66+
return DavMethods.METHOD_MERGE;
67+
case DavMethods.DAV_UPDATE:
68+
return DavMethods.METHOD_UPDATE;
69+
case DavMethods.DAV_MKWORKSPACE:
70+
return DavMethods.METHOD_MKWORKSPACE;
71+
case DavMethods.DAV_MKACTIVITY:
72+
return DavMethods.METHOD_MKACTIVITY;
73+
case DavMethods.DAV_BASELINE_CONTROL:
74+
return DavMethods.METHOD_BASELINE_CONTROL;
75+
case DavMethods.DAV_ACL:
76+
return DavMethods.METHOD_ACL;
77+
case DavMethods.DAV_REBIND:
78+
return DavMethods.METHOD_REBIND;
79+
case DavMethods.DAV_UNBIND:
80+
return DavMethods.METHOD_UNBIND;
81+
case DavMethods.DAV_BIND:
82+
return DavMethods.METHOD_BIND;
83+
default:
84+
// any other method
85+
return "UNKNOWN";
86+
}
87+
}
88+
}

0 commit comments

Comments
 (0)