001package ca.uhn.fhir.rest.client.impl;
002
003import java.io.IOException;
004
005import org.apache.http.HttpException;
006import org.apache.http.HttpRequest;
007import org.apache.http.HttpRequestInterceptor;
008import org.apache.http.auth.AuthState;
009import org.apache.http.auth.Credentials;
010import org.apache.http.auth.UsernamePasswordCredentials;
011import org.apache.http.client.protocol.HttpClientContext;
012import org.apache.http.impl.auth.BasicScheme;
013import org.apache.http.protocol.HttpContext;
014
015import ca.uhn.fhir.rest.client.api.IBasicClient;
016import ca.uhn.fhir.rest.client.api.IClientInterceptor;
017import ca.uhn.fhir.rest.client.api.IGenericClient;
018
019/*
020 * #%L
021 * HAPI FHIR - Client Framework
022 * %%
023 * Copyright (C) 2014 - 2019 University Health Network
024 * %%
025 * Licensed under the Apache License, Version 2.0 (the "License");
026 * you may not use this file except in compliance with the License.
027 * You may obtain a copy of the License at
028 * 
029 *      http://www.apache.org/licenses/LICENSE-2.0
030 * 
031 * Unless required by applicable law or agreed to in writing, software
032 * distributed under the License is distributed on an "AS IS" BASIS,
033 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
034 * See the License for the specific language governing permissions and
035 * limitations under the License.
036 * #L%
037 */
038
039
040/**
041 * @deprecated Use {@link ca.uhn.fhir.rest.client.interceptor.BasicAuthInterceptor} instead. Note that BasicAuthInterceptor class is a HAPI client interceptor instead of being a commons-httpclient interceptor, so you register it to your client instance once it's created using {@link IGenericClient#registerInterceptor(IClientInterceptor)} or {@link IBasicClient#registerInterceptor(IClientInterceptor)} instead 
042 */
043@Deprecated
044public class HttpBasicAuthInterceptor  implements HttpRequestInterceptor {
045
046        private String myUsername;
047        private String myPassword;
048        
049    public HttpBasicAuthInterceptor(String theUsername, String thePassword) {
050                super();
051                myUsername = theUsername;
052                myPassword = thePassword;
053        }
054
055        @Override
056        public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException {
057        AuthState authState = (AuthState) context.getAttribute(HttpClientContext.TARGET_AUTH_STATE);
058
059        if (authState.getAuthScheme() == null) {
060            Credentials creds = new UsernamePasswordCredentials(myUsername, myPassword);
061            authState.update(new BasicScheme(), creds);
062        }
063
064    }
065
066}