1 package ca.uhn.fhir.rest.server.interceptor;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 import java.io.UnsupportedEncodingException;
24 import java.net.URLEncoder;
25 import java.util.Map.Entry;
26
27 import javax.servlet.http.HttpServletRequest;
28 import javax.servlet.http.HttpServletResponse;
29
30 import org.apache.commons.lang3.StringUtils;
31 import org.apache.commons.lang3.Validate;
32 import org.apache.commons.lang3.text.StrLookup;
33 import org.apache.commons.lang3.text.StrSubstitutor;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 import ca.uhn.fhir.rest.method.RequestDetails;
38 import ca.uhn.fhir.rest.server.exceptions.AuthenticationException;
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73 public class LoggingInterceptor extends InterceptorAdapter {
74
75 private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(LoggingInterceptor.class);
76
77 private Logger myLogger = ourLog;
78 private String myMessageFormat = "${operationType} - ${idOrResourceName}";
79
80 @Override
81 public boolean incomingRequestPostProcessed(final RequestDetails theRequestDetails, final HttpServletRequest theRequest, HttpServletResponse theResponse) throws AuthenticationException {
82
83
84 StrLookup<?> lookup = new MyLookup(theRequest, theRequestDetails);
85 StrSubstitutor subs = new StrSubstitutor(lookup, "${", "}", '\\');
86
87
88 String line = subs.replace(myMessageFormat);
89 myLogger.info(line);
90
91 return true;
92 }
93
94 public void setLogger(Logger theLogger) {
95 Validate.notNull(theLogger, "Logger can not be null");
96 myLogger = theLogger;
97 }
98
99 public void setLoggerName(String theLoggerName) {
100 Validate.notBlank(theLoggerName, "Logger name can not be null/empty");
101 myLogger = LoggerFactory.getLogger(theLoggerName);
102
103 }
104
105
106
107
108 public void setMessageFormat(String theMessageFormat) {
109 Validate.notBlank(theMessageFormat, "Message format can not be null/empty");
110 myMessageFormat = theMessageFormat;
111 }
112
113 private static final class MyLookup extends StrLookup<String> {
114 private final HttpServletRequest myRequest;
115 private final RequestDetails myRequestDetails;
116
117 private MyLookup(HttpServletRequest theRequest, RequestDetails theRequestDetails) {
118 myRequest = theRequest;
119 myRequestDetails = theRequestDetails;
120 }
121
122 @Override
123 public String lookup(String theKey) {
124 if ("operationType".equals(theKey)) {
125 if (myRequestDetails.getResourceOperationType() != null) {
126 return myRequestDetails.getResourceOperationType().getCode();
127 }
128 if (myRequestDetails.getSystemOperationType() != null) {
129 return myRequestDetails.getSystemOperationType().getCode();
130 }
131 if (myRequestDetails.getOtherOperationType() != null) {
132 return myRequestDetails.getOtherOperationType().getCode();
133 }
134 return "";
135 }
136 if ("id".equals(theKey)) {
137 if (myRequestDetails.getId() != null) {
138 return myRequestDetails.getId().getValue();
139 }
140 return "";
141 }
142 if ("idOrResourceName".equals(theKey)) {
143 if (myRequestDetails.getId() != null) {
144 return myRequestDetails.getId().getValue();
145 }
146 if (myRequestDetails.getResourceName() != null) {
147 return myRequestDetails.getResourceName();
148 }
149 return "";
150 }
151 if (theKey.equals("requestParameters")) {
152 StringBuilder b = new StringBuilder();
153 for (Entry<String, String[]> next : myRequestDetails.getParameters().entrySet()) {
154 for (String nextValue : next.getValue()) {
155 if (b.length() == 0) {
156 b.append('?');
157 } else {
158 b.append('&');
159 }
160 try {
161 b.append(URLEncoder.encode(next.getKey(), "UTF-8"));
162 b.append('=');
163 b.append(URLEncoder.encode(nextValue, "UTF-8"));
164 } catch (UnsupportedEncodingException e) {
165 throw new ca.uhn.fhir.context.ConfigurationException("UTF-8 not supported", e);
166 }
167 }
168 }
169 return b.toString();
170 }
171 if (theKey.startsWith("requestHeader.")) {
172 String val = myRequest.getHeader(theKey.substring("requestHeader.".length()));
173 return StringUtils.defaultString(val);
174 }
175 if (theKey.startsWith("remoteAddr")) {
176 return StringUtils.defaultString(myRequest.getRemoteAddr());
177 }
178 return "!VAL!";
179 }
180 }
181
182 }