001package ca.uhn.fhir.rest.client.interceptor;
002
003/*
004 * #%L
005 * HAPI FHIR - Client Framework
006 * %%
007 * Copyright (C) 2014 - 2019 University Health Network
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.io.IOException;
024
025import ca.uhn.fhir.rest.client.api.IClientInterceptor;
026import ca.uhn.fhir.rest.client.api.IHttpRequest;
027import ca.uhn.fhir.rest.client.api.IHttpResponse;
028
029/**
030 * HTTP interceptor to be used for adding HTTP headers containing user identifying info for auditing purposes to the request
031 */
032public class UserInfoInterceptor implements IClientInterceptor {
033        
034        public static final String HEADER_USER_ID = "fhir-user-id";
035        public static final String HEADER_USER_NAME = "fhir-user-name"; 
036        public static final String HEADER_APPLICATION_NAME = "fhir-app-name";
037        
038        private String myUserId;
039        private String myUserName;              
040        private String myAppName;
041        
042    public UserInfoInterceptor(String theUserId, String theUserName, String theAppName) {
043                super();
044                myUserId = theUserId;
045                myUserName = theUserName;               
046                myAppName = theAppName;
047        }
048
049        @Override
050        public void interceptRequest(IHttpRequest theRequest) {
051                if(myUserId != null) theRequest.addHeader(HEADER_USER_ID, myUserId);
052                if(myUserName != null) theRequest.addHeader(HEADER_USER_NAME, myUserName);              
053                if(myAppName != null) theRequest.addHeader(HEADER_APPLICATION_NAME, myAppName);
054        }
055
056        @Override
057        public void interceptResponse(IHttpResponse theResponse) throws IOException {
058                // nothing
059        }
060
061        
062
063}