001package ca.uhn.fhir.rest.server.interceptor;
002
003/*
004 * #%L
005 * HAPI FHIR - Server Framework
006 * %%
007 * Copyright (C) 2014 - 2022 Smile CDR, Inc.
008 * %%
009 * Licensed under the Apache License, Version 2.0 (the "License");
010 * you may not use this file except in compliance with the License.
011 * You may obtain a copy of the License at
012 *
013 *      http://www.apache.org/licenses/LICENSE-2.0
014 *
015 * Unless required by applicable law or agreed to in writing, software
016 * distributed under the License is distributed on an "AS IS" BASIS,
017 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018 * See the License for the specific language governing permissions and
019 * limitations under the License.
020 * #L%
021 */
022
023import java.util.Enumeration;
024
025import javax.servlet.http.HttpServletRequest;
026import javax.servlet.http.HttpServletResponse;
027
028import ca.uhn.fhir.rest.api.server.RequestDetails;
029import ca.uhn.fhir.rest.server.exceptions.AuthenticationException;
030
031/**
032 * This interceptor creates verbose server log entries containing the complete request and response payloads. 
033 * <p> 
034 * This interceptor is mainly intended for debugging since it will generate very large log entries and
035 * could potentially be a security risk since it logs every header and complete payload. Use with caution! 
036 * </p>
037 */
038public class VerboseLoggingInterceptor extends InterceptorAdapter {
039
040        private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(VerboseLoggingInterceptor.class);
041
042        @Override
043        public boolean incomingRequestPostProcessed(RequestDetails theRequestDetails, HttpServletRequest theRequest, HttpServletResponse theResponse) throws AuthenticationException {
044                
045                StringBuilder b = new StringBuilder("Incoming request: ");
046                b.append(theRequest.getMethod());
047                b.append(" ");
048                b.append(theRequest.getRequestURL());
049                b.append("\n");
050                
051                for (Enumeration<String> headerEnumeration = theRequest.getHeaderNames(); headerEnumeration.hasMoreElements(); ) {
052                        String nextName = headerEnumeration.nextElement();
053                        for (Enumeration<String> valueEnumeration = theRequest.getHeaders(nextName); valueEnumeration.hasMoreElements(); ) {
054                                b.append(" * ").append(nextName).append(": ").append(valueEnumeration.nextElement()).append("\n");
055                        }
056                }
057                
058                ourLog.info(b.toString());
059                return true;
060        }
061
062        
063        
064}