<commit_before>package de.micromata.confluence.rest;

import de.micromata.confluence.rest.client.ContentClient;
import de.micromata.confluence.rest.client.SearchClient;
import de.micromata.confluence.rest.client.SpaceClient;
import de.micromata.confluence.rest.client.UserClient;
import de.micromata.confluence.rest.core.ContentClientImpl;
import de.micromata.confluence.rest.core.SearchClientImpl;
import de.micromata.confluence.rest.core.SpaceClientImpl;
import de.micromata.confluence.rest.core.UserClientImpl;
import de.micromata.confluence.rest.core.misc.RestParamConstants;
import de.micromata.confluence.rest.core.misc.RestPathConstants;
import de.micromata.confluence.rest.core.util.HttpMethodFactory;
import de.micromata.confluence.rest.core.util.URIHelper;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.AuthCache;
import org.apache.http.client.CookieStore;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.auth.BasicScheme;
import org.apache.http.impl.client.*;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;

/**
 * Created by cschulc on 01.07.2016.
 */
public class ConfluenceRestClient implements RestPathConstants, RestParamConstants {


    private static final String HTTP = "http";
    private static final String HTTPS = "https";

    protected final ExecutorService executorService;

    private URI baseUri;
    private String username = StringUtils.EMPTY;
    private CloseableHttpClient httpclient;
    private HttpHost proxy;
    private CookieStore cookieStore = new BasicCookieStore();
    private HttpClientContext clientContext;

    private UserClient userClient;

    private SpaceClient spaceClient;

    private ContentClient contentClient;

    private SearchClient searchClient;

    public ConfluenceRestClient(ExecutorService executorService) {
        this.executorService = executorService;
    }

    public int connect(URI uri, String username, String password) throws IOException, URISyntaxException, ExecutionException, InterruptedException {
        return connect(uri, username, password, null);
    }

    private int connect(URI uri, String username, String password, HttpHost procxyHost) throws URISyntaxException, IOException {
        this.username = username;
        String host = uri.getHost();
        int port = getPort(uri.toURL());
        String scheme = HTTP;
        if (port == 443) scheme = HTTPS;
        HttpHost target = new HttpHost(host, port, scheme);
        CredentialsProvider credsProvider = new BasicCredentialsProvider();
        credsProvider.setCredentials(
                new AuthScope(target.getHostName(), target.getPort()),
                new UsernamePasswordCredentials(username, password));
        httpclient = HttpClients.custom()
                .setDefaultCredentialsProvider(credsProvider)
                .build();
        // Create AuthCache instance
        AuthCache authCache = new BasicAuthCache();
        // Generate BASIC scheme object and add it to the local<commit_msg>Implemented proxy support.<commit_after>package de.micromata.confluence.rest;

import de.micromata.confluence.rest.client.ContentClient;
import de.micromata.confluence.rest.client.SearchClient;
import de.micromata.confluence.rest.client.SpaceClient;
import de.micromata.confluence.rest.client.UserClient;
import de.micromata.confluence.rest.core.ContentClientImpl;
import de.micromata.confluence.rest.core.SearchClientImpl;
import de.micromata.confluence.rest.core.SpaceClientImpl;
import de.micromata.confluence.rest.core.UserClientImpl;
import de.micromata.confluence.rest.core.misc.RestParamConstants;
import de.micromata.confluence.rest.core.misc.RestPathConstants;
import de.micromata.confluence.rest.core.util.HttpMethodFactory;
import de.micromata.confluence.rest.core.util.URIHelper;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.AuthCache;
import org.apache.http.client.CookieStore;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.auth.BasicScheme;
import org.apache.http.impl.client.*;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import org.apache.http.impl.conn.DefaultProxyRoutePlanner;

/**
 * Client to interact with Confluence server's REST API.
 *
 * Author: <NAME> (<EMAIL>), <NAME> (<EMAIL>)
 * Created: 01.07.2016
 * Modified: 19.04.2017
 * Project: ConfluenceTransferPlugin
 */
public class ConfluenceRestClient implements RestPathConstants, RestParamConstants {

    private static final String HTTP = "http";
    private static final String HTTPS = "https";

    protected final ExecutorService executorService;

    private URI baseUri;
    private String username = StringUtils.EMPTY;
    private CloseableHttpClient httpclient;
    private HttpHost proxy;
    private CookieStore cookieStore = new BasicCookieStore();
    private HttpClientContext clientContext;

    private UserClient userClient;

    private SpaceClient spaceClient;

    private ContentClient contentClient;

    private SearchClient searchClient;

    public ConfluenceRestClient(ExecutorService executorService) {
        this.executorService = executorService;
    }

    public int connect(URI uri, String username, String password) throws IOException, URISyntaxException, ExecutionException, InterruptedException {
        return connect(uri, username, password, null);
    }

    public int connect(URI uri, String username, String password, HttpHost procxyHost) throws URISyntaxException, IOException {
        this.proxy = procxyHost;
        this.username = username;
        String host = uri.getHost();
        int port = getPort(uri.toURL());
        String scheme = HTTP;
        if (port == 443) scheme = HTTPS;
        HttpHost target = new HttpHost(host, port, scheme);
        CredentialsProvider credsProvider = new BasicCredentialsProvider();
        credsProvider.setCredentials(
                new AuthScope(target.getHostName(), target.getPort()),
                new UsernamePasswordCredentials(username, password));
        HttpClientBuilder clientBuilder = HttpClients.custom()
                .setDefaultCredentialsProvider(credsProvider);
        if (this.proxy != null) {
            DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner(this.proxy);
            clientBuilder.setRoutePlanner(routePlanner);
        }
        httpclient = clientBuilder.build();
        // Create AuthCache instance
        AuthCache authCache = new BasicAuthCache();
        // Generate BASIC scheme object and add it to the local
{'fraction_non_alphanumeric': 0.08840141862603441, 'fraction_numerical': 0.004203336398266123, 'mean_word_length': 4.331932773109243, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5452430', 'n_tokens_mistral': 2136, 'n_tokens_neox': 2026, 'n_words': 461}