001package ca.uhn.fhir.rest.client;
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;
016
017/*
018 * #%L
019 * HAPI FHIR - Core Library
020 * %%
021 * Copyright (C) 2014 - 2016 University Health Network
022 * %%
023 * Licensed under the Apache License, Version 2.0 (the "License");
024 * you may not use this file except in compliance with the License.
025 * You may obtain a copy of the License at
026 * 
027 *      http://www.apache.org/licenses/LICENSE-2.0
028 * 
029 * Unless required by applicable law or agreed to in writing, software
030 * distributed under the License is distributed on an "AS IS" BASIS,
031 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
032 * See the License for the specific language governing permissions and
033 * limitations under the License.
034 * #L%
035 */
036
037
038/**
039 * @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 
040 */
041@Deprecated
042public class HttpBasicAuthInterceptor  implements HttpRequestInterceptor {
043
044        private String myUsername;
045        private String myPassword;
046        
047    public HttpBasicAuthInterceptor(String theUsername, String thePassword) {
048                super();
049                myUsername = theUsername;
050                myPassword = thePassword;
051        }
052
053        @Override
054        public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException {
055        AuthState authState = (AuthState) context.getAttribute(HttpClientContext.TARGET_AUTH_STATE);
056
057        if (authState.getAuthScheme() == null) {
058            Credentials creds = new UsernamePasswordCredentials(myUsername, myPassword);
059            authState.update(new BasicScheme(), creds);
060        }
061
062    }
063
064}