<commit_before>import java.io.*;
import java.net.URI;

import org.apache.http.HttpHost;
import org.apache.http.HttpRequest;
import org.apache.http.HttpException;
import org.apache.http.HttpRequestInterceptor;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.AuthState;
import org.apache.http.auth.Credentials;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.impl.auth.BasicScheme;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.protocol.HttpContext;
import org.apache.http.protocol.HttpCoreContext;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.apache.solr.client.solrj.request.CollectionAdminRequest;
import org.apache.solr.client.solrj.response.CollectionAdminResponse;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.client.solrj.response.UpdateResponse;
import org.apache.solr.common.SolrInputDocument;

import com.ibm.watson.developer_cloud.retrieve_and_rank.v1.RetrieveAndRank;
import com.ibm.watson.developer_cloud.retrieve_and_rank.v1.model.*;


public class WatsonRank {
    //<declaration>
    public static final String SOLR_CLUSTER_ID = "sce46d929b_1d1e_4bd3_bf60_8472e75cb73a";
    public static final String RANKER_ID = "76643bx23-rank-3<PASSWORD>";
    public static final String USERNAME = "64e80095-84de-4d28-9e7d-4f92f0c881d2";
    public static final String PASSWORD = "<PASSWORD>";
    public static String configName = "example_config";
    public static String collectionName = "example_collection";
    public static String rankerName = "example_ranker";
    private static RetrieveAndRank service = new RetrieveAndRank();
    private static HttpSolrClient solrClient;
    //</declration>

    //<construction>
    public WatsonRank() {
        service.setUsernameAndPassword(USERNAME, PASSWORD);
    }

    private static HttpSolrClient getSolrClient(String uri, String username, String password) {
        return new HttpSolrClient(service.getSolrUrl(SOLR_CLUSTER_ID), createHttpClient(uri, username, password));
    }

    private static HttpClient createHttpClient(String uri, String username, String password) {
        final URI scopeUri = URI.create(uri);

        final BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
        credentialsProvider.setCredentials(new AuthScope(scopeUri.getHost(), scopeUri.getPort()),
            new UsernamePasswordCredentials("{username}", "{password}"));

        final HttpClientBuilder builder = HttpClientBuilder.create()
            .setMaxConnTotal(128)
            .setMaxConnPerRoute(32)
            .setDefaultRequestConfig(RequestConfig.copy(RequestConfig.DEFAULT).setRedirectsEnabled(true).build())
            .setDefaultCredentialsProvider(credentialsProvider)
            .addInterceptorFirst(new PreemptiveAuthInterceptor());
        return builder.build();
    }
    //</construction>

    //<upload>
    public void uploadConfig(String filePath, String newConfigName) {
        File configZip = new File(filePath);
        service.uploadSolrClusterConfigurationZip(SOLR_CLUSTER_ID, newConfigName, configZip);
    }

    public void uploadData(String title, String author, String body, String url, int n) {
        SolrInputDocument newdoc = new SolrInputDocument();
        newdoc.addField("id", n);
        newdoc.addField("author", author);
        newdoc.addField("url", url);
        newdoc.addField("body", body);
        newdoc.addField("title", title);

        try {

            System.out.println("Indexing document...");
            UpdateResponse addResponse = solrClient.add(collectionName, newdoc);
            System.out.println(addResponse);
        } catch (Exception e) {
            e.printStackTrace();
        }

        // Commit the document to the index so that it will be available for searching.
        try {
            solrClient.commit(collectionName);
        } catch (Exception e) {
            e.printStackTrace();
        }

        System.out.println("Indexed and committed document.");
    }
	//</upload>

    //<functions>
    public QueryResponse queryWatson(String question) {
 		solrClient = getSolrClient(service.getSolrUrl(SOLR_CLUSTER_ID), USERNAME, PASSWORD);
		SolrQuery query = new SolrQuery(question);
		query.setParam("ranker_id", "B2E325-rank-67");
		query.setRequestHandler("/fcselect"); // use if your solrconfig.xml file does not specify fcselect as the default request handler
		QueryResponse response = solrClient.query("example_collection", query);
        return response;
    }
    //</functions>

    //<gets>
    public void getConfigs() {
        System.out.println(service.getSolrClusterConfigurations(SOLR_CLUSTER_ID));
    }
    //</gets>

    public static String curl(String request) {
        String output = "";<commit_msg>ReadyForUse<commit_after>import java.io.*;
import java.net.URI;

import org.apache.http.HttpHost;
import org.apache.http.HttpRequest;
import org.apache.http.HttpException;
import org.apache.http.HttpRequestInterceptor;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.AuthState;
import org.apache.http.auth.Credentials;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.impl.auth.BasicScheme;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.protocol.HttpContext;
import org.apache.http.protocol.HttpCoreContext;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.apache.solr.client.solrj.request.CollectionAdminRequest;
import org.apache.solr.client.solrj.response.CollectionAdminResponse;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.client.solrj.response.UpdateResponse;
import org.apache.solr.common.SolrInputDocument;

import com.ibm.watson.developer_cloud.retrieve_and_rank.v1.RetrieveAndRank;
import com.ibm.watson.developer_cloud.retrieve_and_rank.v1.model.*;


public class WatsonRank {
    //<declaration>
    public static final String SOLR_CLUSTER_ID = "sce46d929b_1d1e_4bd3_bf60_8472e75cb73a";
    public static final String RANKER_ID = "76643bx23-rank-3206";
    public static final String USERNAME = "64e80095-84de-4d28-9e7d-4f92f0c881d2";
    public static final String PASSWORD = "<PASSWORD>";
    public static String configName = "example_config";
    public static String collectionName = "example_collection";
    public static String rankerName = "example_ranker";
    private static RetrieveAndRank service = new RetrieveAndRank();
    private static HttpSolrClient solrClient;
    //</declration>

    //<construction>
    public WatsonRank() {
        service.setUsernameAndPassword(USERNAME, PASSWORD);
    }

    private static HttpSolrClient getSolrClient(String uri, String username, String password) {
        return new HttpSolrClient(service.getSolrUrl(SOLR_CLUSTER_ID), createHttpClient(uri, username, password));
    }

    private static HttpClient createHttpClient(String uri, String username, String password) {
        final URI scopeUri = URI.create(uri);

        final BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
        credentialsProvider.setCredentials(new AuthScope(scopeUri.getHost(), scopeUri.getPort()),
            new UsernamePasswordCredentials("{username}", "{password}"));

        final HttpClientBuilder builder = HttpClientBuilder.create()
            .setMaxConnTotal(128)
            .setMaxConnPerRoute(32)
            .setDefaultRequestConfig(RequestConfig.copy(RequestConfig.DEFAULT).setRedirectsEnabled(true).build())
            .setDefaultCredentialsProvider(credentialsProvider)
            .addInterceptorFirst(new PreemptiveAuthInterceptor());
        return builder.build();
    }
    //</construction>

    //<upload>
    public void uploadConfig(String filePath, String newConfigName) {
        File configZip = new File(filePath);
        service.uploadSolrClusterConfigurationZip(SOLR_CLUSTER_ID, newConfigName, configZip);
    }

    public void uploadData(String body, int idNum) {
        SolrInputDocument newdoc = new SolrInputDocument();
        newdoc.addField("id", idNum);
        newdoc.addField("body", body);

        try {

            System.out.println("Indexing document...");
            UpdateResponse addResponse = solrClient.add(collectionName, newdoc);
            System.out.println(addResponse);
        } catch (Exception e) {
            e.printStackTrace();
        }

        // Commit the document to the index so that it will be available for searching.
        try {
            solrClient.commit(collectionName);
        } catch (Exception e) {
            e.printStackTrace();
        }

        System.out.println("Indexed and committed document.");
    }
	//</upload>

    //<functions>
    public QueryResponse queryWatson(String question) {
 		solrClient = getSolrClient(service.getSolrUrl(SOLR_CLUSTER_ID), USERNAME, PASSWORD);
		SolrQuery query = new SolrQuery(question);
		query.setParam("ranker_id", RANKER_ID);
		query.setRequestHandler("/fcselect"); // use if your solrconfig.xml file does not specify fcselect as the default request handler
		QueryResponse response = solrClient.query("example_collection", query);
        return response;
    }
    //</functions>

    //<gets>
    public void getConfigs() {
        System.out.println(service.getSolrClusterConfigurations(SOLR_CLUSTER_ID));
    }
    //</gets>

    public static String curl(String request) {
        String output = "";
{'fraction_non_alphanumeric': 0.10228381807534648, 'fraction_numerical': 0.011809486628928362, 'mean_word_length': 3.8888358778625953, 'pattern_counts': {'":': 0, '<': 26, '<?xml version=': 0, '>': 26, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, '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': '2599158', 'n_tokens_mistral': 3075, 'n_tokens_neox': 2941, 'n_words': 580}