001/**
002 * Copyright 2015 DuraSpace, Inc.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.fcrepo.client;
017
018import static org.apache.commons.lang3.StringUtils.isBlank;
019import static org.slf4j.LoggerFactory.getLogger;
020
021import java.io.IOException;
022
023import org.apache.http.HttpException;
024import org.apache.http.HttpHost;
025import org.apache.http.HttpRequest;
026import org.apache.http.HttpRequestInterceptor;
027import org.apache.http.auth.AuthScope;
028import org.apache.http.auth.AuthState;
029import org.apache.http.auth.Credentials;
030import org.apache.http.auth.UsernamePasswordCredentials;
031import org.apache.http.client.CredentialsProvider;
032import org.apache.http.client.protocol.HttpClientContext;
033import org.apache.http.impl.auth.BasicScheme;
034import org.apache.http.impl.client.BasicCredentialsProvider;
035import org.apache.http.impl.client.CloseableHttpClient;
036import org.apache.http.impl.client.HttpClients;
037import org.apache.http.protocol.HttpContext;
038import org.apache.http.protocol.HttpCoreContext;
039import org.slf4j.Logger;
040
041/**
042 * A utility class for building an httpclient for interacting with a Fedora repository
043 *
044 * @author Aaron Coburn
045 * @since March 9, 2015
046 */
047public class FcrepoHttpClientBuilder {
048
049    private String username;
050
051    private String password;
052
053    private String host;
054
055    private static final Logger LOGGER = getLogger(FcrepoHttpClientBuilder.class);
056
057    /**
058     * Create a FcrepoHttpClientBuilder object with which it is possible to create
059     * an HttpClient object
060     *
061     * @param username an optional username for authentication
062     * @param password an optional password for authentication
063     * @param host an optional realm for authentication
064     */
065    public FcrepoHttpClientBuilder(final String username, final String password, final String host) {
066        this.username = username;
067        this.password = password;
068        this.host = host;
069    }
070
071    /**
072     *  Build an HttpClient
073     *
074     *  @return an HttpClient
075     */
076    public CloseableHttpClient build() {
077
078        if (isBlank(username) || isBlank(password)) {
079            return HttpClients.createSystem();
080        } else {
081            LOGGER.debug("Accessing fcrepo with user credentials");
082
083            final CredentialsProvider credsProvider = new BasicCredentialsProvider();
084            AuthScope scope = null;
085
086            if (isBlank(host)) {
087                scope = new AuthScope(AuthScope.ANY);
088            } else {
089                scope = new AuthScope(new HttpHost(host));
090            }
091            credsProvider.setCredentials(
092                    scope,
093                    new UsernamePasswordCredentials(username, password));
094            return HttpClients.custom()
095                    .setDefaultCredentialsProvider(credsProvider)
096                    .useSystemProperties()
097                    .addInterceptorFirst(new PreemptiveAuthInterceptor())
098                    .build();
099        }
100    }
101
102    static class PreemptiveAuthInterceptor implements HttpRequestInterceptor {
103
104        public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException {
105            final AuthState authState = (AuthState) context.getAttribute(HttpClientContext.TARGET_AUTH_STATE);
106            // If no auth scheme available yet, try to initialize it
107            // preemptively
108            if (authState.getAuthScheme() == null) {
109                final CredentialsProvider credsProvider = (CredentialsProvider)
110                        context.getAttribute(HttpClientContext.CREDS_PROVIDER);
111                final HttpHost targetHost = (HttpHost) context.getAttribute(HttpCoreContext.HTTP_TARGET_HOST);
112                final AuthScope authScope = new AuthScope(targetHost.getHostName(), targetHost.getPort());
113                final Credentials creds = credsProvider.getCredentials(authScope);
114                if (creds == null) {
115                    LOGGER.debug("Cannot initiate preemtive authentication, Credentials not found!");
116                }
117                authState.update(new BasicScheme(), creds);
118            }
119        }
120    }
121}