de.cuseb.bilderbuch.helpers.HttpClientProviderService.java Source code

Java tutorial

Introduction

Here is the source code for de.cuseb.bilderbuch.helpers.HttpClientProviderService.java

Source

/**
 * bilderbuch - voice driven image search on the web
 * Copyright (C) 2014  Jan Mnnich
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package de.cuseb.bilderbuch.helpers;

import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.HttpClient;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.HttpClients;
import org.springframework.stereotype.Service;

@Service
public class HttpClientProviderService {

    public HttpClient getHttpClient() {
        return HttpClients.createMinimal();
    }

    public HttpClient getHttpClientWithBasicAuth(String username, String password) {

        CredentialsProvider credsProvider = new BasicCredentialsProvider();
        credsProvider.setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT),
                new UsernamePasswordCredentials(username, password));

        return HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build();
    }

}